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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 /// This is a class for reading raw data from a root file.
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
26 /// The file name and the event number are arguments of the constructor
27 /// of AliRawReaderRoot.
29 ///////////////////////////////////////////////////////////////////////////////
33 #include <TTreeIndex.h>
34 #include "AliRawReaderRoot.h"
35 #include "AliRawVEvent.h"
36 #include "AliRawEventHeaderBase.h"
37 #include "AliRawVEquipment.h"
38 #include "AliRawEquipmentHeader.h"
39 #include "AliRawData.h"
42 ClassImp(AliRawReaderRoot)
43 Bool_t AliRawReaderRoot::fgUseOrder = kFALSE;
46 AliRawReaderRoot::AliRawReaderRoot() :
61 // default constructor
65 AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
68 fEventIndex(eventNumber),
80 // create an object to read digits from the given input file for the
81 // event with the given number
83 TDirectory* dir = gDirectory;
84 fFile = TFile::Open(fileName);
86 if (!fFile || !fFile->IsOpen()) {
87 Error("AliRawReaderRoot", "could not open file %s", fileName);
91 TTree* tree = (TTree*) fFile->Get("RAW");
93 Error("AliRawReaderRoot", "no raw data tree found");
97 fBranch = tree->GetBranch("rawevent");
99 Error("AliRawReaderRoot", "no raw data branch found");
104 fBranch->SetAddress(&fEvent);
105 if (fEventIndex >= 0) {
106 if (fBranch->GetEntry(fEventIndex) <= 0) {
107 Error("AliRawReaderRoot", "no event with number %d found", fEventIndex);
111 fEventHeader = fEvent->GetHeader();
115 AliRawReaderRoot::AliRawReaderRoot(AliRawVEvent* event) :
120 fEventHeader(event->GetHeader()),
130 // create an object to read digits from the given raw event
131 if (!fEvent) fIsValid = kFALSE;
134 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
135 AliRawReader(rawReader),
138 fEventIndex(rawReader.fEventIndex),
141 fSubEventIndex(rawReader.fSubEventIndex),
143 fEquipmentIndex(rawReader.fEquipmentIndex),
152 if (rawReader.fFile) {
153 TDirectory* dir = gDirectory;
154 fFile = TFile::Open(rawReader.fFile->GetName());
156 if (!fFile || !fFile->IsOpen()) {
157 Error("AliRawReaderRoot", "could not open file %s",
158 rawReader.fFile->GetName());
162 TTree* tree = (TTree*) fFile->Get("RAW");
164 Error("AliRawReaderRoot", "no raw data tree found");
168 fBranch = tree->GetBranch("rawevent");
170 Error("AliRawReaderRoot", "no raw data branch found");
175 fBranch->SetAddress(&fEvent);
176 if (fEventIndex >= 0) {
177 if (fBranch->GetEntry(fEventIndex) <= 0) {
178 Error("AliRawReaderRoot", "no event with number %d found",
183 fEventHeader = fEvent->GetHeader();
186 fEvent = rawReader.fEvent;
187 fEventHeader = rawReader.fEventHeader;
190 if (fSubEventIndex > 0) {
191 fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
192 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex);
193 fRawData = fEquipment->GetRawData();
195 fHeader = (AliRawDataHeader*) ((UChar_t*) fRawData->GetBuffer() +
196 ((UChar_t*) rawReader.fHeader -
197 (UChar_t*) rawReader.fRawData->GetBuffer()));
198 fPosition = (UChar_t*) fRawData->GetBuffer() +
199 (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
200 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
204 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
207 // assignment operator
209 this->~AliRawReaderRoot();
210 new(this) AliRawReaderRoot(rawReader);
214 AliRawReaderRoot::~AliRawReaderRoot()
216 // delete objects and close root file
219 if (fEvent) delete fEvent;
225 const AliRawEventHeaderBase* AliRawReaderRoot::GetEventHeader() const
227 // Get the even header
228 // Return NULL in case of failure
232 UInt_t AliRawReaderRoot::GetType() const
234 // get the type from the event header
236 if (!fEventHeader) return 0;
237 return fEventHeader->Get("Type");
240 UInt_t AliRawReaderRoot::GetRunNumber() const
242 // get the run number from the event header
244 if (!fEventHeader) return 0;
245 return fEventHeader->Get("RunNb");
248 const UInt_t* AliRawReaderRoot::GetEventId() const
250 // get the event id from the event header
252 if (!fEventHeader) return NULL;
253 return fEventHeader->GetP("Id");
256 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
258 // get the trigger pattern from the event header
260 if (!fEventHeader) return NULL;
261 return fEventHeader->GetP("TriggerPattern");
264 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
266 // get the detector pattern from the event header
268 if (!fEventHeader) return NULL;
269 return fEventHeader->GetP("DetectorPattern");
272 const UInt_t* AliRawReaderRoot::GetAttributes() const
274 // get the type attributes from the event header
276 if (!fEventHeader) return NULL;
277 return fEventHeader->GetP("TypeAttribute");
280 const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
282 // get the type attributes from the sub event header
284 if (!fSubEvent) return NULL;
285 return fSubEvent->GetHeader()->GetP("TypeAttribute");
288 UInt_t AliRawReaderRoot::GetLDCId() const
290 // get the LDC Id from the event header
292 if (!fEvent || !fSubEvent) return 0;
293 return fSubEvent->GetHeader()->Get("LdcId");
296 UInt_t AliRawReaderRoot::GetGDCId() const
298 // get the GDC Id from the event header
300 if (!fEventHeader) return 0;
301 return fEventHeader->Get("GdcId");
304 UInt_t AliRawReaderRoot::GetTimestamp() const
306 if (!fEventHeader) return 0;
307 return fEventHeader->Get("Timestamp");
310 Int_t AliRawReaderRoot::GetEquipmentSize() const
312 // get the size of the equipment
314 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
315 return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
318 Int_t AliRawReaderRoot::GetEquipmentType() const
320 // get the type from the equipment header
322 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
323 return fEquipment->GetEquipmentHeader()->GetEquipmentType();
326 Int_t AliRawReaderRoot::GetEquipmentId() const
328 // get the ID from the equipment header
330 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
331 return fEquipment->GetEquipmentHeader()->GetId();
334 const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
336 // get the attributes from the equipment header
338 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
339 return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
342 Int_t AliRawReaderRoot::GetEquipmentElementSize() const
344 // get the basic element size from the equipment header
346 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
347 return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
350 Int_t AliRawReaderRoot::GetEquipmentHeaderSize() const
352 // get the size of the equipment header (28 bytes by default)
354 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
355 return fEquipment->GetEquipmentHeader()->HeaderSize();
358 // _________________________________________________________________________
359 void AliRawReaderRoot::SwapData(const void* inbuf, const void* outbuf, UInt_t size) {
360 // The method swaps the contents of the
361 // raw-data event header
362 UInt_t intCount = (size+3)/sizeof(UInt_t);
364 UInt_t* buf = (UInt_t*) inbuf; // temporary integers buffer
365 for (UInt_t i=0; i<intCount; i++, buf++) {
366 UInt_t value = SwapWord(*buf);
368 memcpy((UInt_t*)outbuf+i, &value, size%sizeof(UInt_t));
370 memcpy((UInt_t*)outbuf+i, &value, sizeof(UInt_t));
373 // _________________________________________________________________________
375 Bool_t AliRawReaderRoot::ReadHeader()
377 // read a data header at the current position
378 // returns kFALSE if the data header could not be read
381 if (!fEvent) return kFALSE;
384 // skip payload (if event was not selected)
385 if (fCount > 0) fPosition += fCount;
387 // get the first or the next equipment if at the end of an equipment
388 if (!fEquipment || (fPosition >= fEnd)) {
390 // get the first or the next sub event if at the end of a sub event
391 if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
393 // check for end of event data
394 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
395 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
397 // check the magic word of the sub event
398 if (!fSubEvent->GetHeader()->IsValid()) {
399 Error("ReadHeader", "wrong magic number in sub event!");
400 fSubEvent->GetHeader()->Dump();
401 fErrorCode = kErrMagic;
410 // get the next equipment and raw data
412 if (fEquipmentIndex >= fSubEvent->GetNEquipments()) {
416 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
417 if (!fEquipment) continue;
422 fRawData = fEquipment->GetRawData();
427 fPosition = (UChar_t*) fRawData->GetBuffer();
428 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
431 // continue with the next equipment if no data left in the payload
432 if (fPosition >= fEnd) continue;
434 if (fRequireHeader) {
435 // check that there are enough bytes left for the data header
436 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
437 Error("ReadHeader", "could not read data header!");
438 Warning("ReadHeader", "skipping %ld bytes", fEnd - fPosition);
439 fEquipment->GetEquipmentHeader()->Dump();
442 fErrorCode = kErrNoDataHeader;
446 // "read" the data header
447 fHeader = (AliRawDataHeader*) fPosition;
449 SwapData((void*) fHeader, (void*) fHeaderSwapped, sizeof(AliRawDataHeader));
450 fHeader=fHeaderSwapped;
452 if ((fPosition + fHeader->fSize) != fEnd) {
453 if (fHeader->fSize != 0xFFFFFFFF)
454 Warning("ReadHeader",
455 "Equipment %d : raw data size found in the header is wrong (%d != %ld)! Using the equipment size instead !",
456 fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
457 fHeader->fSize = fEnd - fPosition;
459 fPosition += sizeof(AliRawDataHeader);
462 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
463 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
465 // check consistency of data size in the header and in the sub event
466 if (fPosition + fCount > fEnd) {
467 Error("ReadHeader", "size in data header exceeds event size!");
468 Warning("ReadHeader", "skipping %ld bytes", fEnd - fPosition);
469 fEquipment->GetEquipmentHeader()->Dump();
472 fErrorCode = kErrSize;
477 fCount = fEnd - fPosition;
480 } while (!IsSelected());
485 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
487 // reads the next payload at the current position
488 // returns kFALSE if the data could not be read
491 while (fCount == 0) {
492 if (!ReadHeader()) return kFALSE;
500 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
502 // reads the next block of data at the current position
503 // returns kFALSE if the data could not be read
506 if (fPosition + size > fEnd) {
507 Error("ReadNext", "could not read data!");
508 fErrorCode = kErrOutOfBounds;
511 memcpy(data, fPosition, size);
519 Bool_t AliRawReaderRoot::Reset()
521 // reset the current position to the beginning of the event
531 fPosition = fEnd = NULL;
536 Bool_t AliRawReaderRoot::NextEvent()
538 // go to the next event in the root file
540 if (!fBranch) return kFALSE;
542 // check if it uses order or not
543 if (fgUseOrder && !fIndex) MakeIndex();
549 fBranch->SetAddress(&fEvent);
550 Int_t entryToGet = fEventIndex + 1;
553 && entryToGet<fBranch->GetEntries()
554 && entryToGet>-1 ) entryToGet = fIndex[entryToGet];
555 if (fBranch->GetEntry(entryToGet) <= 0)
557 fEventHeader = fEvent->GetHeader();
559 } while (!IsEventSelected());
564 Bool_t AliRawReaderRoot::RewindEvents()
566 // go back to the beginning of the root file
568 if (!fBranch) return kFALSE;
574 fBranch->SetAddress(&fEvent);
579 Bool_t AliRawReaderRoot::GotoEvent(Int_t event)
581 // go to a particular event
582 // Uses the absolute event index inside the
585 if (!fBranch) return kFALSE;
587 // check if it uses order or not
588 if (fgUseOrder && !fIndex) MakeIndex();
593 fBranch->SetAddress(&fEvent);
594 Int_t entryToGet = event;
597 && entryToGet<fBranch->GetEntries()
598 && entryToGet>-1 ) entryToGet = fIndex[entryToGet];
599 if (fBranch->GetEntry(entryToGet) <= 0)
601 fEventHeader = fEvent->GetHeader();
607 Int_t AliRawReaderRoot::GetNumberOfEvents() const
609 // Get the total number of events in
612 if (!fBranch) return -1;
614 return fBranch->GetEntries();
617 Int_t AliRawReaderRoot::CheckData() const
619 // check the consistency of the data
621 if (!fEvent) return 0;
623 AliRawVEvent* subEvent = NULL;
624 Int_t subEventIndex = 0;
625 AliRawVEquipment* equipment = NULL;
626 Int_t equipmentIndex = 0;
627 UChar_t* position = 0;
632 // get the first or the next sub event if at the end of an equipment
633 if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
635 // check for end of event data
636 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
637 subEvent = fEvent->GetSubEvent(subEventIndex++);
639 // check the magic word of the sub event
640 if (!fSubEvent->GetHeader()->IsValid()) {
648 // get the next equipment and raw data
649 if (equipmentIndex >= subEvent->GetNEquipments()) {
653 equipment = subEvent->GetEquipment(equipmentIndex++);
654 if (!equipment) continue;
655 AliRawData* rawData = equipment->GetRawData();
656 if (!rawData) continue;
657 position = (UChar_t*) rawData->GetBuffer();
658 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
660 // continue with the next sub event if no data left in the payload
661 if (position >= end) continue;
663 if (fRequireHeader) {
664 // check that there are enough bytes left for the data header
665 if (position + sizeof(AliRawDataHeader) > end) {
666 result |= kErrNoDataHeader;
670 // check consistency of data size in the header and in the equipment
671 AliRawDataHeader* header = (AliRawDataHeader*) position;
672 if ((position + header->fSize) != end) {
673 if (header->fSize != 0xFFFFFFFF)
674 Warning("ReadHeader",
675 "Equipment %d : raw data size found in the header is wrong (%d != %ld)! Using the equipment size instead !",
676 equipment->GetEquipmentHeader()->GetId(),header->fSize, end - position);
677 header->fSize = end - position;
687 AliRawReader* AliRawReaderRoot::CloneSingleEvent() const
689 // Clones the current event and
690 // creates raw-reader for the cloned event
691 // Can be used in order to make asynchronious
692 // access to the current raw data within
693 // several threads (online event display/reco)
696 // Root formatted raw data
697 AliRawVEvent *gdcRootEvent = (AliRawVEvent*)fEvent->Clone();
698 for (Int_t ldcCounter=0; ldcCounter < gdcRootEvent->GetNSubEvents(); ldcCounter++) {
699 AliRawVEvent *ldcRootEvent = gdcRootEvent->GetSubEvent(ldcCounter);
700 AliRawVEvent *subEvent = fEvent->GetSubEvent(ldcCounter);
701 for (Int_t eqCounter=0; eqCounter < ldcRootEvent->GetNEquipments(); eqCounter++) {
702 AliRawVEquipment *equipment=ldcRootEvent->GetEquipment(eqCounter);
703 AliRawVEquipment *eq = subEvent->GetEquipment(eqCounter);
704 equipment->CloneRawData(eq->GetRawData());
707 // Reset original event and newly
709 gdcRootEvent->GetSubEvent(-1);
710 fEvent->GetSubEvent(-1);
711 return new AliRawReaderRoot(gdcRootEvent);
716 void AliRawReaderRoot::MakeIndex() {
719 TTree * rawTree = fBranch->GetTree();
721 rawTree->BuildIndex("-fEvtHdrs[0].fSize"); // Minus sign to get largest first
722 TTreeIndex * treeInd = (TTreeIndex*)rawTree->GetTreeIndex();
723 if (treeInd) fIndex = treeInd->GetIndex();