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 "AliRawEquipmentHeader.h"
37 #include "AliRawData.h"
40 ClassImp(AliRawReaderRoot)
43 AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
46 fEventIndex(eventNumber),
54 // create an object to read digits from the given input file for the
55 // event with the given number
57 TDirectory* dir = gDirectory;
58 fFile = TFile::Open(fileName);
60 if (!fFile || !fFile->IsOpen()) {
61 Error("AliRawReaderRoot", "could not open file %s", fileName);
64 TTree* tree = (TTree*) fFile->Get("RAW");
66 Error("AliRawReaderRoot", "no raw data tree found");
69 fBranch = tree->GetBranch("rawevent");
71 Error("AliRawReaderRoot", "no raw data branch found");
75 fEvent = new AliRawEvent;
76 fBranch->SetAddress(&fEvent);
77 if (fEventIndex >= 0) {
78 if (fBranch->GetEntry(fEventIndex) <= 0) {
79 Error("AliRawReaderRoot", "no event with number %d found", fEventIndex);
85 AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event) :
96 // create an object to read digits from the given raw event
100 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
101 AliRawReader(rawReader),
104 fEventIndex(rawReader.fEventIndex),
106 fSubEventIndex(rawReader.fSubEventIndex),
114 if (rawReader.fFile) {
115 TDirectory* dir = gDirectory;
116 fFile = TFile::Open(rawReader.fFile->GetName());
118 if (!fFile || !fFile->IsOpen()) {
119 Error("AliRawReaderRoot", "could not open file %s",
120 rawReader.fFile->GetName());
123 TTree* tree = (TTree*) fFile->Get("RAW");
125 Error("AliRawReaderRoot", "no raw data tree found");
128 fBranch = tree->GetBranch("rawevent");
130 Error("AliRawReaderRoot", "no raw data branch found");
134 fEvent = new AliRawEvent;
135 fBranch->SetAddress(&fEvent);
136 if (fEventIndex >= 0) {
137 if (fBranch->GetEntry(fEventIndex) <= 0) {
138 Error("AliRawReaderRoot", "no event with number %d found",
144 fEvent = rawReader.fEvent;
147 if (fSubEventIndex > 0) {
148 fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
149 fRawData = fSubEvent->GetRawData();
151 fHeader = (AliRawDataHeader*) ((UChar_t*) fRawData->GetBuffer() +
152 ((UChar_t*) rawReader.fHeader -
153 (UChar_t*) rawReader.fRawData->GetBuffer()));
154 fPosition = (UChar_t*) fRawData->GetBuffer() +
155 (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
156 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
160 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
163 // assignment operator
165 this->~AliRawReaderRoot();
166 new(this) AliRawReaderRoot(rawReader);
170 AliRawReaderRoot::~AliRawReaderRoot()
172 // delete objects and close root file
175 if (fEvent) delete fEvent;
182 UInt_t AliRawReaderRoot::GetType() const
184 // get the type from the event header
186 if (!fEvent) return 0;
187 return fEvent->GetHeader()->GetType();
190 UInt_t AliRawReaderRoot::GetRunNumber() const
192 // get the run number from the event header
194 if (!fEvent) return 0;
195 return fEvent->GetHeader()->GetRunNumber();
198 const UInt_t* AliRawReaderRoot::GetEventId() const
200 // get the event id from the event header
202 if (!fEvent) return NULL;
203 return fEvent->GetHeader()->GetId();
206 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
208 // get the trigger pattern from the event header
210 if (!fEvent) return NULL;
211 return fEvent->GetHeader()->GetTriggerPattern();
214 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
216 // get the detector pattern from the event header
218 if (!fEvent) return NULL;
219 return fEvent->GetHeader()->GetDetectorPattern();
222 const UInt_t* AliRawReaderRoot::GetAttributes() const
224 // get the type attributes from the event header
226 if (!fEvent) return NULL;
227 return fEvent->GetHeader()->GetTypeAttribute();
230 UInt_t AliRawReaderRoot::GetLDCId() const
232 // get the LDC Id from the event header
234 if (!fEvent || !fEvent->GetSubEvent(fSubEventIndex)) return 0;
235 return fEvent->GetSubEvent(fSubEventIndex)->GetHeader()->GetLDCId();
238 UInt_t AliRawReaderRoot::GetGDCId() const
240 // get the GDC Id from the event header
242 if (!fEvent) return 0;
243 return fEvent->GetHeader()->GetGDCId();
247 Int_t AliRawReaderRoot::GetEquipmentSize() const
249 // get the size of the equipment
251 if (!fEvent || !fEvent->GetEquipmentHeader()) return 0;
252 return fEvent->GetEquipmentHeader()->GetEquipmentSize();
255 Int_t AliRawReaderRoot::GetEquipmentType() const
257 // get the type from the equipment header
259 if (!fEvent || !fEvent->GetEquipmentHeader()) return -1;
260 return fEvent->GetEquipmentHeader()->GetEquipmentType();
263 Int_t AliRawReaderRoot::GetEquipmentId() const
265 // get the ID from the equipment header
267 if (!fEvent || !fEvent->GetEquipmentHeader()) return -1;
268 return fEvent->GetEquipmentHeader()->GetId();
271 const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
273 // get the attributes from the equipment header
275 if (!fEvent || !fEvent->GetEquipmentHeader()) return NULL;
276 return fEvent->GetEquipmentHeader()->GetTypeAttribute();
279 Int_t AliRawReaderRoot::GetEquipmentElementSize() const
281 // get the basic element size from the equipment header
283 if (!fEvent || !fEvent->GetEquipmentHeader()) return 0;
284 return fEvent->GetEquipmentHeader()->GetBasicSizeType();
288 Bool_t AliRawReaderRoot::ReadHeader()
290 // read a data header at the current position
291 // returns kFALSE if the data header could not be read
294 if (!fEvent) return kFALSE;
297 // skip payload (if event was not selected)
298 if (fCount > 0) fPosition += fCount;
300 // get the first or the next sub event if at the end of a sub event
301 if (!fSubEvent || (fPosition >= fEnd)) {
303 // check for end of event data
304 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
305 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
307 // check the magic word of the sub event
308 if (!fSubEvent->GetHeader()->IsValid()) {
309 Error("ReadHeader", "wrong magic number in sub event!");
310 fSubEvent->GetHeader()->Dump();
311 fErrorCode = kErrMagic;
315 fRawData = fSubEvent->GetRawData();
317 fPosition = (UChar_t*) fRawData->GetBuffer();
318 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
321 // continue with the next sub event if no data left in the payload
322 if (fPosition >= fEnd) continue;
324 // check that there are enough bytes left for the data header
325 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
326 Error("ReadHeader", "could not read data header!");
327 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
328 fSubEvent->GetHeader()->Dump();
331 fErrorCode = kErrNoDataHeader;
335 // "read" the data header
336 fHeader = (AliRawDataHeader*) fPosition;
337 fPosition += sizeof(AliRawDataHeader);
338 if (fHeader->fSize != 0xFFFFFFFF) {
339 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
341 fCount = fEnd - fPosition;
344 // check consistency of data size in the header and in the sub event
345 if (fPosition + fCount > fEnd) {
346 Error("ReadHeader", "size in data header exceeds event size!");
347 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
348 fSubEvent->GetHeader()->Dump();
351 fErrorCode = kErrSize;
355 } while (!IsSelected());
360 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
362 // reads the next payload at the current position
363 // returns kFALSE if the data could not be read
366 while (fCount == 0) {
367 if (!ReadHeader()) return kFALSE;
375 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
377 // reads the next block of data at the current position
378 // returns kFALSE if the data could not be read
381 if (fPosition + size > fEnd) {
382 Error("ReadNext", "could not read data!");
383 fErrorCode = kErrOutOfBounds;
386 memcpy(data, fPosition, size);
393 Bool_t AliRawReaderRoot::Reset()
395 // reset the current position to the beginning of the event
403 fPosition = fEnd = NULL;
408 Bool_t AliRawReaderRoot::NextEvent()
410 // go to the next event in the root file
412 if (!fFile) return kFALSE;
415 if (fBranch->GetEntry(fEventIndex+1) <= 0) {
417 fEvent = new AliRawEvent;
418 fBranch->SetAddress(&fEvent);
422 } while (!IsEventSelected());
426 Bool_t AliRawReaderRoot::RewindEvents()
428 // go back to the beginning of the root file
430 if (!fFile) return kFALSE;
434 fEvent = new AliRawEvent;
435 fBranch->SetAddress(&fEvent);
440 Int_t AliRawReaderRoot::CheckData() const
442 // check the consistency of the data
444 if (!fEvent) return 0;
446 AliRawEvent* subEvent = NULL;
447 Int_t subEventIndex = 0;
448 UChar_t* position = 0;
453 // get the first or the next sub event if at the end of a sub event
454 if (!subEvent || (position >= end)) {
456 // check for end of event data
457 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
458 subEvent = fEvent->GetSubEvent(subEventIndex++);
460 // check the magic word of the sub event
461 if (!fSubEvent->GetHeader()->IsValid()) {
466 AliRawData* rawData = subEvent->GetRawData();
467 position = (UChar_t*) rawData->GetBuffer();
468 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
471 // continue with the next sub event if no data left in the payload
472 if (position >= end) continue;
474 // check that there are enough bytes left for the data header
475 if (position + sizeof(AliRawDataHeader) > end) {
476 result |= kErrNoDataHeader;
481 // check consistency of data size in the header and in the sub event
482 AliRawDataHeader* header = (AliRawDataHeader*) position;
483 if (fHeader->fSize != 0xFFFFFFFF) {
484 if (position + header->fSize > end) {
488 position += header->fSize;