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