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);
82 if (eventNumber < 0) return;
84 eventHeaderStruct header;
85 UInt_t headerSize = sizeof(eventHeaderStruct);
86 while (fread(&header, 1, headerSize, fFile) == headerSize) {
87 if (eventNumber == 0) {
88 UChar_t* buffer = new UChar_t[header.eventSize];
89 fseek(fFile, -(long)headerSize, SEEK_CUR);
90 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) break;
91 fEvent = (eventHeaderStruct*) buffer;
94 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
99 Fatal("AliRawReaderDate", "this class was compiled without DATE");
103 AliRawReaderDate::AliRawReaderDate(const AliRawReaderDate& rawReader) :
104 AliRawReader(rawReader),
105 fFile(rawReader.fFile),
106 fEvent(rawReader.fEvent),
107 fSubEvent(rawReader.fSubEvent),
108 fEquipment(rawReader.fEquipment),
109 fPosition(rawReader.fPosition),
115 Fatal("AliRawReaderDate", "copy constructor not implemented");
118 AliRawReaderDate& AliRawReaderDate::operator = (const AliRawReaderDate&
121 // assignment operator
123 Fatal("operator =", "assignment operator not implemented");
127 AliRawReaderDate::~AliRawReaderDate()
140 UInt_t AliRawReaderDate::GetType() const
142 // get the type from the event header
145 if (!fEvent) return 0;
146 return fEvent->eventType;
152 UInt_t AliRawReaderDate::GetRunNumber() const
154 // get the run number from the event header
157 if (!fEvent) return 0;
158 return fEvent->eventRunNb;
164 const UInt_t* AliRawReaderDate::GetEventId() const
166 // get the event id from the event header
169 if (!fEvent) return NULL;
170 return fEvent->eventId;
176 const UInt_t* AliRawReaderDate::GetTriggerPattern() const
178 // get the trigger pattern from the event header
181 if (!fEvent) return NULL;
182 return fEvent->eventTriggerPattern;
188 const UInt_t* AliRawReaderDate::GetDetectorPattern() const
190 // get the detector pattern from the event header
193 if (!fEvent) return NULL;
194 return fEvent->eventDetectorPattern;
200 const UInt_t* AliRawReaderDate::GetAttributes() const
202 // get the type attributes from the event header
205 if (!fEvent) return NULL;
206 return fEvent->eventTypeAttribute;
212 const UInt_t* AliRawReaderDate::GetSubEventAttributes() const
214 // get the type attributes from the sub event header
217 if (!fSubEvent) return NULL;
218 return fSubEvent->eventTypeAttribute;
224 UInt_t AliRawReaderDate::GetLDCId() const
226 // get the LDC Id from the event header
229 if (!fSubEvent) return 0;
230 return fSubEvent->eventLdcId;
236 UInt_t AliRawReaderDate::GetGDCId() const
238 // get the GDC Id from the event header
241 if (!fEvent) return 0;
242 return fEvent->eventGdcId;
248 UInt_t AliRawReaderDate::GetTimestamp() const
250 // get the timestamp from the event header
253 if (!fEvent) return 0;
254 return fEvent->eventTimestamp;
260 Int_t AliRawReaderDate::GetEquipmentSize() const
262 // get the size of the equipment (including the header)
265 if (!fEquipment) return 0;
266 if (fSubEvent->eventVersion <= 0x00030001) {
267 return fEquipment->equipmentSize + sizeof(equipmentHeaderStruct);
269 return fEquipment->equipmentSize;
276 Int_t AliRawReaderDate::GetEquipmentType() const
278 // get the type from the equipment header
281 if (!fEquipment) return -1;
282 return fEquipment->equipmentType;
288 Int_t AliRawReaderDate::GetEquipmentId() const
290 // get the ID from the equipment header
293 if (!fEquipment) return -1;
294 return fEquipment->equipmentId;
300 const UInt_t* AliRawReaderDate::GetEquipmentAttributes() const
302 // get the attributes from the equipment header
305 if (!fEquipment) return NULL;
306 return fEquipment->equipmentTypeAttribute;
312 Int_t AliRawReaderDate::GetEquipmentElementSize() const
314 // get the basic element size from the equipment header
317 if (!fEquipment) return 0;
318 return fEquipment->equipmentBasicElementSize;
324 Int_t AliRawReaderDate::GetEquipmentHeaderSize() const
326 // Get the equipment header size
327 // 28 bytes by default
329 return sizeof(equipmentHeaderStruct);
335 Bool_t AliRawReaderDate::ReadHeader()
337 // read a data header at the current position
338 // returns kFALSE if the data header could not be read
344 if (!fEvent) return kFALSE;
345 // check whether there are sub events
346 if (fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
349 // skip payload (if event was not selected)
350 if (fCount > 0) fPosition += fCount;
352 // get the first or the next equipment if at the end of an equipment
353 if (!fEquipment || (fPosition >= fEnd)) {
356 // get the first or the next sub event if at the end of a sub event
358 (fPosition >= ((UChar_t*)fSubEvent) + fSubEvent->eventSize)) {
360 // check for end of event data
361 if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
362 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
364 fSubEvent = fEvent; // no super event
365 } else if (fSubEvent) {
366 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fSubEvent) +
367 fSubEvent->eventSize);
369 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
370 fEvent->eventHeadSize);
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;
384 // continue if no data in the subevent
385 if (fSubEvent->eventSize == fSubEvent->eventHeadSize) {
386 fPosition = fEnd = ((UChar_t*)fSubEvent) + fSubEvent->eventSize;
391 fEquipment = (equipmentHeaderStruct*)
392 (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
395 fEquipment = (equipmentHeaderStruct*) fEnd;
399 fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
400 if (fSubEvent->eventVersion <= 0x00030001) {
401 fEnd = fPosition + fEquipment->equipmentSize;
403 fEnd = ((UChar_t*)fEquipment) + fEquipment->equipmentSize;
407 // continue with the next sub event if no data left in the payload
408 if (fPosition >= fEnd) continue;
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!");
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);
421 fErrorCode = kErrNoDataHeader;
425 // "read" the data header
426 fHeader = (AliRawDataHeader*) fPosition;
427 if ((fPosition + fHeader->fSize) != fEnd) {
428 if ((fHeader->fSize != 0xFFFFFFFF) &&
429 (fEquipment->equipmentId != 4352))
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);
433 fHeader->fSize = fEnd - fPosition;
435 fPosition += sizeof(AliRawDataHeader);
438 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
439 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
441 // check consistency of data size in the header and in the sub event
442 if (fPosition + fCount > fEnd) {
443 Error("ReadHeader", "size in data header exceeds event size!");
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);
451 fErrorCode = kErrSize;
456 fCount = fEnd - fPosition;
459 } while (!fEquipment || !IsSelected());
467 Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
469 // reads the next payload at the current position
470 // returns kFALSE if the data could not be read
473 while (fCount == 0) {
474 if (!ReadHeader()) return kFALSE;
482 Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
484 // reads the next block of data at the current position
485 // returns kFALSE if the data could not be read
488 if (fPosition + size > fEnd) {
489 Error("ReadNext", "could not read data!");
490 fErrorCode = kErrOutOfBounds;
493 memcpy(data, fPosition, size);
500 Bool_t AliRawReaderDate::Reset()
502 // reset the current position to the beginning of the event
509 fPosition = fEnd = NULL;
514 Bool_t AliRawReaderDate::NextEvent()
516 // go to the next event in the date file
520 if (fEventNumber < 0 && fEvent) {
529 eventHeaderStruct header;
530 UInt_t headerSize = sizeof(eventHeaderStruct);
531 if (fEvent) delete[] fEvent;
534 while (fread(&header, 1, headerSize, fFile) == headerSize) {
535 if (!IsEventSelected()) {
536 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
539 UChar_t* buffer = new UChar_t[header.eventSize];
540 fseek(fFile, -(long)headerSize, SEEK_CUR);
541 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) {
542 Error("NextEvent", "could not read event from file");
546 fEvent = (eventHeaderStruct*) buffer;
557 Bool_t AliRawReaderDate::RewindEvents()
559 // go back to the beginning of the date file
562 fseek(fFile, 0, SEEK_SET);
569 Int_t AliRawReaderDate::CheckData() const
571 // check the consistency of the data
574 if (!fEvent) return 0;
575 // check whether there are sub events
576 if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
578 eventHeaderStruct* subEvent = NULL;
579 UChar_t* position = 0;
584 // get the first or the next sub event if at the end of a sub event
585 if (!subEvent || (position >= end)) {
587 // check for end of event data
588 if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
589 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
591 subEvent = fEvent; // no super event
592 } else if (subEvent) {
593 subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) +
594 subEvent->eventSize);
596 subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
597 fEvent->eventHeadSize);
600 // check the magic word of the sub event
601 if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
606 position = ((UChar_t*)subEvent) + subEvent->eventHeadSize +
607 sizeof(equipmentHeaderStruct);
608 end = ((UChar_t*)subEvent) + subEvent->eventSize;
611 // continue with the next sub event if no data left in the payload
612 if (position >= end) continue;
614 if (fRequireHeader) {
615 // check that there are enough bytes left for the data header
616 if (position + sizeof(AliRawDataHeader) > end) {
617 result |= kErrNoDataHeader;
622 // check consistency of data size in the data header and in the sub event
623 AliRawDataHeader* header = (AliRawDataHeader*) position;
624 if ((position + header->fSize) != end) {
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);
629 header->fSize = end - position;