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 **************************************************************************/
16 ///////////////////////////////////////////////////////////////////////////////
18 // This is a class for reading a raw data from a root file and providing
19 // information about digits
21 ///////////////////////////////////////////////////////////////////////////////
25 #include "AliRawReaderRoot.h"
26 #include "AliRawEvent.h"
27 #include "AliRawEventHeader.h"
28 #include "AliRawEquipmentHeader.h"
29 #include "AliRawData.h"
32 ClassImp(AliRawReaderRoot)
35 AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber)
37 // create an object to read digits from the given input file for the
38 // event with the given number
41 TDirectory* dir = gDirectory;
42 fFile = TFile::Open(fileName);
44 if (!fFile || !fFile->IsOpen()) {
45 Error("AliRawReaderRoot", "could not open file %s", fileName);
48 TTree* tree = (TTree*) fFile->Get("RAW");
50 Error("AliRawReaderRoot", "no raw data tree found");
53 TBranch* branch = tree->GetBranch("rawevent");
55 Error("AliRawReaderRoot", "no raw data branch found");
59 fEvent = new AliRawEvent;
60 branch->SetAddress(&fEvent);
61 if (branch->GetEntry(eventNumber) <= 0) {
62 Error("AliRawReaderRoot", "no event with number %d found", eventNumber);
72 fPosition = fEnd = NULL;
75 AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event)
77 // create an object to read digits from the given raw event
88 fPosition = fEnd = NULL;
91 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
92 AliRawReader(rawReader)
97 fEvent = rawReader.fEvent;
99 fSubEventIndex = rawReader.fSubEventIndex;
100 fSubEvent = rawReader.fSubEvent;
101 fRawData = rawReader.fRawData;
102 fHeader = rawReader.fHeader;
104 fCount = rawReader.fCount;
105 fPosition = rawReader.fPosition;
106 fEnd = rawReader.fEnd;
109 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
112 // assignment operator
114 this->~AliRawReaderRoot();
115 new(this) AliRawReaderRoot(rawReader);
119 AliRawReaderRoot::~AliRawReaderRoot()
121 // delete objects and close root file
124 if (fEvent) delete fEvent;
131 UInt_t AliRawReaderRoot::GetType() const
133 // get the type from the event header
135 if (!fEvent) return 0;
136 return fEvent->GetHeader()->GetType();
139 UInt_t AliRawReaderRoot::GetRunNumber() const
141 // get the run number from the event header
143 if (!fEvent) return 0;
144 return fEvent->GetHeader()->GetRunNumber();
147 const UInt_t* AliRawReaderRoot::GetEventId() const
149 // get the event id from the event header
151 if (!fEvent) return NULL;
152 return fEvent->GetHeader()->GetId();
155 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
157 // get the trigger pattern from the event header
159 if (!fEvent) return NULL;
160 return fEvent->GetHeader()->GetTriggerPattern();
163 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
165 // get the detector pattern from the event header
167 if (!fEvent) return NULL;
168 return fEvent->GetHeader()->GetDetectorPattern();
171 const UInt_t* AliRawReaderRoot::GetAttributes() const
173 // get the type attributes from the event header
175 if (!fEvent) return NULL;
176 return fEvent->GetHeader()->GetTypeAttribute();
179 UInt_t AliRawReaderRoot::GetLDCId() const
181 // get the LDC Id from the event header
183 if (!fEvent || !fEvent->GetSubEvent(fSubEventIndex)) return 0;
184 return fEvent->GetSubEvent(fSubEventIndex)->GetHeader()->GetLDCId();
187 UInt_t AliRawReaderRoot::GetGDCId() const
189 // get the GDC Id from the event header
191 if (!fEvent) return 0;
192 return fEvent->GetHeader()->GetGDCId();
196 Int_t AliRawReaderRoot::GetEquipmentSize() const
198 // get the size of the equipment
200 if (!fEvent || !fEvent->GetEquipmentHeader()) return 0;
201 return fEvent->GetEquipmentHeader()->GetEquipmentSize();
204 Int_t AliRawReaderRoot::GetEquipmentType() const
206 // get the type from the equipment header
208 if (!fEvent || !fEvent->GetEquipmentHeader()) return -1;
209 return fEvent->GetEquipmentHeader()->GetEquipmentType();
212 Int_t AliRawReaderRoot::GetEquipmentId() const
214 // get the ID from the equipment header
216 if (!fEvent || !fEvent->GetEquipmentHeader()) return -1;
217 return fEvent->GetEquipmentHeader()->GetId();
220 const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
222 // get the attributes from the equipment header
224 if (!fEvent || !fEvent->GetEquipmentHeader()) return NULL;
225 return fEvent->GetEquipmentHeader()->GetTypeAttribute();
228 Int_t AliRawReaderRoot::GetEquipmentElementSize() const
230 // get the basic element size from the equipment header
232 if (!fEvent || !fEvent->GetEquipmentHeader()) return 0;
233 return fEvent->GetEquipmentHeader()->GetBasicSizeType();
237 Bool_t AliRawReaderRoot::ReadHeader()
239 // read a data header at the current position
240 // returns kFALSE if the data header could not be read
243 if (!fEvent) return kFALSE;
246 // skip payload (if event was not selected)
247 if (fCount > 0) fPosition += fCount;
249 // get the first or the next sub event if at the end of a sub event
250 if (!fSubEvent || (fPosition >= fEnd)) {
252 // check for end of event data
253 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
254 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
256 // check the magic word of the sub event
257 if (!fSubEvent->GetHeader()->IsValid()) {
258 Error("ReadHeader", "wrong magic number in sub event!");
259 fSubEvent->GetHeader()->Dump();
260 fErrorCode = kErrMagic;
264 fRawData = fSubEvent->GetRawData();
266 fPosition = (UChar_t*) fRawData->GetBuffer();
267 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
270 // continue with the next sub event if no data left in the payload
271 if (fPosition >= fEnd) continue;
273 // check that there are enough bytes left for the data header
274 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
275 Error("ReadHeader", "could not read data header!");
276 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
277 fSubEvent->GetHeader()->Dump();
280 fErrorCode = kErrNoDataHeader;
284 // "read" the data header
285 fHeader = (AliRawDataHeader*) fPosition;
286 fPosition += sizeof(AliRawDataHeader);
287 if (fHeader->fSize != 0xFFFFFFFF) {
288 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
290 fCount = fEnd - fPosition;
293 // check consistency of data size in the header and in the sub event
294 if (fPosition + fCount > fEnd) {
295 Error("ReadHeader", "size in data header exceeds event size!");
296 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
297 fSubEvent->GetHeader()->Dump();
300 fErrorCode = kErrSize;
304 } while (!IsSelected());
309 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
311 // reads the next payload at the current position
312 // returns kFALSE if the data could not be read
315 while (fCount == 0) {
316 if (!ReadHeader()) return kFALSE;
324 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
326 // reads the next block of data at the current position
327 // returns kFALSE if the data could not be read
330 if (fPosition + size > fEnd) {
331 Error("ReadNext", "could not read data!");
332 fErrorCode = kErrOutOfBounds;
335 memcpy(data, fPosition, size);
342 Bool_t AliRawReaderRoot::Reset()
344 // reset the current position to the beginning of the event
352 fPosition = fEnd = NULL;
357 Int_t AliRawReaderRoot::CheckData() const
359 // check the consistency of the data
361 if (!fEvent) return 0;
363 AliRawEvent* subEvent = NULL;
364 Int_t subEventIndex = 0;
365 UChar_t* position = 0;
370 // get the first or the next sub event if at the end of a sub event
371 if (!subEvent || (position >= end)) {
373 // check for end of event data
374 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
375 subEvent = fEvent->GetSubEvent(subEventIndex++);
377 // check the magic word of the sub event
378 if (!fSubEvent->GetHeader()->IsValid()) {
383 AliRawData* rawData = subEvent->GetRawData();
384 position = (UChar_t*) rawData->GetBuffer();
385 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
388 // continue with the next sub event if no data left in the payload
389 if (position >= end) continue;
391 // check that there are enough bytes left for the data header
392 if (position + sizeof(AliRawDataHeader) > end) {
393 result |= kErrNoDataHeader;
398 // check consistency of data size in the header and in the sub event
399 AliRawDataHeader* header = (AliRawDataHeader*) position;
400 if (fHeader->fSize != 0xFFFFFFFF) {
401 if (position + header->fSize > end) {
405 position += header->fSize;