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