]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReaderDate.cxx
Track points are moved according to vertex position so vertex is in (0,0,0)
[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   fRequireHeader(kTRUE),
43   fEvent(NULL),
44   fSubEvent(NULL),
45   fEquipment(NULL),
46   fIsOwner(kFALSE),
47   fPosition(NULL),
48   fEnd(NULL)
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   fRequireHeader(kTRUE),
68   fEvent(NULL),
69   fSubEvent(NULL),
70   fEquipment(NULL),
71   fIsOwner(kFALSE),
72   fPosition(NULL),
73   fEnd(NULL)
74 {
75 // create an object to read digits from the given date event
76
77 #ifdef ALI_DATE
78   FILE* file = fopen(fileName, "rb");
79   if (!file) {
80     Error("AliRawReaderDate", "could not open file %s", fileName);
81     return;
82   }
83   eventHeaderStruct header;
84   UInt_t headerSize = sizeof(eventHeaderStruct);
85   while (fread(&header, 1, headerSize, file) == headerSize) {
86     if (eventNumber == 0) {
87       UChar_t* buffer = new UChar_t[header.eventSize];
88       fseek(file, -headerSize, SEEK_CUR);
89       if (fread(buffer, 1, header.eventSize, file) != header.eventSize) break;
90       fEvent = (eventHeaderStruct*) buffer;
91       fIsOwner = kTRUE;
92       break;
93     }
94     fseek(file, header.eventSize-headerSize, SEEK_CUR);
95     eventNumber--;
96   }
97   fclose(file);
98
99 #else
100   Fatal("AliRawReaderDate", "this class was compiled without DATE");
101 #endif
102 }
103
104 AliRawReaderDate::AliRawReaderDate(const AliRawReaderDate& rawReader) :
105   AliRawReader(rawReader),
106   fRequireHeader(rawReader.fRequireHeader),
107   fEvent(rawReader.fEvent),
108   fSubEvent(rawReader.fSubEvent),
109   fEquipment(rawReader.fEquipment),
110   fIsOwner(kFALSE),
111   fPosition(rawReader.fPosition),
112   fEnd(rawReader.fEnd)
113
114 {
115 // copy constructor
116
117 }
118
119 AliRawReaderDate& AliRawReaderDate::operator = (const AliRawReaderDate& 
120                                                 rawReader)
121 {
122 // assignment operator
123
124   this->~AliRawReaderDate();
125   new(this) AliRawReaderDate(rawReader);
126   return *this;
127 }
128
129 AliRawReaderDate::~AliRawReaderDate()
130 {
131 // destructor
132
133 #ifdef ALI_DATE
134   if (fIsOwner) delete[] fEvent;
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 UInt_t AliRawReaderDate::GetLDCId() const
212 {
213 // get the LDC Id from the event header
214
215 #ifdef ALI_DATE
216   if (!fSubEvent) return 0;
217   return fSubEvent->eventLdcId;
218 #else
219   return 0;
220 #endif
221 }
222
223 UInt_t AliRawReaderDate::GetGDCId() const
224 {
225 // get the GDC Id from the event header
226
227 #ifdef ALI_DATE
228   if (!fEvent) return 0;
229   return fEvent->eventGdcId;
230 #else
231   return 0;
232 #endif
233 }
234
235
236 Int_t AliRawReaderDate::GetEquipmentSize() const
237 {
238 // get the size of the equipment
239
240 #ifdef ALI_DATE
241   if (!fEquipment) return 0;
242   return fEquipment->equipmentSize;
243 #else
244   return 0;
245 #endif
246 }
247
248 Int_t AliRawReaderDate::GetEquipmentType() const
249 {
250 // get the type from the equipment header
251
252 #ifdef ALI_DATE
253   if (!fEquipment) return -1;
254   return fEquipment->equipmentType;
255 #else
256   return 0;
257 #endif
258 }
259
260 Int_t AliRawReaderDate::GetEquipmentId() const
261 {
262 // get the ID from the equipment header
263
264 #ifdef ALI_DATE
265   if (!fEquipment) return -1;
266   return fEquipment->equipmentId;
267 #else
268   return 0;
269 #endif
270 }
271
272 const UInt_t* AliRawReaderDate::GetEquipmentAttributes() const
273 {
274 // get the attributes from the equipment header
275
276 #ifdef ALI_DATE
277   if (!fEquipment) return NULL;
278   return fEquipment->equipmentTypeAttribute;
279 #else
280   return 0;
281 #endif
282 }
283
284 Int_t AliRawReaderDate::GetEquipmentElementSize() const
285 {
286 // get the basic element size from the equipment header
287
288 #ifdef ALI_DATE
289   if (!fEquipment) return 0;
290   return fEquipment->equipmentBasicElementSize;
291 #else
292   return 0;
293 #endif
294 }
295
296
297 Bool_t AliRawReaderDate::ReadHeader()
298 {
299 // read a data header at the current position
300 // returns kFALSE if the data header could not be read
301
302   fErrorCode = 0;
303
304 #ifdef ALI_DATE
305   if (!fEvent) return kFALSE;
306   // check whether there are sub events
307   if (fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
308
309   do {
310     // skip payload (if event was not selected)
311     if (fCount > 0) fPosition += fCount;
312
313     // get the first or the next equipment if at the end of an equipment
314     if (!fEquipment || (fPosition >= fEnd)) {
315
316       // get the first or the next sub event if at the end of a sub event
317       if (!fSubEvent || 
318           (fPosition >= ((UChar_t*)fSubEvent) + fSubEvent->eventSize)) {
319
320         // check for end of event data
321         if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
322         if (fSubEvent) {
323           fSubEvent = (eventHeaderStruct*) (((UChar_t*)fSubEvent) + 
324                                             fSubEvent->eventSize);
325         } else {
326           fSubEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) + 
327                                             fEvent->eventHeadSize);
328         }
329
330         // check the magic word of the sub event
331         if (fSubEvent->eventMagic != EVENT_MAGIC_NUMBER) {
332           Error("ReadHeader", "wrong magic number in sub event!\n"
333                 " run: %d  event: %d %d  LDC: %d  GDC: %d\n", 
334                 fSubEvent->eventRunNb, 
335                 fSubEvent->eventId[0], fSubEvent->eventId[1],
336                 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
337           fErrorCode = kErrMagic;
338           return kFALSE;
339         }
340
341         fEquipment = (equipmentHeaderStruct*)
342           (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
343
344       } else {
345         fEquipment = (equipmentHeaderStruct*) fEnd;
346       }
347
348       fCount = 0;
349       fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
350       fEnd = fPosition + fEquipment->equipmentSize;
351     }
352
353     // continue with the next sub event if no data left in the payload
354     if (fPosition >= fEnd) continue;
355
356     fHeader = NULL;
357     if (fRequireHeader) {
358       // check that there are enough bytes left for the data header
359       if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
360         Error("ReadHeader", "could not read data header data!");
361         Warning("ReadHeader", "skipping %d bytes\n"
362                 " run: %d  event: %d %d  LDC: %d  GDC: %d\n", 
363                 fEnd - fPosition, fSubEvent->eventRunNb, 
364                 fSubEvent->eventId[0], fSubEvent->eventId[1],
365                 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
366         fCount = 0;
367         fPosition = fEnd;
368         fErrorCode = kErrNoDataHeader;
369         continue;
370       }
371
372       // "read" the data header
373       fHeader = (AliRawDataHeader*) fPosition;
374       fPosition += sizeof(AliRawDataHeader);
375     }
376
377     if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
378       fCount = fHeader->fSize - sizeof(AliRawDataHeader);
379
380       // check consistency of data size in the header and in the sub event
381       if (fPosition + fCount > fEnd) {
382         Error("ReadHeader", "size in data header exceeds event size!");
383         Warning("ReadHeader", "skipping %d bytes\n"
384                 " run: %d  event: %d %d  LDC: %d  GDC: %d\n", 
385                 fEnd - fPosition, fSubEvent->eventRunNb, 
386                 fSubEvent->eventId[0], fSubEvent->eventId[1],
387                 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
388         fCount = 0;
389         fPosition = fEnd;
390         fErrorCode = kErrSize;
391         continue;
392       }
393
394     } else {
395       fCount = fEnd - fPosition;
396     }
397
398   } while (!IsSelected());
399
400   return kTRUE;
401 #else
402   return kFALSE;
403 #endif
404 }
405
406 Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
407 {
408 // reads the next payload at the current position
409 // returns kFALSE if the data could not be read
410
411   fErrorCode = 0;
412   while (fCount == 0) {
413     if (!ReadHeader()) return kFALSE;
414   }
415   data = fPosition;
416   fPosition += fCount;  
417   fCount = 0;
418   return kTRUE;
419 }
420
421 Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
422 {
423 // reads the next block of data at the current position
424 // returns kFALSE if the data could not be read
425
426   fErrorCode = 0;
427   if (fPosition + size > fEnd) {
428     Error("ReadNext", "could not read data!");
429     fErrorCode = kErrOutOfBounds;
430     return kFALSE;
431   }
432   memcpy(data, fPosition, size);
433   fPosition += size;
434   fCount -= size;
435   return kTRUE;
436 }
437
438
439 Bool_t AliRawReaderDate::Reset()
440 {
441 // reset the current position to the beginning of the event
442
443 #ifdef ALI_DATE
444   fSubEvent = NULL;
445 #endif
446   fCount = 0;
447   fPosition = fEnd = NULL;
448   return kTRUE;
449 }
450
451
452 Int_t AliRawReaderDate::CheckData() const
453 {
454 // check the consistency of the data
455
456 #ifdef ALI_DATE
457   if (!fEvent) return 0;
458   // check whether there are sub events
459   if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
460
461   eventHeaderStruct* subEvent = NULL;
462   UChar_t* position = 0;
463   UChar_t* end = 0;
464   Int_t result = 0;
465
466   while (kTRUE) {
467     // get the first or the next sub event if at the end of a sub event
468     if (!subEvent || (position >= end)) {
469
470       // check for end of event data
471       if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
472       if (subEvent) {
473         subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) + 
474                                          subEvent->eventSize);
475       } else {
476         subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) + 
477                                          fEvent->eventHeadSize);
478       }
479
480       // check the magic word of the sub event
481       if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
482         result |= kErrMagic;
483         return result;
484       }
485
486       position = ((UChar_t*)subEvent) + subEvent->eventHeadSize + 
487         sizeof(equipmentHeaderStruct);
488       end = ((UChar_t*)subEvent) + subEvent->eventSize;
489     }
490
491     // continue with the next sub event if no data left in the payload
492     if (position >= end) continue;
493
494     // check that there are enough bytes left for the data header
495     if (position + sizeof(AliRawDataHeader) > end) {
496       result |= kErrNoDataHeader;
497       position = end;
498       continue;
499     }
500
501     // check consistency of data size in the data header and in the sub event
502     AliRawDataHeader* header = (AliRawDataHeader*) position;
503     if (header->fSize != 0xFFFFFFFF) {
504       if (position + header->fSize > end) {
505         result |= kErrSize;
506         position = end;
507       } else {
508         position += header->fSize;
509       }
510     } else {
511       position = end;
512     }
513   };
514
515 #endif
516   return 0;
517 }