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 // Now check the version of the header
200 if (fHeader) version = fHeader->GetVersion();
203 fHeaderV3 = (AliRawDataHeaderV3*) ((UChar_t*) fRawData->GetBuffer() +
204 ((UChar_t*) rawReader.fHeaderV3 -
205 (UChar_t*) rawReader.fRawData->GetBuffer()));
207 fPosition = (UChar_t*) fRawData->GetBuffer() +
208 (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
209 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
213 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
216 // assignment operator
218 this->~AliRawReaderRoot();
219 new(this) AliRawReaderRoot(rawReader);
223 AliRawReaderRoot::~AliRawReaderRoot()
225 // delete objects and close root file
228 if (fEvent) delete fEvent;
234 const AliRawEventHeaderBase* AliRawReaderRoot::GetEventHeader() const
236 // Get the even header
237 // Return NULL in case of failure
241 UInt_t AliRawReaderRoot::GetType() const
243 // get the type from the event header
245 if (!fEventHeader) return 0;
246 return fEventHeader->Get("Type");
249 UInt_t AliRawReaderRoot::GetRunNumber() const
251 // get the run number from the event header
253 if (!fEventHeader) return 0;
254 return fEventHeader->Get("RunNb");
257 const UInt_t* AliRawReaderRoot::GetEventId() const
259 // get the event id from the event header
261 if (!fEventHeader) return NULL;
262 return fEventHeader->GetP("Id");
265 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
267 // get the trigger pattern from the event header
269 if (!fEventHeader) return NULL;
270 return fEventHeader->GetP("TriggerPattern");
273 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
275 // get the detector pattern from the event header
277 if (!fEventHeader) return NULL;
278 return fEventHeader->GetP("DetectorPattern");
281 const UInt_t* AliRawReaderRoot::GetAttributes() const
283 // get the type attributes from the event header
285 if (!fEventHeader) return NULL;
286 return fEventHeader->GetP("TypeAttribute");
289 const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
291 // get the type attributes from the sub event header
293 if (!fSubEvent) return NULL;
294 return fSubEvent->GetHeader()->GetP("TypeAttribute");
297 UInt_t AliRawReaderRoot::GetLDCId() const
299 // get the LDC Id from the event header
301 if (!fEvent || !fSubEvent) return 0;
302 return fSubEvent->GetHeader()->Get("LdcId");
305 UInt_t AliRawReaderRoot::GetGDCId() const
307 // get the GDC Id from the event header
309 if (!fEventHeader) return 0;
310 return fEventHeader->Get("GdcId");
313 UInt_t AliRawReaderRoot::GetTimestamp() const
315 if (!fEventHeader) return 0;
316 return fEventHeader->Get("Timestamp");
319 Int_t AliRawReaderRoot::GetEquipmentSize() const
321 // get the size of the equipment
323 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
324 return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
327 Int_t AliRawReaderRoot::GetEquipmentType() const
329 // get the type from the equipment header
331 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
332 return fEquipment->GetEquipmentHeader()->GetEquipmentType();
335 Int_t AliRawReaderRoot::GetEquipmentId() const
337 // get the ID from the equipment header
339 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
340 return fEquipment->GetEquipmentHeader()->GetId();
343 const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
345 // get the attributes from the equipment header
347 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
348 return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
351 Int_t AliRawReaderRoot::GetEquipmentElementSize() const
353 // get the basic element size from the equipment header
355 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
356 return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
359 Int_t AliRawReaderRoot::GetEquipmentHeaderSize() const
361 // get the size of the equipment header (28 bytes by default)
363 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
364 return fEquipment->GetEquipmentHeader()->HeaderSize();
367 // _________________________________________________________________________
368 void AliRawReaderRoot::SwapData(const void* inbuf, const void* outbuf, UInt_t size) {
369 // The method swaps the contents of the
370 // raw-data event header
371 UInt_t intCount = (size+3)/sizeof(UInt_t);
373 UInt_t* buf = (UInt_t*) inbuf; // temporary integers buffer
374 for (UInt_t i=0; i<intCount; i++, buf++) {
375 UInt_t value = SwapWord(*buf);
377 memcpy((UInt_t*)outbuf+i, &value, size%sizeof(UInt_t));
379 memcpy((UInt_t*)outbuf+i, &value, sizeof(UInt_t));
382 // _________________________________________________________________________
384 Bool_t AliRawReaderRoot::ReadHeader()
386 // read a data header at the current position
387 // returns kFALSE if the data header could not be read
390 if (!fEvent) return kFALSE;
393 // skip payload (if event was not selected)
394 if (fCount > 0) fPosition += fCount;
396 // get the first or the next equipment if at the end of an equipment
397 if (!fEquipment || (fPosition >= fEnd)) {
399 // get the first or the next sub event if at the end of a sub event
400 if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
402 // check for end of event data
403 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
404 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
406 // check the magic word of the sub event
407 if (!fSubEvent->GetHeader()->IsValid()) {
408 Error("ReadHeader", "wrong magic number in sub event!");
409 fSubEvent->GetHeader()->Dump();
410 fErrorCode = kErrMagic;
419 // get the next equipment and raw data
421 if (fEquipmentIndex >= fSubEvent->GetNEquipments()) {
425 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
426 if (!fEquipment) continue;
431 fRawData = fEquipment->GetRawData();
436 fPosition = (UChar_t*) fRawData->GetBuffer();
437 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
440 // continue with the next equipment if no data left in the payload
441 if (fPosition >= fEnd) continue;
443 if (fRequireHeader) {
444 // check that there are enough bytes left for the data header
445 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
446 Error("ReadHeader", "could not read data header!");
447 Warning("ReadHeader", "skipping %ld bytes", fEnd - fPosition);
448 fEquipment->GetEquipmentHeader()->Dump();
451 fErrorCode = kErrNoDataHeader;
455 // "read" the data header
456 fHeader = (AliRawDataHeader*) fPosition;
457 // Now check the version of the header
459 if (fHeader) version=fHeader->GetVersion();
462 SwapData((void*) fHeader, (void*) fHeaderSwapped, sizeof(AliRawDataHeader));
463 fHeader=fHeaderSwapped;
465 if ((fPosition + fHeader->fSize) != fEnd) {
466 if (fHeader->fSize != 0xFFFFFFFF)
467 Warning("ReadHeader",
468 "Equipment %d : raw data size found in the header is wrong (%d != %ld)! Using the equipment size instead !",
469 fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
470 fHeader->fSize = fEnd - fPosition;
472 fPosition += sizeof(AliRawDataHeader);
474 } else if (version==3) {
475 fHeaderV3 = (AliRawDataHeaderV3*) fPosition;
477 SwapData((void*) fHeaderV3, (void*) fHeaderSwapped, sizeof(AliRawDataHeaderV3));
478 fHeaderV3=fHeaderSwappedV3;
480 if ((fPosition + fHeaderV3->fSize) != fEnd) {
481 if (fHeaderV3->fSize != 0xFFFFFFFF)
482 Warning("ReadHeader",
483 "Equipment %d : raw data size found in the header is wrong (%d != %ld)! Using the equipment size instead !",
484 fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
485 fHeaderV3->fSize = fEnd - fPosition;
487 fPosition += sizeof(AliRawDataHeaderV3);
492 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
493 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
495 // check consistency of data size in the header and in the sub event
496 if (fPosition + fCount > fEnd) {
497 Error("ReadHeader", "size in data header exceeds event size!");
498 Warning("ReadHeader", "skipping %ld bytes", fEnd - fPosition);
499 fEquipment->GetEquipmentHeader()->Dump();
502 fErrorCode = kErrSize;
505 } else if (fHeaderV3 && (fHeaderV3->fSize != 0xFFFFFFFF)) {
506 fCount = fHeaderV3->fSize - sizeof(AliRawDataHeaderV3);
508 // check consistency of data size in the header and in the sub event
509 if (fPosition + fCount > fEnd) {
510 Error("ReadHeader", "size in data header exceeds event size!");
511 Warning("ReadHeader", "skipping %ld bytes", fEnd - fPosition);
512 fEquipment->GetEquipmentHeader()->Dump();
515 fErrorCode = kErrSize;
520 fCount = fEnd - fPosition;
523 } while (!IsSelected());
528 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
530 // reads the next payload at the current position
531 // returns kFALSE if the data could not be read
534 while (fCount == 0) {
535 if (!ReadHeader()) return kFALSE;
543 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
545 // reads the next block of data at the current position
546 // returns kFALSE if the data could not be read
549 if (fPosition + size > fEnd) {
550 Error("ReadNext", "could not read data!");
551 fErrorCode = kErrOutOfBounds;
554 memcpy(data, fPosition, size);
562 Bool_t AliRawReaderRoot::Reset()
564 // reset the current position to the beginning of the event
575 fPosition = fEnd = NULL;
580 Bool_t AliRawReaderRoot::NextEvent()
582 // go to the next event in the root file
584 if (!fBranch) return kFALSE;
586 // check if it uses order or not
587 if (fgUseOrder && !fIndex) MakeIndex();
593 fBranch->SetAddress(&fEvent);
594 Int_t entryToGet = fEventIndex + 1;
597 && entryToGet<fBranch->GetEntries()
598 && entryToGet>-1 ) entryToGet = fIndex[entryToGet];
599 if (fBranch->GetEntry(entryToGet) <= 0)
601 fEventHeader = fEvent->GetHeader();
603 } while (!IsEventSelected());
608 Bool_t AliRawReaderRoot::RewindEvents()
610 // go back to the beginning of the root file
612 if (!fBranch) return kFALSE;
618 fBranch->SetAddress(&fEvent);
623 Bool_t AliRawReaderRoot::GotoEvent(Int_t event)
625 // go to a particular event
626 // Uses the absolute event index inside the
629 if (!fBranch) return kFALSE;
631 // check if it uses order or not
632 if (fgUseOrder && !fIndex) MakeIndex();
637 fBranch->SetAddress(&fEvent);
638 Int_t entryToGet = event;
641 && entryToGet<fBranch->GetEntries()
642 && entryToGet>-1 ) entryToGet = fIndex[entryToGet];
643 if (fBranch->GetEntry(entryToGet) <= 0)
645 fEventHeader = fEvent->GetHeader();
651 Int_t AliRawReaderRoot::GetNumberOfEvents() const
653 // Get the total number of events in
656 if (!fBranch) return -1;
658 return fBranch->GetEntries();
661 Int_t AliRawReaderRoot::CheckData() const
663 // check the consistency of the data
665 if (!fEvent) return 0;
667 AliRawVEvent* subEvent = NULL;
668 Int_t subEventIndex = 0;
669 AliRawVEquipment* equipment = NULL;
670 Int_t equipmentIndex = 0;
671 UChar_t* position = 0;
676 // get the first or the next sub event if at the end of an equipment
677 if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
679 // check for end of event data
680 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
681 subEvent = fEvent->GetSubEvent(subEventIndex++);
683 // check the magic word of the sub event
684 if (!fSubEvent->GetHeader()->IsValid()) {
692 // get the next equipment and raw data
693 if (equipmentIndex >= subEvent->GetNEquipments()) {
697 equipment = subEvent->GetEquipment(equipmentIndex++);
698 if (!equipment) continue;
699 AliRawData* rawData = equipment->GetRawData();
700 if (!rawData) continue;
701 position = (UChar_t*) rawData->GetBuffer();
702 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
704 // continue with the next sub event if no data left in the payload
705 if (position >= end) continue;
707 if (fRequireHeader) {
708 // check that there are enough bytes left for the data header
709 if (position + sizeof(AliRawDataHeader) > end) {
710 result |= kErrNoDataHeader;
714 // check consistency of data size in the header and in the equipment
715 AliRawDataHeader* header = (AliRawDataHeader*) position;
716 if ((position + header->fSize) != end) {
717 if (header->fSize != 0xFFFFFFFF)
718 Warning("ReadHeader",
719 "Equipment %d : raw data size found in the header is wrong (%d != %ld)! Using the equipment size instead !",
720 equipment->GetEquipmentHeader()->GetId(),header->fSize, end - position);
721 header->fSize = end - position;
731 AliRawReader* AliRawReaderRoot::CloneSingleEvent() const
733 // Clones the current event and
734 // creates raw-reader for the cloned event
735 // Can be used in order to make asynchronious
736 // access to the current raw data within
737 // several threads (online event display/reco)
740 // Root formatted raw data
741 AliRawVEvent *gdcRootEvent = (AliRawVEvent*)fEvent->Clone();
742 for (Int_t ldcCounter=0; ldcCounter < gdcRootEvent->GetNSubEvents(); ldcCounter++) {
743 AliRawVEvent *ldcRootEvent = gdcRootEvent->GetSubEvent(ldcCounter);
744 AliRawVEvent *subEvent = fEvent->GetSubEvent(ldcCounter);
745 for (Int_t eqCounter=0; eqCounter < ldcRootEvent->GetNEquipments(); eqCounter++) {
746 AliRawVEquipment *equipment=ldcRootEvent->GetEquipment(eqCounter);
747 AliRawVEquipment *eq = subEvent->GetEquipment(eqCounter);
748 equipment->CloneRawData(eq->GetRawData());
751 // Reset original event and newly
753 gdcRootEvent->GetSubEvent(-1);
754 fEvent->GetSubEvent(-1);
755 return new AliRawReaderRoot(gdcRootEvent);
760 void AliRawReaderRoot::MakeIndex() {
763 TTree * rawTree = fBranch->GetTree();
765 rawTree->BuildIndex("-fEvtHdrs[0].fSize"); // Minus sign to get largest first
766 TTreeIndex * treeInd = (TTreeIndex*)rawTree->GetTreeIndex();
767 if (treeInd) fIndex = treeInd->GetIndex();