1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 /// This is a class for reading raw data from a date file or event.
22 /// The AliRawReaderDate is constructed either with a pointer to a
23 /// date event or with a file name and an event number.
25 ///////////////////////////////////////////////////////////////////////////////
27 #include "AliRawReaderDate.h"
32 ClassImp(AliRawReaderDate)
35 AliRawReaderDate::AliRawReaderDate(
37 void* event, Bool_t owner
39 void* /* event */, Bool_t owner
50 // create an object to read digits from the given date event
53 fEvent = (eventHeaderStruct*) event;
55 Fatal("AliRawReaderDate", "this class was compiled without DATE");
59 AliRawReaderDate::AliRawReaderDate(
61 const char* fileName, Int_t eventNumber
63 const char* /*fileName*/,
75 // create an object to read digits from the given date event
78 fFile = fopen(fileName, "rb");
80 Error("AliRawReaderDate", "could not open file %s", fileName);
84 if (eventNumber < 0) return;
86 eventHeaderStruct header;
87 UInt_t headerSize = sizeof(eventHeaderStruct);
88 while (fread(&header, 1, headerSize, fFile) == headerSize) {
89 if (eventNumber == 0) {
90 UChar_t* buffer = new UChar_t[header.eventSize];
91 fseek(fFile, -(long)headerSize, SEEK_CUR);
92 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) break;
93 fEvent = (eventHeaderStruct*) buffer;
96 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
101 Fatal("AliRawReaderDate", "this class was compiled without DATE");
105 AliRawReaderDate::AliRawReaderDate(const AliRawReaderDate& rawReader) :
106 AliRawReader(rawReader),
107 fFile(rawReader.fFile),
108 fEvent(rawReader.fEvent),
109 fSubEvent(rawReader.fSubEvent),
110 fEquipment(rawReader.fEquipment),
111 fPosition(rawReader.fPosition),
112 fEnd(rawReader.fEnd),
113 fOwner(rawReader.fOwner)
118 Fatal("AliRawReaderDate", "copy constructor not implemented");
121 AliRawReaderDate& AliRawReaderDate::operator = (const AliRawReaderDate&
124 // assignment operator
126 Fatal("operator =", "assignment operator not implemented");
130 AliRawReaderDate::~AliRawReaderDate()
135 if (fEvent && fOwner) delete[] fEvent;
143 UInt_t AliRawReaderDate::GetType() const
145 // get the type from the event header
148 if (!fEvent) return 0;
149 return fEvent->eventType;
155 UInt_t AliRawReaderDate::GetRunNumber() const
157 // get the run number from the event header
160 if (!fEvent) return 0;
161 return fEvent->eventRunNb;
167 const UInt_t* AliRawReaderDate::GetEventId() const
169 // get the event id from the event header
172 if (!fEvent) return NULL;
173 return fEvent->eventId;
179 const UInt_t* AliRawReaderDate::GetTriggerPattern() const
181 // get the trigger pattern from the event header
184 if (!fEvent) return NULL;
185 return fEvent->eventTriggerPattern;
191 const UInt_t* AliRawReaderDate::GetDetectorPattern() const
193 // get the detector pattern from the event header
196 if (!fEvent) return NULL;
197 return fEvent->eventDetectorPattern;
203 const UInt_t* AliRawReaderDate::GetAttributes() const
205 // get the type attributes from the event header
208 if (!fEvent) return NULL;
209 return fEvent->eventTypeAttribute;
215 const UInt_t* AliRawReaderDate::GetSubEventAttributes() const
217 // get the type attributes from the sub event header
220 if (!fSubEvent) return NULL;
221 return fSubEvent->eventTypeAttribute;
227 UInt_t AliRawReaderDate::GetLDCId() const
229 // get the LDC Id from the event header
232 if (!fSubEvent) return 0;
233 return fSubEvent->eventLdcId;
239 UInt_t AliRawReaderDate::GetGDCId() const
241 // get the GDC Id from the event header
244 if (!fEvent) return 0;
245 return fEvent->eventGdcId;
251 UInt_t AliRawReaderDate::GetTimestamp() const
253 // get the timestamp from the event header
256 if (!fEvent) return 0;
257 return fEvent->eventTimestamp;
263 Int_t AliRawReaderDate::GetEquipmentSize() const
265 // get the size of the equipment (including the header)
268 if (!fEquipment) return 0;
269 if (fSubEvent->eventVersion <= 0x00030001) {
270 return fEquipment->equipmentSize + sizeof(equipmentHeaderStruct);
272 return fEquipment->equipmentSize;
279 Int_t AliRawReaderDate::GetEquipmentType() const
281 // get the type from the equipment header
284 if (!fEquipment) return -1;
285 return fEquipment->equipmentType;
291 Int_t AliRawReaderDate::GetEquipmentId() const
293 // get the ID from the equipment header
296 if (!fEquipment) return -1;
297 return fEquipment->equipmentId;
303 const UInt_t* AliRawReaderDate::GetEquipmentAttributes() const
305 // get the attributes from the equipment header
308 if (!fEquipment) return NULL;
309 return fEquipment->equipmentTypeAttribute;
315 Int_t AliRawReaderDate::GetEquipmentElementSize() const
317 // get the basic element size from the equipment header
320 if (!fEquipment) return 0;
321 return fEquipment->equipmentBasicElementSize;
327 Int_t AliRawReaderDate::GetEquipmentHeaderSize() const
329 // Get the equipment header size
330 // 28 bytes by default
332 return sizeof(equipmentHeaderStruct);
338 Bool_t AliRawReaderDate::ReadHeader()
340 // read a data header at the current position
341 // returns kFALSE if the data header could not be read
347 if (!fEvent) return kFALSE;
348 // check whether there are sub events
349 if (fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
352 // skip payload (if event was not selected)
353 if (fCount > 0) fPosition += fCount;
355 // get the first or the next equipment if at the end of an equipment
356 if (!fEquipment || (fPosition >= fEnd)) {
359 // get the first or the next sub event if at the end of a sub event
361 (fPosition >= ((UChar_t*)fSubEvent) + fSubEvent->eventSize)) {
363 // check for end of event data
364 if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
365 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
367 fSubEvent = fEvent; // no super event
368 } else if (fSubEvent) {
369 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fSubEvent) +
370 fSubEvent->eventSize);
372 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
373 fEvent->eventHeadSize);
376 // check the magic word of the sub event
377 if (fSubEvent->eventMagic != EVENT_MAGIC_NUMBER) {
378 Error("ReadHeader", "wrong magic number in sub event!\n"
379 " run: %d event: %d %d LDC: %d GDC: %d\n",
380 fSubEvent->eventRunNb,
381 fSubEvent->eventId[0], fSubEvent->eventId[1],
382 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
383 fErrorCode = kErrMagic;
387 // continue if no data in the subevent
388 if (fSubEvent->eventSize == fSubEvent->eventHeadSize) {
389 fPosition = fEnd = ((UChar_t*)fSubEvent) + fSubEvent->eventSize;
394 fEquipment = (equipmentHeaderStruct*)
395 (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
398 fEquipment = (equipmentHeaderStruct*) fEnd;
402 fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
403 if (fSubEvent->eventVersion <= 0x00030001) {
404 fEnd = fPosition + fEquipment->equipmentSize;
406 fEnd = ((UChar_t*)fEquipment) + fEquipment->equipmentSize;
410 // continue with the next sub event if no data left in the payload
411 if (fPosition >= fEnd) continue;
413 if (fRequireHeader) {
414 // check that there are enough bytes left for the data header
415 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
416 Error("ReadHeader", "could not read data header data!");
417 Warning("ReadHeader", "skipping %d bytes\n"
418 " run: %d event: %d %d LDC: %d GDC: %d\n",
419 fEnd - fPosition, fSubEvent->eventRunNb,
420 fSubEvent->eventId[0], fSubEvent->eventId[1],
421 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
424 fErrorCode = kErrNoDataHeader;
428 // "read" the data header
429 fHeader = (AliRawDataHeader*) fPosition;
430 // Now check the version of the header
432 if (fHeader) version=fHeader->GetVersion();
435 if ((fPosition + fHeader->fSize) != fEnd) {
436 if ((fHeader->fSize != 0xFFFFFFFF) &&
437 (fEquipment->equipmentId != 4352))
438 Warning("ReadHeader",
439 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
440 fHeader->fSize, fEnd - fPosition);
441 fHeader->fSize = fEnd - fPosition;
443 fPosition += sizeof(AliRawDataHeader);
445 } else if (version==3) {
446 fHeaderV3 = (AliRawDataHeaderV3*) fPosition;
447 if ((fPosition + fHeaderV3->fSize) != fEnd) {
448 if ((fHeaderV3->fSize != 0xFFFFFFFF) &&
449 (fEquipment->equipmentId != 4352))
450 Warning("ReadHeader",
451 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
452 fHeaderV3->fSize, fEnd - fPosition);
453 fHeaderV3->fSize = fEnd - fPosition;
455 fPosition += sizeof(AliRawDataHeaderV3);
460 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
461 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
463 // check consistency of data size in the header and in the sub event
464 if (fPosition + fCount > fEnd) {
465 Error("ReadHeader", "size in data header exceeds event size!");
466 Warning("ReadHeader", "skipping %d bytes\n"
467 " run: %d event: %d %d LDC: %d GDC: %d\n",
468 fEnd - fPosition, fSubEvent->eventRunNb,
469 fSubEvent->eventId[0], fSubEvent->eventId[1],
470 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
473 fErrorCode = kErrSize;
477 } else if (fHeaderV3 && (fHeaderV3->fSize != 0xFFFFFFFF)) {
478 fCount = fHeaderV3->fSize - sizeof(AliRawDataHeaderV3);
480 // check consistency of data size in the header and in the sub event
481 if (fPosition + fCount > fEnd) {
482 Error("ReadHeader", "size in data header exceeds event size!");
483 Warning("ReadHeader", "skipping %d bytes\n"
484 " run: %d event: %d %d LDC: %d GDC: %d\n",
485 fEnd - fPosition, fSubEvent->eventRunNb,
486 fSubEvent->eventId[0], fSubEvent->eventId[1],
487 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
490 fErrorCode = kErrSize;
495 fCount = fEnd - fPosition;
498 } while (!fEquipment || !IsSelected());
506 Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
508 // reads the next payload at the current position
509 // returns kFALSE if the data could not be read
512 while (fCount == 0) {
513 if (!ReadHeader()) return kFALSE;
521 Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
523 // reads the next block of data at the current position
524 // returns kFALSE if the data could not be read
527 if (fPosition + size > fEnd) {
528 Error("ReadNext", "could not read data!");
529 fErrorCode = kErrOutOfBounds;
532 memcpy(data, fPosition, size);
539 Bool_t AliRawReaderDate::Reset()
541 // reset the current position to the beginning of the event
548 fPosition = fEnd = NULL;
555 Bool_t AliRawReaderDate::NextEvent()
557 // go to the next event in the date file
561 if (fEventNumber < 0 && fEvent) {
570 eventHeaderStruct header;
571 UInt_t headerSize = sizeof(eventHeaderStruct);
572 if (fEvent) delete[] fEvent;
575 while (fread(&header, 1, headerSize, fFile) == headerSize) {
576 if (!IsEventSelected()) {
577 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
580 UChar_t* buffer = new UChar_t[header.eventSize];
581 fseek(fFile, -(long)headerSize, SEEK_CUR);
582 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) {
583 Error("NextEvent", "could not read event from file");
587 fEvent = (eventHeaderStruct*) buffer;
598 Bool_t AliRawReaderDate::RewindEvents()
600 // go back to the beginning of the date file
603 fseek(fFile, 0, SEEK_SET);
610 Int_t AliRawReaderDate::CheckData() const
612 // check the consistency of the data
615 if (!fEvent) return 0;
616 // check whether there are sub events
617 if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
619 eventHeaderStruct* subEvent = NULL;
620 UChar_t* position = 0;
625 // get the first or the next sub event if at the end of a sub event
626 if (!subEvent || (position >= end)) {
628 // check for end of event data
629 if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
630 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
632 subEvent = fEvent; // no super event
633 } else if (subEvent) {
634 subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) +
635 subEvent->eventSize);
637 subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
638 fEvent->eventHeadSize);
641 // check the magic word of the sub event
642 if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
647 position = ((UChar_t*)subEvent) + subEvent->eventHeadSize +
648 sizeof(equipmentHeaderStruct);
649 end = ((UChar_t*)subEvent) + subEvent->eventSize;
652 // continue with the next sub event if no data left in the payload
653 if (position >= end) continue;
655 if (fRequireHeader) {
656 // check that there are enough bytes left for the data header
657 if (position + sizeof(AliRawDataHeader) > end) {
658 result |= kErrNoDataHeader;
663 // check consistency of data size in the data header and in the sub event
664 AliRawDataHeader* header = (AliRawDataHeader*) position;
665 if ((position + header->fSize) != end) {
666 if (header->fSize != 0xFFFFFFFF)
667 Warning("ReadHeader",
668 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
669 header->fSize, end - position);
670 header->fSize = end - position;
681 AliRawReader* AliRawReaderDate::CloneSingleEvent() const
683 // Clones the current event and
684 // creates raw-reader for the cloned event
685 // Can be used in order to make asynchronious
686 // access to the current raw data within
687 // several threads (online event display/reco)
691 UInt_t evSize = fEvent->eventSize;
693 UChar_t *newEvent = new UChar_t[evSize];
694 memcpy(newEvent,fEvent,evSize);
695 return new AliRawReaderDate((void *)newEvent,kTRUE);
699 Fatal("AliRawReaderDate", "this class was compiled without DATE");