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 ///////////////////////////////////////////////////////////////////////////////
23 #include "AliRawReaderRoot.h"
24 #include "AliRawEvent.h"
27 ClassImp(AliRawReaderRoot)
30 AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber)
32 // create an object to read digits from the given input file for the
33 // event with the given number
35 TDirectory* dir = gDirectory;
36 fFile = TFile::Open(fileName);
38 if (!fFile || !fFile->IsOpen()) {
39 Error("AliRawReaderRoot", "could not open file %s", fileName);
42 TTree* tree = (TTree*) fFile->Get("RAW");
44 Error("AliRawReaderRoot", "no raw data tree found");
47 TBranch* branch = tree->GetBranch("rawevent");
49 Error("AliRawReaderRoot", "no raw data branch found");
53 fEvent = new AliRawEvent;
54 branch->SetAddress(&fEvent);
55 if (branch->GetEntry(eventNumber) <= 0) {
56 Error("AliRawReaderRoot", "no event with number %d found", eventNumber);
66 fPosition = fEnd = NULL;
69 AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event)
71 // create an object to read digits from the given raw event
82 fPosition = fEnd = NULL;
85 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
86 AliRawReader(rawReader)
91 fEvent = rawReader.fEvent;
93 fSubEventIndex = rawReader.fSubEventIndex;
94 fSubEvent = rawReader.fSubEvent;
95 fRawData = rawReader.fRawData;
96 fMiniHeader = rawReader.fMiniHeader;
98 fCount = rawReader.fCount;
99 fPosition = rawReader.fPosition;
100 fEnd = rawReader.fEnd;
103 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
106 // assignment operator
108 this->~AliRawReaderRoot();
109 new(this) AliRawReaderRoot(rawReader);
113 AliRawReaderRoot::~AliRawReaderRoot()
115 // delete objects and close root file
118 if (fEvent) delete fEvent;
125 UInt_t AliRawReaderRoot::GetType() const
127 // get the type from the event header
129 if (!fEvent) return 0;
130 return fEvent->GetHeader()->GetType();
133 UInt_t AliRawReaderRoot::GetRunNumber() const
135 // get the run number from the event header
137 if (!fEvent) return 0;
138 return fEvent->GetHeader()->GetRunNumber();
141 const UInt_t* AliRawReaderRoot::GetEventId() const
143 // get the event id from the event header
145 if (!fEvent) return NULL;
146 return fEvent->GetHeader()->GetId();
149 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
151 // get the trigger pattern from the event header
153 if (!fEvent) return NULL;
154 return fEvent->GetHeader()->GetTriggerPattern();
157 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
159 // get the detector pattern from the event header
161 if (!fEvent) return NULL;
162 return fEvent->GetHeader()->GetDetectorPattern();
165 const UInt_t* AliRawReaderRoot::GetAttributes() const
167 // get the type attributes from the event header
169 if (!fEvent) return NULL;
170 return fEvent->GetHeader()->GetTypeAttribute();
173 UInt_t AliRawReaderRoot::GetGDCId() const
175 // get the GDC Id from the event header
177 if (!fEvent) return 0;
178 return fEvent->GetHeader()->GetGDCId();
182 Bool_t AliRawReaderRoot::ReadMiniHeader()
184 // read a mini header at the current position
185 // returns kFALSE if the mini header could not be read
188 if (!fEvent) return kFALSE;
191 // skip payload (if event was not selected)
192 if (fCount > 0) fPosition += fCount;
194 // get the first or the next sub event if at the end of a sub event
195 if (!fSubEvent || (fPosition >= fEnd)) {
197 // check for end of event data
198 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
199 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
201 // check the magic word of the sub event
202 if (!fSubEvent->GetHeader()->IsValid()) {
203 Error("ReadMiniHeader", "wrong magic number in sub event!");
204 fSubEvent->GetHeader()->Dump();
205 fErrorCode = kErrMagic;
209 fRawData = fSubEvent->GetRawData();
211 fPosition = (UChar_t*) fRawData->GetBuffer();
212 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
215 // continue with the next sub event if no data left in the payload
216 if (fPosition >= fEnd) continue;
218 // check that there are enough bytes left for the mini header
219 if (fPosition + sizeof(AliMiniHeader) > fEnd) {
220 Error("ReadMiniHeader", "could not read mini header data!");
221 Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition);
222 fSubEvent->GetHeader()->Dump();
225 fErrorCode = kErrNoMiniHeader;
229 // "read" and check the mini header
230 fMiniHeader = (AliMiniHeader*) fPosition;
231 fPosition += sizeof(AliMiniHeader);
232 if (!CheckMiniHeader()) {
233 Error("ReadMiniHeader", "wrong magic word in mini header!");
234 Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition);
235 fSubEvent->GetHeader()->Dump();
238 fErrorCode = kErrMiniMagic;
241 fCount = fMiniHeader->fSize;
243 // check consistency of data size in the mini header and in the sub event
244 if (fPosition + fCount > fEnd) {
245 Error("ReadMiniHeader", "size in mini header exceeds event size!");
246 Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition);
247 fSubEvent->GetHeader()->Dump();
250 fErrorCode = kErrSize;
254 } while (!IsSelected());
259 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
261 // reads the next payload at the current position
262 // returns kFALSE if the data could not be read
265 while (fCount == 0) {
266 if (!ReadMiniHeader()) return kFALSE;
274 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
276 // reads the next block of data at the current position
277 // returns kFALSE if the data could not be read
280 if (fPosition + size > fEnd) {
281 Error("ReadNext", "could not read data!");
282 fErrorCode = kErrOutOfBounds;
285 memcpy(data, fPosition, size);
292 Bool_t AliRawReaderRoot::Reset()
294 // reset the current position to the beginning of the event
302 fPosition = fEnd = NULL;
307 Int_t AliRawReaderRoot::CheckData() const
309 // check the consistency of the data
311 if (!fEvent) return 0;
313 AliRawEvent* subEvent = NULL;
314 Int_t subEventIndex = 0;
315 UChar_t* position = 0;
319 // get the first or the next sub event if at the end of a sub event
320 if (!subEvent || (position >= end)) {
322 // check for end of event data
323 if (subEventIndex >= fEvent->GetNSubEvents()) return 0;
324 subEvent = fEvent->GetSubEvent(subEventIndex++);
326 // check the magic word of the sub event
327 if (!fSubEvent->GetHeader()->IsValid()) return kErrMagic;
329 AliRawData* rawData = subEvent->GetRawData();
330 position = (UChar_t*) rawData->GetBuffer();
331 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
334 // continue with the next sub event if no data left in the payload
335 if (position >= end) continue;
337 // check that there are enough bytes left for the mini header
338 if (position + sizeof(AliMiniHeader) > end) return kErrNoMiniHeader;
340 // "read" and check the mini header
341 AliMiniHeader* miniHeader = (AliMiniHeader*) position;
342 position += sizeof(AliMiniHeader);
343 if (!CheckMiniHeader(miniHeader)) return kErrMiniMagic;
345 // check consistency of data size in the mini header and in the sub event
346 if (position + miniHeader->fSize > end) return kErrSize;
347 position += miniHeader->fSize;