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
36 TDirectory* dir = gDirectory;
37 fFile = TFile::Open(fileName);
39 if (!fFile || !fFile->IsOpen()) {
40 Error("AliRawReaderRoot", "could not open file %s", fileName);
43 TTree* tree = (TTree*) fFile->Get("RAW");
45 Error("AliRawReaderRoot", "no raw data tree found");
48 TBranch* branch = tree->GetBranch("rawevent");
50 Error("AliRawReaderRoot", "no raw data branch found");
54 fEvent = new AliRawEvent;
55 branch->SetAddress(&fEvent);
56 if (branch->GetEntry(eventNumber) <= 0) {
57 Error("AliRawReaderRoot", "no event with number %d found", eventNumber);
67 fPosition = fEnd = NULL;
70 AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event)
72 // create an object to read digits from the given raw event
83 fPosition = fEnd = NULL;
86 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
87 AliRawReader(rawReader)
92 fEvent = rawReader.fEvent;
94 fSubEventIndex = rawReader.fSubEventIndex;
95 fSubEvent = rawReader.fSubEvent;
96 fRawData = rawReader.fRawData;
97 fMiniHeader = rawReader.fMiniHeader;
99 fCount = rawReader.fCount;
100 fPosition = rawReader.fPosition;
101 fEnd = rawReader.fEnd;
104 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
107 // assignment operator
109 this->~AliRawReaderRoot();
110 new(this) AliRawReaderRoot(rawReader);
114 AliRawReaderRoot::~AliRawReaderRoot()
116 // delete objects and close root file
119 if (fEvent) delete fEvent;
126 UInt_t AliRawReaderRoot::GetType() const
128 // get the type from the event header
130 if (!fEvent) return 0;
131 return fEvent->GetHeader()->GetType();
134 UInt_t AliRawReaderRoot::GetRunNumber() const
136 // get the run number from the event header
138 if (!fEvent) return 0;
139 return fEvent->GetHeader()->GetRunNumber();
142 const UInt_t* AliRawReaderRoot::GetEventId() const
144 // get the event id from the event header
146 if (!fEvent) return NULL;
147 return fEvent->GetHeader()->GetId();
150 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
152 // get the trigger pattern from the event header
154 if (!fEvent) return NULL;
155 return fEvent->GetHeader()->GetTriggerPattern();
158 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
160 // get the detector pattern from the event header
162 if (!fEvent) return NULL;
163 return fEvent->GetHeader()->GetDetectorPattern();
166 const UInt_t* AliRawReaderRoot::GetAttributes() const
168 // get the type attributes from the event header
170 if (!fEvent) return NULL;
171 return fEvent->GetHeader()->GetTypeAttribute();
174 UInt_t AliRawReaderRoot::GetGDCId() const
176 // get the GDC Id from the event header
178 if (!fEvent) return 0;
179 return fEvent->GetHeader()->GetGDCId();
183 Bool_t AliRawReaderRoot::ReadMiniHeader()
185 // read a mini header at the current position
186 // returns kFALSE if the mini header could not be read
189 if (!fEvent) return kFALSE;
192 // skip payload (if event was not selected)
193 if (fCount > 0) fPosition += fCount;
195 // get the first or the next sub event if at the end of a sub event
196 if (!fSubEvent || (fPosition >= fEnd)) {
198 // check for end of event data
199 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
200 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
202 // check the magic word of the sub event
203 if (!fSubEvent->GetHeader()->IsValid()) {
204 Error("ReadMiniHeader", "wrong magic number in sub event!");
205 fSubEvent->GetHeader()->Dump();
206 fErrorCode = kErrMagic;
210 fRawData = fSubEvent->GetRawData();
212 fPosition = (UChar_t*) fRawData->GetBuffer();
213 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
216 // continue with the next sub event if no data left in the payload
217 if (fPosition >= fEnd) continue;
219 // check that there are enough bytes left for the mini header
220 if (fPosition + sizeof(AliMiniHeader) > fEnd) {
221 Error("ReadMiniHeader", "could not read mini header data!");
222 Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition);
223 fSubEvent->GetHeader()->Dump();
226 fErrorCode = kErrNoMiniHeader;
230 // "read" and check the mini header
231 fMiniHeader = (AliMiniHeader*) fPosition;
232 fPosition += sizeof(AliMiniHeader);
233 if (!CheckMiniHeader()) {
234 Error("ReadMiniHeader", "wrong magic word in mini header!");
235 Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition);
236 fSubEvent->GetHeader()->Dump();
239 fErrorCode = kErrMiniMagic;
242 fCount = fMiniHeader->fSize;
244 // check consistency of data size in the mini header and in the sub event
245 if (fPosition + fCount > fEnd) {
246 Error("ReadMiniHeader", "size in mini header exceeds event size!");
247 Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition);
248 fSubEvent->GetHeader()->Dump();
251 fErrorCode = kErrSize;
255 } while (!IsSelected());
260 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
262 // reads the next payload at the current position
263 // returns kFALSE if the data could not be read
266 while (fCount == 0) {
267 if (!ReadMiniHeader()) return kFALSE;
275 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
277 // reads the next block of data at the current position
278 // returns kFALSE if the data could not be read
281 if (fPosition + size > fEnd) {
282 Error("ReadNext", "could not read data!");
283 fErrorCode = kErrOutOfBounds;
286 memcpy(data, fPosition, size);
293 Bool_t AliRawReaderRoot::Reset()
295 // reset the current position to the beginning of the event
303 fPosition = fEnd = NULL;
308 Int_t AliRawReaderRoot::CheckData() const
310 // check the consistency of the data
312 if (!fEvent) return 0;
314 AliRawEvent* subEvent = NULL;
315 Int_t subEventIndex = 0;
316 UChar_t* position = 0;
321 // get the first or the next sub event if at the end of a sub event
322 if (!subEvent || (position >= end)) {
324 // check for end of event data
325 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
326 subEvent = fEvent->GetSubEvent(subEventIndex++);
328 // check the magic word of the sub event
329 if (!fSubEvent->GetHeader()->IsValid()) {
334 AliRawData* rawData = subEvent->GetRawData();
335 position = (UChar_t*) rawData->GetBuffer();
336 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
339 // continue with the next sub event if no data left in the payload
340 if (position >= end) continue;
342 // check that there are enough bytes left for the mini header
343 if (position + sizeof(AliMiniHeader) > end) {
344 result |= kErrNoMiniHeader;
349 // "read" and check the mini header
350 AliMiniHeader* miniHeader = (AliMiniHeader*) position;
351 position += sizeof(AliMiniHeader);
352 if (!CheckMiniHeader(miniHeader)) {
353 result |= kErrMiniMagic;
358 // check consistency of data size in the mini header and in the sub event
359 if (position + miniHeader->fSize > end) result |= kErrSize;
360 position += miniHeader->fSize;