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