]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReaderDate.cxx
Changes for #92834: Implement trigger aliases in AliRoot, RAW data reconstruction
[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       if ((fPosition + fHeader->fSize) != fEnd) {
431         if ((fHeader->fSize != 0xFFFFFFFF) &&
432             (fEquipment->equipmentId != 4352))
433           Warning("ReadHeader",
434                   "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
435                   fHeader->fSize, fEnd - fPosition);
436         fHeader->fSize = fEnd - fPosition;
437       }
438       fPosition += sizeof(AliRawDataHeader);
439     }
440
441     if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
442       fCount = fHeader->fSize - sizeof(AliRawDataHeader);
443
444       // check consistency of data size in the header and in the sub event
445       if (fPosition + fCount > fEnd) {
446         Error("ReadHeader", "size in data header exceeds event size!");
447         Warning("ReadHeader", "skipping %d bytes\n"
448                 " run: %d  event: %d %d  LDC: %d  GDC: %d\n", 
449                 fEnd - fPosition, fSubEvent->eventRunNb, 
450                 fSubEvent->eventId[0], fSubEvent->eventId[1],
451                 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
452         fCount = 0;
453         fPosition = fEnd;
454         fErrorCode = kErrSize;
455         continue;
456       }
457
458     } else {
459       fCount = fEnd - fPosition;
460     }
461
462   } while (!fEquipment || !IsSelected());
463
464   return kTRUE;
465 #else
466   return kFALSE;
467 #endif
468 }
469
470 Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
471 {
472 // reads the next payload at the current position
473 // returns kFALSE if the data could not be read
474
475   fErrorCode = 0;
476   while (fCount == 0) {
477     if (!ReadHeader()) return kFALSE;
478   }
479   data = fPosition;
480   fPosition += fCount;  
481   fCount = 0;
482   return kTRUE;
483 }
484
485 Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
486 {
487 // reads the next block of data at the current position
488 // returns kFALSE if the data could not be read
489
490   fErrorCode = 0;
491   if (fPosition + size > fEnd) {
492     Error("ReadNext", "could not read data!");
493     fErrorCode = kErrOutOfBounds;
494     return kFALSE;
495   }
496   memcpy(data, fPosition, size);
497   fPosition += size;
498   fCount -= size;
499   return kTRUE;
500 }
501
502
503 Bool_t AliRawReaderDate::Reset()
504 {
505 // reset the current position to the beginning of the event
506
507 #ifdef ALI_DATE
508   fSubEvent = NULL;
509   fEquipment = NULL;
510 #endif
511   fCount = 0;
512   fPosition = fEnd = NULL;
513   return kTRUE;
514 }
515
516
517 Bool_t AliRawReaderDate::NextEvent()
518 {
519 // go to the next event in the date file
520
521 #ifdef ALI_DATE
522   if (!fFile) {
523     if (fEventNumber < 0 && fEvent) {
524       fEventNumber++;
525       return kTRUE;
526     }
527     else
528       return kFALSE;
529   }
530
531   Reset();
532   eventHeaderStruct header;
533   UInt_t headerSize = sizeof(eventHeaderStruct);
534   if (fEvent) delete[] fEvent;
535   fEvent = &header;
536
537   while (fread(&header, 1, headerSize, fFile) == headerSize) {
538     if (!IsEventSelected()) {
539       fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
540       continue;
541     }
542     UChar_t* buffer = new UChar_t[header.eventSize];
543     fseek(fFile, -(long)headerSize, SEEK_CUR);
544     if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) {
545       Error("NextEvent", "could not read event from file");
546       delete[] buffer;
547       break;
548     }
549     fEvent = (eventHeaderStruct*) buffer;
550     fEventNumber++;
551     return kTRUE;
552   };
553
554   fEvent = NULL;
555 #endif
556
557   return kFALSE;
558 }
559
560 Bool_t AliRawReaderDate::RewindEvents()
561 {
562 // go back to the beginning of the date file
563
564   if (fFile)
565     fseek(fFile, 0, SEEK_SET);
566
567   fEventNumber = -1;
568   return Reset();
569 }
570
571
572 Int_t AliRawReaderDate::CheckData() const
573 {
574 // check the consistency of the data
575
576 #ifdef ALI_DATE
577   if (!fEvent) return 0;
578   // check whether there are sub events
579   if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
580
581   eventHeaderStruct* subEvent = NULL;
582   UChar_t* position = 0;
583   UChar_t* end = 0;
584   Int_t result = 0;
585
586   while (kTRUE) {
587     // get the first or the next sub event if at the end of a sub event
588     if (!subEvent || (position >= end)) {
589
590       // check for end of event data
591       if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
592       if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute, 
593                                  ATTR_SUPER_EVENT)) {
594         subEvent = fEvent;   // no super event
595       } else if (subEvent) {
596         subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) + 
597                                          subEvent->eventSize);
598       } else {
599         subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) + 
600                                          fEvent->eventHeadSize);
601       }
602
603       // check the magic word of the sub event
604       if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
605         result |= kErrMagic;
606         return result;
607       }
608
609       position = ((UChar_t*)subEvent) + subEvent->eventHeadSize + 
610         sizeof(equipmentHeaderStruct);
611       end = ((UChar_t*)subEvent) + subEvent->eventSize;
612     }
613
614     // continue with the next sub event if no data left in the payload
615     if (position >= end) continue;
616
617     if (fRequireHeader) {
618     // check that there are enough bytes left for the data header
619       if (position + sizeof(AliRawDataHeader) > end) {
620         result |= kErrNoDataHeader;
621         position = end;
622         continue;
623       }
624
625       // check consistency of data size in the data header and in the sub event
626       AliRawDataHeader* header = (AliRawDataHeader*) position;
627       if ((position + header->fSize) != end) {
628         if (header->fSize != 0xFFFFFFFF)
629           Warning("ReadHeader",
630                   "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
631                   header->fSize, end - position);
632         header->fSize = end - position;
633         result |= kErrSize;
634       }
635     }
636     position = end;
637   };
638
639 #endif
640   return 0;
641 }
642
643 AliRawReader* AliRawReaderDate::CloneSingleEvent() const
644 {
645   // Clones the current event and
646   // creates raw-reader for the cloned event
647   // Can be used in order to make asynchronious
648   // access to the current raw data within
649   // several threads (online event display/reco)
650
651 #ifdef ALI_DATE
652   if (fEvent) {
653     UInt_t evSize = fEvent->eventSize;
654     if (evSize) {
655       UChar_t *newEvent = new UChar_t[evSize];
656       memcpy(newEvent,fEvent,evSize);
657       return new AliRawReaderDate((void *)newEvent,kTRUE);
658     }
659   }
660 #else
661   Fatal("AliRawReaderDate", "this class was compiled without DATE");
662 #endif
663   return NULL;
664 }