New IsRawReaderValid() method which returns false in case the raw-data file, chain...
[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
6923e953 43AliRawReaderRoot::AliRawReaderRoot() :
44 fFile(NULL),
45 fBranch(NULL),
46 fEventIndex(-1),
47 fEvent(NULL),
48 fSubEventIndex(0),
49 fSubEvent(NULL),
50 fEquipmentIndex(0),
51 fEquipment(NULL),
52 fRawData(NULL),
53 fPosition(NULL),
54 fEnd(NULL)
55{
56// default constructor
57
58}
04fa961a 59
dd9a70fe 60AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
61 fFile(NULL),
62 fBranch(NULL),
63 fEventIndex(eventNumber),
64 fEvent(NULL),
65 fSubEventIndex(0),
66 fSubEvent(NULL),
94d918a7 67 fEquipmentIndex(0),
68 fEquipment(NULL),
dd9a70fe 69 fRawData(NULL),
70 fPosition(NULL),
71 fEnd(NULL)
04fa961a 72{
73// create an object to read digits from the given input file for the
74// event with the given number
75
03c6d9a3 76 TDirectory* dir = gDirectory;
04fa961a 77 fFile = TFile::Open(fileName);
03c6d9a3 78 dir->cd();
04fa961a 79 if (!fFile || !fFile->IsOpen()) {
80 Error("AliRawReaderRoot", "could not open file %s", fileName);
a97af23d 81 fIsValid = kFALSE;
04fa961a 82 return;
83 }
84 TTree* tree = (TTree*) fFile->Get("RAW");
85 if (!tree) {
86 Error("AliRawReaderRoot", "no raw data tree found");
a97af23d 87 fIsValid = kFALSE;
04fa961a 88 return;
89 }
dd9a70fe 90 fBranch = tree->GetBranch("rawevent");
91 if (!fBranch) {
04fa961a 92 Error("AliRawReaderRoot", "no raw data branch found");
a97af23d 93 fIsValid = kFALSE;
04fa961a 94 return;
95 }
96
97 fEvent = new AliRawEvent;
dd9a70fe 98 fBranch->SetAddress(&fEvent);
99 if (fEventIndex >= 0) {
100 if (fBranch->GetEntry(fEventIndex) <= 0) {
101 Error("AliRawReaderRoot", "no event with number %d found", fEventIndex);
a97af23d 102 fIsValid = kFALSE;
dd9a70fe 103 return;
104 }
04fa961a 105 }
04fa961a 106}
107
dd9a70fe 108AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event) :
109 fFile(NULL),
110 fBranch(NULL),
111 fEventIndex(-1),
112 fEvent(event),
113 fSubEventIndex(0),
114 fSubEvent(NULL),
94d918a7 115 fEquipmentIndex(0),
116 fEquipment(NULL),
dd9a70fe 117 fRawData(NULL),
118 fPosition(NULL),
119 fEnd(NULL)
04fa961a 120{
121// create an object to read digits from the given raw event
a97af23d 122 if (!fEvent) fIsValid = kFALSE;
04fa961a 123}
124
42d20574 125AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
dd9a70fe 126 AliRawReader(rawReader),
127 fFile(NULL),
128 fBranch(NULL),
129 fEventIndex(rawReader.fEventIndex),
130 fEvent(NULL),
131 fSubEventIndex(rawReader.fSubEventIndex),
132 fSubEvent(NULL),
94d918a7 133 fEquipmentIndex(rawReader.fEquipmentIndex),
134 fEquipment(NULL),
dd9a70fe 135 fRawData(NULL),
136 fPosition(NULL),
137 fEnd(NULL)
42d20574 138{
139// copy constructor
140
dd9a70fe 141 if (rawReader.fFile) {
142 TDirectory* dir = gDirectory;
143 fFile = TFile::Open(rawReader.fFile->GetName());
144 dir->cd();
145 if (!fFile || !fFile->IsOpen()) {
146 Error("AliRawReaderRoot", "could not open file %s",
147 rawReader.fFile->GetName());
a97af23d 148 fIsValid = kFALSE;
dd9a70fe 149 return;
150 }
151 TTree* tree = (TTree*) fFile->Get("RAW");
152 if (!tree) {
153 Error("AliRawReaderRoot", "no raw data tree found");
a97af23d 154 fIsValid = kFALSE;
dd9a70fe 155 return;
156 }
157 fBranch = tree->GetBranch("rawevent");
158 if (!fBranch) {
159 Error("AliRawReaderRoot", "no raw data branch found");
a97af23d 160 fIsValid = kFALSE;
dd9a70fe 161 return;
162 }
163
164 fEvent = new AliRawEvent;
165 fBranch->SetAddress(&fEvent);
166 if (fEventIndex >= 0) {
167 if (fBranch->GetEntry(fEventIndex) <= 0) {
168 Error("AliRawReaderRoot", "no event with number %d found",
169 fEventIndex);
a97af23d 170 fIsValid = kFALSE;
dd9a70fe 171 return;
172 }
173 }
174 } else {
175 fEvent = rawReader.fEvent;
176 }
177
178 if (fSubEventIndex > 0) {
179 fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
94d918a7 180 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex);
181 fRawData = fEquipment->GetRawData();
dd9a70fe 182 fCount = 0;
183 fHeader = (AliRawDataHeader*) ((UChar_t*) fRawData->GetBuffer() +
184 ((UChar_t*) rawReader.fHeader -
185 (UChar_t*) rawReader.fRawData->GetBuffer()));
186 fPosition = (UChar_t*) fRawData->GetBuffer() +
187 (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
188 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
189 }
42d20574 190}
191
192AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
193 rawReader)
194{
195// assignment operator
196
197 this->~AliRawReaderRoot();
198 new(this) AliRawReaderRoot(rawReader);
199 return *this;
200}
201
04fa961a 202AliRawReaderRoot::~AliRawReaderRoot()
203{
204// delete objects and close root file
205
206 if (fFile) {
207 if (fEvent) delete fEvent;
208 fFile->Close();
209 delete fFile;
210 }
211}
212
1fb5b4a8 213const AliRawEventHeaderBase* AliRawReaderRoot::GetEventHeader() const
214{
215 // Get the even header
216 // Return NULL in case of failure
217 if (!fEvent) return NULL;
218 return fEvent->GetHeader();
219}
04fa961a 220
42d20574 221UInt_t AliRawReaderRoot::GetType() const
04fa961a 222{
223// get the type from the event header
224
225 if (!fEvent) return 0;
f2dc6b20 226 return fEvent->GetHeader()->Get("Type");
04fa961a 227}
228
42d20574 229UInt_t AliRawReaderRoot::GetRunNumber() const
04fa961a 230{
231// get the run number from the event header
232
233 if (!fEvent) return 0;
f2dc6b20 234 return fEvent->GetHeader()->Get("RunNb");
04fa961a 235}
236
42d20574 237const UInt_t* AliRawReaderRoot::GetEventId() const
04fa961a 238{
239// get the event id from the event header
240
241 if (!fEvent) return NULL;
f2dc6b20 242 return fEvent->GetHeader()->GetP("Id");
04fa961a 243}
244
42d20574 245const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
04fa961a 246{
247// get the trigger pattern from the event header
248
249 if (!fEvent) return NULL;
f2dc6b20 250 return fEvent->GetHeader()->GetP("TriggerPattern");
04fa961a 251}
252
42d20574 253const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
04fa961a 254{
255// get the detector pattern from the event header
256
257 if (!fEvent) return NULL;
f2dc6b20 258 return fEvent->GetHeader()->GetP("DetectorPattern");
04fa961a 259}
260
42d20574 261const UInt_t* AliRawReaderRoot::GetAttributes() const
04fa961a 262{
263// get the type attributes from the event header
264
265 if (!fEvent) return NULL;
f2dc6b20 266 return fEvent->GetHeader()->GetP("TypeAttribute");
04fa961a 267}
268
e94ad92c 269const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
270{
271// get the type attributes from the sub event header
272
273 if (!fSubEvent) return NULL;
f2dc6b20 274 return fSubEvent->GetHeader()->GetP("TypeAttribute");
e94ad92c 275}
276
c946ab02 277UInt_t AliRawReaderRoot::GetLDCId() const
278{
279// get the LDC Id from the event header
280
94d918a7 281 if (!fEvent || !fSubEvent) return 0;
f2dc6b20 282 return fSubEvent->GetHeader()->Get("LdcId");
c946ab02 283}
284
42d20574 285UInt_t AliRawReaderRoot::GetGDCId() const
04fa961a 286{
287// get the GDC Id from the event header
288
289 if (!fEvent) return 0;
f2dc6b20 290 return fEvent->GetHeader()->Get("GdcId");
04fa961a 291}
292
741c154c 293UInt_t AliRawReaderRoot::GetTimestamp() const
294{
295 if (!fEvent) return 0;
296 return fEvent->GetHeader()->Get("Timestamp");
297}
04fa961a 298
c946ab02 299Int_t AliRawReaderRoot::GetEquipmentSize() const
300{
301// get the size of the equipment
302
94d918a7 303 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
304 return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
c946ab02 305}
306
307Int_t AliRawReaderRoot::GetEquipmentType() const
308{
309// get the type from the equipment header
310
94d918a7 311 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
312 return fEquipment->GetEquipmentHeader()->GetEquipmentType();
c946ab02 313}
314
315Int_t AliRawReaderRoot::GetEquipmentId() const
316{
317// get the ID from the equipment header
318
94d918a7 319 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
320 return fEquipment->GetEquipmentHeader()->GetId();
c946ab02 321}
322
323const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
324{
325// get the attributes from the equipment header
326
94d918a7 327 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
328 return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
c946ab02 329}
330
331Int_t AliRawReaderRoot::GetEquipmentElementSize() const
332{
333// get the basic element size from the equipment header
334
94d918a7 335 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
336 return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
c946ab02 337}
338
299738b9 339Int_t AliRawReaderRoot::GetEquipmentHeaderSize() const
340{
341// get the size of the equipment header (28 bytes by default)
342
343 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
344 return fEquipment->GetEquipmentHeader()->HeaderSize();
345}
346
edd06192 347// _________________________________________________________________________
edd06192 348void AliRawReaderRoot::SwapData(const void* inbuf, const void* outbuf, UInt_t size) {
349 // The method swaps the contents of the
350 // raw-data event header
91f76e9b 351 UInt_t intCount = (size+3)/sizeof(UInt_t);
edd06192 352
353 UInt_t* buf = (UInt_t*) inbuf; // temporary integers buffer
354 for (UInt_t i=0; i<intCount; i++, buf++) {
355 UInt_t value = SwapWord(*buf);
dae3f0d9 356 if (i==(intCount-1))
357 memcpy((UInt_t*)outbuf+i, &value, size%sizeof(UInt_t));
358 else
359 memcpy((UInt_t*)outbuf+i, &value, sizeof(UInt_t));
edd06192 360 }
361}
362// _________________________________________________________________________
c946ab02 363
364Bool_t AliRawReaderRoot::ReadHeader()
04fa961a 365{
39f9963f 366// read a data header at the current position
367// returns kFALSE if the data header could not be read
04fa961a 368
b4857df7 369 fErrorCode = 0;
04fa961a 370 if (!fEvent) return kFALSE;
b4857df7 371
04fa961a 372 do {
b4857df7 373 // skip payload (if event was not selected)
374 if (fCount > 0) fPosition += fCount;
375
94d918a7 376 // get the first or the next equipment if at the end of an equipment
377 if (!fEquipment || (fPosition >= fEnd)) {
b4857df7 378
94d918a7 379 // get the first or the next sub event if at the end of a sub event
380 if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
b4857df7 381
94d918a7 382 // check for end of event data
383 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
384 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
385
386 // check the magic word of the sub event
387 if (!fSubEvent->GetHeader()->IsValid()) {
388 Error("ReadHeader", "wrong magic number in sub event!");
389 fSubEvent->GetHeader()->Dump();
390 fErrorCode = kErrMagic;
391 return kFALSE;
392 }
393
394 fEquipmentIndex = 0;
395 fEquipment = NULL;
396 fRawData = NULL;
b4857df7 397 }
398
94d918a7 399 // get the next equipment and raw data
04fa961a 400 fCount = 0;
94d918a7 401 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
402 if (!fEquipment) continue;
3d65cc80 403 if (!IsSelected()) {
404 fPosition = fEnd;
405 continue;
406 }
94d918a7 407 fRawData = fEquipment->GetRawData();
408 if (!fRawData) {
409 fPosition = fEnd;
410 continue;
411 }
04fa961a 412 fPosition = (UChar_t*) fRawData->GetBuffer();
413 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
414 }
b4857df7 415
94d918a7 416 // continue with the next equipment if no data left in the payload
b4857df7 417 if (fPosition >= fEnd) continue;
418
18882b3b 419 if (fRequireHeader) {
420 // check that there are enough bytes left for the data header
421 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
422 Error("ReadHeader", "could not read data header!");
423 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
424 fEquipment->GetEquipmentHeader()->Dump();
425 fCount = 0;
426 fPosition = fEnd;
427 fErrorCode = kErrNoDataHeader;
428 continue;
429 }
430
431 // "read" the data header
432 fHeader = (AliRawDataHeader*) fPosition;
edd06192 433#ifndef R__BYTESWAP
434 SwapData((void*) fHeader, (void*) fHeaderSwapped, sizeof(AliRawDataHeader));
435 fHeader=fHeaderSwapped;
436#endif
299738b9 437 if ((fPosition + fHeader->fSize) != fEnd) {
806e573c 438 if (fHeader->fSize != 0xFFFFFFFF)
439 Warning("ReadHeader",
440 "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
441 fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
299738b9 442 fHeader->fSize = fEnd - fPosition;
443 }
18882b3b 444 fPosition += sizeof(AliRawDataHeader);
04fa961a 445 }
b4857df7 446
7c726b91 447 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
39f9963f 448 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
7c726b91 449
450 // check consistency of data size in the header and in the sub event
451 if (fPosition + fCount > fEnd) {
452 Error("ReadHeader", "size in data header exceeds event size!");
453 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
454 fEquipment->GetEquipmentHeader()->Dump();
455 fCount = 0;
456 fPosition = fEnd;
457 fErrorCode = kErrSize;
458 continue;
459 }
460
39f9963f 461 } else {
462 fCount = fEnd - fPosition;
72dd1d4f 463 }
b4857df7 464
04fa961a 465 } while (!IsSelected());
b4857df7 466
04fa961a 467 return kTRUE;
468}
469
470Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
471{
472// reads the next payload at the current position
473// returns kFALSE if the data could not be read
474
b4857df7 475 fErrorCode = 0;
04fa961a 476 while (fCount == 0) {
c946ab02 477 if (!ReadHeader()) return kFALSE;
04fa961a 478 }
479 data = fPosition;
480 fPosition += fCount;
481 fCount = 0;
482 return kTRUE;
483}
484
485Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
486{
487// reads the next block of data at the current position
488// returns kFALSE if the data could not be read
489
b4857df7 490 fErrorCode = 0;
04fa961a 491 if (fPosition + size > fEnd) {
492 Error("ReadNext", "could not read data!");
b4857df7 493 fErrorCode = kErrOutOfBounds;
04fa961a 494 return kFALSE;
495 }
496 memcpy(data, fPosition, size);
dae3f0d9 497
04fa961a 498 fPosition += size;
499 fCount -= size;
500 return kTRUE;
501}
502
503
504Bool_t AliRawReaderRoot::Reset()
505{
506// reset the current position to the beginning of the event
507
508 fSubEventIndex = 0;
509 fSubEvent = NULL;
94d918a7 510 fEquipmentIndex = 0;
511 fEquipment = NULL;
04fa961a 512 fRawData = NULL;
39f9963f 513 fHeader = NULL;
04fa961a 514
515 fCount = 0;
516 fPosition = fEnd = NULL;
517 return kTRUE;
518}
519
b4857df7 520
dd9a70fe 521Bool_t AliRawReaderRoot::NextEvent()
522{
523// go to the next event in the root file
524
2da4ef20 525 if (!fBranch) return kFALSE;
dd9a70fe 526
527 do {
d41ecdd0 528 delete fEvent;
529 fEvent = new AliRawEvent;
530 fBranch->SetAddress(&fEvent);
531 if (fBranch->GetEntry(fEventIndex+1) <= 0)
dd9a70fe 532 return kFALSE;
dd9a70fe 533 fEventIndex++;
534 } while (!IsEventSelected());
38cf12f3 535 fEventNumber++;
dd9a70fe 536 return Reset();
537}
538
539Bool_t AliRawReaderRoot::RewindEvents()
540{
541// go back to the beginning of the root file
542
2da4ef20 543 if (!fBranch) return kFALSE;
dd9a70fe 544
545 fEventIndex = -1;
546 delete fEvent;
547 fEvent = new AliRawEvent;
548 fBranch->SetAddress(&fEvent);
38cf12f3 549 fEventNumber = -1;
dd9a70fe 550 return Reset();
551}
552
636c1780 553Bool_t AliRawReaderRoot::GotoEvent(Int_t event)
554{
555 // go to a particular event
556 // Uses the absolute event index inside the
557 // raw-data file
558
559 if (!fBranch) return kFALSE;
560
561 delete fEvent;
562 fEvent = new AliRawEvent;
563 fBranch->SetAddress(&fEvent);
564 if (fBranch->GetEntry(event) <= 0)
565 return kFALSE;
566 fEventIndex = event;
567 fEventNumber++;
568 return Reset();
569}
dd9a70fe 570
25e82ff5 571Int_t AliRawReaderRoot::GetNumberOfEvents() const
572{
573 // Get the total number of events in
574 // the raw-data tree
575
576 if (!fBranch) return -1;
577
578 return fBranch->GetEntries();
579}
580
b4857df7 581Int_t AliRawReaderRoot::CheckData() const
582{
583// check the consistency of the data
584
585 if (!fEvent) return 0;
586
587 AliRawEvent* subEvent = NULL;
588 Int_t subEventIndex = 0;
94d918a7 589 AliRawEquipment* equipment = NULL;
590 Int_t equipmentIndex = 0;
b4857df7 591 UChar_t* position = 0;
592 UChar_t* end = 0;
be50fca2 593 Int_t result = 0;
b4857df7 594
595 while (kTRUE) {
94d918a7 596 // get the first or the next sub event if at the end of an equipment
597 if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
b4857df7 598
599 // check for end of event data
be50fca2 600 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
b4857df7 601 subEvent = fEvent->GetSubEvent(subEventIndex++);
602
603 // check the magic word of the sub event
be50fca2 604 if (!fSubEvent->GetHeader()->IsValid()) {
605 result |= kErrMagic;
606 return result;
607 }
b4857df7 608
94d918a7 609 equipmentIndex = 0;
b4857df7 610 }
611
94d918a7 612 // get the next equipment and raw data
613 equipment = subEvent->GetEquipment(equipmentIndex++);
614 if (!equipment) continue;
615 AliRawData* rawData = equipment->GetRawData();
616 if (!rawData) continue;
617 position = (UChar_t*) rawData->GetBuffer();
618 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
619
b4857df7 620 // continue with the next sub event if no data left in the payload
621 if (position >= end) continue;
622
18882b3b 623 if (fRequireHeader) {
39f9963f 624 // check that there are enough bytes left for the data header
18882b3b 625 if (position + sizeof(AliRawDataHeader) > end) {
626 result |= kErrNoDataHeader;
627 continue;
628 }
b4857df7 629
18882b3b 630 // check consistency of data size in the header and in the equipment
631 AliRawDataHeader* header = (AliRawDataHeader*) position;
299738b9 632 if ((position + header->fSize) != end) {
806e573c 633 if (header->fSize != 0xFFFFFFFF)
634 Warning("ReadHeader",
635 "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
636 equipment->GetEquipmentHeader()->GetId(),header->fSize, end - position);
299738b9 637 header->fSize = end - position;
638 result |= kErrSize;
39f9963f 639 }
be50fca2 640 }
299738b9 641 position = end;
b4857df7 642 };
fd0de2e2 643
644 return result;
b4857df7 645}