Add ADCbaseline to AliTRDdigitsParam
[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
b900a426 132 if (fEvent) delete[] fEvent;
dd9a70fe 133 if (fFile) {
dd9a70fe 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) {
da1241fd 428 if ((fHeader->fSize != 0xFFFFFFFF) &&
429 (fEquipment->equipmentId != 4352))
806e573c 430 Warning("ReadHeader",
431 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
432 fHeader->fSize, fEnd - fPosition);
299738b9 433 fHeader->fSize = fEnd - fPosition;
434 }
39f9963f 435 fPosition += sizeof(AliRawDataHeader);
72dd1d4f 436 }
c946ab02 437
39f9963f 438 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
439 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
c946ab02 440
39f9963f 441 // check consistency of data size in the header and in the sub event
c946ab02 442 if (fPosition + fCount > fEnd) {
39f9963f 443 Error("ReadHeader", "size in data header exceeds event size!");
c946ab02 444 Warning("ReadHeader", "skipping %d bytes\n"
445 " run: %d event: %d %d LDC: %d GDC: %d\n",
446 fEnd - fPosition, fSubEvent->eventRunNb,
447 fSubEvent->eventId[0], fSubEvent->eventId[1],
448 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
449 fCount = 0;
450 fPosition = fEnd;
451 fErrorCode = kErrSize;
452 continue;
453 }
454
455 } else {
456 fCount = fEnd - fPosition;
16048fcf 457 }
b4857df7 458
921a8308 459 } while (!fEquipment || !IsSelected());
b4857df7 460
16048fcf 461 return kTRUE;
462#else
463 return kFALSE;
464#endif
465}
466
467Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
468{
469// reads the next payload at the current position
470// returns kFALSE if the data could not be read
471
b4857df7 472 fErrorCode = 0;
16048fcf 473 while (fCount == 0) {
c946ab02 474 if (!ReadHeader()) return kFALSE;
16048fcf 475 }
476 data = fPosition;
477 fPosition += fCount;
478 fCount = 0;
479 return kTRUE;
480}
481
482Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
483{
484// reads the next block of data at the current position
485// returns kFALSE if the data could not be read
486
b4857df7 487 fErrorCode = 0;
16048fcf 488 if (fPosition + size > fEnd) {
489 Error("ReadNext", "could not read data!");
b4857df7 490 fErrorCode = kErrOutOfBounds;
16048fcf 491 return kFALSE;
492 }
493 memcpy(data, fPosition, size);
494 fPosition += size;
495 fCount -= size;
496 return kTRUE;
497}
498
499
500Bool_t AliRawReaderDate::Reset()
501{
502// reset the current position to the beginning of the event
503
504#ifdef ALI_DATE
505 fSubEvent = NULL;
d7aa1c7c 506 fEquipment = NULL;
16048fcf 507#endif
508 fCount = 0;
509 fPosition = fEnd = NULL;
510 return kTRUE;
511}
b4857df7 512
513
dd9a70fe 514Bool_t AliRawReaderDate::NextEvent()
515{
516// go to the next event in the date file
517
5bec8712 518#ifdef ALI_DATE
b04576f7 519 if (!fFile) {
520 if (fEventNumber < 0 && fEvent) {
521 fEventNumber++;
522 return kTRUE;
523 }
524 else
525 return kFALSE;
526 }
dd9a70fe 527
d7aa1c7c 528 Reset();
dd9a70fe 529 eventHeaderStruct header;
530 UInt_t headerSize = sizeof(eventHeaderStruct);
531 if (fEvent) delete[] fEvent;
532 fEvent = &header;
533
534 while (fread(&header, 1, headerSize, fFile) == headerSize) {
535 if (!IsEventSelected()) {
536 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
537 continue;
538 }
539 UChar_t* buffer = new UChar_t[header.eventSize];
b91d160c 540 fseek(fFile, -(long)headerSize, SEEK_CUR);
dd9a70fe 541 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) {
542 Error("NextEvent", "could not read event from file");
543 delete[] buffer;
544 break;
545 }
546 fEvent = (eventHeaderStruct*) buffer;
38cf12f3 547 fEventNumber++;
dd9a70fe 548 return kTRUE;
549 };
550
551 fEvent = NULL;
bba5b21b 552#endif
5bec8712 553
dd9a70fe 554 return kFALSE;
555}
556
557Bool_t AliRawReaderDate::RewindEvents()
558{
559// go back to the beginning of the date file
560
b04576f7 561 if (fFile)
562 fseek(fFile, 0, SEEK_SET);
dd9a70fe 563
38cf12f3 564 fEventNumber = -1;
dd9a70fe 565 return Reset();
566}
567
568
b4857df7 569Int_t AliRawReaderDate::CheckData() const
570{
571// check the consistency of the data
572
573#ifdef ALI_DATE
574 if (!fEvent) return 0;
575 // check whether there are sub events
576 if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
577
578 eventHeaderStruct* subEvent = NULL;
579 UChar_t* position = 0;
580 UChar_t* end = 0;
be50fca2 581 Int_t result = 0;
b4857df7 582
583 while (kTRUE) {
584 // get the first or the next sub event if at the end of a sub event
585 if (!subEvent || (position >= end)) {
586
587 // check for end of event data
be50fca2 588 if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
5ba96ac1 589 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
590 ATTR_SUPER_EVENT)) {
591 subEvent = fEvent; // no super event
592 } else if (subEvent) {
b4857df7 593 subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) +
594 subEvent->eventSize);
595 } else {
596 subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
597 fEvent->eventHeadSize);
598 }
599
600 // check the magic word of the sub event
be50fca2 601 if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
602 result |= kErrMagic;
603 return result;
604 }
b4857df7 605
606 position = ((UChar_t*)subEvent) + subEvent->eventHeadSize +
607 sizeof(equipmentHeaderStruct);
608 end = ((UChar_t*)subEvent) + subEvent->eventSize;
609 }
610
611 // continue with the next sub event if no data left in the payload
612 if (position >= end) continue;
613
18882b3b 614 if (fRequireHeader) {
39f9963f 615 // check that there are enough bytes left for the data header
18882b3b 616 if (position + sizeof(AliRawDataHeader) > end) {
617 result |= kErrNoDataHeader;
39f9963f 618 position = end;
18882b3b 619 continue;
620 }
621
622 // check consistency of data size in the data header and in the sub event
623 AliRawDataHeader* header = (AliRawDataHeader*) position;
299738b9 624 if ((position + header->fSize) != end) {
806e573c 625 if (header->fSize != 0xFFFFFFFF)
626 Warning("ReadHeader",
627 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
628 header->fSize, end - position);
299738b9 629 header->fSize = end - position;
630 result |= kErrSize;
39f9963f 631 }
be50fca2 632 }
18882b3b 633 position = end;
b4857df7 634 };
635
636#endif
637 return 0;
638}
b900a426 639
640AliRawReader* AliRawReaderDate::CloneSingleEvent() const
641{
642 // Clones the current event and
643 // creates raw-reader for the cloned event
644 // Can be used in order to make asynchronious
645 // access to the current raw data within
646 // several threads (online event display/reco)
647
648#ifdef ALI_DATE
649 if (fEvent) {
650 UInt_t evSize = fEvent->eventSize;
651 if (evSize) {
652 UChar_t *newEvent = new UChar_t[evSize];
653 memcpy(newEvent,fEvent,evSize);
654 return new AliRawReaderDate((void *)newEvent);
655 }
656 }
657#else
658 Fatal("AliRawReaderDate", "this class was compiled without DATE");
659#endif
660 return NULL;
661}