]> git.uio.no Git - u/mrichter/AliRoot.git/blame - RAW/AliRawReaderRoot.cxx
support multiple equipments per sub event in the root raw data format
[u/mrichter/AliRoot.git] / RAW / AliRawReaderRoot.cxx
CommitLineData
04fa961a 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
15
bea6b2a4 16/* $Id$ */
17
04fa961a 18///////////////////////////////////////////////////////////////////////////////
bea6b2a4 19///
20/// This is a class for reading raw data from a root file.
21///
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
24/// AliRawEvent.
25///
26/// The file name and the event number are arguments of the constructor
27/// of AliRawReaderRoot.
28///
04fa961a 29///////////////////////////////////////////////////////////////////////////////
30
a197a4ce 31#include <TFile.h>
32#include <TTree.h>
04fa961a 33#include "AliRawReaderRoot.h"
42d20574 34#include "AliRawEvent.h"
a197a4ce 35#include "AliRawEventHeader.h"
94d918a7 36#include "AliRawEquipment.h"
a197a4ce 37#include "AliRawEquipmentHeader.h"
38#include "AliRawData.h"
04fa961a 39
40
41ClassImp(AliRawReaderRoot)
42
43
dd9a70fe 44AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
45 fFile(NULL),
46 fBranch(NULL),
47 fEventIndex(eventNumber),
48 fEvent(NULL),
49 fSubEventIndex(0),
50 fSubEvent(NULL),
94d918a7 51 fEquipmentIndex(0),
52 fEquipment(NULL),
dd9a70fe 53 fRawData(NULL),
54 fPosition(NULL),
55 fEnd(NULL)
04fa961a 56{
57// create an object to read digits from the given input file for the
58// event with the given number
59
03c6d9a3 60 TDirectory* dir = gDirectory;
04fa961a 61 fFile = TFile::Open(fileName);
03c6d9a3 62 dir->cd();
04fa961a 63 if (!fFile || !fFile->IsOpen()) {
64 Error("AliRawReaderRoot", "could not open file %s", fileName);
65 return;
66 }
67 TTree* tree = (TTree*) fFile->Get("RAW");
68 if (!tree) {
69 Error("AliRawReaderRoot", "no raw data tree found");
70 return;
71 }
dd9a70fe 72 fBranch = tree->GetBranch("rawevent");
73 if (!fBranch) {
04fa961a 74 Error("AliRawReaderRoot", "no raw data branch found");
75 return;
76 }
77
78 fEvent = new AliRawEvent;
dd9a70fe 79 fBranch->SetAddress(&fEvent);
80 if (fEventIndex >= 0) {
81 if (fBranch->GetEntry(fEventIndex) <= 0) {
82 Error("AliRawReaderRoot", "no event with number %d found", fEventIndex);
83 return;
84 }
04fa961a 85 }
04fa961a 86}
87
dd9a70fe 88AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event) :
89 fFile(NULL),
90 fBranch(NULL),
91 fEventIndex(-1),
92 fEvent(event),
93 fSubEventIndex(0),
94 fSubEvent(NULL),
94d918a7 95 fEquipmentIndex(0),
96 fEquipment(NULL),
dd9a70fe 97 fRawData(NULL),
98 fPosition(NULL),
99 fEnd(NULL)
04fa961a 100{
101// create an object to read digits from the given raw event
102
04fa961a 103}
104
42d20574 105AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
dd9a70fe 106 AliRawReader(rawReader),
107 fFile(NULL),
108 fBranch(NULL),
109 fEventIndex(rawReader.fEventIndex),
110 fEvent(NULL),
111 fSubEventIndex(rawReader.fSubEventIndex),
112 fSubEvent(NULL),
94d918a7 113 fEquipmentIndex(rawReader.fEquipmentIndex),
114 fEquipment(NULL),
dd9a70fe 115 fRawData(NULL),
116 fPosition(NULL),
117 fEnd(NULL)
42d20574 118{
119// copy constructor
120
dd9a70fe 121 if (rawReader.fFile) {
122 TDirectory* dir = gDirectory;
123 fFile = TFile::Open(rawReader.fFile->GetName());
124 dir->cd();
125 if (!fFile || !fFile->IsOpen()) {
126 Error("AliRawReaderRoot", "could not open file %s",
127 rawReader.fFile->GetName());
128 return;
129 }
130 TTree* tree = (TTree*) fFile->Get("RAW");
131 if (!tree) {
132 Error("AliRawReaderRoot", "no raw data tree found");
133 return;
134 }
135 fBranch = tree->GetBranch("rawevent");
136 if (!fBranch) {
137 Error("AliRawReaderRoot", "no raw data branch found");
138 return;
139 }
140
141 fEvent = new AliRawEvent;
142 fBranch->SetAddress(&fEvent);
143 if (fEventIndex >= 0) {
144 if (fBranch->GetEntry(fEventIndex) <= 0) {
145 Error("AliRawReaderRoot", "no event with number %d found",
146 fEventIndex);
147 return;
148 }
149 }
150 } else {
151 fEvent = rawReader.fEvent;
152 }
153
154 if (fSubEventIndex > 0) {
155 fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
94d918a7 156 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex);
157 fRawData = fEquipment->GetRawData();
dd9a70fe 158 fCount = 0;
159 fHeader = (AliRawDataHeader*) ((UChar_t*) fRawData->GetBuffer() +
160 ((UChar_t*) rawReader.fHeader -
161 (UChar_t*) rawReader.fRawData->GetBuffer()));
162 fPosition = (UChar_t*) fRawData->GetBuffer() +
163 (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
164 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
165 }
42d20574 166}
167
168AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
169 rawReader)
170{
171// assignment operator
172
173 this->~AliRawReaderRoot();
174 new(this) AliRawReaderRoot(rawReader);
175 return *this;
176}
177
04fa961a 178AliRawReaderRoot::~AliRawReaderRoot()
179{
180// delete objects and close root file
181
182 if (fFile) {
183 if (fEvent) delete fEvent;
184 fFile->Close();
185 delete fFile;
186 }
187}
188
189
42d20574 190UInt_t AliRawReaderRoot::GetType() const
04fa961a 191{
192// get the type from the event header
193
194 if (!fEvent) return 0;
195 return fEvent->GetHeader()->GetType();
196}
197
42d20574 198UInt_t AliRawReaderRoot::GetRunNumber() const
04fa961a 199{
200// get the run number from the event header
201
202 if (!fEvent) return 0;
203 return fEvent->GetHeader()->GetRunNumber();
204}
205
42d20574 206const UInt_t* AliRawReaderRoot::GetEventId() const
04fa961a 207{
208// get the event id from the event header
209
210 if (!fEvent) return NULL;
211 return fEvent->GetHeader()->GetId();
212}
213
42d20574 214const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
04fa961a 215{
216// get the trigger pattern from the event header
217
218 if (!fEvent) return NULL;
219 return fEvent->GetHeader()->GetTriggerPattern();
220}
221
42d20574 222const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
04fa961a 223{
224// get the detector pattern from the event header
225
226 if (!fEvent) return NULL;
227 return fEvent->GetHeader()->GetDetectorPattern();
228}
229
42d20574 230const UInt_t* AliRawReaderRoot::GetAttributes() const
04fa961a 231{
232// get the type attributes from the event header
233
234 if (!fEvent) return NULL;
235 return fEvent->GetHeader()->GetTypeAttribute();
236}
237
c946ab02 238UInt_t AliRawReaderRoot::GetLDCId() const
239{
240// get the LDC Id from the event header
241
94d918a7 242 if (!fEvent || !fSubEvent) return 0;
243 return fSubEvent->GetHeader()->GetLDCId();
c946ab02 244}
245
42d20574 246UInt_t AliRawReaderRoot::GetGDCId() const
04fa961a 247{
248// get the GDC Id from the event header
249
250 if (!fEvent) return 0;
251 return fEvent->GetHeader()->GetGDCId();
252}
253
254
c946ab02 255Int_t AliRawReaderRoot::GetEquipmentSize() const
256{
257// get the size of the equipment
258
94d918a7 259 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
260 return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
c946ab02 261}
262
263Int_t AliRawReaderRoot::GetEquipmentType() const
264{
265// get the type from the equipment header
266
94d918a7 267 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
268 return fEquipment->GetEquipmentHeader()->GetEquipmentType();
c946ab02 269}
270
271Int_t AliRawReaderRoot::GetEquipmentId() const
272{
273// get the ID from the equipment header
274
94d918a7 275 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
276 return fEquipment->GetEquipmentHeader()->GetId();
c946ab02 277}
278
279const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
280{
281// get the attributes from the equipment header
282
94d918a7 283 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
284 return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
c946ab02 285}
286
287Int_t AliRawReaderRoot::GetEquipmentElementSize() const
288{
289// get the basic element size from the equipment header
290
94d918a7 291 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
292 return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
c946ab02 293}
294
295
296Bool_t AliRawReaderRoot::ReadHeader()
04fa961a 297{
39f9963f 298// read a data header at the current position
299// returns kFALSE if the data header could not be read
04fa961a 300
b4857df7 301 fErrorCode = 0;
04fa961a 302 if (!fEvent) return kFALSE;
b4857df7 303
04fa961a 304 do {
b4857df7 305 // skip payload (if event was not selected)
306 if (fCount > 0) fPosition += fCount;
307
94d918a7 308 // get the first or the next equipment if at the end of an equipment
309 if (!fEquipment || (fPosition >= fEnd)) {
b4857df7 310
94d918a7 311 // get the first or the next sub event if at the end of a sub event
312 if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
b4857df7 313
94d918a7 314 // check for end of event data
315 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
316 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
317
318 // check the magic word of the sub event
319 if (!fSubEvent->GetHeader()->IsValid()) {
320 Error("ReadHeader", "wrong magic number in sub event!");
321 fSubEvent->GetHeader()->Dump();
322 fErrorCode = kErrMagic;
323 return kFALSE;
324 }
325
326 fEquipmentIndex = 0;
327 fEquipment = NULL;
328 fRawData = NULL;
b4857df7 329 }
330
94d918a7 331 // get the next equipment and raw data
04fa961a 332 fCount = 0;
94d918a7 333 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
334 if (!fEquipment) continue;
335 fRawData = fEquipment->GetRawData();
336 if (!fRawData) {
337 fPosition = fEnd;
338 continue;
339 }
04fa961a 340 fPosition = (UChar_t*) fRawData->GetBuffer();
341 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
342 }
b4857df7 343
94d918a7 344 // continue with the next equipment if no data left in the payload
b4857df7 345 if (fPosition >= fEnd) continue;
346
39f9963f 347 // check that there are enough bytes left for the data header
348 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
349 Error("ReadHeader", "could not read data header!");
c946ab02 350 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
94d918a7 351 fEquipment->GetEquipmentHeader()->Dump();
b4857df7 352 fCount = 0;
353 fPosition = fEnd;
39f9963f 354 fErrorCode = kErrNoDataHeader;
b4857df7 355 continue;
04fa961a 356 }
b4857df7 357
39f9963f 358 // "read" the data header
359 fHeader = (AliRawDataHeader*) fPosition;
360 fPosition += sizeof(AliRawDataHeader);
361 if (fHeader->fSize != 0xFFFFFFFF) {
362 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
363 } else {
364 fCount = fEnd - fPosition;
72dd1d4f 365 }
b4857df7 366
39f9963f 367 // check consistency of data size in the header and in the sub event
b4857df7 368 if (fPosition + fCount > fEnd) {
39f9963f 369 Error("ReadHeader", "size in data header exceeds event size!");
c946ab02 370 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
94d918a7 371 fEquipment->GetEquipmentHeader()->Dump();
03c6d9a3 372 fCount = 0;
373 fPosition = fEnd;
b4857df7 374 fErrorCode = kErrSize;
03c6d9a3 375 continue;
04fa961a 376 }
b4857df7 377
04fa961a 378 } while (!IsSelected());
b4857df7 379
04fa961a 380 return kTRUE;
381}
382
383Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
384{
385// reads the next payload at the current position
386// returns kFALSE if the data could not be read
387
b4857df7 388 fErrorCode = 0;
04fa961a 389 while (fCount == 0) {
c946ab02 390 if (!ReadHeader()) return kFALSE;
04fa961a 391 }
392 data = fPosition;
393 fPosition += fCount;
394 fCount = 0;
395 return kTRUE;
396}
397
398Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
399{
400// reads the next block of data at the current position
401// returns kFALSE if the data could not be read
402
b4857df7 403 fErrorCode = 0;
04fa961a 404 if (fPosition + size > fEnd) {
405 Error("ReadNext", "could not read data!");
b4857df7 406 fErrorCode = kErrOutOfBounds;
04fa961a 407 return kFALSE;
408 }
409 memcpy(data, fPosition, size);
410 fPosition += size;
411 fCount -= size;
412 return kTRUE;
413}
414
415
416Bool_t AliRawReaderRoot::Reset()
417{
418// reset the current position to the beginning of the event
419
420 fSubEventIndex = 0;
421 fSubEvent = NULL;
94d918a7 422 fEquipmentIndex = 0;
423 fEquipment = NULL;
04fa961a 424 fRawData = NULL;
39f9963f 425 fHeader = NULL;
04fa961a 426
427 fCount = 0;
428 fPosition = fEnd = NULL;
429 return kTRUE;
430}
431
b4857df7 432
dd9a70fe 433Bool_t AliRawReaderRoot::NextEvent()
434{
435// go to the next event in the root file
436
437 if (!fFile) return kFALSE;
438
439 do {
440 if (fBranch->GetEntry(fEventIndex+1) <= 0) {
441 delete fEvent;
442 fEvent = new AliRawEvent;
443 fBranch->SetAddress(&fEvent);
444 return kFALSE;
445 }
446 fEventIndex++;
447 } while (!IsEventSelected());
448 return Reset();
449}
450
451Bool_t AliRawReaderRoot::RewindEvents()
452{
453// go back to the beginning of the root file
454
455 if (!fFile) return kFALSE;
456
457 fEventIndex = -1;
458 delete fEvent;
459 fEvent = new AliRawEvent;
460 fBranch->SetAddress(&fEvent);
461 return Reset();
462}
463
464
b4857df7 465Int_t AliRawReaderRoot::CheckData() const
466{
467// check the consistency of the data
468
469 if (!fEvent) return 0;
470
471 AliRawEvent* subEvent = NULL;
472 Int_t subEventIndex = 0;
94d918a7 473 AliRawEquipment* equipment = NULL;
474 Int_t equipmentIndex = 0;
b4857df7 475 UChar_t* position = 0;
476 UChar_t* end = 0;
be50fca2 477 Int_t result = 0;
b4857df7 478
479 while (kTRUE) {
94d918a7 480 // get the first or the next sub event if at the end of an equipment
481 if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
b4857df7 482
483 // check for end of event data
be50fca2 484 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
b4857df7 485 subEvent = fEvent->GetSubEvent(subEventIndex++);
486
487 // check the magic word of the sub event
be50fca2 488 if (!fSubEvent->GetHeader()->IsValid()) {
489 result |= kErrMagic;
490 return result;
491 }
b4857df7 492
94d918a7 493 equipmentIndex = 0;
b4857df7 494 }
495
94d918a7 496 // get the next equipment and raw data
497 equipment = subEvent->GetEquipment(equipmentIndex++);
498 if (!equipment) continue;
499 AliRawData* rawData = equipment->GetRawData();
500 if (!rawData) continue;
501 position = (UChar_t*) rawData->GetBuffer();
502 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
503
b4857df7 504 // continue with the next sub event if no data left in the payload
505 if (position >= end) continue;
506
39f9963f 507 // check that there are enough bytes left for the data header
508 if (position + sizeof(AliRawDataHeader) > end) {
509 result |= kErrNoDataHeader;
be50fca2 510 continue;
511 }
b4857df7 512
94d918a7 513 // check consistency of data size in the header and in the equipment
39f9963f 514 AliRawDataHeader* header = (AliRawDataHeader*) position;
94d918a7 515 if (header->fSize != 0xFFFFFFFF) {
39f9963f 516 if (position + header->fSize > end) {
517 result |= kErrSize;
39f9963f 518 }
be50fca2 519 }
b4857df7 520 };
fd0de2e2 521
522 return result;
b4857df7 523}