1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 /// This is a class for reading raw data from a root file.
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
26 /// The file name and the event number are arguments of the constructor
27 /// of AliRawReaderRoot.
29 ///////////////////////////////////////////////////////////////////////////////
33 #include "AliRawReaderRoot.h"
34 #include "AliRawEvent.h"
35 #include "AliRawEventHeader.h"
36 #include "AliRawEquipment.h"
37 #include "AliRawEquipmentHeader.h"
38 #include "AliRawData.h"
41 ClassImp(AliRawReaderRoot)
44 AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
47 fEventIndex(eventNumber),
57 // create an object to read digits from the given input file for the
58 // event with the given number
60 TDirectory* dir = gDirectory;
61 fFile = TFile::Open(fileName);
63 if (!fFile || !fFile->IsOpen()) {
64 Error("AliRawReaderRoot", "could not open file %s", fileName);
67 TTree* tree = (TTree*) fFile->Get("RAW");
69 Error("AliRawReaderRoot", "no raw data tree found");
72 fBranch = tree->GetBranch("rawevent");
74 Error("AliRawReaderRoot", "no raw data branch found");
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);
88 AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event) :
101 // create an object to read digits from the given raw event
105 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
106 AliRawReader(rawReader),
109 fEventIndex(rawReader.fEventIndex),
111 fSubEventIndex(rawReader.fSubEventIndex),
113 fEquipmentIndex(rawReader.fEquipmentIndex),
121 if (rawReader.fFile) {
122 TDirectory* dir = gDirectory;
123 fFile = TFile::Open(rawReader.fFile->GetName());
125 if (!fFile || !fFile->IsOpen()) {
126 Error("AliRawReaderRoot", "could not open file %s",
127 rawReader.fFile->GetName());
130 TTree* tree = (TTree*) fFile->Get("RAW");
132 Error("AliRawReaderRoot", "no raw data tree found");
135 fBranch = tree->GetBranch("rawevent");
137 Error("AliRawReaderRoot", "no raw data branch found");
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",
151 fEvent = rawReader.fEvent;
154 if (fSubEventIndex > 0) {
155 fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
156 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex);
157 fRawData = fEquipment->GetRawData();
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();
168 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
171 // assignment operator
173 this->~AliRawReaderRoot();
174 new(this) AliRawReaderRoot(rawReader);
178 AliRawReaderRoot::~AliRawReaderRoot()
180 // delete objects and close root file
183 if (fEvent) delete fEvent;
190 UInt_t AliRawReaderRoot::GetType() const
192 // get the type from the event header
194 if (!fEvent) return 0;
195 return fEvent->GetHeader()->GetType();
198 UInt_t AliRawReaderRoot::GetRunNumber() const
200 // get the run number from the event header
202 if (!fEvent) return 0;
203 return fEvent->GetHeader()->GetRunNumber();
206 const UInt_t* AliRawReaderRoot::GetEventId() const
208 // get the event id from the event header
210 if (!fEvent) return NULL;
211 return fEvent->GetHeader()->GetId();
214 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
216 // get the trigger pattern from the event header
218 if (!fEvent) return NULL;
219 return fEvent->GetHeader()->GetTriggerPattern();
222 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
224 // get the detector pattern from the event header
226 if (!fEvent) return NULL;
227 return fEvent->GetHeader()->GetDetectorPattern();
230 const UInt_t* AliRawReaderRoot::GetAttributes() const
232 // get the type attributes from the event header
234 if (!fEvent) return NULL;
235 return fEvent->GetHeader()->GetTypeAttribute();
238 const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
240 // get the type attributes from the sub event header
242 if (!fSubEvent) return NULL;
243 return fSubEvent->GetHeader()->GetTypeAttribute();
246 UInt_t AliRawReaderRoot::GetLDCId() const
248 // get the LDC Id from the event header
250 if (!fEvent || !fSubEvent) return 0;
251 return fSubEvent->GetHeader()->GetLDCId();
254 UInt_t AliRawReaderRoot::GetGDCId() const
256 // get the GDC Id from the event header
258 if (!fEvent) return 0;
259 return fEvent->GetHeader()->GetGDCId();
263 Int_t AliRawReaderRoot::GetEquipmentSize() const
265 // get the size of the equipment
267 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
268 return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
271 Int_t AliRawReaderRoot::GetEquipmentType() const
273 // get the type from the equipment header
275 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
276 return fEquipment->GetEquipmentHeader()->GetEquipmentType();
279 Int_t AliRawReaderRoot::GetEquipmentId() const
281 // get the ID from the equipment header
283 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
284 return fEquipment->GetEquipmentHeader()->GetId();
287 const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
289 // get the attributes from the equipment header
291 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
292 return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
295 Int_t AliRawReaderRoot::GetEquipmentElementSize() const
297 // get the basic element size from the equipment header
299 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
300 return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
304 Bool_t AliRawReaderRoot::ReadHeader()
306 // read a data header at the current position
307 // returns kFALSE if the data header could not be read
310 if (!fEvent) return kFALSE;
313 // skip payload (if event was not selected)
314 if (fCount > 0) fPosition += fCount;
316 // get the first or the next equipment if at the end of an equipment
317 if (!fEquipment || (fPosition >= fEnd)) {
319 // get the first or the next sub event if at the end of a sub event
320 if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
322 // check for end of event data
323 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
324 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
326 // check the magic word of the sub event
327 if (!fSubEvent->GetHeader()->IsValid()) {
328 Error("ReadHeader", "wrong magic number in sub event!");
329 fSubEvent->GetHeader()->Dump();
330 fErrorCode = kErrMagic;
339 // get the next equipment and raw data
341 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
342 if (!fEquipment) continue;
343 fRawData = fEquipment->GetRawData();
348 fPosition = (UChar_t*) fRawData->GetBuffer();
349 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
352 // continue with the next equipment if no data left in the payload
353 if (fPosition >= fEnd) continue;
355 // check that there are enough bytes left for the data header
356 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
357 Error("ReadHeader", "could not read data header!");
358 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
359 fEquipment->GetEquipmentHeader()->Dump();
362 fErrorCode = kErrNoDataHeader;
366 // "read" the data header
367 fHeader = (AliRawDataHeader*) fPosition;
368 fPosition += sizeof(AliRawDataHeader);
369 if (fHeader->fSize != 0xFFFFFFFF) {
370 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
372 fCount = fEnd - fPosition;
375 // check consistency of data size in the header and in the sub event
376 if (fPosition + fCount > fEnd) {
377 Error("ReadHeader", "size in data header exceeds event size!");
378 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
379 fEquipment->GetEquipmentHeader()->Dump();
382 fErrorCode = kErrSize;
386 } while (!IsSelected());
391 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
393 // reads the next payload at the current position
394 // returns kFALSE if the data could not be read
397 while (fCount == 0) {
398 if (!ReadHeader()) return kFALSE;
406 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
408 // reads the next block of data at the current position
409 // returns kFALSE if the data could not be read
412 if (fPosition + size > fEnd) {
413 Error("ReadNext", "could not read data!");
414 fErrorCode = kErrOutOfBounds;
417 memcpy(data, fPosition, size);
424 Bool_t AliRawReaderRoot::Reset()
426 // reset the current position to the beginning of the event
436 fPosition = fEnd = NULL;
441 Bool_t AliRawReaderRoot::NextEvent()
443 // go to the next event in the root file
445 if (!fFile) return kFALSE;
448 if (fBranch->GetEntry(fEventIndex+1) <= 0) {
450 fEvent = new AliRawEvent;
451 fBranch->SetAddress(&fEvent);
455 } while (!IsEventSelected());
459 Bool_t AliRawReaderRoot::RewindEvents()
461 // go back to the beginning of the root file
463 if (!fFile) return kFALSE;
467 fEvent = new AliRawEvent;
468 fBranch->SetAddress(&fEvent);
473 Int_t AliRawReaderRoot::CheckData() const
475 // check the consistency of the data
477 if (!fEvent) return 0;
479 AliRawEvent* subEvent = NULL;
480 Int_t subEventIndex = 0;
481 AliRawEquipment* equipment = NULL;
482 Int_t equipmentIndex = 0;
483 UChar_t* position = 0;
488 // get the first or the next sub event if at the end of an equipment
489 if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
491 // check for end of event data
492 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
493 subEvent = fEvent->GetSubEvent(subEventIndex++);
495 // check the magic word of the sub event
496 if (!fSubEvent->GetHeader()->IsValid()) {
504 // get the next equipment and raw data
505 equipment = subEvent->GetEquipment(equipmentIndex++);
506 if (!equipment) continue;
507 AliRawData* rawData = equipment->GetRawData();
508 if (!rawData) continue;
509 position = (UChar_t*) rawData->GetBuffer();
510 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
512 // continue with the next sub event if no data left in the payload
513 if (position >= end) continue;
515 // check that there are enough bytes left for the data header
516 if (position + sizeof(AliRawDataHeader) > end) {
517 result |= kErrNoDataHeader;
521 // check consistency of data size in the header and in the equipment
522 AliRawDataHeader* header = (AliRawDataHeader*) position;
523 if (header->fSize != 0xFFFFFFFF) {
524 if (position + header->fSize > end) {