]> git.uio.no Git - u/mrichter/AliRoot.git/blame - RAW/AliRawReaderRoot.cxx
- Dipole rotated wr to ALICE coordinate system
[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"
f2dc6b20 35#include "AliRawEventHeaderBase.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
1fb5b4a8 189const AliRawEventHeaderBase* AliRawReaderRoot::GetEventHeader() const
190{
191 // Get the even header
192 // Return NULL in case of failure
193 if (!fEvent) return NULL;
194 return fEvent->GetHeader();
195}
04fa961a 196
42d20574 197UInt_t AliRawReaderRoot::GetType() const
04fa961a 198{
199// get the type from the event header
200
201 if (!fEvent) return 0;
f2dc6b20 202 return fEvent->GetHeader()->Get("Type");
04fa961a 203}
204
42d20574 205UInt_t AliRawReaderRoot::GetRunNumber() const
04fa961a 206{
207// get the run number from the event header
208
209 if (!fEvent) return 0;
f2dc6b20 210 return fEvent->GetHeader()->Get("RunNb");
04fa961a 211}
212
42d20574 213const UInt_t* AliRawReaderRoot::GetEventId() const
04fa961a 214{
215// get the event id from the event header
216
217 if (!fEvent) return NULL;
f2dc6b20 218 return fEvent->GetHeader()->GetP("Id");
04fa961a 219}
220
42d20574 221const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
04fa961a 222{
223// get the trigger pattern from the event header
224
225 if (!fEvent) return NULL;
f2dc6b20 226 return fEvent->GetHeader()->GetP("TriggerPattern");
04fa961a 227}
228
42d20574 229const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
04fa961a 230{
231// get the detector pattern from the event header
232
233 if (!fEvent) return NULL;
f2dc6b20 234 return fEvent->GetHeader()->GetP("DetectorPattern");
04fa961a 235}
236
42d20574 237const UInt_t* AliRawReaderRoot::GetAttributes() const
04fa961a 238{
239// get the type attributes from the event header
240
241 if (!fEvent) return NULL;
f2dc6b20 242 return fEvent->GetHeader()->GetP("TypeAttribute");
04fa961a 243}
244
e94ad92c 245const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
246{
247// get the type attributes from the sub event header
248
249 if (!fSubEvent) return NULL;
f2dc6b20 250 return fSubEvent->GetHeader()->GetP("TypeAttribute");
e94ad92c 251}
252
c946ab02 253UInt_t AliRawReaderRoot::GetLDCId() const
254{
255// get the LDC Id from the event header
256
94d918a7 257 if (!fEvent || !fSubEvent) return 0;
f2dc6b20 258 return fSubEvent->GetHeader()->Get("LdcId");
c946ab02 259}
260
42d20574 261UInt_t AliRawReaderRoot::GetGDCId() const
04fa961a 262{
263// get the GDC Id from the event header
264
265 if (!fEvent) return 0;
f2dc6b20 266 return fEvent->GetHeader()->Get("GdcId");
04fa961a 267}
268
269
c946ab02 270Int_t AliRawReaderRoot::GetEquipmentSize() const
271{
272// get the size of the equipment
273
94d918a7 274 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
275 return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
c946ab02 276}
277
278Int_t AliRawReaderRoot::GetEquipmentType() const
279{
280// get the type from the equipment header
281
94d918a7 282 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
283 return fEquipment->GetEquipmentHeader()->GetEquipmentType();
c946ab02 284}
285
286Int_t AliRawReaderRoot::GetEquipmentId() const
287{
288// get the ID from the equipment header
289
94d918a7 290 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
291 return fEquipment->GetEquipmentHeader()->GetId();
c946ab02 292}
293
294const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
295{
296// get the attributes from the equipment header
297
94d918a7 298 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
299 return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
c946ab02 300}
301
302Int_t AliRawReaderRoot::GetEquipmentElementSize() const
303{
304// get the basic element size from the equipment header
305
94d918a7 306 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
307 return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
c946ab02 308}
309
299738b9 310Int_t AliRawReaderRoot::GetEquipmentHeaderSize() const
311{
312// get the size of the equipment header (28 bytes by default)
313
314 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
315 return fEquipment->GetEquipmentHeader()->HeaderSize();
316}
317
c946ab02 318
319Bool_t AliRawReaderRoot::ReadHeader()
04fa961a 320{
39f9963f 321// read a data header at the current position
322// returns kFALSE if the data header could not be read
04fa961a 323
b4857df7 324 fErrorCode = 0;
04fa961a 325 if (!fEvent) return kFALSE;
b4857df7 326
04fa961a 327 do {
b4857df7 328 // skip payload (if event was not selected)
329 if (fCount > 0) fPosition += fCount;
330
94d918a7 331 // get the first or the next equipment if at the end of an equipment
332 if (!fEquipment || (fPosition >= fEnd)) {
b4857df7 333
94d918a7 334 // get the first or the next sub event if at the end of a sub event
335 if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
b4857df7 336
94d918a7 337 // check for end of event data
338 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
339 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
340
341 // check the magic word of the sub event
342 if (!fSubEvent->GetHeader()->IsValid()) {
343 Error("ReadHeader", "wrong magic number in sub event!");
344 fSubEvent->GetHeader()->Dump();
345 fErrorCode = kErrMagic;
346 return kFALSE;
347 }
348
349 fEquipmentIndex = 0;
350 fEquipment = NULL;
351 fRawData = NULL;
b4857df7 352 }
353
94d918a7 354 // get the next equipment and raw data
04fa961a 355 fCount = 0;
94d918a7 356 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
357 if (!fEquipment) continue;
358 fRawData = fEquipment->GetRawData();
359 if (!fRawData) {
360 fPosition = fEnd;
361 continue;
362 }
04fa961a 363 fPosition = (UChar_t*) fRawData->GetBuffer();
364 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
365 }
b4857df7 366
94d918a7 367 // continue with the next equipment if no data left in the payload
b4857df7 368 if (fPosition >= fEnd) continue;
369
18882b3b 370 if (fRequireHeader) {
371 // check that there are enough bytes left for the data header
372 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
373 Error("ReadHeader", "could not read data header!");
374 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
375 fEquipment->GetEquipmentHeader()->Dump();
376 fCount = 0;
377 fPosition = fEnd;
378 fErrorCode = kErrNoDataHeader;
379 continue;
380 }
381
382 // "read" the data header
383 fHeader = (AliRawDataHeader*) fPosition;
299738b9 384 if ((fPosition + fHeader->fSize) != fEnd) {
385 Warning("ReadHeader",
386 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
387 fHeader->fSize, fEnd - fPosition);
388 fHeader->fSize = fEnd - fPosition;
389 }
18882b3b 390 fPosition += sizeof(AliRawDataHeader);
04fa961a 391 }
b4857df7 392
7c726b91 393 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
39f9963f 394 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
7c726b91 395
396 // check consistency of data size in the header and in the sub event
397 if (fPosition + fCount > fEnd) {
398 Error("ReadHeader", "size in data header exceeds event size!");
399 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
400 fEquipment->GetEquipmentHeader()->Dump();
401 fCount = 0;
402 fPosition = fEnd;
403 fErrorCode = kErrSize;
404 continue;
405 }
406
39f9963f 407 } else {
408 fCount = fEnd - fPosition;
72dd1d4f 409 }
b4857df7 410
04fa961a 411 } while (!IsSelected());
b4857df7 412
04fa961a 413 return kTRUE;
414}
415
416Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
417{
418// reads the next payload at the current position
419// returns kFALSE if the data could not be read
420
b4857df7 421 fErrorCode = 0;
04fa961a 422 while (fCount == 0) {
c946ab02 423 if (!ReadHeader()) return kFALSE;
04fa961a 424 }
425 data = fPosition;
426 fPosition += fCount;
427 fCount = 0;
428 return kTRUE;
429}
430
431Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
432{
433// reads the next block of data at the current position
434// returns kFALSE if the data could not be read
435
b4857df7 436 fErrorCode = 0;
04fa961a 437 if (fPosition + size > fEnd) {
438 Error("ReadNext", "could not read data!");
b4857df7 439 fErrorCode = kErrOutOfBounds;
04fa961a 440 return kFALSE;
441 }
442 memcpy(data, fPosition, size);
443 fPosition += size;
444 fCount -= size;
445 return kTRUE;
446}
447
448
449Bool_t AliRawReaderRoot::Reset()
450{
451// reset the current position to the beginning of the event
452
453 fSubEventIndex = 0;
454 fSubEvent = NULL;
94d918a7 455 fEquipmentIndex = 0;
456 fEquipment = NULL;
04fa961a 457 fRawData = NULL;
39f9963f 458 fHeader = NULL;
04fa961a 459
460 fCount = 0;
461 fPosition = fEnd = NULL;
462 return kTRUE;
463}
464
b4857df7 465
dd9a70fe 466Bool_t AliRawReaderRoot::NextEvent()
467{
468// go to the next event in the root file
469
470 if (!fFile) return kFALSE;
471
472 do {
d41ecdd0 473 delete fEvent;
474 fEvent = new AliRawEvent;
475 fBranch->SetAddress(&fEvent);
476 if (fBranch->GetEntry(fEventIndex+1) <= 0)
dd9a70fe 477 return kFALSE;
dd9a70fe 478 fEventIndex++;
479 } while (!IsEventSelected());
38cf12f3 480 fEventNumber++;
dd9a70fe 481 return Reset();
482}
483
484Bool_t AliRawReaderRoot::RewindEvents()
485{
486// go back to the beginning of the root file
487
488 if (!fFile) return kFALSE;
489
490 fEventIndex = -1;
491 delete fEvent;
492 fEvent = new AliRawEvent;
493 fBranch->SetAddress(&fEvent);
38cf12f3 494 fEventNumber = -1;
dd9a70fe 495 return Reset();
496}
497
498
b4857df7 499Int_t AliRawReaderRoot::CheckData() const
500{
501// check the consistency of the data
502
503 if (!fEvent) return 0;
504
505 AliRawEvent* subEvent = NULL;
506 Int_t subEventIndex = 0;
94d918a7 507 AliRawEquipment* equipment = NULL;
508 Int_t equipmentIndex = 0;
b4857df7 509 UChar_t* position = 0;
510 UChar_t* end = 0;
be50fca2 511 Int_t result = 0;
b4857df7 512
513 while (kTRUE) {
94d918a7 514 // get the first or the next sub event if at the end of an equipment
515 if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
b4857df7 516
517 // check for end of event data
be50fca2 518 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
b4857df7 519 subEvent = fEvent->GetSubEvent(subEventIndex++);
520
521 // check the magic word of the sub event
be50fca2 522 if (!fSubEvent->GetHeader()->IsValid()) {
523 result |= kErrMagic;
524 return result;
525 }
b4857df7 526
94d918a7 527 equipmentIndex = 0;
b4857df7 528 }
529
94d918a7 530 // get the next equipment and raw data
531 equipment = subEvent->GetEquipment(equipmentIndex++);
532 if (!equipment) continue;
533 AliRawData* rawData = equipment->GetRawData();
534 if (!rawData) continue;
535 position = (UChar_t*) rawData->GetBuffer();
536 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
537
b4857df7 538 // continue with the next sub event if no data left in the payload
539 if (position >= end) continue;
540
18882b3b 541 if (fRequireHeader) {
39f9963f 542 // check that there are enough bytes left for the data header
18882b3b 543 if (position + sizeof(AliRawDataHeader) > end) {
544 result |= kErrNoDataHeader;
545 continue;
546 }
b4857df7 547
18882b3b 548 // check consistency of data size in the header and in the equipment
549 AliRawDataHeader* header = (AliRawDataHeader*) position;
299738b9 550 if ((position + header->fSize) != end) {
551 Warning("ReadHeader",
552 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
553 header->fSize, end - position);
554 header->fSize = end - position;
555 result |= kErrSize;
39f9963f 556 }
be50fca2 557 }
299738b9 558 position = end;
b4857df7 559 };
fd0de2e2 560
561 return result;
b4857df7 562}