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 if ((fPosition + fHeader->fSize) != fEnd) {
431 if ((fHeader->fSize != 0xFFFFFFFF) &&
432 (fEquipment->equipmentId != 4352))
433 Warning("ReadHeader",
434 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
435 fHeader->fSize, fEnd - fPosition);
436 fHeader->fSize = fEnd - fPosition;
438 fPosition += sizeof(AliRawDataHeader);
441 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
442 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
444 // check consistency of data size in the header and in the sub event
445 if (fPosition + fCount > fEnd) {
446 Error("ReadHeader", "size in data header exceeds event size!");
447 Warning("ReadHeader", "skipping %d bytes\n"
448 " run: %d event: %d %d LDC: %d GDC: %d\n",
449 fEnd - fPosition, fSubEvent->eventRunNb,
450 fSubEvent->eventId[0], fSubEvent->eventId[1],
451 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
454 fErrorCode = kErrSize;
459 fCount = fEnd - fPosition;
462 } while (!fEquipment || !IsSelected());
470 Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
472 // reads the next payload at the current position
473 // returns kFALSE if the data could not be read
476 while (fCount == 0) {
477 if (!ReadHeader()) return kFALSE;
485 Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
487 // reads the next block of data at the current position
488 // returns kFALSE if the data could not be read
491 if (fPosition + size > fEnd) {
492 Error("ReadNext", "could not read data!");
493 fErrorCode = kErrOutOfBounds;
496 memcpy(data, fPosition, size);
503 Bool_t AliRawReaderDate::Reset()
505 // reset the current position to the beginning of the event
512 fPosition = fEnd = NULL;
517 Bool_t AliRawReaderDate::NextEvent()
519 // go to the next event in the date file
523 if (fEventNumber < 0 && fEvent) {
532 eventHeaderStruct header;
533 UInt_t headerSize = sizeof(eventHeaderStruct);
534 if (fEvent) delete[] fEvent;
537 while (fread(&header, 1, headerSize, fFile) == headerSize) {
538 if (!IsEventSelected()) {
539 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
542 UChar_t* buffer = new UChar_t[header.eventSize];
543 fseek(fFile, -(long)headerSize, SEEK_CUR);
544 if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) {
545 Error("NextEvent", "could not read event from file");
549 fEvent = (eventHeaderStruct*) buffer;
560 Bool_t AliRawReaderDate::RewindEvents()
562 // go back to the beginning of the date file
565 fseek(fFile, 0, SEEK_SET);
572 Int_t AliRawReaderDate::CheckData() const
574 // check the consistency of the data
577 if (!fEvent) return 0;
578 // check whether there are sub events
579 if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
581 eventHeaderStruct* subEvent = NULL;
582 UChar_t* position = 0;
587 // get the first or the next sub event if at the end of a sub event
588 if (!subEvent || (position >= end)) {
590 // check for end of event data
591 if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
592 if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
594 subEvent = fEvent; // no super event
595 } else if (subEvent) {
596 subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) +
597 subEvent->eventSize);
599 subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
600 fEvent->eventHeadSize);
603 // check the magic word of the sub event
604 if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
609 position = ((UChar_t*)subEvent) + subEvent->eventHeadSize +
610 sizeof(equipmentHeaderStruct);
611 end = ((UChar_t*)subEvent) + subEvent->eventSize;
614 // continue with the next sub event if no data left in the payload
615 if (position >= end) continue;
617 if (fRequireHeader) {
618 // check that there are enough bytes left for the data header
619 if (position + sizeof(AliRawDataHeader) > end) {
620 result |= kErrNoDataHeader;
625 // check consistency of data size in the data header and in the sub event
626 AliRawDataHeader* header = (AliRawDataHeader*) position;
627 if ((position + header->fSize) != end) {
628 if (header->fSize != 0xFFFFFFFF)
629 Warning("ReadHeader",
630 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
631 header->fSize, end - position);
632 header->fSize = end - position;
643 AliRawReader* AliRawReaderDate::CloneSingleEvent() const
645 // Clones the current event and
646 // creates raw-reader for the cloned event
647 // Can be used in order to make asynchronious
648 // access to the current raw data within
649 // several threads (online event display/reco)
653 UInt_t evSize = fEvent->eventSize;
655 UChar_t *newEvent = new UChar_t[evSize];
656 memcpy(newEvent,fEvent,evSize);
657 return new AliRawReaderDate((void *)newEvent,kTRUE);
661 Fatal("AliRawReaderDate", "this class was compiled without DATE");