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