]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReaderFile.cxx
New raw-reader from root chain.
[u/mrichter/AliRoot.git] / RAW / AliRawReaderFile.cxx
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
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
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 /// 
31 ///////////////////////////////////////////////////////////////////////////////
32
33 #include <cstdlib>
34 #include "AliRawReaderFile.h"
35 #include "AliDAQ.h"
36 #include <TSystem.h>
37 #include <TArrayC.h>
38
39
40 ClassImp(AliRawReaderFile)
41
42
43 AliRawReaderFile::AliRawReaderFile(Int_t eventNumber) :
44   fEventIndex(eventNumber),
45   fDirName("."),
46   fDirectory(NULL),
47   fStream(NULL),
48   fEquipmentId(-1),
49   fBuffer(NULL),
50   fBufferSize(0),
51   fEquipmentSize(0),
52   fDDLIndex(NULL),
53   fDDLCurrent(-1),
54   fType(7),
55   fRunNb(0),
56   fDetectorPattern(0),
57   fTimestamp(0)
58 {
59 // create an object to read digits from the given event
60 // in the current directory
61
62   fDirectory = OpenDirectory();
63   OpenNextFile();
64   fHeader = new AliRawDataHeader;
65
66   fId[0] = fId[1] = 0;
67   fTriggerPattern[0] = fTriggerPattern[1] = 0;
68 }
69
70 AliRawReaderFile::AliRawReaderFile(const char* dirName, Int_t eventNumber) :
71   fEventIndex(eventNumber),
72   fDirName(dirName),
73   fDirectory(NULL),
74   fStream(NULL),
75   fEquipmentId(-1),
76   fBuffer(NULL),
77   fBufferSize(0),
78   fEquipmentSize(0),
79   fDDLIndex(NULL),
80   fDDLCurrent(-1),
81   fType(7),
82   fRunNb(0),
83   fDetectorPattern(0),
84   fTimestamp(0)
85 {
86 // create an object to read digits from the given directory
87
88   fDirectory = OpenDirectory();
89   OpenNextFile();
90   fHeader = new AliRawDataHeader;
91
92   fId[0] = fId[1] = 0;
93   fTriggerPattern[0] = fTriggerPattern[1] = 0;
94 }
95
96 AliRawReaderFile::~AliRawReaderFile()
97 {
98 // close the input file
99
100   if (fDirectory) gSystem->FreeDirectory(fDirectory);
101   if (fStream) {
102 #if defined(__HP_aCC) || defined(__DECCXX)
103     if (fStream->rdbuf()->is_open()) fStream->close();
104 #else
105     if (fStream->is_open()) fStream->close();
106 #endif
107     delete fStream;
108   }
109   if (fHeader) delete fHeader;
110   if (fBuffer) delete[] fBuffer;
111   if (fDDLIndex) delete fDDLIndex; fDDLIndex=NULL;
112 }
113
114 void AliRawReaderFile::RequireHeader(Bool_t required)
115 {
116   // Reading of raw data in case of missing
117   // raw data header is not implemented for
118   // this class
119   if (!required) {
120     Warning("AliRawReaderFile","Reading of raw data without raw data header!");
121     if (fHeader) delete fHeader;
122     fHeader = NULL;
123   }
124   else {
125     if (!fHeader) fHeader = new AliRawDataHeader;
126   }
127
128   AliRawReader::RequireHeader(required);
129 }
130
131 TString AliRawReaderFile::GetDirName() const
132 {
133 // return the current directory name
134
135   TString dirName(fDirName);
136   if (fEventIndex >= 0) {
137     dirName += "/raw";
138     dirName += fEventIndex;
139   }
140   return dirName;
141 }
142
143 void* AliRawReaderFile::OpenDirectory()
144 {
145 // open and return the directory
146
147   TString dirName = GetDirName();
148   void* directory = gSystem->OpenDirectory(dirName);
149   if (!directory) {
150     Error("OpenDirectory", "could not open directory %s", dirName.Data());
151   }
152   return directory;
153 }
154
155 Bool_t AliRawReaderFile::CreateFileIndex()
156 {
157 // scan the files of the directory and create index of all DDL files
158 // returns kFALSE if no DDL files available
159   Bool_t result=kFALSE;
160   fDDLCurrent=-1;
161   if (fDDLIndex) return fDDLIndex->GetSize()>0;
162   if (!fDirectory) return kFALSE;
163   fDDLIndex=new TArrayC(0);
164   if (!fDDLIndex) return kFALSE;
165   TString entry;
166   while (entry = gSystem->GetDirEntry(fDirectory)) {
167     const char* filename=entry.Data();
168     if (!filename || entry.IsNull()) break;
169     if (!entry.EndsWith(".ddl")) continue;
170     result=kTRUE;
171     entry.Remove(0, entry.Last('_')+1);
172     entry.Remove(entry.Length()-4);
173     Int_t equipmentId = atoi(entry.Data());
174     if (fDDLIndex->GetSize()<=equipmentId) {
175       fDDLIndex->Set(equipmentId+1);
176     }
177     char* array=(char*)fDDLIndex->GetArray();
178     array[equipmentId]=1;
179   }
180
181   return result;
182 }
183
184 Bool_t AliRawReaderFile::OpenNextFile()
185 {
186 // open the next file
187 // returns kFALSE if the current file is the last one
188
189   if (!fDDLIndex && !CreateFileIndex()) return kFALSE;
190   if (fSelectMinEquipmentId>=0 && fSelectMinEquipmentId>fEquipmentId)
191     fDDLCurrent=fSelectMinEquipmentId-1;
192
193   if (fStream) {
194 #if defined(__HP_aCC) || defined(__DECCXX)
195     if (fStream->rdbuf()->is_open()) fStream->close();
196 #else
197     if (fStream->is_open()) fStream->close();
198 #endif
199     delete fStream;
200     fStream = NULL;
201     fEquipmentId = -1;
202     fEquipmentSize = 0;
203   }
204
205   if (!fDirectory) return kFALSE;
206   while (++fDDLCurrent<(fDDLIndex->GetSize()) && 
207          (fDDLCurrent<=fSelectMaxEquipmentId || fSelectMaxEquipmentId<0)) {
208     if (fDDLIndex->At(fDDLCurrent)==0) continue;
209     Int_t dummy=0;
210     TString entry;
211     entry.Form("%s_%d.ddl", AliDAQ::DetectorNameFromDdlID(fDDLCurrent, dummy), fDDLCurrent);
212     char* fileName = gSystem->ConcatFileName(GetDirName(), entry);
213     if (!fileName) continue;
214     // read the timestamp
215     FileStat_t buf;
216     if (gSystem->GetPathInfo(fileName,buf) == 0) {
217       fTimestamp = buf.fMtime;
218     }
219 #ifndef __DECCXX 
220     fStream = new fstream(fileName, ios::binary|ios::in);
221 #else
222     fStream = new fstream(fileName, ios::in);
223 #endif
224     delete [] fileName;
225     break;
226   }
227
228   if (!fStream) return kFALSE;
229   fEquipmentId = fDDLCurrent;
230 #if defined(__HP_aCC) || defined(__DECCXX)
231   return (fStream->rdbuf()->is_open());
232 #else
233   return (fStream->is_open());
234 #endif
235 }
236
237
238 Bool_t AliRawReaderFile::ReadHeader()
239 {
240 // read a data header at the current stream position
241 // returns kFALSE if the mini header could not be read
242
243   if (!fStream && !OpenNextFile()) return kFALSE;
244   do {
245     if (fCount > 0) fStream->seekg(Int_t(fStream->tellg()) + fCount);
246     if (fHeader) {
247       while (!fStream->read((char*) fHeader, sizeof(AliRawDataHeader))) {
248         if (!OpenNextFile()) return kFALSE;
249       }
250     }
251     else {
252       if (fStream->eof())
253         if (!OpenNextFile()) return kFALSE;
254     }
255     if (fHeader && fHeader->fSize != 0xFFFFFFFF) {
256       fCount = fHeader->fSize - sizeof(AliRawDataHeader);
257     } else {
258       UInt_t currentPos = fStream->tellg();
259       fStream->seekg(0, ios::end);
260       fCount = UInt_t(fStream->tellg()) - currentPos;
261       fStream->seekg(currentPos);
262     }
263     fEquipmentSize = fCount;
264     if (fHeader) fEquipmentSize += sizeof(AliRawDataHeader);
265   } while (!IsSelected());
266   return kTRUE;
267 }
268
269 Bool_t AliRawReaderFile::ReadNextData(UChar_t*& data)
270 {
271 // reads the next payload at the current stream position
272 // returns kFALSE if the data could not be read
273
274   while (fCount == 0) {
275     if (!ReadHeader()) return kFALSE;
276   }
277   if (fBufferSize < fCount) {
278     if (fBuffer) delete[] fBuffer;
279     fBufferSize = Int_t(fCount*1.2);
280     fBuffer = new UChar_t[fBufferSize];
281   }
282   if (!fStream->read((char*) fBuffer, fCount)) {
283     Error("ReadNext", "could not read data!");
284     return kFALSE;
285   }
286   fCount = 0;
287
288   data = fBuffer;
289   return kTRUE;
290 }
291
292 Bool_t AliRawReaderFile::ReadNext(UChar_t* data, Int_t size)
293 {
294 // reads the next block of data at the current stream position
295 // returns kFALSE if the data could not be read
296
297   if (!fStream->read((char*) data, size)) {
298     Error("ReadNext", "could not read data!");
299     return kFALSE;
300   }
301   fCount -= size;
302   return kTRUE;
303 }
304
305
306 Bool_t AliRawReaderFile::Reset()
307 {
308 // reset the current stream position to the first DDL file of the curevent
309
310   void* directory = OpenDirectory();
311   if (!directory) return kFALSE;
312
313   if (fStream) {
314 #if defined(__HP_aCC) || defined(__DECCXX)
315     if (fStream->rdbuf()->is_open()) fStream->close();
316 #else
317     if (fStream->is_open()) fStream->close();
318 #endif
319     delete fStream;
320     fStream = NULL;
321   }
322
323   if (fDirectory) gSystem->FreeDirectory(fDirectory);
324   fDirectory = directory;
325
326   // Matthias 05.06.2008
327   // do not open the next file. That might collide with a subsequent
328   // SelectEquipment call as the 'file pointer' is already set.
329   // This is important for the indexing of the DDL files.
330   // ---------------------------------------------------------
331   // All ReadNext functions first require the fCount member to be
332   // non zero or call ReadHeader. That allows to postpone the call
333   // to OpenNextFile to the next invocation of ReadHeader.
334   // ReadHeader has been mofified according to that.
335   /*
336   OpenNextFile();
337   */
338   fEquipmentId=-1;
339   fDDLCurrent=-1;
340   fCount = 0;
341   return kTRUE;
342 }
343
344 Bool_t AliRawReaderFile::NextEvent()
345 {
346 // go to the next event directory
347
348   if (fDDLIndex) delete fDDLIndex;
349   fDDLIndex=NULL;
350   if (fEventIndex < -1) return kFALSE;
351
352   do {
353     TString dirName = fDirName + "/raw";
354     dirName += (fEventIndex + 1);
355     void* directory = gSystem->OpenDirectory(dirName);
356     if (!directory) return kFALSE;
357     gSystem->FreeDirectory(directory);
358
359     fEventIndex++;
360     Reset();
361   } while (!IsEventSelected());
362
363   // Read the header of the first payload
364   // in order to fill the 'fake' event header
365   if (ReadHeader() && fHeader) {
366     fId[0] = ((fHeader->GetEventID2() >> 20) & 0xf);
367     fId[1] = (fHeader->GetEventID1() & 0xfff) | ((fHeader->GetEventID2() & 0xfffff) << 12);
368     fTriggerPattern[0] = (fHeader->GetTriggerClasses() & 0xffffffff);
369     fTriggerPattern[1] = ((fHeader->GetTriggerClasses() >> 32) & 0x3ffff);
370   }
371   else {
372     Warning("AliRawReaderFile","Can not read CDH header! The event header fields will be empty!");
373   }
374   Reset();
375
376   fEventNumber++;
377
378   return kTRUE;
379 }
380
381 Bool_t AliRawReaderFile::RewindEvents()
382 {
383 // reset the event counter
384
385   if (fEventIndex >= 0)  fEventIndex = -1;
386   fEventNumber = -1;
387   return Reset();
388 }