]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReaderDate.cxx
DAs upgraded to AliZDCRawStream class
[u/mrichter/AliRoot.git] / RAW / AliRawReaderDate.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 from a date file or event.
21 ///
22 /// The AliRawReaderDate is constructed either with a pointer to a
23 /// date event or with a file name and an event number.
24 ///
25 ///////////////////////////////////////////////////////////////////////////////
26
27 #include "AliRawReaderDate.h"
28 #ifdef ALI_DATE
29 #include "event.h"
30 #endif
31
32 ClassImp(AliRawReaderDate)
33
34
35 AliRawReaderDate::AliRawReaderDate(
36 #ifdef ALI_DATE
37                                    void* event
38 #else
39                                    void* /* event */
40 #endif
41                                    ) :
42   fFile(NULL),
43   fEvent(NULL),
44   fSubEvent(NULL),
45   fEquipment(NULL),
46   fPosition(NULL),
47   fEnd(NULL)
48 {
49 // create an object to read digits from the given date event
50
51 #ifdef ALI_DATE
52   fEvent = (eventHeaderStruct*) event;
53 #else
54   Fatal("AliRawReaderDate", "this class was compiled without DATE");
55 #endif
56 }
57
58 AliRawReaderDate::AliRawReaderDate(
59 #ifdef ALI_DATE
60                                    const char* fileName, Int_t eventNumber
61 #else
62                                    const char* /*fileName*/, 
63                                    Int_t /*eventNumber*/
64 #endif
65                                    ) :
66   fFile(NULL),
67   fEvent(NULL),
68   fSubEvent(NULL),
69   fEquipment(NULL),
70   fPosition(NULL),
71   fEnd(NULL)
72 {
73 // create an object to read digits from the given date event
74
75 #ifdef ALI_DATE
76   fFile = fopen(fileName, "rb");
77   if (!fFile) {
78     Error("AliRawReaderDate", "could not open file %s", fileName);
79     fIsValid = kFALSE;
80     return;
81   }
82   if (eventNumber < 0) return;
83
84   eventHeaderStruct header;
85   UInt_t headerSize = sizeof(eventHeaderStruct);
86   while (fread(&header, 1, headerSize, fFile) == headerSize) {
87     if (eventNumber == 0) {
88       UChar_t* buffer = new UChar_t[header.eventSize];
89       fseek(fFile, -(long)headerSize, SEEK_CUR);
90       if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) break;
91       fEvent = (eventHeaderStruct*) buffer;
92       break;
93     }
94     fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
95     eventNumber--;
96   }
97
98 #else
99   Fatal("AliRawReaderDate", "this class was compiled without DATE");
100 #endif
101 }
102
103 AliRawReaderDate::AliRawReaderDate(const AliRawReaderDate& rawReader) :
104   AliRawReader(rawReader),
105   fFile(rawReader.fFile),
106   fEvent(rawReader.fEvent),
107   fSubEvent(rawReader.fSubEvent),
108   fEquipment(rawReader.fEquipment),
109   fPosition(rawReader.fPosition),
110   fEnd(rawReader.fEnd)
111
112 {
113 // copy constructor
114
115   Fatal("AliRawReaderDate", "copy constructor not implemented");
116 }
117
118 AliRawReaderDate& AliRawReaderDate::operator = (const AliRawReaderDate& 
119                                                 /*rawReader*/)
120 {
121 // assignment operator
122
123   Fatal("operator =", "assignment operator not implemented");
124   return *this;
125 }
126
127 AliRawReaderDate::~AliRawReaderDate()
128 {
129 // destructor
130
131 #ifdef ALI_DATE
132   if (fFile) {
133     delete[] fEvent;
134     fclose(fFile);
135   }
136 #endif
137 }
138
139
140 UInt_t AliRawReaderDate::GetType() const
141 {
142 // get the type from the event header
143
144 #ifdef ALI_DATE
145   if (!fEvent) return 0;
146   return fEvent->eventType;
147 #else
148   return 0;
149 #endif
150 }
151
152 UInt_t AliRawReaderDate::GetRunNumber() const
153 {
154 // get the run number from the event header
155
156 #ifdef ALI_DATE
157   if (!fEvent) return 0;
158   return fEvent->eventRunNb;
159 #else
160   return 0;
161 #endif
162 }
163
164 const UInt_t* AliRawReaderDate::GetEventId() const
165 {
166 // get the event id from the event header
167
168 #ifdef ALI_DATE
169   if (!fEvent) return NULL;
170   return fEvent->eventId;
171 #else
172   return NULL;
173 #endif
174 }
175
176 const UInt_t* AliRawReaderDate::GetTriggerPattern() const
177 {
178 // get the trigger pattern from the event header
179
180 #ifdef ALI_DATE
181   if (!fEvent) return NULL;
182   return fEvent->eventTriggerPattern;
183 #else
184   return NULL;
185 #endif
186 }
187
188 const UInt_t* AliRawReaderDate::GetDetectorPattern() const
189 {
190 // get the detector pattern from the event header
191
192 #ifdef ALI_DATE
193   if (!fEvent) return NULL;
194   return fEvent->eventDetectorPattern;
195 #else
196   return NULL;
197 #endif
198 }
199
200 const UInt_t* AliRawReaderDate::GetAttributes() const
201 {
202 // get the type attributes from the event header
203
204 #ifdef ALI_DATE
205   if (!fEvent) return NULL;
206   return fEvent->eventTypeAttribute;
207 #else
208   return NULL;
209 #endif
210 }
211
212 const UInt_t* AliRawReaderDate::GetSubEventAttributes() const
213 {
214 // get the type attributes from the sub event header
215
216 #ifdef ALI_DATE
217   if (!fSubEvent) return NULL;
218   return fSubEvent->eventTypeAttribute;
219 #else
220   return NULL;
221 #endif
222 }
223
224 UInt_t AliRawReaderDate::GetLDCId() const
225 {
226 // get the LDC Id from the event header
227
228 #ifdef ALI_DATE
229   if (!fSubEvent) return 0;
230   return fSubEvent->eventLdcId;
231 #else
232   return 0;
233 #endif
234 }
235
236 UInt_t AliRawReaderDate::GetGDCId() const
237 {
238 // get the GDC Id from the event header
239
240 #ifdef ALI_DATE
241   if (!fEvent) return 0;
242   return fEvent->eventGdcId;
243 #else
244   return 0;
245 #endif
246 }
247
248 UInt_t AliRawReaderDate::GetTimestamp() const
249 {
250 // get the timestamp from the event header
251
252 #ifdef ALI_DATE
253   if (!fEvent) return 0;
254   return fEvent->eventTimestamp;
255 #else
256   return 0;
257 #endif
258 }
259
260 Int_t AliRawReaderDate::GetEquipmentSize() const
261 {
262 // get the size of the equipment (including the header)
263
264 #ifdef ALI_DATE
265   if (!fEquipment) return 0;
266   if (fSubEvent->eventVersion <= 0x00030001) {
267     return fEquipment->equipmentSize + sizeof(equipmentHeaderStruct);
268   } else {
269     return fEquipment->equipmentSize;
270   }
271 #else
272   return 0;
273 #endif
274 }
275
276 Int_t AliRawReaderDate::GetEquipmentType() const
277 {
278 // get the type from the equipment header
279
280 #ifdef ALI_DATE
281   if (!fEquipment) return -1;
282   return fEquipment->equipmentType;
283 #else
284   return 0;
285 #endif
286 }
287
288 Int_t AliRawReaderDate::GetEquipmentId() const
289 {
290 // get the ID from the equipment header
291
292 #ifdef ALI_DATE
293   if (!fEquipment) return -1;
294   return fEquipment->equipmentId;
295 #else
296   return 0;
297 #endif
298 }
299
300 const UInt_t* AliRawReaderDate::GetEquipmentAttributes() const
301 {
302 // get the attributes from the equipment header
303
304 #ifdef ALI_DATE
305   if (!fEquipment) return NULL;
306   return fEquipment->equipmentTypeAttribute;
307 #else
308   return 0;
309 #endif
310 }
311
312 Int_t AliRawReaderDate::GetEquipmentElementSize() const
313 {
314 // get the basic element size from the equipment header
315
316 #ifdef ALI_DATE
317   if (!fEquipment) return 0;
318   return fEquipment->equipmentBasicElementSize;
319 #else
320   return 0;
321 #endif
322 }
323
324 Int_t AliRawReaderDate::GetEquipmentHeaderSize() const
325 {
326   // Get the equipment header size
327   // 28 bytes by default
328 #ifdef ALI_DATE
329   return sizeof(equipmentHeaderStruct);
330 #else
331   return 0;
332 #endif
333 }
334
335 Bool_t AliRawReaderDate::ReadHeader()
336 {
337 // read a data header at the current position
338 // returns kFALSE if the data header could not be read
339
340   fErrorCode = 0;
341
342 #ifdef ALI_DATE
343   fHeader = NULL;
344   if (!fEvent) return kFALSE;
345   // check whether there are sub events
346   if (fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
347
348   do {
349     // skip payload (if event was not selected)
350     if (fCount > 0) fPosition += fCount;
351
352     // get the first or the next equipment if at the end of an equipment
353     if (!fEquipment || (fPosition >= fEnd)) {
354       fEquipment = NULL;
355
356       // get the first or the next sub event if at the end of a sub event
357       if (!fSubEvent || 
358           (fPosition >= ((UChar_t*)fSubEvent) + fSubEvent->eventSize)) {
359
360         // check for end of event data
361         if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
362         if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute, 
363                                    ATTR_SUPER_EVENT)) {
364           fSubEvent = fEvent;   // no super event
365         } else if (fSubEvent) {
366           fSubEvent = (eventHeaderStruct*) (((UChar_t*)fSubEvent) + 
367                                             fSubEvent->eventSize);
368         } else {
369           fSubEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) + 
370                                             fEvent->eventHeadSize);
371         }
372
373         // check the magic word of the sub event
374         if (fSubEvent->eventMagic != EVENT_MAGIC_NUMBER) {
375           Error("ReadHeader", "wrong magic number in sub event!\n"
376                 " run: %d  event: %d %d  LDC: %d  GDC: %d\n", 
377                 fSubEvent->eventRunNb, 
378                 fSubEvent->eventId[0], fSubEvent->eventId[1],
379                 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
380           fErrorCode = kErrMagic;
381           return kFALSE;
382         }
383
384         // continue if no data in the subevent
385         if (fSubEvent->eventSize == fSubEvent->eventHeadSize) {
386           fPosition = fEnd = ((UChar_t*)fSubEvent) + fSubEvent->eventSize;
387           fCount = 0;
388           continue;
389         }
390
391         fEquipment = (equipmentHeaderStruct*)
392           (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
393
394       } else {
395         fEquipment = (equipmentHeaderStruct*) fEnd;
396       }
397
398       fCount = 0;
399       fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
400       if (fSubEvent->eventVersion <= 0x00030001) {
401         fEnd = fPosition + fEquipment->equipmentSize;
402       } else {
403         fEnd = ((UChar_t*)fEquipment) + fEquipment->equipmentSize;
404       }
405     }
406
407     // continue with the next sub event if no data left in the payload
408     if (fPosition >= fEnd) continue;
409
410     if (fRequireHeader) {
411       // check that there are enough bytes left for the data header
412       if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
413         Error("ReadHeader", "could not read data header data!");
414         Warning("ReadHeader", "skipping %d bytes\n"
415                 " run: %d  event: %d %d  LDC: %d  GDC: %d\n", 
416                 fEnd - fPosition, fSubEvent->eventRunNb, 
417                 fSubEvent->eventId[0], fSubEvent->eventId[1],
418                 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
419         fCount = 0;
420         fPosition = fEnd;
421         fErrorCode = kErrNoDataHeader;
422         continue;
423       }
424
425       // "read" the data header
426       fHeader = (AliRawDataHeader*) fPosition;
427       if ((fPosition + fHeader->fSize) != fEnd) {
428         if ((fHeader->fSize != 0xFFFFFFFF) &&
429             (fEquipment->equipmentId != 4352))
430           Warning("ReadHeader",
431                   "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
432                   fHeader->fSize, fEnd - fPosition);
433         fHeader->fSize = fEnd - fPosition;
434       }
435       fPosition += sizeof(AliRawDataHeader);
436     }
437
438     if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
439       fCount = fHeader->fSize - sizeof(AliRawDataHeader);
440
441       // check consistency of data size in the header and in the sub event
442       if (fPosition + fCount > fEnd) {
443         Error("ReadHeader", "size in data header exceeds event size!");
444         Warning("ReadHeader", "skipping %d bytes\n"
445                 " run: %d  event: %d %d  LDC: %d  GDC: %d\n", 
446                 fEnd - fPosition, fSubEvent->eventRunNb, 
447                 fSubEvent->eventId[0], fSubEvent->eventId[1],
448                 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
449         fCount = 0;
450         fPosition = fEnd;
451         fErrorCode = kErrSize;
452         continue;
453       }
454
455     } else {
456       fCount = fEnd - fPosition;
457     }
458
459   } while (!fEquipment || !IsSelected());
460
461   return kTRUE;
462 #else
463   return kFALSE;
464 #endif
465 }
466
467 Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
468 {
469 // reads the next payload at the current position
470 // returns kFALSE if the data could not be read
471
472   fErrorCode = 0;
473   while (fCount == 0) {
474     if (!ReadHeader()) return kFALSE;
475   }
476   data = fPosition;
477   fPosition += fCount;  
478   fCount = 0;
479   return kTRUE;
480 }
481
482 Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
483 {
484 // reads the next block of data at the current position
485 // returns kFALSE if the data could not be read
486
487   fErrorCode = 0;
488   if (fPosition + size > fEnd) {
489     Error("ReadNext", "could not read data!");
490     fErrorCode = kErrOutOfBounds;
491     return kFALSE;
492   }
493   memcpy(data, fPosition, size);
494   fPosition += size;
495   fCount -= size;
496   return kTRUE;
497 }
498
499
500 Bool_t AliRawReaderDate::Reset()
501 {
502 // reset the current position to the beginning of the event
503
504 #ifdef ALI_DATE
505   fSubEvent = NULL;
506   fEquipment = NULL;
507 #endif
508   fCount = 0;
509   fPosition = fEnd = NULL;
510   return kTRUE;
511 }
512
513
514 Bool_t AliRawReaderDate::NextEvent()
515 {
516 // go to the next event in the date file
517
518 #ifdef ALI_DATE
519   if (!fFile) {
520     if (fEventNumber < 0 && fEvent) {
521       fEventNumber++;
522       return kTRUE;
523     }
524     else
525       return kFALSE;
526   }
527
528   Reset();
529   eventHeaderStruct header;
530   UInt_t headerSize = sizeof(eventHeaderStruct);
531   if (fEvent) delete[] fEvent;
532   fEvent = &header;
533
534   while (fread(&header, 1, headerSize, fFile) == headerSize) {
535     if (!IsEventSelected()) {
536       fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
537       continue;
538     }
539     UChar_t* buffer = new UChar_t[header.eventSize];
540     fseek(fFile, -(long)headerSize, SEEK_CUR);
541     if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) {
542       Error("NextEvent", "could not read event from file");
543       delete[] buffer;
544       break;
545     }
546     fEvent = (eventHeaderStruct*) buffer;
547     fEventNumber++;
548     return kTRUE;
549   };
550
551   fEvent = NULL;
552 #endif
553
554   return kFALSE;
555 }
556
557 Bool_t AliRawReaderDate::RewindEvents()
558 {
559 // go back to the beginning of the date file
560
561   if (fFile)
562     fseek(fFile, 0, SEEK_SET);
563
564   fEventNumber = -1;
565   return Reset();
566 }
567
568
569 Int_t AliRawReaderDate::CheckData() const
570 {
571 // check the consistency of the data
572
573 #ifdef ALI_DATE
574   if (!fEvent) return 0;
575   // check whether there are sub events
576   if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
577
578   eventHeaderStruct* subEvent = NULL;
579   UChar_t* position = 0;
580   UChar_t* end = 0;
581   Int_t result = 0;
582
583   while (kTRUE) {
584     // get the first or the next sub event if at the end of a sub event
585     if (!subEvent || (position >= end)) {
586
587       // check for end of event data
588       if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
589       if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute, 
590                                  ATTR_SUPER_EVENT)) {
591         subEvent = fEvent;   // no super event
592       } else if (subEvent) {
593         subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) + 
594                                          subEvent->eventSize);
595       } else {
596         subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) + 
597                                          fEvent->eventHeadSize);
598       }
599
600       // check the magic word of the sub event
601       if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
602         result |= kErrMagic;
603         return result;
604       }
605
606       position = ((UChar_t*)subEvent) + subEvent->eventHeadSize + 
607         sizeof(equipmentHeaderStruct);
608       end = ((UChar_t*)subEvent) + subEvent->eventSize;
609     }
610
611     // continue with the next sub event if no data left in the payload
612     if (position >= end) continue;
613
614     if (fRequireHeader) {
615     // check that there are enough bytes left for the data header
616       if (position + sizeof(AliRawDataHeader) > end) {
617         result |= kErrNoDataHeader;
618         position = end;
619         continue;
620       }
621
622       // check consistency of data size in the data header and in the sub event
623       AliRawDataHeader* header = (AliRawDataHeader*) position;
624       if ((position + header->fSize) != end) {
625         if (header->fSize != 0xFFFFFFFF)
626           Warning("ReadHeader",
627                   "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
628                   header->fSize, end - position);
629         header->fSize = end - position;
630         result |= kErrSize;
631       }
632     }
633     position = end;
634   };
635
636 #endif
637   return 0;
638 }