]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSRawReaderDate.cxx
Implementation of ITS tracking for HLT. The tracking is based on the off-line AliITSt...
[u/mrichter/AliRoot.git] / PHOS / AliPHOSRawReaderDate.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 ///
20 /// This is a class for reading raw data from a date file or event.
21 ///
22 /// The AliPHOSRawReaderDate is constructed either with a pointer to a
23 /// date event or with a file name and an event number.
24 ///
25 ///////////////////////////////////////////////////////////////////////////////
26 #include <stdio.h>
27
28 #include "AliPHOSRawReaderDate.h"
29 #include "Riostream.h"
30
31 ClassImp(AliPHOSRawReaderDate)
32
33 //---------------------------------------------------------
34 AliPHOSRawReaderDate::AliPHOSRawReaderDate(void* event) :
35   fRequireHeader(kTRUE),
36   fFile(NULL),
37   fEvent(NULL),
38   fSubEvent(NULL),
39   fEquipment(NULL),
40   fPosition(NULL),
41   fEnd(NULL)
42 {
43 // create an object to read digits from the given date event
44   fEvent = (eventHeaderStruct*) event;
45 }
46 //---------------------------------------------------------
47 AliPHOSRawReaderDate::AliPHOSRawReaderDate(const char* fileName, Int_t eventNumber):
48   fRequireHeader(kTRUE),
49   fFile(NULL),
50   fEvent(NULL),
51   fSubEvent(NULL),
52   fEquipment(NULL),
53   fPosition(NULL),
54   fEnd(NULL)
55 {
56 // create an object to read digits from the given date event
57 //   char command[256];
58 //   if(strstr(fileName,".gz") )
59 //     sprintf(command,"zcat %s",fileName);
60 //   else
61 //     sprintf(command,"cat %s",fileName);    
62 //   printf("Comand %s \n",command) ;
63 //   fFile  = popen("zcat Run_3186.dat.gz", "rb");
64
65   fFile  = fopen(fileName, "rb");
66
67   if (!fFile) {
68     Error("AliPHOSRawReaderDate", "could not open file %s", fileName);
69     return;
70   }
71   if (eventNumber < 0) return;
72
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;
81       break;
82     }
83     fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
84     eventNumber--;
85   }
86 }
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),
96   fEnd(rawReader.fEnd)
97
98 {
99 // copy constructor
100
101   Fatal("AliPHOSRawReaderDate", "copy constructor not implemented");
102 }
103 //---------------------------------------------------------
104 AliPHOSRawReaderDate& AliPHOSRawReaderDate::operator = (const AliPHOSRawReaderDate& 
105                                                 /*rawReader*/)
106 {
107 // assignment operator
108
109   Fatal("operator =", "assignment operator not implemented");
110   return *this;
111 }
112 //---------------------------------------------------------
113 AliPHOSRawReaderDate::~AliPHOSRawReaderDate()
114 {
115 // destructor
116
117   if (fFile) {
118     delete[] fEvent;
119     //   pclose(fFile);
120     fclose(fFile);
121   }
122 }
123 //---------------------------------------------------------
124 Bool_t AliPHOSRawReaderDate::ReadHeader()
125 {
126 // read a data header at the current position
127 // returns kFALSE if the data header could not be read
128
129   fErrorCode = 0;
130
131   fHeader = NULL;
132   if (!fEvent) return kFALSE;
133
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) {
137       SwappEvent(fEvent);
138     } 
139     else {
140       Error("ReadHader","Wrong event magic number, MAGIC == %08x (expected %08x).\n",
141             fEvent->eventMagic, EVENT_MAGIC_NUMBER);
142       fErrorCode = kErrMagic;
143       return kFALSE;
144     }
145   }
146
147   // check whether there are sub events
148   if ((UInt_t)fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
149   
150   do {
151     // skip payload (if event was not selected)
152     if (fCount > 0) fPosition += fCount;
153     
154     // get the first or the next equipment if at the end of an equipment
155     if (!fEquipment || (fPosition >= fEnd)) {
156       fEquipment = NULL;
157      
158       // printf("No equipment defined \n") ;
159  
160       // get the first or the next sub event if at the end of a sub event
161       if (!fSubEvent || 
162           (fPosition >= ((UChar_t*)fSubEvent) + fSubEvent->eventSize)) {
163
164         // printf("GetNext subevent \n") ;
165
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)) ;
167         
168         // check for end of event data
169         if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
170
171         // printf("position passed \n") ;
172
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 ") ;
180         } else {
181           fSubEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) + 
182                                             fEvent->eventHeadSize);
183           // printf("first subevent \n") ;
184         }
185         
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);
190           } 
191           else {
192             Error("ReadHeader", "wrong magic number in sub event!\n"
193                   " run: %d \n",fSubEvent->eventRunNb) ; 
194             fErrorCode = kErrMagic;
195             return kFALSE;
196           }
197         }
198         
199         // continue if no data in the subevent
200         if ((UInt_t)fSubEvent->eventSize == fSubEvent->eventHeadSize) {
201           fPosition = fEnd = ((UChar_t*)fSubEvent) + fSubEvent->eventSize;
202           fCount = 0;
203           // printf("no data in subevent \n") ;
204           continue;
205         }
206         
207         // printf("Set equipment after header of subevent \n") ;
208         fEquipment = (equipmentHeaderStruct*)
209           (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
210         
211       } else {
212         fEquipment = (equipmentHeaderStruct*) fEnd;
213         // printf("equipment set at fEnd \n" ) ;
214       }
215       
216       fCount = 0;
217       fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
218       fEnd = fPosition + fEquipment->rawDataLen;
219     }
220     
221     // continue with the next sub event if no data left in the payload
222     if (fPosition >= fEnd) continue;
223     
224     fCount = fEnd - fPosition;
225     //    if(IsSelected())
226       // printf("Selected \n") ;
227     // else
228       // printf("Not selected \n") ;
229     
230   } while (!fEquipment || !IsSelected());
231   
232   return kTRUE;
233 }
234 //---------------------------------------------------------
235 Bool_t AliPHOSRawReaderDate::ReadNextData(UChar_t*& data)
236 {
237 // reads the next payload at the current position
238 // returns kFALSE if the data could not be read
239
240   fErrorCode = 0;
241   while (fCount == 0) {
242     if (!ReadHeader()) return kFALSE;
243   }
244   data = fPosition;
245   fPosition += fCount;  
246   // printf("fCount %d \n",fCount) ;
247   fCount = 0;
248   return kTRUE;
249 }
250 //---------------------------------------------------------
251 Bool_t AliPHOSRawReaderDate::ReadNext(UChar_t* data, Int_t size)
252 {
253 // reads the next block of data at the current position
254 // returns kFALSE if the data could not be read
255
256   fErrorCode = 0;
257   if (fPosition + size > fEnd) {
258     Error("ReadNext", "could not read data!");
259     fErrorCode = kErrOutOfBounds;
260     return kFALSE;
261   }
262   memcpy(data, fPosition, size);
263   fPosition += size;
264   fCount -= size;
265   return kTRUE;
266 }
267 //---------------------------------------------------------
268 Bool_t AliPHOSRawReaderDate::Reset()
269 {
270 // reset the current position to the beginning of the event
271
272   fSubEvent = NULL;
273   fCount = 0;
274   fPosition = fEnd = NULL;
275   return kTRUE;
276 }
277 //---------------------------------------------------------
278 Bool_t AliPHOSRawReaderDate::NextEvent()
279 {
280 // go to the next event in the date file
281
282   if (!fFile) return kFALSE;
283
284   // printf("Old fEvent = %p \n",(UChar_t*)fEvent) ;
285   eventHeaderStruct header;
286   UInt_t headerSize = sizeof(eventHeaderStruct);
287   if (fEvent) delete[] fEvent;
288   fEvent = &header;
289   // printf("New fEvent = %p \n",(UChar_t*)fEvent) ; 
290   
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);
296       continue;
297     }
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");
302       delete[] buffer;
303       break;
304     }
305     fEvent = (eventHeaderStruct*) buffer;
306     // printf("Read fEvent = %p \n",(UChar_t*)fEvent) ;
307     fSubEvent = NULL ;
308     fEquipment = NULL ;
309     fPosition = NULL ;
310     fEnd = NULL ;
311     return kTRUE;
312   };
313
314   fEvent = NULL;
315   return kFALSE;
316 }
317 //---------------------------------------------------------
318 Bool_t AliPHOSRawReaderDate::RewindEvents()
319 {
320 // go back to the beginning of the date file
321
322   if (!fFile) return kFALSE;
323
324   fseek(fFile, 0, SEEK_SET);
325   return Reset();
326 }
327 //---------------------------------------------------------
328 Int_t AliPHOSRawReaderDate::CheckData() const
329 {
330 // check the consistency of the data
331
332   if (!fEvent) return 0;
333   // check whether there are sub events
334   if ((UInt_t)fEvent->eventSize <= fEvent->eventHeadSize) return 0;
335
336   eventHeaderStruct* subEvent = NULL;
337   UChar_t* position = 0;
338   UChar_t* end = 0;
339   Int_t result = 0;
340
341   while (kTRUE) {
342     // get the first or the next sub event if at the end of a sub event
343     if (!subEvent || (position >= end)) {
344
345       // check for end of event data
346       if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
347       //???DP
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);
354 //       } else {
355 //      subEvent = (eventHeaderStruct*) (((UChar_t*)fEvent) + 
356 //                                       fEvent->eventHeadSize);
357 //       }
358
359       // check the magic word of the sub event
360       if (subEvent->eventMagic != EVENT_MAGIC_NUMBER) {
361         result |= kErrMagic;
362         return result;
363       }
364
365       position = ((UChar_t*)subEvent) + subEvent->eventHeadSize + 
366         sizeof(equipmentHeaderStruct);
367       end = ((UChar_t*)subEvent) + subEvent->eventSize;
368     }
369
370     // continue with the next sub event if no data left in the payload
371     if (position >= end) continue;
372
373     // check that there are enough bytes left for the data header
374     if (position + sizeof(AliRawDataHeader) > end) {
375       result |= kErrNoDataHeader;
376       position = end;
377       continue;
378     }
379
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) {
384         result |= kErrSize;
385         position = end;
386       } else {
387         position += header->fSize;
388       }
389     } else {
390       position = end;
391     }
392   };
393
394   return 0;
395 }
396 //---------------------------------------------------------
397 void AliPHOSRawReaderDate::SwappEvent(eventHeaderStruct * event)
398 {
399   if(event->eventMagic == EVENT_MAGIC_NUMBER_SWAPPED)
400     {
401       ChangeOrder(event->eventSize);
402       ChangeOrder(event->eventType);
403       ChangeOrder(event->detectorId[0]);
404     }
405 }