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 FILE* file = fopen(fileName, "rb");
80 Error("AliRawReaderDate", "could not open file %s", fileName);
83 eventHeaderStruct header;
84 UInt_t headerSize = sizeof(eventHeaderStruct);
85 while (fread(&header, 1, headerSize, file) == headerSize) {
86 if (eventNumber == 0) {
87 UChar_t* buffer = new UChar_t[header.eventSize];
88 fseek(file, -headerSize, SEEK_CUR);
89 if (fread(buffer, 1, header.eventSize, file) != header.eventSize) break;
90 fEvent = (eventHeaderStruct*) buffer;
94 fseek(file, 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 fEvent(rawReader.fEvent),
108 fSubEvent(rawReader.fSubEvent),
109 fEquipment(rawReader.fEquipment),
111 fPosition(rawReader.fPosition),
119 AliRawReaderDate& AliRawReaderDate::operator = (const AliRawReaderDate&
122 // assignment operator
124 this->~AliRawReaderDate();
125 new(this) AliRawReaderDate(rawReader);
129 AliRawReaderDate::~AliRawReaderDate()
134 if (fIsOwner) delete[] fEvent;
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 UInt_t AliRawReaderDate::GetLDCId() const
213 // get the LDC Id from the event header
216 if (!fSubEvent) return 0;
217 return fSubEvent->eventLdcId;
223 UInt_t AliRawReaderDate::GetGDCId() const
225 // get the GDC Id from the event header
228 if (!fEvent) return 0;
229 return fEvent->eventGdcId;
236 Int_t AliRawReaderDate::GetEquipmentSize() const
238 // get the size of the equipment
241 if (!fEquipment) return 0;
242 return fEquipment->equipmentSize;
248 Int_t AliRawReaderDate::GetEquipmentType() const
250 // get the type from the equipment header
253 if (!fEquipment) return -1;
254 return fEquipment->equipmentType;
260 Int_t AliRawReaderDate::GetEquipmentId() const
262 // get the ID from the equipment header
265 if (!fEquipment) return -1;
266 return fEquipment->equipmentId;
272 const UInt_t* AliRawReaderDate::GetEquipmentAttributes() const
274 // get the attributes from the equipment header
277 if (!fEquipment) return NULL;
278 return fEquipment->equipmentTypeAttribute;
284 Int_t AliRawReaderDate::GetEquipmentElementSize() const
286 // get the basic element size from the equipment header
289 if (!fEquipment) return 0;
290 return fEquipment->equipmentBasicElementSize;
297 Bool_t AliRawReaderDate::ReadHeader()
299 // read a data header at the current position
300 // returns kFALSE if the data header could not be read
305 if (!fEvent) return kFALSE;
306 // check whether there are sub events
307 if (fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
310 // skip payload (if event was not selected)
311 if (fCount > 0) fPosition += fCount;
313 // get the first or the next equipment if at the end of an equipment
314 if (!fEquipment || (fPosition >= fEnd)) {
316 // get the first or the next sub event if at the end of a sub event
318 (fPosition >= ((UChar_t*)fSubEvent) + fSubEvent->eventSize)) {
320 // check for end of event data
321 if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
323 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fSubEvent) +
324 fSubEvent->eventSize);
326 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
327 fEvent->eventHeadSize);
330 // check the magic word of the sub event
331 if (fSubEvent->eventMagic != EVENT_MAGIC_NUMBER) {
332 Error("ReadHeader", "wrong magic number in sub event!\n"
333 " run: %d event: %d %d LDC: %d GDC: %d\n",
334 fSubEvent->eventRunNb,
335 fSubEvent->eventId[0], fSubEvent->eventId[1],
336 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
337 fErrorCode = kErrMagic;
341 fEquipment = (equipmentHeaderStruct*)
342 (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
345 fEquipment = (equipmentHeaderStruct*) fEnd;
349 fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
350 fEnd = fPosition + fEquipment->equipmentSize;
353 // continue with the next sub event if no data left in the payload
354 if (fPosition >= fEnd) continue;
357 if (fRequireHeader) {
358 // check that there are enough bytes left for the data header
359 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
360 Error("ReadHeader", "could not read data header data!");
361 Warning("ReadHeader", "skipping %d bytes\n"
362 " run: %d event: %d %d LDC: %d GDC: %d\n",
363 fEnd - fPosition, fSubEvent->eventRunNb,
364 fSubEvent->eventId[0], fSubEvent->eventId[1],
365 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
368 fErrorCode = kErrNoDataHeader;
372 // "read" the data header
373 fHeader = (AliRawDataHeader*) fPosition;
374 fPosition += sizeof(AliRawDataHeader);
377 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
378 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
380 // check consistency of data size in the header and in the sub event
381 if (fPosition + fCount > fEnd) {
382 Error("ReadHeader", "size in data header exceeds event size!");
383 Warning("ReadHeader", "skipping %d bytes\n"
384 " run: %d event: %d %d LDC: %d GDC: %d\n",
385 fEnd - fPosition, fSubEvent->eventRunNb,
386 fSubEvent->eventId[0], fSubEvent->eventId[1],
387 fSubEvent->eventLdcId, fSubEvent->eventGdcId);
390 fErrorCode = kErrSize;
395 fCount = fEnd - fPosition;
398 } while (!IsSelected());
406 Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
408 // reads the next payload at the current position
409 // returns kFALSE if the data could not be read
412 while (fCount == 0) {
413 if (!ReadHeader()) return kFALSE;
421 Bool_t AliRawReaderDate::ReadNext(UChar_t* data, Int_t size)
423 // reads the next block of data at the current position
424 // returns kFALSE if the data could not be read
427 if (fPosition + size > fEnd) {
428 Error("ReadNext", "could not read data!");
429 fErrorCode = kErrOutOfBounds;
432 memcpy(data, fPosition, size);
439 Bool_t AliRawReaderDate::Reset()
441 // reset the current position to the beginning of the event
447 fPosition = fEnd = NULL;
452 Int_t AliRawReaderDate::CheckData() const
454 // check the consistency of the data
457 if (!fEvent) return 0;
458 // check whether there are sub events
459 if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
461 eventHeaderStruct* subEvent = NULL;
462 UChar_t* position = 0;
467 // get the first or the next sub event if at the end of a sub event
468 if (!subEvent || (position >= end)) {
470 // check for end of event data
471 if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
473 subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) +
474 subEvent->eventSize);
476 subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
477 fEvent->eventHeadSize);
480 // check the magic word of the sub event
481 if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
486 position = ((UChar_t*)subEvent) + subEvent->eventHeadSize +
487 sizeof(equipmentHeaderStruct);
488 end = ((UChar_t*)subEvent) + subEvent->eventSize;
491 // continue with the next sub event if no data left in the payload
492 if (position >= end) continue;
494 // check that there are enough bytes left for the data header
495 if (position + sizeof(AliRawDataHeader) > end) {
496 result |= kErrNoDataHeader;
501 // check consistency of data size in the data header and in the sub event
502 AliRawDataHeader* header = (AliRawDataHeader*) position;
503 if (header->fSize != 0xFFFFFFFF) {
504 if (position + header->fSize > end) {
508 position += header->fSize;