]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RAW/AliRawReaderRoot.cxx
Bug fix: the object counter should be never reset inside the raw-data root-ification...
[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       fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
406       if (!fEquipment) continue;
407       if (!IsSelected()) {
408         fPosition = fEnd;
409         continue;
410       }
411       fRawData = fEquipment->GetRawData();
412       if (!fRawData) {
413         fPosition = fEnd;
414         continue;
415       }
416       fPosition = (UChar_t*) fRawData->GetBuffer();
417       fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
418     }
419
420     // continue with the next equipment if no data left in the payload
421     if (fPosition >= fEnd) continue;
422
423     if (fRequireHeader) {
424       // check that there are enough bytes left for the data header
425       if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
426         Error("ReadHeader", "could not read data header!");
427         Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
428         fEquipment->GetEquipmentHeader()->Dump();
429         fCount = 0;
430         fPosition = fEnd;
431         fErrorCode = kErrNoDataHeader;
432         continue;
433       }
434
435       // "read" the data header
436       fHeader = (AliRawDataHeader*) fPosition;
437 #ifndef R__BYTESWAP
438       SwapData((void*) fHeader, (void*) fHeaderSwapped, sizeof(AliRawDataHeader));
439       fHeader=fHeaderSwapped;
440 #endif
441       if ((fPosition + fHeader->fSize) != fEnd) {
442         if (fHeader->fSize != 0xFFFFFFFF)
443           Warning("ReadHeader",
444                   "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
445                   fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
446         fHeader->fSize = fEnd - fPosition;
447       }
448       fPosition += sizeof(AliRawDataHeader);
449     }
450
451     if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
452       fCount = fHeader->fSize - sizeof(AliRawDataHeader);
453
454       // check consistency of data size in the header and in the sub event
455       if (fPosition + fCount > fEnd) {  
456         Error("ReadHeader", "size in data header exceeds event size!");
457         Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
458         fEquipment->GetEquipmentHeader()->Dump();
459         fCount = 0;
460         fPosition = fEnd;
461         fErrorCode = kErrSize;
462         continue;
463       }
464
465     } else {
466       fCount = fEnd - fPosition;
467     }
468
469   } while (!IsSelected());
470
471   return kTRUE;
472 }
473
474 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
475 {
476 // reads the next payload at the current position
477 // returns kFALSE if the data could not be read
478
479   fErrorCode = 0;
480   while (fCount == 0) {
481     if (!ReadHeader()) return kFALSE;
482   }
483   data = fPosition;
484   fPosition += fCount;  
485   fCount = 0;
486   return kTRUE;
487 }
488
489 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
490 {
491 // reads the next block of data at the current position
492 // returns kFALSE if the data could not be read
493
494   fErrorCode = 0;
495   if (fPosition + size > fEnd) {
496     Error("ReadNext", "could not read data!");
497     fErrorCode = kErrOutOfBounds;
498     return kFALSE;
499   }
500   memcpy(data, fPosition, size);
501
502   fPosition += size;
503   fCount -= size;
504   return kTRUE;
505 }
506
507
508 Bool_t AliRawReaderRoot::Reset()
509 {
510 // reset the current position to the beginning of the event
511
512   fSubEventIndex = 0;
513   fSubEvent = NULL;
514   fEquipmentIndex = 0;
515   fEquipment = NULL;
516   fRawData = NULL;
517   fHeader = NULL;
518
519   fCount = 0;
520   fPosition = fEnd = NULL;
521   return kTRUE;
522 }
523
524
525 Bool_t AliRawReaderRoot::NextEvent()
526 {
527 // go to the next event in the root file
528
529   if (!fBranch) return kFALSE;
530
531   do {
532     delete fEvent;
533     fEvent = NULL;
534     fEventHeader = NULL;
535     fBranch->SetAddress(&fEvent);
536     if (fBranch->GetEntry(fEventIndex+1) <= 0)
537       return kFALSE;
538     fEventHeader = fEvent->GetHeader();
539     fEventIndex++;
540   } while (!IsEventSelected());
541   fEventNumber++;
542   return Reset();
543 }
544
545 Bool_t AliRawReaderRoot::RewindEvents()
546 {
547 // go back to the beginning of the root file
548
549   if (!fBranch) return kFALSE;
550
551   fEventIndex = -1;
552   delete fEvent;
553   fEvent = NULL;
554   fEventHeader = NULL;
555   fBranch->SetAddress(&fEvent);
556   fEventNumber = -1;
557   return Reset();
558 }
559
560 Bool_t  AliRawReaderRoot::GotoEvent(Int_t event)
561 {
562   // go to a particular event
563   // Uses the absolute event index inside the
564   // raw-data file
565
566   if (!fBranch) return kFALSE;
567
568   delete fEvent;
569   fEvent = NULL;
570   fEventHeader = NULL;
571   fBranch->SetAddress(&fEvent);
572   if (fBranch->GetEntry(event) <= 0)
573     return kFALSE;
574   fEventHeader = fEvent->GetHeader();
575   fEventIndex = event;
576   fEventNumber++;
577   return Reset();
578 }
579
580 Int_t AliRawReaderRoot::GetNumberOfEvents() const
581 {
582   // Get the total number of events in
583   // the raw-data tree
584
585   if (!fBranch) return -1;
586
587   return fBranch->GetEntries();
588 }
589
590 Int_t AliRawReaderRoot::CheckData() const
591 {
592 // check the consistency of the data
593
594   if (!fEvent) return 0;
595
596   AliRawVEvent* subEvent = NULL;
597   Int_t subEventIndex = 0;
598   AliRawVEquipment* equipment = NULL;
599   Int_t equipmentIndex = 0;
600   UChar_t* position = 0;
601   UChar_t* end = 0;
602   Int_t result = 0;
603
604   while (kTRUE) {
605     // get the first or the next sub event if at the end of an equipment
606     if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
607
608       // check for end of event data
609       if (subEventIndex >= fEvent->GetNSubEvents()) return result;
610       subEvent = fEvent->GetSubEvent(subEventIndex++);
611
612       // check the magic word of the sub event
613       if (!fSubEvent->GetHeader()->IsValid()) {
614         result |= kErrMagic;
615         return result;
616       }
617
618       equipmentIndex = 0;
619     }
620
621     // get the next equipment and raw data
622     equipment = subEvent->GetEquipment(equipmentIndex++);
623     if (!equipment) continue;
624     AliRawData* rawData = equipment->GetRawData();
625     if (!rawData) continue;
626     position = (UChar_t*) rawData->GetBuffer();
627     end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
628
629     // continue with the next sub event if no data left in the payload
630     if (position >= end) continue;
631
632     if (fRequireHeader) {
633     // check that there are enough bytes left for the data header
634       if (position + sizeof(AliRawDataHeader) > end) {
635         result |= kErrNoDataHeader;
636         continue;
637       }
638
639       // check consistency of data size in the header and in the equipment
640       AliRawDataHeader* header = (AliRawDataHeader*) position;
641       if ((position + header->fSize) != end) {
642         if (header->fSize != 0xFFFFFFFF)
643           Warning("ReadHeader",
644                   "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
645                   equipment->GetEquipmentHeader()->GetId(),header->fSize, end - position);
646         header->fSize = end - position;
647         result |= kErrSize;
648       }
649     }
650     position = end;
651   };
652
653   return result;
654 }