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