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