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 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
406 if (!fEquipment) continue;
411 fRawData = fEquipment->GetRawData();
416 fPosition = (UChar_t*) fRawData->GetBuffer();
417 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
420 // continue with the next equipment if no data left in the payload
421 if (fPosition >= fEnd) continue;
423 if (fRequireHeader) {
424 // check that there are enough bytes left for the data header
425 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
426 Error("ReadHeader", "could not read data header!");
427 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
428 fEquipment->GetEquipmentHeader()->Dump();
431 fErrorCode = kErrNoDataHeader;
435 // "read" the data header
436 fHeader = (AliRawDataHeader*) fPosition;
438 SwapData((void*) fHeader, (void*) fHeaderSwapped, sizeof(AliRawDataHeader));
439 fHeader=fHeaderSwapped;
441 if ((fPosition + fHeader->fSize) != fEnd) {
442 if (fHeader->fSize != 0xFFFFFFFF)
443 Warning("ReadHeader",
444 "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
445 fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
446 fHeader->fSize = fEnd - fPosition;
448 fPosition += sizeof(AliRawDataHeader);
451 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
452 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
454 // check consistency of data size in the header and in the sub event
455 if (fPosition + fCount > fEnd) {
456 Error("ReadHeader", "size in data header exceeds event size!");
457 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
458 fEquipment->GetEquipmentHeader()->Dump();
461 fErrorCode = kErrSize;
466 fCount = fEnd - fPosition;
469 } while (!IsSelected());
474 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
476 // reads the next payload at the current position
477 // returns kFALSE if the data could not be read
480 while (fCount == 0) {
481 if (!ReadHeader()) return kFALSE;
489 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
491 // reads the next block of data at the current position
492 // returns kFALSE if the data could not be read
495 if (fPosition + size > fEnd) {
496 Error("ReadNext", "could not read data!");
497 fErrorCode = kErrOutOfBounds;
500 memcpy(data, fPosition, size);
508 Bool_t AliRawReaderRoot::Reset()
510 // reset the current position to the beginning of the event
520 fPosition = fEnd = NULL;
525 Bool_t AliRawReaderRoot::NextEvent()
527 // go to the next event in the root file
529 if (!fBranch) return kFALSE;
535 fBranch->SetAddress(&fEvent);
536 if (fBranch->GetEntry(fEventIndex+1) <= 0)
538 fEventHeader = fEvent->GetHeader();
540 } while (!IsEventSelected());
545 Bool_t AliRawReaderRoot::RewindEvents()
547 // go back to the beginning of the root file
549 if (!fBranch) return kFALSE;
555 fBranch->SetAddress(&fEvent);
560 Bool_t AliRawReaderRoot::GotoEvent(Int_t event)
562 // go to a particular event
563 // Uses the absolute event index inside the
566 if (!fBranch) return kFALSE;
571 fBranch->SetAddress(&fEvent);
572 if (fBranch->GetEntry(event) <= 0)
574 fEventHeader = fEvent->GetHeader();
580 Int_t AliRawReaderRoot::GetNumberOfEvents() const
582 // Get the total number of events in
585 if (!fBranch) return -1;
587 return fBranch->GetEntries();
590 Int_t AliRawReaderRoot::CheckData() const
592 // check the consistency of the data
594 if (!fEvent) return 0;
596 AliRawVEvent* subEvent = NULL;
597 Int_t subEventIndex = 0;
598 AliRawVEquipment* equipment = NULL;
599 Int_t equipmentIndex = 0;
600 UChar_t* position = 0;
605 // get the first or the next sub event if at the end of an equipment
606 if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
608 // check for end of event data
609 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
610 subEvent = fEvent->GetSubEvent(subEventIndex++);
612 // check the magic word of the sub event
613 if (!fSubEvent->GetHeader()->IsValid()) {
621 // get the next equipment and raw data
622 equipment = subEvent->GetEquipment(equipmentIndex++);
623 if (!equipment) continue;
624 AliRawData* rawData = equipment->GetRawData();
625 if (!rawData) continue;
626 position = (UChar_t*) rawData->GetBuffer();
627 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
629 // continue with the next sub event if no data left in the payload
630 if (position >= end) continue;
632 if (fRequireHeader) {
633 // check that there are enough bytes left for the data header
634 if (position + sizeof(AliRawDataHeader) > end) {
635 result |= kErrNoDataHeader;
639 // check consistency of data size in the header and in the equipment
640 AliRawDataHeader* header = (AliRawDataHeader*) position;
641 if ((position + header->fSize) != end) {
642 if (header->fSize != 0xFFFFFFFF)
643 Warning("ReadHeader",
644 "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
645 equipment->GetEquipmentHeader()->GetId(),header->fSize, end - position);
646 header->fSize = end - position;