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(
49 // create an object to read digits from the given date event
52 fEvent = (eventHeaderStruct*) event;
54 Fatal("AliRawReaderDate", "this class was compiled without DATE");
58 AliRawReaderDate::AliRawReaderDate(
60 const char* fileName, Int_t eventNumber
62 const char* /*fileName*/,
73 // create an object to read digits from the given date event
76 fFile = fopen(fileName, "rb");
78 Error("AliRawReaderDate", "could not open file %s", fileName);
81 if (eventNumber < 0) return;
83 eventHeaderStruct header;
84 UInt_t headerSize = sizeof(eventHeaderStruct);
85 while (fread(&header, 1, headerSize, fFile) == headerSize) {
86 if (eventNumber == 0) {
87 UChar_t* buffer = new UChar_t[header.eventSize];
88 fseek(fFile, -(long)headerSize, SEEK_CUR);
89 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) break;
90 fEvent = (eventHeaderStruct*) buffer;
93 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
98 Fatal("AliRawReaderDate", "this class was compiled without DATE");
102 AliRawReaderDate::AliRawReaderDate(const AliRawReaderDate& rawReader) :
103 AliRawReader(rawReader),
104 fFile(rawReader.fFile),
105 fEvent(rawReader.fEvent),
106 fSubEvent(rawReader.fSubEvent),
107 fEquipment(rawReader.fEquipment),
108 fPosition(rawReader.fPosition),
114 Fatal("AliRawReaderDate", "copy constructor not implemented");
117 AliRawReaderDate& AliRawReaderDate::operator = (const AliRawReaderDate&
120 // assignment operator
122 Fatal("operator =", "assignment operator not implemented");
126 AliRawReaderDate::~AliRawReaderDate()
139 UInt_t AliRawReaderDate::GetType() const
141 // get the type from the event header
144 if (!fEvent) return 0;
145 return fEvent->eventType;
151 UInt_t AliRawReaderDate::GetRunNumber() const
153 // get the run number from the event header
156 if (!fEvent) return 0;
157 return fEvent->eventRunNb;
163 const UInt_t* AliRawReaderDate::GetEventId() const
165 // get the event id from the event header
168 if (!fEvent) return NULL;
169 return fEvent->eventId;
175 const UInt_t* AliRawReaderDate::GetTriggerPattern() const
177 // get the trigger pattern from the event header
180 if (!fEvent) return NULL;
181 return fEvent->eventTriggerPattern;
187 const UInt_t* AliRawReaderDate::GetDetectorPattern() const
189 // get the detector pattern from the event header
192 if (!fEvent) return NULL;
193 return fEvent->eventDetectorPattern;
199 const UInt_t* AliRawReaderDate::GetAttributes() const
201 // get the type attributes from the event header
204 if (!fEvent) return NULL;
205 return fEvent->eventTypeAttribute;
211 const UInt_t* AliRawReaderDate::GetSubEventAttributes() const
213 // get the type attributes from the sub event header
216 if (!fSubEvent) return NULL;
217 return fSubEvent->eventTypeAttribute;
223 UInt_t AliRawReaderDate::GetLDCId() const
225 // get the LDC Id from the event header
228 if (!fSubEvent) return 0;
229 return fSubEvent->eventLdcId;
235 UInt_t AliRawReaderDate::GetGDCId() const
237 // get the GDC Id from the event header
240 if (!fEvent) return 0;
241 return fEvent->eventGdcId;
247 UInt_t AliRawReaderDate::GetTimestamp() const
249 // get the timestamp from the event header
252 if (!fEvent) return 0;
253 return fEvent->eventTimestamp;
259 Int_t AliRawReaderDate::GetEquipmentSize() const
261 // get the size of the equipment (including the header)
264 if (!fEquipment) return 0;
265 if (fSubEvent->eventVersion <= 0x00030001) {
266 return fEquipment->equipmentSize + sizeof(equipmentHeaderStruct);
268 return fEquipment->equipmentSize;
275 Int_t AliRawReaderDate::GetEquipmentType() const
277 // get the type from the equipment header
280 if (!fEquipment) return -1;
281 return fEquipment->equipmentType;
287 Int_t AliRawReaderDate::GetEquipmentId() const
289 // get the ID from the equipment header
292 if (!fEquipment) return -1;
293 return fEquipment->equipmentId;
299 const UInt_t* AliRawReaderDate::GetEquipmentAttributes() const
301 // get the attributes from the equipment header
304 if (!fEquipment) return NULL;
305 return fEquipment->equipmentTypeAttribute;
311 Int_t AliRawReaderDate::GetEquipmentElementSize() const
313 // get the basic element size from the equipment header
316 if (!fEquipment) return 0;
317 return fEquipment->equipmentBasicElementSize;
323 Int_t AliRawReaderDate::GetEquipmentHeaderSize() const
325 // Get the equipment header size
326 // 28 bytes by default
328 return sizeof(equipmentHeaderStruct);
334 Bool_t AliRawReaderDate::ReadHeader()
336 // read a data header at the current position
337 // returns kFALSE if the data header could not be read
343 if (!fEvent) return kFALSE;
344 // check whether there are sub events
345 if (fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
348 // skip payload (if event was not selected)
349 if (fCount > 0) fPosition += fCount;
351 // get the first or the next equipment if at the end of an equipment
352 if (!fEquipment || (fPosition >= fEnd)) {
355 // get the first or the next sub event if at the end of a sub event
357 (fPosition >= ((UChar_t*)fSubEvent) + fSubEvent->eventSize)) {
359 // check for end of event data
360 if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
361 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
363 fSubEvent = fEvent; // no super event
364 } else if (fSubEvent) {
365 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fSubEvent) +
366 fSubEvent->eventSize);
368 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
369 fEvent->eventHeadSize);
372 // check the magic word of the sub event
373 if (fSubEvent->eventMagic != EVENT_MAGIC_NUMBER) {
374 Error("ReadHeader", "wrong magic number in sub event!\n"
375 " run: %d event: %d %d LDC: %d GDC: %d\n",
376 fSubEvent->eventRunNb,
377 fSubEvent->eventId[0], fSubEvent->eventId[1],
378 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
379 fErrorCode = kErrMagic;
383 // continue if no data in the subevent
384 if (fSubEvent->eventSize == fSubEvent->eventHeadSize) {
385 fPosition = fEnd = ((UChar_t*)fSubEvent) + fSubEvent->eventSize;
390 fEquipment = (equipmentHeaderStruct*)
391 (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
394 fEquipment = (equipmentHeaderStruct*) fEnd;
398 fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
399 if (fSubEvent->eventVersion <= 0x00030001) {
400 fEnd = fPosition + fEquipment->equipmentSize;
402 fEnd = ((UChar_t*)fEquipment) + fEquipment->equipmentSize;
406 // continue with the next sub event if no data left in the payload
407 if (fPosition >= fEnd) continue;
409 if (fRequireHeader) {
410 // check that there are enough bytes left for the data header
411 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
412 Error("ReadHeader", "could not read data header data!");
413 Warning("ReadHeader", "skipping %d bytes\n"
414 " run: %d event: %d %d LDC: %d GDC: %d\n",
415 fEnd - fPosition, fSubEvent->eventRunNb,
416 fSubEvent->eventId[0], fSubEvent->eventId[1],
417 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
420 fErrorCode = kErrNoDataHeader;
424 // "read" the data header
425 fHeader = (AliRawDataHeader*) fPosition;
426 if ((fPosition + fHeader->fSize) != fEnd) {
427 if (fHeader->fSize != 0xFFFFFFFF)
428 Warning("ReadHeader",
429 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
430 fHeader->fSize, fEnd - fPosition);
431 fHeader->fSize = fEnd - fPosition;
433 fPosition += sizeof(AliRawDataHeader);
436 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
437 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
439 // check consistency of data size in the header and in the sub event
440 if (fPosition + fCount > fEnd) {
441 Error("ReadHeader", "size in data header exceeds event size!");
442 Warning("ReadHeader", "skipping %d bytes\n"
443 " run: %d event: %d %d LDC: %d GDC: %d\n",
444 fEnd - fPosition, fSubEvent->eventRunNb,
445 fSubEvent->eventId[0], fSubEvent->eventId[1],
446 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
449 fErrorCode = kErrSize;
454 fCount = fEnd - fPosition;
457 } while (!fEquipment || !IsSelected());
465 Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
467 // reads the next payload at the current position
468 // returns kFALSE if the data could not be read
471 while (fCount == 0) {
472 if (!ReadHeader()) return kFALSE;
480 Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
482 // reads the next block of data at the current position
483 // returns kFALSE if the data could not be read
486 if (fPosition + size > fEnd) {
487 Error("ReadNext", "could not read data!");
488 fErrorCode = kErrOutOfBounds;
491 memcpy(data, fPosition, size);
498 Bool_t AliRawReaderDate::Reset()
500 // reset the current position to the beginning of the event
507 fPosition = fEnd = NULL;
512 Bool_t AliRawReaderDate::NextEvent()
514 // go to the next event in the date file
517 if (!fFile) return kFALSE;
520 eventHeaderStruct header;
521 UInt_t headerSize = sizeof(eventHeaderStruct);
522 if (fEvent) delete[] fEvent;
525 while (fread(&header, 1, headerSize, fFile) == headerSize) {
526 if (!IsEventSelected()) {
527 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
530 UChar_t* buffer = new UChar_t[header.eventSize];
531 fseek(fFile, -(long)headerSize, SEEK_CUR);
532 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) {
533 Error("NextEvent", "could not read event from file");
537 fEvent = (eventHeaderStruct*) buffer;
548 Bool_t AliRawReaderDate::RewindEvents()
550 // go back to the beginning of the date file
552 if (!fFile) return kFALSE;
554 fseek(fFile, 0, SEEK_SET);
560 Int_t AliRawReaderDate::CheckData() const
562 // check the consistency of the data
565 if (!fEvent) return 0;
566 // check whether there are sub events
567 if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
569 eventHeaderStruct* subEvent = NULL;
570 UChar_t* position = 0;
575 // get the first or the next sub event if at the end of a sub event
576 if (!subEvent || (position >= end)) {
578 // check for end of event data
579 if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
580 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
582 subEvent = fEvent; // no super event
583 } else if (subEvent) {
584 subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) +
585 subEvent->eventSize);
587 subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
588 fEvent->eventHeadSize);
591 // check the magic word of the sub event
592 if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
597 position = ((UChar_t*)subEvent) + subEvent->eventHeadSize +
598 sizeof(equipmentHeaderStruct);
599 end = ((UChar_t*)subEvent) + subEvent->eventSize;
602 // continue with the next sub event if no data left in the payload
603 if (position >= end) continue;
605 if (fRequireHeader) {
606 // check that there are enough bytes left for the data header
607 if (position + sizeof(AliRawDataHeader) > end) {
608 result |= kErrNoDataHeader;
613 // check consistency of data size in the data header and in the sub event
614 AliRawDataHeader* header = (AliRawDataHeader*) position;
615 if ((position + header->fSize) != end) {
616 if (header->fSize != 0xFFFFFFFF)
617 Warning("ReadHeader",
618 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
619 header->fSize, end - position);
620 header->fSize = end - position;