AliMDC and DB classes moved from library to alimdc executable
[u/mrichter/AliRoot.git] / RAW / AliRawReaderFile.cxx
CommitLineData
04fa961a 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
bea6b2a4 16/* $Id$ */
17
04fa961a 18///////////////////////////////////////////////////////////////////////////////
bea6b2a4 19///
20/// This is a class for reading raw data files.
21///
22/// The files of one event are expected to be in one directory. The name
23/// of the directory is "raw" + the event number. Each file contains
24/// the raw data (with data header) of one DDL. The convention for the
25/// file names is "DET_#DDL.ddl". "DET" is the name of the detector and
26/// "#DDL" is the unique equipment ID.
27///
28/// The constructor of AliRawReaderFile takes the event number or the
29/// directory name as argument.
30///
04fa961a 31///////////////////////////////////////////////////////////////////////////////
32
33#include "AliRawReaderFile.h"
39f9963f 34#include <TSystem.h>
04fa961a 35
36
37ClassImp(AliRawReaderFile)
38
39
39f9963f 40AliRawReaderFile::AliRawReaderFile(Int_t eventNumber) :
dd9a70fe 41 fEventIndex(eventNumber),
42 fDirName("."),
39f9963f 43 fDirectory(NULL),
44 fStream(NULL),
45 fEquipmentId(-1),
46 fBuffer(NULL),
47 fBufferSize(0)
04fa961a 48{
39f9963f 49// create an object to read digits from the given event
dd9a70fe 50// in the current directory
04fa961a 51
dd9a70fe 52 fDirectory = OpenDirectory();
39f9963f 53 OpenNextFile();
54 fHeader = new AliRawDataHeader;
55}
56
dd9a70fe 57AliRawReaderFile::AliRawReaderFile(const char* dirName, Int_t eventNumber) :
58 fEventIndex(eventNumber),
39f9963f 59 fDirName(dirName),
60 fDirectory(NULL),
61 fStream(NULL),
62 fEquipmentId(-1),
63 fBuffer(NULL),
64 fBufferSize(0)
65{
66// create an object to read digits from the given directory
67
dd9a70fe 68 fDirectory = OpenDirectory();
39f9963f 69 OpenNextFile();
70 fHeader = new AliRawDataHeader;
04fa961a 71}
72
42d20574 73AliRawReaderFile::AliRawReaderFile(const AliRawReaderFile& rawReader) :
74 AliRawReader(rawReader)
75{
76 Fatal("AliRawReaderFile", "copy constructor not implemented");
77}
78
79AliRawReaderFile& AliRawReaderFile::operator = (const AliRawReaderFile&
80 /* rawReader */)
81{
82 Fatal("operator =", "assignment operator not implemented");
83 return *this;
84}
85
04fa961a 86AliRawReaderFile::~AliRawReaderFile()
87{
88// close the input file
89
39f9963f 90 if (fDirectory) gSystem->FreeDirectory(fDirectory);
04fa961a 91 if (fStream) {
92#if defined(__HP_aCC) || defined(__DECCXX)
93 if (fStream->rdbuf()->is_open()) fStream->close();
94#else
95 if (fStream->is_open()) fStream->close();
96#endif
97 delete fStream;
98 }
39f9963f 99 delete fHeader;
04fa961a 100 if (fBuffer) delete[] fBuffer;
101}
102
103
dd9a70fe 104TString AliRawReaderFile::GetDirName() const
105{
106// return the current directory name
107
108 TString dirName(fDirName);
109 if (fEventIndex >= 0) {
110 dirName += "/raw";
111 dirName += fEventIndex;
112 }
113 return dirName;
114}
115
116void* AliRawReaderFile::OpenDirectory()
117{
118// open and return the directory
119
120 TString dirName = GetDirName();
121 void* directory = gSystem->OpenDirectory(dirName);
122 if (!directory) {
123 Error("OpenDirectory", "could not open directory %s", dirName.Data());
124 }
125 return directory;
126}
127
04fa961a 128Bool_t AliRawReaderFile::OpenNextFile()
129{
42d20574 130// open the next file
131// returns kFALSE if the current file is the last one
132
04fa961a 133 if (fStream) {
134#if defined(__HP_aCC) || defined(__DECCXX)
135 if (fStream->rdbuf()->is_open()) fStream->close();
136#else
137 if (fStream->is_open()) fStream->close();
138#endif
139 delete fStream;
140 fStream = NULL;
39f9963f 141 fEquipmentId = -1;
04fa961a 142 }
04fa961a 143
39f9963f 144 if (!fDirectory) return kFALSE;
145 TString entry;
146 while (entry = gSystem->GetDirEntry(fDirectory)) {
147 if (entry.IsNull()) return kFALSE;
148 if (!entry.EndsWith(".ddl")) continue;
dd9a70fe 149 char* fileName = gSystem->ConcatFileName(GetDirName(), entry);
04fa961a 150#ifndef __DECCXX
39f9963f 151 fStream = new fstream(fileName, ios::binary|ios::in);
04fa961a 152#else
39f9963f 153 fStream = new fstream(fileName, ios::in);
04fa961a 154#endif
39f9963f 155 break;
156 }
157
158 if (!fStream) return kFALSE;
159 entry.Remove(0, entry.Last('_')+1);
160 entry.Remove(entry.Length()-4);
161 fEquipmentId = atoi(entry.Data());
04fa961a 162#if defined(__HP_aCC) || defined(__DECCXX)
163 return (fStream->rdbuf()->is_open());
164#else
165 return (fStream->is_open());
166#endif
167}
168
169
39f9963f 170Bool_t AliRawReaderFile::ReadHeader()
04fa961a 171{
39f9963f 172// read a data header at the current stream position
04fa961a 173// returns kFALSE if the mini header could not be read
174
175 if (!fStream) return kFALSE;
176 do {
177 if (fCount > 0) fStream->seekg(Int_t(fStream->tellg()) + fCount);
39f9963f 178 while (!fStream->read((char*) fHeader, sizeof(AliRawDataHeader))) {
04fa961a 179 if (!OpenNextFile()) return kFALSE;
180 }
39f9963f 181 if (fHeader->fSize != 0xFFFFFFFF) {
182 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
183 } else {
184 UInt_t currentPos = fStream->tellg();
185 fStream->seekg(0, ios::end);
186 fCount = UInt_t(fStream->tellg()) - currentPos;
187 fStream->seekg(currentPos);
188 }
04fa961a 189 } while (!IsSelected());
190 return kTRUE;
191}
192
193Bool_t AliRawReaderFile::ReadNextData(UChar_t*& data)
194{
195// reads the next payload at the current stream position
196// returns kFALSE if the data could not be read
197
198 while (fCount == 0) {
39f9963f 199 if (!ReadHeader()) return kFALSE;
04fa961a 200 }
201 if (fBufferSize < fCount) {
202 if (fBuffer) delete[] fBuffer;
203 fBufferSize = Int_t(fCount*1.2);
204 fBuffer = new UChar_t[fBufferSize];
205 }
206 if (!fStream->read((char*) fBuffer, fCount)) {
207 Error("ReadNext", "could not read data!");
208 return kFALSE;
209 }
210 fCount = 0;
211
212 data = fBuffer;
213 return kTRUE;
214}
215
216Bool_t AliRawReaderFile::ReadNext(UChar_t* data, Int_t size)
217{
218// reads the next block of data at the current stream position
219// returns kFALSE if the data could not be read
220
221 if (!fStream->read((char*) data, size)) {
222 Error("ReadNext", "could not read data!");
223 return kFALSE;
224 }
225 fCount -= size;
226 return kTRUE;
227}
228
229
230Bool_t AliRawReaderFile::Reset()
231{
dd9a70fe 232// reset the current stream position to the first DDL file of the curevent
04fa961a 233
dd9a70fe 234 void* directory = OpenDirectory();
235 if (!directory) return kFALSE;
39f9963f 236
237 if (fStream) {
04fa961a 238#if defined(__HP_aCC) || defined(__DECCXX)
239 if (fStream->rdbuf()->is_open()) fStream->close();
240#else
241 if (fStream->is_open()) fStream->close();
242#endif
243 delete fStream;
244 fStream = NULL;
04fa961a 245 }
246
39f9963f 247 if (fDirectory) gSystem->FreeDirectory(fDirectory);
248 fDirectory = directory;
04fa961a 249
39f9963f 250 OpenNextFile();
04fa961a 251 fCount = 0;
252 return kTRUE;
253}
254
dd9a70fe 255Bool_t AliRawReaderFile::NextEvent()
256{
257// go to the next event directory
258
259 if (fEventIndex < -1) return kFALSE;
260
261 do {
262 TString dirName = fDirName + "/raw";
263 dirName += (fEventIndex + 1);
264 void* directory = gSystem->OpenDirectory(dirName);
265 if (!directory) return kFALSE;
266 gSystem->FreeDirectory(directory);
267
268 fEventIndex++;
269 Reset();
270 } while (!IsEventSelected());
271
272 return kTRUE;
273}
274
275Bool_t AliRawReaderFile::RewindEvents()
276{
277// reset the event counter
278
279 if (fEventIndex >= 0) fEventIndex = -1;
280 return Reset();
281}