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 "AliRawEvent.h"
35 #include "AliRawEventHeaderBase.h"
36 #include "AliRawEquipment.h"
37 #include "AliRawEquipmentHeader.h"
38 #include "AliRawData.h"
41 ClassImp(AliRawReaderRoot)
44 AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
47 fEventIndex(eventNumber),
57 // create an object to read digits from the given input file for the
58 // event with the given number
60 TDirectory* dir = gDirectory;
61 fFile = TFile::Open(fileName);
63 if (!fFile || !fFile->IsOpen()) {
64 Error("AliRawReaderRoot", "could not open file %s", fileName);
67 TTree* tree = (TTree*) fFile->Get("RAW");
69 Error("AliRawReaderRoot", "no raw data tree found");
72 fBranch = tree->GetBranch("rawevent");
74 Error("AliRawReaderRoot", "no raw data branch found");
78 fEvent = new AliRawEvent;
79 fBranch->SetAddress(&fEvent);
80 if (fEventIndex >= 0) {
81 if (fBranch->GetEntry(fEventIndex) <= 0) {
82 Error("AliRawReaderRoot", "no event with number %d found", fEventIndex);
88 AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event) :
101 // create an object to read digits from the given raw event
105 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
106 AliRawReader(rawReader),
109 fEventIndex(rawReader.fEventIndex),
111 fSubEventIndex(rawReader.fSubEventIndex),
113 fEquipmentIndex(rawReader.fEquipmentIndex),
121 if (rawReader.fFile) {
122 TDirectory* dir = gDirectory;
123 fFile = TFile::Open(rawReader.fFile->GetName());
125 if (!fFile || !fFile->IsOpen()) {
126 Error("AliRawReaderRoot", "could not open file %s",
127 rawReader.fFile->GetName());
130 TTree* tree = (TTree*) fFile->Get("RAW");
132 Error("AliRawReaderRoot", "no raw data tree found");
135 fBranch = tree->GetBranch("rawevent");
137 Error("AliRawReaderRoot", "no raw data branch found");
141 fEvent = new AliRawEvent;
142 fBranch->SetAddress(&fEvent);
143 if (fEventIndex >= 0) {
144 if (fBranch->GetEntry(fEventIndex) <= 0) {
145 Error("AliRawReaderRoot", "no event with number %d found",
151 fEvent = rawReader.fEvent;
154 if (fSubEventIndex > 0) {
155 fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
156 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex);
157 fRawData = fEquipment->GetRawData();
159 fHeader = (AliRawDataHeader*) ((UChar_t*) fRawData->GetBuffer() +
160 ((UChar_t*) rawReader.fHeader -
161 (UChar_t*) rawReader.fRawData->GetBuffer()));
162 fPosition = (UChar_t*) fRawData->GetBuffer() +
163 (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
164 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
168 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
171 // assignment operator
173 this->~AliRawReaderRoot();
174 new(this) AliRawReaderRoot(rawReader);
178 AliRawReaderRoot::~AliRawReaderRoot()
180 // delete objects and close root file
183 if (fEvent) delete fEvent;
189 const AliRawEventHeaderBase* AliRawReaderRoot::GetEventHeader() const
191 // Get the even header
192 // Return NULL in case of failure
193 if (!fEvent) return NULL;
194 return fEvent->GetHeader();
197 UInt_t AliRawReaderRoot::GetType() const
199 // get the type from the event header
201 if (!fEvent) return 0;
202 return fEvent->GetHeader()->Get("Type");
205 UInt_t AliRawReaderRoot::GetRunNumber() const
207 // get the run number from the event header
209 if (!fEvent) return 0;
210 return fEvent->GetHeader()->Get("RunNb");
213 const UInt_t* AliRawReaderRoot::GetEventId() const
215 // get the event id from the event header
217 if (!fEvent) return NULL;
218 return fEvent->GetHeader()->GetP("Id");
221 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
223 // get the trigger pattern from the event header
225 if (!fEvent) return NULL;
226 return fEvent->GetHeader()->GetP("TriggerPattern");
229 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
231 // get the detector pattern from the event header
233 if (!fEvent) return NULL;
234 return fEvent->GetHeader()->GetP("DetectorPattern");
237 const UInt_t* AliRawReaderRoot::GetAttributes() const
239 // get the type attributes from the event header
241 if (!fEvent) return NULL;
242 return fEvent->GetHeader()->GetP("TypeAttribute");
245 const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
247 // get the type attributes from the sub event header
249 if (!fSubEvent) return NULL;
250 return fSubEvent->GetHeader()->GetP("TypeAttribute");
253 UInt_t AliRawReaderRoot::GetLDCId() const
255 // get the LDC Id from the event header
257 if (!fEvent || !fSubEvent) return 0;
258 return fSubEvent->GetHeader()->Get("LdcId");
261 UInt_t AliRawReaderRoot::GetGDCId() const
263 // get the GDC Id from the event header
265 if (!fEvent) return 0;
266 return fEvent->GetHeader()->Get("GdcId");
269 UInt_t AliRawReaderRoot::GetTimestamp() const
271 if (!fEvent) return 0;
272 return fEvent->GetHeader()->Get("Timestamp");
275 Int_t AliRawReaderRoot::GetEquipmentSize() const
277 // get the size of the equipment
279 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
280 return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
283 Int_t AliRawReaderRoot::GetEquipmentType() const
285 // get the type from the equipment header
287 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
288 return fEquipment->GetEquipmentHeader()->GetEquipmentType();
291 Int_t AliRawReaderRoot::GetEquipmentId() const
293 // get the ID from the equipment header
295 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
296 return fEquipment->GetEquipmentHeader()->GetId();
299 const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
301 // get the attributes from the equipment header
303 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
304 return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
307 Int_t AliRawReaderRoot::GetEquipmentElementSize() const
309 // get the basic element size from the equipment header
311 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
312 return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
315 Int_t AliRawReaderRoot::GetEquipmentHeaderSize() const
317 // get the size of the equipment header (28 bytes by default)
319 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
320 return fEquipment->GetEquipmentHeader()->HeaderSize();
323 // _________________________________________________________________________
324 void AliRawReaderRoot::SwapData(const void* inbuf, const void* outbuf, UInt_t size) {
325 // The method swaps the contents of the
326 // raw-data event header
327 UInt_t intCount = (size+3)/sizeof(UInt_t);
329 UInt_t* buf = (UInt_t*) inbuf; // temporary integers buffer
330 for (UInt_t i=0; i<intCount; i++, buf++) {
331 UInt_t value = SwapWord(*buf);
333 memcpy((UInt_t*)outbuf+i, &value, size%sizeof(UInt_t));
335 memcpy((UInt_t*)outbuf+i, &value, sizeof(UInt_t));
338 // _________________________________________________________________________
340 Bool_t AliRawReaderRoot::ReadHeader()
342 // read a data header at the current position
343 // returns kFALSE if the data header could not be read
346 if (!fEvent) return kFALSE;
349 // skip payload (if event was not selected)
350 if (fCount > 0) fPosition += fCount;
352 // get the first or the next equipment if at the end of an equipment
353 if (!fEquipment || (fPosition >= fEnd)) {
355 // get the first or the next sub event if at the end of a sub event
356 if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
358 // check for end of event data
359 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
360 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
362 // check the magic word of the sub event
363 if (!fSubEvent->GetHeader()->IsValid()) {
364 Error("ReadHeader", "wrong magic number in sub event!");
365 fSubEvent->GetHeader()->Dump();
366 fErrorCode = kErrMagic;
375 // get the next equipment and raw data
377 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
378 if (!fEquipment) continue;
383 fRawData = fEquipment->GetRawData();
388 fPosition = (UChar_t*) fRawData->GetBuffer();
389 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
392 // continue with the next equipment if no data left in the payload
393 if (fPosition >= fEnd) continue;
395 if (fRequireHeader) {
396 // check that there are enough bytes left for the data header
397 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
398 Error("ReadHeader", "could not read data header!");
399 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
400 fEquipment->GetEquipmentHeader()->Dump();
403 fErrorCode = kErrNoDataHeader;
407 // "read" the data header
408 fHeader = (AliRawDataHeader*) fPosition;
410 SwapData((void*) fHeader, (void*) fHeaderSwapped, sizeof(AliRawDataHeader));
411 fHeader=fHeaderSwapped;
413 if ((fPosition + fHeader->fSize) != fEnd) {
414 if (fHeader->fSize != 0xFFFFFFFF)
415 Warning("ReadHeader",
416 "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
417 fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
418 fHeader->fSize = fEnd - fPosition;
420 fPosition += sizeof(AliRawDataHeader);
423 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
424 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
426 // check consistency of data size in the header and in the sub event
427 if (fPosition + fCount > fEnd) {
428 Error("ReadHeader", "size in data header exceeds event size!");
429 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
430 fEquipment->GetEquipmentHeader()->Dump();
433 fErrorCode = kErrSize;
438 fCount = fEnd - fPosition;
441 } while (!IsSelected());
446 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
448 // reads the next payload at the current position
449 // returns kFALSE if the data could not be read
452 while (fCount == 0) {
453 if (!ReadHeader()) return kFALSE;
461 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
463 // reads the next block of data at the current position
464 // returns kFALSE if the data could not be read
467 if (fPosition + size > fEnd) {
468 Error("ReadNext", "could not read data!");
469 fErrorCode = kErrOutOfBounds;
472 memcpy(data, fPosition, size);
480 Bool_t AliRawReaderRoot::Reset()
482 // reset the current position to the beginning of the event
492 fPosition = fEnd = NULL;
497 Bool_t AliRawReaderRoot::NextEvent()
499 // go to the next event in the root file
501 if (!fBranch) return kFALSE;
505 fEvent = new AliRawEvent;
506 fBranch->SetAddress(&fEvent);
507 if (fBranch->GetEntry(fEventIndex+1) <= 0)
510 } while (!IsEventSelected());
515 Bool_t AliRawReaderRoot::RewindEvents()
517 // go back to the beginning of the root file
519 if (!fBranch) return kFALSE;
523 fEvent = new AliRawEvent;
524 fBranch->SetAddress(&fEvent);
530 Int_t AliRawReaderRoot::CheckData() const
532 // check the consistency of the data
534 if (!fEvent) return 0;
536 AliRawEvent* subEvent = NULL;
537 Int_t subEventIndex = 0;
538 AliRawEquipment* equipment = NULL;
539 Int_t equipmentIndex = 0;
540 UChar_t* position = 0;
545 // get the first or the next sub event if at the end of an equipment
546 if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
548 // check for end of event data
549 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
550 subEvent = fEvent->GetSubEvent(subEventIndex++);
552 // check the magic word of the sub event
553 if (!fSubEvent->GetHeader()->IsValid()) {
561 // get the next equipment and raw data
562 equipment = subEvent->GetEquipment(equipmentIndex++);
563 if (!equipment) continue;
564 AliRawData* rawData = equipment->GetRawData();
565 if (!rawData) continue;
566 position = (UChar_t*) rawData->GetBuffer();
567 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
569 // continue with the next sub event if no data left in the payload
570 if (position >= end) continue;
572 if (fRequireHeader) {
573 // check that there are enough bytes left for the data header
574 if (position + sizeof(AliRawDataHeader) > end) {
575 result |= kErrNoDataHeader;
579 // check consistency of data size in the header and in the equipment
580 AliRawDataHeader* header = (AliRawDataHeader*) position;
581 if ((position + header->fSize) != end) {
582 if (header->fSize != 0xFFFFFFFF)
583 Warning("ReadHeader",
584 "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
585 equipment->GetEquipmentHeader()->GetId(),header->fSize, end - position);
586 header->fSize = end - position;