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