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 AliPHOSRawReaderDate is constructed either with a pointer to a
23 /// date event or with a file name and an event number.
25 ///////////////////////////////////////////////////////////////////////////////
28 #include "AliPHOSRawReaderDate.h"
29 #include "Riostream.h"
31 ClassImp(AliPHOSRawReaderDate)
33 //---------------------------------------------------------
34 AliPHOSRawReaderDate::AliPHOSRawReaderDate(void* event) :
35 fRequireHeader(kTRUE),
43 // create an object to read digits from the given date event
44 fEvent = (eventHeaderStruct*) event;
46 //---------------------------------------------------------
47 AliPHOSRawReaderDate::AliPHOSRawReaderDate(const char* fileName, Int_t eventNumber):
48 fRequireHeader(kTRUE),
56 // create an object to read digits from the given date event
58 // if(strstr(fileName,".gz") )
59 // sprintf(command,"zcat %s",fileName);
61 // sprintf(command,"cat %s",fileName);
62 // printf("Comand %s \n",command) ;
63 // fFile = popen("zcat Run_3186.dat.gz", "rb");
65 fFile = fopen(fileName, "rb");
68 Error("AliPHOSRawReaderDate", "could not open file %s", fileName);
71 if (eventNumber < 0) return;
73 eventHeaderStruct header;
74 UInt_t headerSize = sizeof(eventHeaderStruct);
75 while (fread(&header, 1, headerSize, fFile) == headerSize) {
76 if (eventNumber == 0) {
77 UChar_t* buffer = new UChar_t[header.eventSize];
78 fseek(fFile, -headerSize, SEEK_CUR);
79 if (fread(buffer, 1, header.eventSize, fFile) != (UInt_t)header.eventSize) break;
80 fEvent = (eventHeaderStruct*) buffer;
83 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
87 //---------------------------------------------------------
88 AliPHOSRawReaderDate::AliPHOSRawReaderDate(const AliPHOSRawReaderDate& rawReader) :
89 AliRawReader(rawReader),
90 fRequireHeader(rawReader.fRequireHeader),
91 fFile(rawReader.fFile),
92 fEvent(rawReader.fEvent),
93 fSubEvent(rawReader.fSubEvent),
94 fEquipment(rawReader.fEquipment),
95 fPosition(rawReader.fPosition),
101 Fatal("AliPHOSRawReaderDate", "copy constructor not implemented");
103 //---------------------------------------------------------
104 AliPHOSRawReaderDate& AliPHOSRawReaderDate::operator = (const AliPHOSRawReaderDate&
107 // assignment operator
109 Fatal("operator =", "assignment operator not implemented");
112 //---------------------------------------------------------
113 AliPHOSRawReaderDate::~AliPHOSRawReaderDate()
123 //---------------------------------------------------------
124 Bool_t AliPHOSRawReaderDate::ReadHeader()
126 // read a data header at the current position
127 // returns kFALSE if the data header could not be read
132 if (!fEvent) return kFALSE;
134 // Analize, if event header good to use. Swap event, if need.
135 if (fEvent->eventMagic != EVENT_MAGIC_NUMBER){
136 if (fEvent->eventMagic == EVENT_MAGIC_NUMBER_SWAPPED) {
140 Error("ReadHader","Wrong event magic number, MAGIC == %08x (expected %08x).\n",
141 fEvent->eventMagic, EVENT_MAGIC_NUMBER);
142 fErrorCode = kErrMagic;
147 // check whether there are sub events
148 if ((UInt_t)fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
151 // skip payload (if event was not selected)
152 if (fCount > 0) fPosition += fCount;
154 // get the first or the next equipment if at the end of an equipment
155 if (!fEquipment || (fPosition >= fEnd)) {
158 // printf("No equipment defined \n") ;
160 // get the first or the next sub event if at the end of a sub event
162 (fPosition >= ((UChar_t*)fSubEvent) + fSubEvent->eventSize)) {
164 // printf("GetNext subevent \n") ;
166 // printf("fPosition fPosition=%p fEvent=%p Size=%d Ev+size%p \n",(UChar_t*)fPosition,(UChar_t*)fEvent,fEvent->eventSize,(((UChar_t*)fEvent)+fEvent->eventSize)) ;
168 // check for end of event data
169 if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
171 // printf("position passed \n") ;
173 if(!(fEvent->detectorId[0] & SUPER_EVENT_MASK)){
174 fSubEvent = fEvent; // no super event
175 // printf("superevent \n") ;
176 } else if (fSubEvent) {
177 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fSubEvent) +
178 fSubEvent->eventSize);
179 // printf("second subevent \n ") ;
181 fSubEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
182 fEvent->eventHeadSize);
183 // printf("first subevent \n") ;
186 // check the magic word of the sub event
187 if (fSubEvent->eventMagic != EVENT_MAGIC_NUMBER) {
188 if (fSubEvent->eventMagic == EVENT_MAGIC_NUMBER_SWAPPED) {
189 SwappEvent(fSubEvent);
192 Error("ReadHeader", "wrong magic number in sub event!\n"
193 " run: %d \n",fSubEvent->eventRunNb) ;
194 fErrorCode = kErrMagic;
199 // continue if no data in the subevent
200 if ((UInt_t)fSubEvent->eventSize == fSubEvent->eventHeadSize) {
201 fPosition = fEnd = ((UChar_t*)fSubEvent) + fSubEvent->eventSize;
203 // printf("no data in subevent \n") ;
207 // printf("Set equipment after header of subevent \n") ;
208 fEquipment = (equipmentHeaderStruct*)
209 (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
212 fEquipment = (equipmentHeaderStruct*) fEnd;
213 // printf("equipment set at fEnd \n" ) ;
217 fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
218 fEnd = fPosition + fEquipment->rawDataLen;
221 // continue with the next sub event if no data left in the payload
222 if (fPosition >= fEnd) continue;
224 fCount = fEnd - fPosition;
226 // printf("Selected \n") ;
228 // printf("Not selected \n") ;
230 } while (!fEquipment || !IsSelected());
234 //---------------------------------------------------------
235 Bool_t AliPHOSRawReaderDate::ReadNextData(UChar_t*& data)
237 // reads the next payload at the current position
238 // returns kFALSE if the data could not be read
241 while (fCount == 0) {
242 if (!ReadHeader()) return kFALSE;
246 // printf("fCount %d \n",fCount) ;
250 //---------------------------------------------------------
251 Bool_t AliPHOSRawReaderDate::ReadNext(UChar_t* data, Int_t size)
253 // reads the next block of data at the current position
254 // returns kFALSE if the data could not be read
257 if (fPosition + size > fEnd) {
258 Error("ReadNext", "could not read data!");
259 fErrorCode = kErrOutOfBounds;
262 memcpy(data, fPosition, size);
267 //---------------------------------------------------------
268 Bool_t AliPHOSRawReaderDate::Reset()
270 // reset the current position to the beginning of the event
274 fPosition = fEnd = NULL;
277 //---------------------------------------------------------
278 Bool_t AliPHOSRawReaderDate::NextEvent()
280 // go to the next event in the date file
282 if (!fFile) return kFALSE;
284 // printf("Old fEvent = %p \n",(UChar_t*)fEvent) ;
285 eventHeaderStruct header;
286 UInt_t headerSize = sizeof(eventHeaderStruct);
287 if (fEvent) delete[] fEvent;
289 // printf("New fEvent = %p \n",(UChar_t*)fEvent) ;
291 while (fread(&header, 1, headerSize, fFile) == headerSize) {
292 // printf("read \n") ;
293 if (!IsEventSelected()) {
294 // printf("selected \n") ;
295 fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
298 UChar_t* buffer = new UChar_t[header.eventSize];
299 fseek(fFile, -headerSize, SEEK_CUR);
300 if (fread(buffer, 1, header.eventSize, fFile) != (UInt_t)header.eventSize) {
301 Error("NextEvent", "could not read event from file");
305 fEvent = (eventHeaderStruct*) buffer;
306 // printf("Read fEvent = %p \n",(UChar_t*)fEvent) ;
317 //---------------------------------------------------------
318 Bool_t AliPHOSRawReaderDate::RewindEvents()
320 // go back to the beginning of the date file
322 if (!fFile) return kFALSE;
324 fseek(fFile, 0, SEEK_SET);
327 //---------------------------------------------------------
328 Int_t AliPHOSRawReaderDate::CheckData() const
330 // check the consistency of the data
332 if (!fEvent) return 0;
333 // check whether there are sub events
334 if ((UInt_t)fEvent->eventSize <= fEvent->eventHeadSize) return 0;
336 eventHeaderStruct* subEvent = NULL;
337 UChar_t* position = 0;
342 // get the first or the next sub event if at the end of a sub event
343 if (!subEvent || (position >= end)) {
345 // check for end of event data
346 if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
348 // if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute,
349 // ATTR_SUPER_EVENT)) {
350 subEvent = fEvent; // no super event
351 // } else if (subEvent) {
352 // subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) +
353 // subEvent->eventSize);
355 // subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) +
356 // fEvent->eventHeadSize);
359 // check the magic word of the sub event
360 if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
365 position = ((UChar_t*)subEvent) + subEvent->eventHeadSize +
366 sizeof(equipmentHeaderStruct);
367 end = ((UChar_t*)subEvent) + subEvent->eventSize;
370 // continue with the next sub event if no data left in the payload
371 if (position >= end) continue;
373 // check that there are enough bytes left for the data header
374 if (position + sizeof(AliRawDataHeader) > end) {
375 result |= kErrNoDataHeader;
380 // check consistency of data size in the data header and in the sub event
381 AliRawDataHeader* header = (AliRawDataHeader*) position;
382 if (header->fSize != 0xFFFFFFFF) {
383 if (position + header->fSize > end) {
387 position += header->fSize;
396 //---------------------------------------------------------
397 void AliPHOSRawReaderDate::SwappEvent(eventHeaderStruct * event)
399 if(event->eventMagic == EVENT_MAGIC_NUMBER_SWAPPED)
401 ChangeOrder(event->eventSize);
402 ChangeOrder(event->eventType);
403 ChangeOrder(event->detectorId[0]);