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);
84 TTree* tree = (TTree*) fFile->Get("RAW");
86 Error("AliRawReaderRoot", "no raw data tree found");
90 fBranch = tree->GetBranch("rawevent");
92 Error("AliRawReaderRoot", "no raw data branch found");
97 fEvent = new AliRawEvent;
98 fBranch->SetAddress(&fEvent);
99 if (fEventIndex >= 0) {
100 if (fBranch->GetEntry(fEventIndex) <= 0) {
101 Error("AliRawReaderRoot", "no event with number %d found", fEventIndex);
108 AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event) :
121 // create an object to read digits from the given raw event
122 if (!fEvent) fIsValid = kFALSE;
125 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
126 AliRawReader(rawReader),
129 fEventIndex(rawReader.fEventIndex),
131 fSubEventIndex(rawReader.fSubEventIndex),
133 fEquipmentIndex(rawReader.fEquipmentIndex),
141 if (rawReader.fFile) {
142 TDirectory* dir = gDirectory;
143 fFile = TFile::Open(rawReader.fFile->GetName());
145 if (!fFile || !fFile->IsOpen()) {
146 Error("AliRawReaderRoot", "could not open file %s",
147 rawReader.fFile->GetName());
151 TTree* tree = (TTree*) fFile->Get("RAW");
153 Error("AliRawReaderRoot", "no raw data tree found");
157 fBranch = tree->GetBranch("rawevent");
159 Error("AliRawReaderRoot", "no raw data branch found");
164 fEvent = new AliRawEvent;
165 fBranch->SetAddress(&fEvent);
166 if (fEventIndex >= 0) {
167 if (fBranch->GetEntry(fEventIndex) <= 0) {
168 Error("AliRawReaderRoot", "no event with number %d found",
175 fEvent = rawReader.fEvent;
178 if (fSubEventIndex > 0) {
179 fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
180 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex);
181 fRawData = fEquipment->GetRawData();
183 fHeader = (AliRawDataHeader*) ((UChar_t*) fRawData->GetBuffer() +
184 ((UChar_t*) rawReader.fHeader -
185 (UChar_t*) rawReader.fRawData->GetBuffer()));
186 fPosition = (UChar_t*) fRawData->GetBuffer() +
187 (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
188 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
192 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot&
195 // assignment operator
197 this->~AliRawReaderRoot();
198 new(this) AliRawReaderRoot(rawReader);
202 AliRawReaderRoot::~AliRawReaderRoot()
204 // delete objects and close root file
207 if (fEvent) delete fEvent;
213 const AliRawEventHeaderBase* AliRawReaderRoot::GetEventHeader() const
215 // Get the even header
216 // Return NULL in case of failure
217 if (!fEvent) return NULL;
218 return fEvent->GetHeader();
221 UInt_t AliRawReaderRoot::GetType() const
223 // get the type from the event header
225 if (!fEvent) return 0;
226 return fEvent->GetHeader()->Get("Type");
229 UInt_t AliRawReaderRoot::GetRunNumber() const
231 // get the run number from the event header
233 if (!fEvent) return 0;
234 return fEvent->GetHeader()->Get("RunNb");
237 const UInt_t* AliRawReaderRoot::GetEventId() const
239 // get the event id from the event header
241 if (!fEvent) return NULL;
242 return fEvent->GetHeader()->GetP("Id");
245 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
247 // get the trigger pattern from the event header
249 if (!fEvent) return NULL;
250 return fEvent->GetHeader()->GetP("TriggerPattern");
253 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
255 // get the detector pattern from the event header
257 if (!fEvent) return NULL;
258 return fEvent->GetHeader()->GetP("DetectorPattern");
261 const UInt_t* AliRawReaderRoot::GetAttributes() const
263 // get the type attributes from the event header
265 if (!fEvent) return NULL;
266 return fEvent->GetHeader()->GetP("TypeAttribute");
269 const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
271 // get the type attributes from the sub event header
273 if (!fSubEvent) return NULL;
274 return fSubEvent->GetHeader()->GetP("TypeAttribute");
277 UInt_t AliRawReaderRoot::GetLDCId() const
279 // get the LDC Id from the event header
281 if (!fEvent || !fSubEvent) return 0;
282 return fSubEvent->GetHeader()->Get("LdcId");
285 UInt_t AliRawReaderRoot::GetGDCId() const
287 // get the GDC Id from the event header
289 if (!fEvent) return 0;
290 return fEvent->GetHeader()->Get("GdcId");
293 UInt_t AliRawReaderRoot::GetTimestamp() const
295 if (!fEvent) return 0;
296 return fEvent->GetHeader()->Get("Timestamp");
299 Int_t AliRawReaderRoot::GetEquipmentSize() const
301 // get the size of the equipment
303 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
304 return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
307 Int_t AliRawReaderRoot::GetEquipmentType() const
309 // get the type from the equipment header
311 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
312 return fEquipment->GetEquipmentHeader()->GetEquipmentType();
315 Int_t AliRawReaderRoot::GetEquipmentId() const
317 // get the ID from the equipment header
319 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
320 return fEquipment->GetEquipmentHeader()->GetId();
323 const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
325 // get the attributes from the equipment header
327 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
328 return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
331 Int_t AliRawReaderRoot::GetEquipmentElementSize() const
333 // get the basic element size from the equipment header
335 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
336 return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
339 Int_t AliRawReaderRoot::GetEquipmentHeaderSize() const
341 // get the size of the equipment header (28 bytes by default)
343 if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
344 return fEquipment->GetEquipmentHeader()->HeaderSize();
347 // _________________________________________________________________________
348 void AliRawReaderRoot::SwapData(const void* inbuf, const void* outbuf, UInt_t size) {
349 // The method swaps the contents of the
350 // raw-data event header
351 UInt_t intCount = (size+3)/sizeof(UInt_t);
353 UInt_t* buf = (UInt_t*) inbuf; // temporary integers buffer
354 for (UInt_t i=0; i<intCount; i++, buf++) {
355 UInt_t value = SwapWord(*buf);
357 memcpy((UInt_t*)outbuf+i, &value, size%sizeof(UInt_t));
359 memcpy((UInt_t*)outbuf+i, &value, sizeof(UInt_t));
362 // _________________________________________________________________________
364 Bool_t AliRawReaderRoot::ReadHeader()
366 // read a data header at the current position
367 // returns kFALSE if the data header could not be read
370 if (!fEvent) return kFALSE;
373 // skip payload (if event was not selected)
374 if (fCount > 0) fPosition += fCount;
376 // get the first or the next equipment if at the end of an equipment
377 if (!fEquipment || (fPosition >= fEnd)) {
379 // get the first or the next sub event if at the end of a sub event
380 if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
382 // check for end of event data
383 if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
384 fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
386 // check the magic word of the sub event
387 if (!fSubEvent->GetHeader()->IsValid()) {
388 Error("ReadHeader", "wrong magic number in sub event!");
389 fSubEvent->GetHeader()->Dump();
390 fErrorCode = kErrMagic;
399 // get the next equipment and raw data
401 fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
402 if (!fEquipment) continue;
407 fRawData = fEquipment->GetRawData();
412 fPosition = (UChar_t*) fRawData->GetBuffer();
413 fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
416 // continue with the next equipment if no data left in the payload
417 if (fPosition >= fEnd) continue;
419 if (fRequireHeader) {
420 // check that there are enough bytes left for the data header
421 if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
422 Error("ReadHeader", "could not read data header!");
423 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
424 fEquipment->GetEquipmentHeader()->Dump();
427 fErrorCode = kErrNoDataHeader;
431 // "read" the data header
432 fHeader = (AliRawDataHeader*) fPosition;
434 SwapData((void*) fHeader, (void*) fHeaderSwapped, sizeof(AliRawDataHeader));
435 fHeader=fHeaderSwapped;
437 if ((fPosition + fHeader->fSize) != fEnd) {
438 if (fHeader->fSize != 0xFFFFFFFF)
439 Warning("ReadHeader",
440 "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
441 fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
442 fHeader->fSize = fEnd - fPosition;
444 fPosition += sizeof(AliRawDataHeader);
447 if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
448 fCount = fHeader->fSize - sizeof(AliRawDataHeader);
450 // check consistency of data size in the header and in the sub event
451 if (fPosition + fCount > fEnd) {
452 Error("ReadHeader", "size in data header exceeds event size!");
453 Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
454 fEquipment->GetEquipmentHeader()->Dump();
457 fErrorCode = kErrSize;
462 fCount = fEnd - fPosition;
465 } while (!IsSelected());
470 Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
472 // reads the next payload at the current position
473 // returns kFALSE if the data could not be read
476 while (fCount == 0) {
477 if (!ReadHeader()) return kFALSE;
485 Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
487 // reads the next block of data at the current position
488 // returns kFALSE if the data could not be read
491 if (fPosition + size > fEnd) {
492 Error("ReadNext", "could not read data!");
493 fErrorCode = kErrOutOfBounds;
496 memcpy(data, fPosition, size);
504 Bool_t AliRawReaderRoot::Reset()
506 // reset the current position to the beginning of the event
516 fPosition = fEnd = NULL;
521 Bool_t AliRawReaderRoot::NextEvent()
523 // go to the next event in the root file
525 if (!fBranch) return kFALSE;
529 fEvent = new AliRawEvent;
530 fBranch->SetAddress(&fEvent);
531 if (fBranch->GetEntry(fEventIndex+1) <= 0)
534 } while (!IsEventSelected());
539 Bool_t AliRawReaderRoot::RewindEvents()
541 // go back to the beginning of the root file
543 if (!fBranch) return kFALSE;
547 fEvent = new AliRawEvent;
548 fBranch->SetAddress(&fEvent);
553 Bool_t AliRawReaderRoot::GotoEvent(Int_t event)
555 // go to a particular event
556 // Uses the absolute event index inside the
559 if (!fBranch) return kFALSE;
562 fEvent = new AliRawEvent;
563 fBranch->SetAddress(&fEvent);
564 if (fBranch->GetEntry(event) <= 0)
571 Int_t AliRawReaderRoot::GetNumberOfEvents() const
573 // Get the total number of events in
576 if (!fBranch) return -1;
578 return fBranch->GetEntries();
581 Int_t AliRawReaderRoot::CheckData() const
583 // check the consistency of the data
585 if (!fEvent) return 0;
587 AliRawEvent* subEvent = NULL;
588 Int_t subEventIndex = 0;
589 AliRawEquipment* equipment = NULL;
590 Int_t equipmentIndex = 0;
591 UChar_t* position = 0;
596 // get the first or the next sub event if at the end of an equipment
597 if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
599 // check for end of event data
600 if (subEventIndex >= fEvent->GetNSubEvents()) return result;
601 subEvent = fEvent->GetSubEvent(subEventIndex++);
603 // check the magic word of the sub event
604 if (!fSubEvent->GetHeader()->IsValid()) {
612 // get the next equipment and raw data
613 equipment = subEvent->GetEquipment(equipmentIndex++);
614 if (!equipment) continue;
615 AliRawData* rawData = equipment->GetRawData();
616 if (!rawData) continue;
617 position = (UChar_t*) rawData->GetBuffer();
618 end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
620 // continue with the next sub event if no data left in the payload
621 if (position >= end) continue;
623 if (fRequireHeader) {
624 // check that there are enough bytes left for the data header
625 if (position + sizeof(AliRawDataHeader) > end) {
626 result |= kErrNoDataHeader;
630 // check consistency of data size in the header and in the equipment
631 AliRawDataHeader* header = (AliRawDataHeader*) position;
632 if ((position + header->fSize) != end) {
633 if (header->fSize != 0xFFFFFFFF)
634 Warning("ReadHeader",
635 "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
636 equipment->GetEquipmentHeader()->GetId(),header->fSize, end - position);
637 header->fSize = end - position;