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)
43 AliRawReaderRoot::AliRawReaderRoot() :
56 // default constructor
60 AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
63 fEventIndex(eventNumber),
73 // create an object to read digits from the given input file for the
74 // event with the given number
76 TDirectory* dir = gDirectory;
77 fFile = TFile::Open(fileName);
79 if (!fFile || !fFile->IsOpen()) {
80 Error("AliRawReaderRoot", "could not open file %s", fileName);
83 TTree* tree = (TTree*) fFile->Get("RAW");
85 Error("AliRawReaderRoot", "no raw data tree found");
88 fBranch = tree->GetBranch("rawevent");
90 Error("AliRawReaderRoot", "no raw data branch found");
94 fEvent = new AliRawEvent;
95 fBranch->SetAddress(&fEvent);
96 if (fEventIndex >= 0) {
97 if (fBranch->GetEntry(fEventIndex) <= 0) {
98 Error("AliRawReaderRoot", "no event with number %d found", fEventIndex);
104 AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event) :
117 // create an object to read digits from the given raw event
121 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
122 AliRawReader(rawReader),
125 fEventIndex(rawReader.fEventIndex),
127 fSubEventIndex(rawReader.fSubEventIndex),
129 fEquipmentIndex(rawReader.fEquipmentIndex),
137 if (rawReader.fFile) {
138 TDirectory* dir = gDirectory;
139 fFile = TFile::Open(rawReader.fFile->GetName());
141 if (!fFile || !fFile->IsOpen()) {
142 Error("AliRawReaderRoot", "could not open file %s",
143 rawReader.fFile->GetName());
146 TTree* tree = (TTree*) fFile->Get("RAW");
148 Error("AliRawReaderRoot", "no raw data tree found");
151 fBranch = tree->GetBranch("rawevent");
153 Error("AliRawReaderRoot", "no raw data branch found");
157 fEvent = new AliRawEvent;
158 fBranch->SetAddress(&fEvent);
159 if (fEventIndex >= 0) {
160 if (fBranch->GetEntry(fEventIndex) <= 0) {
161 Error("AliRawReaderRoot", "no event with number %d found",
167 fEvent = rawReader.fEvent;
170 if (fSubEventIndex > 0) {
171 fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
172 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex);
173 fRawData = fEquipment->GetRawData();
175 fHeader = (AliRawDataHeader*) ((UChar_t*) fRawData->GetBuffer() +
176 ((UChar_t*) rawReader.fHeader -
177 (UChar_t*) rawReader.fRawData->GetBuffer()));
178 fPosition = (UChar_t*) fRawData->GetBuffer() +
179 (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
180 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
184 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
187 // assignment operator
189 this->~AliRawReaderRoot();
190 new(this) AliRawReaderRoot(rawReader);
194 AliRawReaderRoot::~AliRawReaderRoot()
196 // delete objects and close root file
199 if (fEvent) delete fEvent;
205 const AliRawEventHeaderBase* AliRawReaderRoot::GetEventHeader() const
207 // Get the even header
208 // Return NULL in case of failure
209 if (!fEvent) return NULL;
210 return fEvent->GetHeader();
213 UInt_t AliRawReaderRoot::GetType() const
215 // get the type from the event header
217 if (!fEvent) return 0;
218 return fEvent->GetHeader()->Get("Type");
221 UInt_t AliRawReaderRoot::GetRunNumber() const
223 // get the run number from the event header
225 if (!fEvent) return 0;
226 return fEvent->GetHeader()->Get("RunNb");
229 const UInt_t* AliRawReaderRoot::GetEventId() const
231 // get the event id from the event header
233 if (!fEvent) return NULL;
234 return fEvent->GetHeader()->GetP("Id");
237 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
239 // get the trigger pattern from the event header
241 if (!fEvent) return NULL;
242 return fEvent->GetHeader()->GetP("TriggerPattern");
245 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
247 // get the detector pattern from the event header
249 if (!fEvent) return NULL;
250 return fEvent->GetHeader()->GetP("DetectorPattern");
253 const UInt_t* AliRawReaderRoot::GetAttributes() const
255 // get the type attributes from the event header
257 if (!fEvent) return NULL;
258 return fEvent->GetHeader()->GetP("TypeAttribute");
261 const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
263 // get the type attributes from the sub event header
265 if (!fSubEvent) return NULL;
266 return fSubEvent->GetHeader()->GetP("TypeAttribute");
269 UInt_t AliRawReaderRoot::GetLDCId() const
271 // get the LDC Id from the event header
273 if (!fEvent || !fSubEvent) return 0;
274 return fSubEvent->GetHeader()->Get("LdcId");
277 UInt_t AliRawReaderRoot::GetGDCId() const
279 // get the GDC Id from the event header
281 if (!fEvent) return 0;
282 return fEvent->GetHeader()->Get("GdcId");
285 UInt_t AliRawReaderRoot::GetTimestamp() const
287 if (!fEvent) return 0;
288 return fEvent->GetHeader()->Get("Timestamp");
291 Int_t AliRawReaderRoot::GetEquipmentSize() const
293 // get the size of the equipment
295 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
296 return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
299 Int_t AliRawReaderRoot::GetEquipmentType() const
301 // get the type from the equipment header
303 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
304 return fEquipment->GetEquipmentHeader()->GetEquipmentType();
307 Int_t AliRawReaderRoot::GetEquipmentId() const
309 // get the ID from the equipment header
311 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
312 return fEquipment->GetEquipmentHeader()->GetId();
315 const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
317 // get the attributes from the equipment header
319 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
320 return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
323 Int_t AliRawReaderRoot::GetEquipmentElementSize() const
325 // get the basic element size from the equipment header
327 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
328 return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
331 Int_t AliRawReaderRoot::GetEquipmentHeaderSize() const
333 // get the size of the equipment header (28 bytes by default)
335 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
336 return fEquipment->GetEquipmentHeader()->HeaderSize();
339 // _________________________________________________________________________
340 void AliRawReaderRoot::SwapData(const void* inbuf, const void* outbuf, UInt_t size) {
341 // The method swaps the contents of the
342 // raw-data event header
343 UInt_t intCount = (size+3)/sizeof(UInt_t);
345 UInt_t* buf = (UInt_t*) inbuf; // temporary integers buffer
346 for (UInt_t i=0; i<intCount; i++, buf++) {
347 UInt_t value = SwapWord(*buf);
349 memcpy((UInt_t*)outbuf+i, &value, size%sizeof(UInt_t));
351 memcpy((UInt_t*)outbuf+i, &value, sizeof(UInt_t));
354 // _________________________________________________________________________
356 Bool_t AliRawReaderRoot::ReadHeader()
358 // read a data header at the current position
359 // returns kFALSE if the data header could not be read
362 if (!fEvent) return kFALSE;
365 // skip payload (if event was not selected)
366 if (fCount > 0) fPosition += fCount;
368 // get the first or the next equipment if at the end of an equipment
369 if (!fEquipment || (fPosition >= fEnd)) {
371 // get the first or the next sub event if at the end of a sub event
372 if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
374 // check for end of event data
375 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
376 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
378 // check the magic word of the sub event
379 if (!fSubEvent->GetHeader()->IsValid()) {
380 Error("ReadHeader", "wrong magic number in sub event!");
381 fSubEvent->GetHeader()->Dump();
382 fErrorCode = kErrMagic;
391 // get the next equipment and raw data
393 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
394 if (!fEquipment) continue;
399 fRawData = fEquipment->GetRawData();
404 fPosition = (UChar_t*) fRawData->GetBuffer();
405 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
408 // continue with the next equipment if no data left in the payload
409 if (fPosition >= fEnd) continue;
411 if (fRequireHeader) {
412 // check that there are enough bytes left for the data header
413 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
414 Error("ReadHeader", "could not read data header!");
415 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
416 fEquipment->GetEquipmentHeader()->Dump();
419 fErrorCode = kErrNoDataHeader;
423 // "read" the data header
424 fHeader = (AliRawDataHeader*) fPosition;
426 SwapData((void*) fHeader, (void*) fHeaderSwapped, sizeof(AliRawDataHeader));
427 fHeader=fHeaderSwapped;
429 if ((fPosition + fHeader->fSize) != fEnd) {
430 if (fHeader->fSize != 0xFFFFFFFF)
431 Warning("ReadHeader",
432 "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
433 fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
434 fHeader->fSize = fEnd - fPosition;
436 fPosition += sizeof(AliRawDataHeader);
439 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
440 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
442 // check consistency of data size in the header and in the sub event
443 if (fPosition + fCount > fEnd) {
444 Error("ReadHeader", "size in data header exceeds event size!");
445 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
446 fEquipment->GetEquipmentHeader()->Dump();
449 fErrorCode = kErrSize;
454 fCount = fEnd - fPosition;
457 } while (!IsSelected());
462 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
464 // reads the next payload at the current position
465 // returns kFALSE if the data could not be read
468 while (fCount == 0) {
469 if (!ReadHeader()) return kFALSE;
477 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
479 // reads the next block of data at the current position
480 // returns kFALSE if the data could not be read
483 if (fPosition + size > fEnd) {
484 Error("ReadNext", "could not read data!");
485 fErrorCode = kErrOutOfBounds;
488 memcpy(data, fPosition, size);
496 Bool_t AliRawReaderRoot::Reset()
498 // reset the current position to the beginning of the event
508 fPosition = fEnd = NULL;
513 Bool_t AliRawReaderRoot::NextEvent()
515 // go to the next event in the root file
517 if (!fBranch) return kFALSE;
521 fEvent = new AliRawEvent;
522 fBranch->SetAddress(&fEvent);
523 if (fBranch->GetEntry(fEventIndex+1) <= 0)
526 } while (!IsEventSelected());
531 Bool_t AliRawReaderRoot::RewindEvents()
533 // go back to the beginning of the root file
535 if (!fBranch) return kFALSE;
539 fEvent = new AliRawEvent;
540 fBranch->SetAddress(&fEvent);
546 Int_t AliRawReaderRoot::CheckData() const
548 // check the consistency of the data
550 if (!fEvent) return 0;
552 AliRawEvent* subEvent = NULL;
553 Int_t subEventIndex = 0;
554 AliRawEquipment* equipment = NULL;
555 Int_t equipmentIndex = 0;
556 UChar_t* position = 0;
561 // get the first or the next sub event if at the end of an equipment
562 if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
564 // check for end of event data
565 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
566 subEvent = fEvent->GetSubEvent(subEventIndex++);
568 // check the magic word of the sub event
569 if (!fSubEvent->GetHeader()->IsValid()) {
577 // get the next equipment and raw data
578 equipment = subEvent->GetEquipment(equipmentIndex++);
579 if (!equipment) continue;
580 AliRawData* rawData = equipment->GetRawData();
581 if (!rawData) continue;
582 position = (UChar_t*) rawData->GetBuffer();
583 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
585 // continue with the next sub event if no data left in the payload
586 if (position >= end) continue;
588 if (fRequireHeader) {
589 // check that there are enough bytes left for the data header
590 if (position + sizeof(AliRawDataHeader) > end) {
591 result |= kErrNoDataHeader;
595 // check consistency of data size in the header and in the equipment
596 AliRawDataHeader* header = (AliRawDataHeader*) position;
597 if ((position + header->fSize) != end) {
598 if (header->fSize != 0xFFFFFFFF)
599 Warning("ReadHeader",
600 "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
601 equipment->GetEquipmentHeader()->GetId(),header->fSize, end - position);
602 header->fSize = end - position;