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)
45 // create an object to read digits from the given input file for the
46 // event with the given number
49 TDirectory* dir = gDirectory;
50 fFile = TFile::Open(fileName);
52 if (!fFile || !fFile->IsOpen()) {
53 Error("AliRawReaderRoot", "could not open file %s", fileName);
56 TTree* tree = (TTree*) fFile->Get("RAW");
58 Error("AliRawReaderRoot", "no raw data tree found");
61 TBranch* branch = tree->GetBranch("rawevent");
63 Error("AliRawReaderRoot", "no raw data branch found");
67 fEvent = new AliRawEvent;
68 branch->SetAddress(&fEvent);
69 if (branch->GetEntry(eventNumber) <= 0) {
70 Error("AliRawReaderRoot", "no event with number %d found", eventNumber);
80 fPosition = fEnd = NULL;
83 AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event)
85 // create an object to read digits from the given raw event
96 fPosition = fEnd = NULL;
99 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
100 AliRawReader(rawReader)
105 fEvent = rawReader.fEvent;
107 fSubEventIndex = rawReader.fSubEventIndex;
108 fSubEvent = rawReader.fSubEvent;
109 fRawData = rawReader.fRawData;
110 fHeader = rawReader.fHeader;
112 fCount = rawReader.fCount;
113 fPosition = rawReader.fPosition;
114 fEnd = rawReader.fEnd;
117 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
120 // assignment operator
122 this->~AliRawReaderRoot();
123 new(this) AliRawReaderRoot(rawReader);
127 AliRawReaderRoot::~AliRawReaderRoot()
129 // delete objects and close root file
132 if (fEvent) delete fEvent;
139 UInt_t AliRawReaderRoot::GetType() const
141 // get the type from the event header
143 if (!fEvent) return 0;
144 return fEvent->GetHeader()->GetType();
147 UInt_t AliRawReaderRoot::GetRunNumber() const
149 // get the run number from the event header
151 if (!fEvent) return 0;
152 return fEvent->GetHeader()->GetRunNumber();
155 const UInt_t* AliRawReaderRoot::GetEventId() const
157 // get the event id from the event header
159 if (!fEvent) return NULL;
160 return fEvent->GetHeader()->GetId();
163 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
165 // get the trigger pattern from the event header
167 if (!fEvent) return NULL;
168 return fEvent->GetHeader()->GetTriggerPattern();
171 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
173 // get the detector pattern from the event header
175 if (!fEvent) return NULL;
176 return fEvent->GetHeader()->GetDetectorPattern();
179 const UInt_t* AliRawReaderRoot::GetAttributes() const
181 // get the type attributes from the event header
183 if (!fEvent) return NULL;
184 return fEvent->GetHeader()->GetTypeAttribute();
187 UInt_t AliRawReaderRoot::GetLDCId() const
189 // get the LDC Id from the event header
191 if (!fEvent || !fEvent->GetSubEvent(fSubEventIndex)) return 0;
192 return fEvent->GetSubEvent(fSubEventIndex)->GetHeader()->GetLDCId();
195 UInt_t AliRawReaderRoot::GetGDCId() const
197 // get the GDC Id from the event header
199 if (!fEvent) return 0;
200 return fEvent->GetHeader()->GetGDCId();
204 Int_t AliRawReaderRoot::GetEquipmentSize() const
206 // get the size of the equipment
208 if (!fEvent || !fEvent->GetEquipmentHeader()) return 0;
209 return fEvent->GetEquipmentHeader()->GetEquipmentSize();
212 Int_t AliRawReaderRoot::GetEquipmentType() const
214 // get the type from the equipment header
216 if (!fEvent || !fEvent->GetEquipmentHeader()) return -1;
217 return fEvent->GetEquipmentHeader()->GetEquipmentType();
220 Int_t AliRawReaderRoot::GetEquipmentId() const
222 // get the ID from the equipment header
224 if (!fEvent || !fEvent->GetEquipmentHeader()) return -1;
225 return fEvent->GetEquipmentHeader()->GetId();
228 const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
230 // get the attributes from the equipment header
232 if (!fEvent || !fEvent->GetEquipmentHeader()) return NULL;
233 return fEvent->GetEquipmentHeader()->GetTypeAttribute();
236 Int_t AliRawReaderRoot::GetEquipmentElementSize() const
238 // get the basic element size from the equipment header
240 if (!fEvent || !fEvent->GetEquipmentHeader()) return 0;
241 return fEvent->GetEquipmentHeader()->GetBasicSizeType();
245 Bool_t AliRawReaderRoot::ReadHeader()
247 // read a data header at the current position
248 // returns kFALSE if the data header could not be read
251 if (!fEvent) return kFALSE;
254 // skip payload (if event was not selected)
255 if (fCount > 0) fPosition += fCount;
257 // get the first or the next sub event if at the end of a sub event
258 if (!fSubEvent || (fPosition >= fEnd)) {
260 // check for end of event data
261 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
262 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
264 // check the magic word of the sub event
265 if (!fSubEvent->GetHeader()->IsValid()) {
266 Error("ReadHeader", "wrong magic number in sub event!");
267 fSubEvent->GetHeader()->Dump();
268 fErrorCode = kErrMagic;
272 fRawData = fSubEvent->GetRawData();
274 fPosition = (UChar_t*) fRawData->GetBuffer();
275 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
278 // continue with the next sub event if no data left in the payload
279 if (fPosition >= fEnd) continue;
281 // check that there are enough bytes left for the data header
282 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
283 Error("ReadHeader", "could not read data header!");
284 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
285 fSubEvent->GetHeader()->Dump();
288 fErrorCode = kErrNoDataHeader;
292 // "read" the data header
293 fHeader = (AliRawDataHeader*) fPosition;
294 fPosition += sizeof(AliRawDataHeader);
295 if (fHeader->fSize != 0xFFFFFFFF) {
296 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
298 fCount = fEnd - fPosition;
301 // check consistency of data size in the header and in the sub event
302 if (fPosition + fCount > fEnd) {
303 Error("ReadHeader", "size in data header exceeds event size!");
304 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
305 fSubEvent->GetHeader()->Dump();
308 fErrorCode = kErrSize;
312 } while (!IsSelected());
317 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
319 // reads the next payload at the current position
320 // returns kFALSE if the data could not be read
323 while (fCount == 0) {
324 if (!ReadHeader()) return kFALSE;
332 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
334 // reads the next block of data at the current position
335 // returns kFALSE if the data could not be read
338 if (fPosition + size > fEnd) {
339 Error("ReadNext", "could not read data!");
340 fErrorCode = kErrOutOfBounds;
343 memcpy(data, fPosition, size);
350 Bool_t AliRawReaderRoot::Reset()
352 // reset the current position to the beginning of the event
360 fPosition = fEnd = NULL;
365 Int_t AliRawReaderRoot::CheckData() const
367 // check the consistency of the data
369 if (!fEvent) return 0;
371 AliRawEvent* subEvent = NULL;
372 Int_t subEventIndex = 0;
373 UChar_t* position = 0;
378 // get the first or the next sub event if at the end of a sub event
379 if (!subEvent || (position >= end)) {
381 // check for end of event data
382 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
383 subEvent = fEvent->GetSubEvent(subEventIndex++);
385 // check the magic word of the sub event
386 if (!fSubEvent->GetHeader()->IsValid()) {
391 AliRawData* rawData = subEvent->GetRawData();
392 position = (UChar_t*) rawData->GetBuffer();
393 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
396 // continue with the next sub event if no data left in the payload
397 if (position >= end) continue;
399 // check that there are enough bytes left for the data header
400 if (position + sizeof(AliRawDataHeader) > end) {
401 result |= kErrNoDataHeader;
406 // check consistency of data size in the header and in the sub event
407 AliRawDataHeader* header = (AliRawDataHeader*) position;
408 if (fHeader->fSize != 0xFFFFFFFF) {
409 if (position + header->fSize > end) {
413 position += header->fSize;