]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReaderRoot.cxx
Spec file for production of RPM with static libraries needed by DATE mStreamRecorder
[u/mrichter/AliRoot.git] / RAW / AliRawReaderRoot.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 root file.
21 ///
22 /// The root file is expected to contain a tree of name "RAW" with
23 /// a branch of name "rawevent" which contains objects of type
24 /// AliRawEvent.
25 /// 
26 /// The file name and the event number are arguments of the constructor
27 /// of AliRawReaderRoot.
28 ///
29 ///////////////////////////////////////////////////////////////////////////////
30
31 #include <TFile.h>
32 #include <TTree.h>
33 #include "AliRawReaderRoot.h"
34 #include "AliRawEvent.h"
35 #include "AliRawEventHeaderBase.h"
36 #include "AliRawEquipment.h"
37 #include "AliRawEquipmentHeader.h"
38 #include "AliRawData.h"
39
40
41 ClassImp(AliRawReaderRoot)
42
43
44 AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
45   fFile(NULL),
46   fBranch(NULL),
47   fEventIndex(eventNumber),
48   fEvent(NULL),
49   fSubEventIndex(0),
50   fSubEvent(NULL),
51   fEquipmentIndex(0),
52   fEquipment(NULL),
53   fRawData(NULL),
54   fPosition(NULL),
55   fEnd(NULL)
56 {
57 // create an object to read digits from the given input file for the
58 // event with the given number
59
60   TDirectory* dir = gDirectory;
61   fFile = TFile::Open(fileName);
62   dir->cd();
63   if (!fFile || !fFile->IsOpen()) {
64     Error("AliRawReaderRoot", "could not open file %s", fileName);
65     return;
66   }
67   TTree* tree = (TTree*) fFile->Get("RAW");
68   if (!tree) {
69     Error("AliRawReaderRoot", "no raw data tree found");
70     return;
71   }
72   fBranch = tree->GetBranch("rawevent");
73   if (!fBranch) {
74     Error("AliRawReaderRoot", "no raw data branch found");
75     return;
76   }
77
78   fEvent = new AliRawEvent;
79   fBranch->SetAddress(&fEvent);
80   if (fEventIndex >= 0) {
81     if (fBranch->GetEntry(fEventIndex) <= 0) {
82       Error("AliRawReaderRoot", "no event with number %d found", fEventIndex);
83       return;
84     }
85   }
86 }
87
88 AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event) :
89   fFile(NULL),
90   fBranch(NULL),
91   fEventIndex(-1),
92   fEvent(event),
93   fSubEventIndex(0),
94   fSubEvent(NULL),
95   fEquipmentIndex(0),
96   fEquipment(NULL),
97   fRawData(NULL),
98   fPosition(NULL),
99   fEnd(NULL)
100 {
101 // create an object to read digits from the given raw event
102
103 }
104
105 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
106   AliRawReader(rawReader),
107   fFile(NULL),
108   fBranch(NULL),
109   fEventIndex(rawReader.fEventIndex),
110   fEvent(NULL),
111   fSubEventIndex(rawReader.fSubEventIndex),
112   fSubEvent(NULL),
113   fEquipmentIndex(rawReader.fEquipmentIndex),
114   fEquipment(NULL),
115   fRawData(NULL),
116   fPosition(NULL),
117   fEnd(NULL)
118 {
119 // copy constructor
120
121   if (rawReader.fFile) {
122     TDirectory* dir = gDirectory;
123     fFile = TFile::Open(rawReader.fFile->GetName());
124     dir->cd();
125     if (!fFile || !fFile->IsOpen()) {
126       Error("AliRawReaderRoot", "could not open file %s", 
127             rawReader.fFile->GetName());
128       return;
129     }
130     TTree* tree = (TTree*) fFile->Get("RAW");
131     if (!tree) {
132       Error("AliRawReaderRoot", "no raw data tree found");
133       return;
134     }
135     fBranch = tree->GetBranch("rawevent");
136     if (!fBranch) {
137       Error("AliRawReaderRoot", "no raw data branch found");
138       return;
139     }
140
141     fEvent = new AliRawEvent;
142     fBranch->SetAddress(&fEvent);
143     if (fEventIndex >= 0) {
144       if (fBranch->GetEntry(fEventIndex) <= 0) {
145         Error("AliRawReaderRoot", "no event with number %d found", 
146               fEventIndex);
147         return;
148       }
149     }
150   } else {
151     fEvent = rawReader.fEvent;
152   }
153
154   if (fSubEventIndex > 0) {
155     fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
156     fEquipment = fSubEvent->GetEquipment(fEquipmentIndex);
157     fRawData = fEquipment->GetRawData();
158       fCount = 0;
159     fHeader = (AliRawDataHeader*) ((UChar_t*) fRawData->GetBuffer() + 
160       ((UChar_t*) rawReader.fHeader - 
161        (UChar_t*) rawReader.fRawData->GetBuffer()));
162     fPosition = (UChar_t*) fRawData->GetBuffer() + 
163       (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
164     fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
165   }
166 }
167
168 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot& 
169                                                 rawReader)
170 {
171 // assignment operator
172
173   this->~AliRawReaderRoot();
174   new(this) AliRawReaderRoot(rawReader);
175   return *this;
176 }
177
178 AliRawReaderRoot::~AliRawReaderRoot()
179 {
180 // delete objects and close root file
181
182   if (fFile) {
183     if (fEvent) delete fEvent;
184     fFile->Close();
185     delete fFile;
186   }
187 }
188
189
190 UInt_t AliRawReaderRoot::GetType() const
191 {
192 // get the type from the event header
193
194   if (!fEvent) return 0;
195   return fEvent->GetHeader()->Get("Type");
196 }
197
198 UInt_t AliRawReaderRoot::GetRunNumber() const
199 {
200 // get the run number from the event header
201
202   if (!fEvent) return 0;
203   return fEvent->GetHeader()->Get("RunNb");
204 }
205
206 const UInt_t* AliRawReaderRoot::GetEventId() const
207 {
208 // get the event id from the event header
209
210   if (!fEvent) return NULL;
211   return fEvent->GetHeader()->GetP("Id");
212 }
213
214 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
215 {
216 // get the trigger pattern from the event header
217
218   if (!fEvent) return NULL;
219   return fEvent->GetHeader()->GetP("TriggerPattern");
220 }
221
222 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
223 {
224 // get the detector pattern from the event header
225
226   if (!fEvent) return NULL;
227   return fEvent->GetHeader()->GetP("DetectorPattern");
228 }
229
230 const UInt_t* AliRawReaderRoot::GetAttributes() const
231 {
232 // get the type attributes from the event header
233
234   if (!fEvent) return NULL;
235   return fEvent->GetHeader()->GetP("TypeAttribute");
236 }
237
238 const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
239 {
240 // get the type attributes from the sub event header
241
242   if (!fSubEvent) return NULL;
243   return fSubEvent->GetHeader()->GetP("TypeAttribute");
244 }
245
246 UInt_t AliRawReaderRoot::GetLDCId() const
247 {
248 // get the LDC Id from the event header
249
250   if (!fEvent || !fSubEvent) return 0;
251   return fSubEvent->GetHeader()->Get("LdcId");
252 }
253
254 UInt_t AliRawReaderRoot::GetGDCId() const
255 {
256 // get the GDC Id from the event header
257
258   if (!fEvent) return 0;
259   return fEvent->GetHeader()->Get("GdcId");
260 }
261
262
263 Int_t AliRawReaderRoot::GetEquipmentSize() const
264 {
265 // get the size of the equipment
266
267   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
268   return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
269 }
270
271 Int_t AliRawReaderRoot::GetEquipmentType() const
272 {
273 // get the type from the equipment header
274
275   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
276   return fEquipment->GetEquipmentHeader()->GetEquipmentType();
277 }
278
279 Int_t AliRawReaderRoot::GetEquipmentId() const
280 {
281 // get the ID from the equipment header
282
283   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
284   return fEquipment->GetEquipmentHeader()->GetId();
285 }
286
287 const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
288 {
289 // get the attributes from the equipment header
290
291   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
292   return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
293 }
294
295 Int_t AliRawReaderRoot::GetEquipmentElementSize() const
296 {
297 // get the basic element size from the equipment header
298
299   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
300   return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
301 }
302
303 Int_t AliRawReaderRoot::GetEquipmentHeaderSize() const
304 {
305 // get the size of the equipment header (28 bytes by default)
306
307   if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
308   return fEquipment->GetEquipmentHeader()->HeaderSize();
309 }
310
311
312 Bool_t AliRawReaderRoot::ReadHeader()
313 {
314 // read a data header at the current position
315 // returns kFALSE if the data header could not be read
316
317   fErrorCode = 0;
318   if (!fEvent) return kFALSE;
319
320   do {
321     // skip payload (if event was not selected)
322     if (fCount > 0) fPosition += fCount;
323
324     // get the first or the next equipment if at the end of an equipment
325     if (!fEquipment || (fPosition >= fEnd)) {
326
327       // get the first or the next sub event if at the end of a sub event
328       if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
329
330         // check for end of event data
331         if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
332         fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
333
334         // check the magic word of the sub event
335         if (!fSubEvent->GetHeader()->IsValid()) {
336           Error("ReadHeader", "wrong magic number in sub event!");
337           fSubEvent->GetHeader()->Dump();
338           fErrorCode = kErrMagic;
339           return kFALSE;
340         }
341
342         fEquipmentIndex = 0;
343         fEquipment = NULL;
344         fRawData = NULL;
345       }
346
347       // get the next equipment and raw data
348       fCount = 0;
349       fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
350       if (!fEquipment) continue;
351       fRawData = fEquipment->GetRawData();
352       if (!fRawData) {
353         fPosition = fEnd;
354         continue;
355       }
356       fPosition = (UChar_t*) fRawData->GetBuffer();
357       fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
358     }
359
360     // continue with the next equipment if no data left in the payload
361     if (fPosition >= fEnd) continue;
362
363     if (fRequireHeader) {
364       // check that there are enough bytes left for the data header
365       if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
366         Error("ReadHeader", "could not read data header!");
367         Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
368         fEquipment->GetEquipmentHeader()->Dump();
369         fCount = 0;
370         fPosition = fEnd;
371         fErrorCode = kErrNoDataHeader;
372         continue;
373       }
374
375       // "read" the data header
376       fHeader = (AliRawDataHeader*) fPosition;
377       if ((fPosition + fHeader->fSize) != fEnd) {
378         Warning("ReadHeader",
379                 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
380                 fHeader->fSize, fEnd - fPosition);
381         fHeader->fSize = fEnd - fPosition;
382       }
383       fPosition += sizeof(AliRawDataHeader);
384     }
385
386     if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
387       fCount = fHeader->fSize - sizeof(AliRawDataHeader);
388
389       // check consistency of data size in the header and in the sub event
390       if (fPosition + fCount > fEnd) {  
391         Error("ReadHeader", "size in data header exceeds event size!");
392         Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
393         fEquipment->GetEquipmentHeader()->Dump();
394         fCount = 0;
395         fPosition = fEnd;
396         fErrorCode = kErrSize;
397         continue;
398       }
399
400     } else {
401       fCount = fEnd - fPosition;
402     }
403
404   } while (!IsSelected());
405
406   return kTRUE;
407 }
408
409 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
410 {
411 // reads the next payload at the current position
412 // returns kFALSE if the data could not be read
413
414   fErrorCode = 0;
415   while (fCount == 0) {
416     if (!ReadHeader()) return kFALSE;
417   }
418   data = fPosition;
419   fPosition += fCount;  
420   fCount = 0;
421   return kTRUE;
422 }
423
424 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
425 {
426 // reads the next block of data at the current position
427 // returns kFALSE if the data could not be read
428
429   fErrorCode = 0;
430   if (fPosition + size > fEnd) {
431     Error("ReadNext", "could not read data!");
432     fErrorCode = kErrOutOfBounds;
433     return kFALSE;
434   }
435   memcpy(data, fPosition, size);
436   fPosition += size;
437   fCount -= size;
438   return kTRUE;
439 }
440
441
442 Bool_t AliRawReaderRoot::Reset()
443 {
444 // reset the current position to the beginning of the event
445
446   fSubEventIndex = 0;
447   fSubEvent = NULL;
448   fEquipmentIndex = 0;
449   fEquipment = NULL;
450   fRawData = NULL;
451   fHeader = NULL;
452
453   fCount = 0;
454   fPosition = fEnd = NULL;
455   return kTRUE;
456 }
457
458
459 Bool_t AliRawReaderRoot::NextEvent()
460 {
461 // go to the next event in the root file
462
463   if (!fFile) return kFALSE;
464
465   do {
466     delete fEvent;
467     fEvent = new AliRawEvent;
468     fBranch->SetAddress(&fEvent);
469     if (fBranch->GetEntry(fEventIndex+1) <= 0)
470       return kFALSE;
471     fEventIndex++;
472   } while (!IsEventSelected());
473   return Reset();
474 }
475
476 Bool_t AliRawReaderRoot::RewindEvents()
477 {
478 // go back to the beginning of the root file
479
480   if (!fFile) return kFALSE;
481
482   fEventIndex = -1;
483   delete fEvent;
484   fEvent = new AliRawEvent;
485   fBranch->SetAddress(&fEvent);
486   return Reset();
487 }
488
489
490 Int_t AliRawReaderRoot::CheckData() const
491 {
492 // check the consistency of the data
493
494   if (!fEvent) return 0;
495
496   AliRawEvent* subEvent = NULL;
497   Int_t subEventIndex = 0;
498   AliRawEquipment* equipment = NULL;
499   Int_t equipmentIndex = 0;
500   UChar_t* position = 0;
501   UChar_t* end = 0;
502   Int_t result = 0;
503
504   while (kTRUE) {
505     // get the first or the next sub event if at the end of an equipment
506     if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
507
508       // check for end of event data
509       if (subEventIndex >= fEvent->GetNSubEvents()) return result;
510       subEvent = fEvent->GetSubEvent(subEventIndex++);
511
512       // check the magic word of the sub event
513       if (!fSubEvent->GetHeader()->IsValid()) {
514         result |= kErrMagic;
515         return result;
516       }
517
518       equipmentIndex = 0;
519     }
520
521     // get the next equipment and raw data
522     equipment = subEvent->GetEquipment(equipmentIndex++);
523     if (!equipment) continue;
524     AliRawData* rawData = equipment->GetRawData();
525     if (!rawData) continue;
526     position = (UChar_t*) rawData->GetBuffer();
527     end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
528
529     // continue with the next sub event if no data left in the payload
530     if (position >= end) continue;
531
532     if (fRequireHeader) {
533     // check that there are enough bytes left for the data header
534       if (position + sizeof(AliRawDataHeader) > end) {
535         result |= kErrNoDataHeader;
536         continue;
537       }
538
539       // check consistency of data size in the header and in the equipment
540       AliRawDataHeader* header = (AliRawDataHeader*) position;
541       if ((position + header->fSize) != end) {
542         Warning("ReadHeader",
543                 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
544                 header->fSize, end - position);
545         header->fSize = end - position;
546         result |= kErrSize;
547       }
548     }
549     position = end;
550   };
551
552   return result;
553 }