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(
42 fRequireHeader(kTRUE),
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*/,
67 fRequireHeader(kTRUE),
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);
83 if (eventNumber < 0) return;
85 eventHeaderStruct header;
86 UInt_t headerSize = sizeof(eventHeaderStruct);
87 while (fread(&header, 1, headerSize, fFile) == headerSize) {
88 if (eventNumber == 0) {
89 UChar_t* buffer = new UChar_t[header.eventSize];
90 fseek(fFile, -headerSize, SEEK_CUR);
91 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) break;
92 fEvent = (eventHeaderStruct*) buffer;
95 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
100 Fatal("AliRawReaderDate", "this class was compiled without DATE");
104 AliRawReaderDate::AliRawReaderDate(const AliRawReaderDate& rawReader) :
105 AliRawReader(rawReader),
106 fRequireHeader(rawReader.fRequireHeader),
107 fFile(rawReader.fFile),
108 fEvent(rawReader.fEvent),
109 fSubEvent(rawReader.fSubEvent),
110 fEquipment(rawReader.fEquipment),
111 fPosition(rawReader.fPosition),
117 Fatal("AliRawReaderDate", "copy constructor not implemented");
120 AliRawReaderDate& AliRawReaderDate::operator = (const AliRawReaderDate&
123 // assignment operator
125 Fatal("operator =", "assignment operator not implemented");
129 AliRawReaderDate::~AliRawReaderDate()
142 UInt_t AliRawReaderDate::GetType() const
144 // get the type from the event header
147 if (!fEvent) return 0;
148 return fEvent->eventType;
154 UInt_t AliRawReaderDate::GetRunNumber() const
156 // get the run number from the event header
159 if (!fEvent) return 0;
160 return fEvent->eventRunNb;
166 const UInt_t* AliRawReaderDate::GetEventId() const
168 // get the event id from the event header
171 if (!fEvent) return NULL;
172 return fEvent->eventId;
178 const UInt_t* AliRawReaderDate::GetTriggerPattern() const
180 // get the trigger pattern from the event header
183 if (!fEvent) return NULL;
184 return fEvent->eventTriggerPattern;
190 const UInt_t* AliRawReaderDate::GetDetectorPattern() const
192 // get the detector pattern from the event header
195 if (!fEvent) return NULL;
196 return fEvent->eventDetectorPattern;
202 const UInt_t* AliRawReaderDate::GetAttributes() const
204 // get the type attributes from the event header
207 if (!fEvent) return NULL;
208 return fEvent->eventTypeAttribute;
214 const UInt_t* AliRawReaderDate::GetSubEventAttributes() const
216 // get the type attributes from the sub event header
219 if (!fSubEvent) return NULL;
220 return fSubEvent->eventTypeAttribute;
226 UInt_t AliRawReaderDate::GetLDCId() const
228 // get the LDC Id from the event header
231 if (!fSubEvent) return 0;
232 return fSubEvent->eventLdcId;
238 UInt_t AliRawReaderDate::GetGDCId() const
240 // get the GDC Id from the event header
243 if (!fEvent) return 0;
244 return fEvent->eventGdcId;
251 Int_t AliRawReaderDate::GetEquipmentSize() const
253 // get the size of the equipment (without header)
256 if (!fEquipment) return 0;
257 if (fSubEvent->eventVersion <= 0x00030001) {
258 return fEquipment->equipmentSize;
260 return fEquipment->equipmentSize - sizeof(equipmentHeaderStruct);
267 Int_t AliRawReaderDate::GetEquipmentType() const
269 // get the type from the equipment header
272 if (!fEquipment) return -1;
273 return fEquipment->equipmentType;
279 Int_t AliRawReaderDate::GetEquipmentId() const
281 // get the ID from the equipment header
284 if (!fEquipment) return -1;
285 return fEquipment->equipmentId;
291 const UInt_t* AliRawReaderDate::GetEquipmentAttributes() const
293 // get the attributes from the equipment header
296 if (!fEquipment) return NULL;
297 return fEquipment->equipmentTypeAttribute;
303 Int_t AliRawReaderDate::GetEquipmentElementSize() const
305 // get the basic element size from the equipment header
308 if (!fEquipment) return 0;
309 return fEquipment->equipmentBasicElementSize;
316 Bool_t AliRawReaderDate::ReadHeader()
318 // read a data header at the current position
319 // returns kFALSE if the data header could not be read
325 if (!fEvent) return kFALSE;
326 // check whether there are sub events
327 if (fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
330 // skip payload (if event was not selected)
331 if (fCount > 0) fPosition += fCount;
333 // get the first or the next equipment if at the end of an equipment
334 if (!fEquipment || (fPosition >= fEnd)) {
337 // get the first or the next sub event if at the end of a sub event
339 (fPosition >= ((UChar_t*)fSubEvent) + fSubEvent->eventSize)) {
341 // check for end of event data
342 if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
343 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
345 fSubEvent = fEvent; // no super event
346 } else if (fSubEvent) {
347 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fSubEvent) +
348 fSubEvent->eventSize);
350 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
351 fEvent->eventHeadSize);
354 // check the magic word of the sub event
355 if (fSubEvent->eventMagic != EVENT_MAGIC_NUMBER) {
356 Error("ReadHeader", "wrong magic number in sub event!\n"
357 " run: %d event: %d %d LDC: %d GDC: %d\n",
358 fSubEvent->eventRunNb,
359 fSubEvent->eventId[0], fSubEvent->eventId[1],
360 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
361 fErrorCode = kErrMagic;
365 // continue if no data in the subevent
366 if (fSubEvent->eventSize == fSubEvent->eventHeadSize) {
367 fPosition = fEnd = ((UChar_t*)fSubEvent) + fSubEvent->eventSize;
372 fEquipment = (equipmentHeaderStruct*)
373 (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
376 fEquipment = (equipmentHeaderStruct*) fEnd;
380 fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
381 if (fSubEvent->eventVersion <= 0x00030001) {
382 fEnd = fPosition + fEquipment->equipmentSize;
384 fEnd = ((UChar_t*)fEquipment) + fEquipment->equipmentSize;
388 // continue with the next sub event if no data left in the payload
389 if (fPosition >= fEnd) continue;
391 if (fRequireHeader) {
392 // check that there are enough bytes left for the data header
393 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
394 Error("ReadHeader", "could not read data header data!");
395 Warning("ReadHeader", "skipping %d bytes\n"
396 " run: %d event: %d %d LDC: %d GDC: %d\n",
397 fEnd - fPosition, fSubEvent->eventRunNb,
398 fSubEvent->eventId[0], fSubEvent->eventId[1],
399 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
402 fErrorCode = kErrNoDataHeader;
406 // "read" the data header
407 fHeader = (AliRawDataHeader*) fPosition;
408 fPosition += sizeof(AliRawDataHeader);
411 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
412 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
414 // check consistency of data size in the header and in the sub event
415 if (fPosition + fCount > fEnd) {
416 Error("ReadHeader", "size in data header exceeds event size!");
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 = kErrSize;
429 fCount = fEnd - fPosition;
432 } while (!fEquipment || !IsSelected());
440 Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
442 // reads the next payload at the current position
443 // returns kFALSE if the data could not be read
446 while (fCount == 0) {
447 if (!ReadHeader()) return kFALSE;
455 Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
457 // reads the next block of data at the current position
458 // returns kFALSE if the data could not be read
461 if (fPosition + size > fEnd) {
462 Error("ReadNext", "could not read data!");
463 fErrorCode = kErrOutOfBounds;
466 memcpy(data, fPosition, size);
473 Bool_t AliRawReaderDate::Reset()
475 // reset the current position to the beginning of the event
482 fPosition = fEnd = NULL;
487 Bool_t AliRawReaderDate::NextEvent()
489 // go to the next event in the date file
492 if (!fFile) return kFALSE;
495 eventHeaderStruct header;
496 UInt_t headerSize = sizeof(eventHeaderStruct);
497 if (fEvent) delete[] fEvent;
500 while (fread(&header, 1, headerSize, fFile) == headerSize) {
501 if (!IsEventSelected()) {
502 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
505 UChar_t* buffer = new UChar_t[header.eventSize];
506 fseek(fFile, -headerSize, SEEK_CUR);
507 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) {
508 Error("NextEvent", "could not read event from file");
512 fEvent = (eventHeaderStruct*) buffer;
522 Bool_t AliRawReaderDate::RewindEvents()
524 // go back to the beginning of the date file
526 if (!fFile) return kFALSE;
528 fseek(fFile, 0, SEEK_SET);
533 Int_t AliRawReaderDate::CheckData() const
535 // check the consistency of the data
538 if (!fEvent) return 0;
539 // check whether there are sub events
540 if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
542 eventHeaderStruct* subEvent = NULL;
543 UChar_t* position = 0;
548 // get the first or the next sub event if at the end of a sub event
549 if (!subEvent || (position >= end)) {
551 // check for end of event data
552 if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
553 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
555 subEvent = fEvent; // no super event
556 } else if (subEvent) {
557 subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) +
558 subEvent->eventSize);
560 subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
561 fEvent->eventHeadSize);
564 // check the magic word of the sub event
565 if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
570 position = ((UChar_t*)subEvent) + subEvent->eventHeadSize +
571 sizeof(equipmentHeaderStruct);
572 end = ((UChar_t*)subEvent) + subEvent->eventSize;
575 // continue with the next sub event if no data left in the payload
576 if (position >= end) continue;
578 // check that there are enough bytes left for the data header
579 if (position + sizeof(AliRawDataHeader) > end) {
580 result |= kErrNoDataHeader;
585 // check consistency of data size in the data header and in the sub event
586 AliRawDataHeader* header = (AliRawDataHeader*) position;
587 if (header->fSize != 0xFFFFFFFF) {
588 if (position + header->fSize > end) {
592 position += header->fSize;