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 "AliRawReaderRoot.h"
34 #include "AliRawVEvent.h"
35 #include "AliRawEventHeaderBase.h"
36 #include "AliRawVEquipment.h"
37 #include "AliRawEquipmentHeader.h"
38 #include "AliRawData.h"
41 ClassImp(AliRawReaderRoot)
43 AliRawReaderRoot::AliRawReaderRoot() :
57 // default constructor
61 AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
64 fEventIndex(eventNumber),
75 // create an object to read digits from the given input file for the
76 // event with the given number
78 TDirectory* dir = gDirectory;
79 fFile = TFile::Open(fileName);
81 if (!fFile || !fFile->IsOpen()) {
82 Error("AliRawReaderRoot", "could not open file %s", fileName);
86 TTree* tree = (TTree*) fFile->Get("RAW");
88 Error("AliRawReaderRoot", "no raw data tree found");
92 fBranch = tree->GetBranch("rawevent");
94 Error("AliRawReaderRoot", "no raw data branch found");
99 fBranch->SetAddress(&fEvent);
100 if (fEventIndex >= 0) {
101 if (fBranch->GetEntry(fEventIndex) <= 0) {
102 Error("AliRawReaderRoot", "no event with number %d found", fEventIndex);
106 fEventHeader = fEvent->GetHeader();
110 AliRawReaderRoot::AliRawReaderRoot(AliRawVEvent* event) :
115 fEventHeader(event->GetHeader()),
124 // create an object to read digits from the given raw event
125 if (!fEvent) fIsValid = kFALSE;
128 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
129 AliRawReader(rawReader),
132 fEventIndex(rawReader.fEventIndex),
135 fSubEventIndex(rawReader.fSubEventIndex),
137 fEquipmentIndex(rawReader.fEquipmentIndex),
145 if (rawReader.fFile) {
146 TDirectory* dir = gDirectory;
147 fFile = TFile::Open(rawReader.fFile->GetName());
149 if (!fFile || !fFile->IsOpen()) {
150 Error("AliRawReaderRoot", "could not open file %s",
151 rawReader.fFile->GetName());
155 TTree* tree = (TTree*) fFile->Get("RAW");
157 Error("AliRawReaderRoot", "no raw data tree found");
161 fBranch = tree->GetBranch("rawevent");
163 Error("AliRawReaderRoot", "no raw data branch found");
168 fBranch->SetAddress(&fEvent);
169 if (fEventIndex >= 0) {
170 if (fBranch->GetEntry(fEventIndex) <= 0) {
171 Error("AliRawReaderRoot", "no event with number %d found",
176 fEventHeader = fEvent->GetHeader();
179 fEvent = rawReader.fEvent;
180 fEventHeader = rawReader.fEventHeader;
183 if (fSubEventIndex > 0) {
184 fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
185 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex);
186 fRawData = fEquipment->GetRawData();
188 fHeader = (AliRawDataHeader*) ((UChar_t*) fRawData->GetBuffer() +
189 ((UChar_t*) rawReader.fHeader -
190 (UChar_t*) rawReader.fRawData->GetBuffer()));
191 fPosition = (UChar_t*) fRawData->GetBuffer() +
192 (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
193 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
197 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
200 // assignment operator
202 this->~AliRawReaderRoot();
203 new(this) AliRawReaderRoot(rawReader);
207 AliRawReaderRoot::~AliRawReaderRoot()
209 // delete objects and close root file
212 if (fEvent) delete fEvent;
218 const AliRawEventHeaderBase* AliRawReaderRoot::GetEventHeader() const
220 // Get the even header
221 // Return NULL in case of failure
225 UInt_t AliRawReaderRoot::GetType() const
227 // get the type from the event header
229 if (!fEventHeader) return 0;
230 return fEventHeader->Get("Type");
233 UInt_t AliRawReaderRoot::GetRunNumber() const
235 // get the run number from the event header
237 if (!fEventHeader) return 0;
238 return fEventHeader->Get("RunNb");
241 const UInt_t* AliRawReaderRoot::GetEventId() const
243 // get the event id from the event header
245 if (!fEventHeader) return NULL;
246 return fEventHeader->GetP("Id");
249 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
251 // get the trigger pattern from the event header
253 if (!fEventHeader) return NULL;
254 return fEventHeader->GetP("TriggerPattern");
257 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
259 // get the detector pattern from the event header
261 if (!fEventHeader) return NULL;
262 return fEventHeader->GetP("DetectorPattern");
265 const UInt_t* AliRawReaderRoot::GetAttributes() const
267 // get the type attributes from the event header
269 if (!fEventHeader) return NULL;
270 return fEventHeader->GetP("TypeAttribute");
273 const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
275 // get the type attributes from the sub event header
277 if (!fSubEvent) return NULL;
278 return fSubEvent->GetHeader()->GetP("TypeAttribute");
281 UInt_t AliRawReaderRoot::GetLDCId() const
283 // get the LDC Id from the event header
285 if (!fEvent || !fSubEvent) return 0;
286 return fSubEvent->GetHeader()->Get("LdcId");
289 UInt_t AliRawReaderRoot::GetGDCId() const
291 // get the GDC Id from the event header
293 if (!fEventHeader) return 0;
294 return fEventHeader->Get("GdcId");
297 UInt_t AliRawReaderRoot::GetTimestamp() const
299 if (!fEventHeader) return 0;
300 return fEventHeader->Get("Timestamp");
303 Int_t AliRawReaderRoot::GetEquipmentSize() const
305 // get the size of the equipment
307 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
308 return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
311 Int_t AliRawReaderRoot::GetEquipmentType() const
313 // get the type from the equipment header
315 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
316 return fEquipment->GetEquipmentHeader()->GetEquipmentType();
319 Int_t AliRawReaderRoot::GetEquipmentId() const
321 // get the ID from the equipment header
323 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
324 return fEquipment->GetEquipmentHeader()->GetId();
327 const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
329 // get the attributes from the equipment header
331 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
332 return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
335 Int_t AliRawReaderRoot::GetEquipmentElementSize() const
337 // get the basic element size from the equipment header
339 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
340 return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
343 Int_t AliRawReaderRoot::GetEquipmentHeaderSize() const
345 // get the size of the equipment header (28 bytes by default)
347 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
348 return fEquipment->GetEquipmentHeader()->HeaderSize();
351 // _________________________________________________________________________
352 void AliRawReaderRoot::SwapData(const void* inbuf, const void* outbuf, UInt_t size) {
353 // The method swaps the contents of the
354 // raw-data event header
355 UInt_t intCount = (size+3)/sizeof(UInt_t);
357 UInt_t* buf = (UInt_t*) inbuf; // temporary integers buffer
358 for (UInt_t i=0; i<intCount; i++, buf++) {
359 UInt_t value = SwapWord(*buf);
361 memcpy((UInt_t*)outbuf+i, &value, size%sizeof(UInt_t));
363 memcpy((UInt_t*)outbuf+i, &value, sizeof(UInt_t));
366 // _________________________________________________________________________
368 Bool_t AliRawReaderRoot::ReadHeader()
370 // read a data header at the current position
371 // returns kFALSE if the data header could not be read
374 if (!fEvent) return kFALSE;
377 // skip payload (if event was not selected)
378 if (fCount > 0) fPosition += fCount;
380 // get the first or the next equipment if at the end of an equipment
381 if (!fEquipment || (fPosition >= fEnd)) {
383 // get the first or the next sub event if at the end of a sub event
384 if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
386 // check for end of event data
387 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
388 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
390 // check the magic word of the sub event
391 if (!fSubEvent->GetHeader()->IsValid()) {
392 Error("ReadHeader", "wrong magic number in sub event!");
393 fSubEvent->GetHeader()->Dump();
394 fErrorCode = kErrMagic;
403 // get the next equipment and raw data
405 if (fEquipmentIndex >= fSubEvent->GetNEquipments()) {
409 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
410 if (!fEquipment) continue;
415 fRawData = fEquipment->GetRawData();
420 fPosition = (UChar_t*) fRawData->GetBuffer();
421 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
424 // continue with the next equipment if no data left in the payload
425 if (fPosition >= fEnd) continue;
427 if (fRequireHeader) {
428 // check that there are enough bytes left for the data header
429 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
430 Error("ReadHeader", "could not read data header!");
431 Warning("ReadHeader", "skipping %ld bytes", fEnd - fPosition);
432 fEquipment->GetEquipmentHeader()->Dump();
435 fErrorCode = kErrNoDataHeader;
439 // "read" the data header
440 fHeader = (AliRawDataHeader*) fPosition;
442 SwapData((void*) fHeader, (void*) fHeaderSwapped, sizeof(AliRawDataHeader));
443 fHeader=fHeaderSwapped;
445 if ((fPosition + fHeader->fSize) != fEnd) {
446 if (fHeader->fSize != 0xFFFFFFFF)
447 Warning("ReadHeader",
448 "Equipment %d : raw data size found in the header is wrong (%d != %ld)! Using the equipment size instead !",
449 fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
450 fHeader->fSize = fEnd - fPosition;
452 fPosition += sizeof(AliRawDataHeader);
455 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
456 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
458 // check consistency of data size in the header and in the sub event
459 if (fPosition + fCount > fEnd) {
460 Error("ReadHeader", "size in data header exceeds event size!");
461 Warning("ReadHeader", "skipping %ld bytes", fEnd - fPosition);
462 fEquipment->GetEquipmentHeader()->Dump();
465 fErrorCode = kErrSize;
470 fCount = fEnd - fPosition;
473 } while (!IsSelected());
478 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
480 // reads the next payload at the current position
481 // returns kFALSE if the data could not be read
484 while (fCount == 0) {
485 if (!ReadHeader()) return kFALSE;
493 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
495 // reads the next block of data at the current position
496 // returns kFALSE if the data could not be read
499 if (fPosition + size > fEnd) {
500 Error("ReadNext", "could not read data!");
501 fErrorCode = kErrOutOfBounds;
504 memcpy(data, fPosition, size);
512 Bool_t AliRawReaderRoot::Reset()
514 // reset the current position to the beginning of the event
524 fPosition = fEnd = NULL;
529 Bool_t AliRawReaderRoot::NextEvent()
531 // go to the next event in the root file
533 if (!fBranch) return kFALSE;
539 fBranch->SetAddress(&fEvent);
540 if (fBranch->GetEntry(fEventIndex+1) <= 0)
542 fEventHeader = fEvent->GetHeader();
544 } while (!IsEventSelected());
549 Bool_t AliRawReaderRoot::RewindEvents()
551 // go back to the beginning of the root file
553 if (!fBranch) return kFALSE;
559 fBranch->SetAddress(&fEvent);
564 Bool_t AliRawReaderRoot::GotoEvent(Int_t event)
566 // go to a particular event
567 // Uses the absolute event index inside the
570 if (!fBranch) return kFALSE;
575 fBranch->SetAddress(&fEvent);
576 if (fBranch->GetEntry(event) <= 0)
578 fEventHeader = fEvent->GetHeader();
584 Int_t AliRawReaderRoot::GetNumberOfEvents() const
586 // Get the total number of events in
589 if (!fBranch) return -1;
591 return fBranch->GetEntries();
594 Int_t AliRawReaderRoot::CheckData() const
596 // check the consistency of the data
598 if (!fEvent) return 0;
600 AliRawVEvent* subEvent = NULL;
601 Int_t subEventIndex = 0;
602 AliRawVEquipment* equipment = NULL;
603 Int_t equipmentIndex = 0;
604 UChar_t* position = 0;
609 // get the first or the next sub event if at the end of an equipment
610 if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
612 // check for end of event data
613 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
614 subEvent = fEvent->GetSubEvent(subEventIndex++);
616 // check the magic word of the sub event
617 if (!fSubEvent->GetHeader()->IsValid()) {
625 // get the next equipment and raw data
626 if (equipmentIndex >= subEvent->GetNEquipments()) {
630 equipment = subEvent->GetEquipment(equipmentIndex++);
631 if (!equipment) continue;
632 AliRawData* rawData = equipment->GetRawData();
633 if (!rawData) continue;
634 position = (UChar_t*) rawData->GetBuffer();
635 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
637 // continue with the next sub event if no data left in the payload
638 if (position >= end) continue;
640 if (fRequireHeader) {
641 // check that there are enough bytes left for the data header
642 if (position + sizeof(AliRawDataHeader) > end) {
643 result |= kErrNoDataHeader;
647 // check consistency of data size in the header and in the equipment
648 AliRawDataHeader* header = (AliRawDataHeader*) position;
649 if ((position + header->fSize) != end) {
650 if (header->fSize != 0xFFFFFFFF)
651 Warning("ReadHeader",
652 "Equipment %d : raw data size found in the header is wrong (%d != %ld)! Using the equipment size instead !",
653 equipment->GetEquipmentHeader()->GetId(),header->fSize, end - position);
654 header->fSize = end - position;
664 AliRawReader* AliRawReaderRoot::CloneSingleEvent() const
666 // Clones the current event and
667 // creates raw-reader for the cloned event
668 // Can be used in order to make asynchronious
669 // access to the current raw data within
670 // several threads (online event display/reco)
673 // Root formatted raw data
674 AliRawVEvent *gdcRootEvent = (AliRawVEvent*)fEvent->Clone();
675 for (Int_t ldcCounter=0; ldcCounter < gdcRootEvent->GetNSubEvents(); ldcCounter++) {
676 AliRawVEvent *ldcRootEvent = gdcRootEvent->GetSubEvent(ldcCounter);
677 AliRawVEvent *subEvent = fEvent->GetSubEvent(ldcCounter);
678 for (Int_t eqCounter=0; eqCounter < ldcRootEvent->GetNEquipments(); eqCounter++) {
679 AliRawVEquipment *equipment=ldcRootEvent->GetEquipment(eqCounter);
680 AliRawVEquipment *eq = subEvent->GetEquipment(eqCounter);
681 equipment->CloneRawData(eq->GetRawData());
684 // Reset original event and newly
686 gdcRootEvent->GetSubEvent(-1);
687 fEvent->GetSubEvent(-1);
688 return new AliRawReaderRoot(gdcRootEvent);