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