]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RAW/AliRawReaderRoot.cxx
Removing meaningless type qualifier on return type (icc warning 858)
[u/mrichter/AliRoot.git] / RAW / AliRawReaderRoot.cxx
index d5d921477f9e63dfacd06a55fb7c141ed58fda2d..ed16d4b8191fbfa395e50a5c812dad0480c90515 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
+/* $Id$ */
+
 ///////////////////////////////////////////////////////////////////////////////
-//
-// This is a class for reading a raw data from a root file and providing
-// information about digits
-//
+///
+/// This is a class for reading raw data from a root file.
+///
+/// The root file is expected to contain a tree of name "RAW" with
+/// a branch of name "rawevent" which contains objects of type
+/// AliRawEvent.
+/// 
+/// The file name and the event number are arguments of the constructor
+/// of AliRawReaderRoot.
+///
 ///////////////////////////////////////////////////////////////////////////////
 
+#include <TFile.h>
+#include <TTree.h>
 #include "AliRawReaderRoot.h"
 #include "AliRawEvent.h"
+#include "AliRawEventHeader.h"
+#include "AliRawEquipment.h"
+#include "AliRawEquipmentHeader.h"
+#include "AliRawData.h"
 
 
 ClassImp(AliRawReaderRoot)
 
 
-AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber)
+AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
+  fFile(NULL),
+  fBranch(NULL),
+  fEventIndex(eventNumber),
+  fEvent(NULL),
+  fSubEventIndex(0),
+  fSubEvent(NULL),
+  fEquipmentIndex(0),
+  fEquipment(NULL),
+  fRawData(NULL),
+  fPosition(NULL),
+  fEnd(NULL)
 {
 // create an object to read digits from the given input file for the
 // event with the given number
@@ -44,60 +69,100 @@ AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber)
     Error("AliRawReaderRoot", "no raw data tree found");
     return;
   }
-  TBranch* branch = tree->GetBranch("rawevent");
-  if (!branch) {
+  fBranch = tree->GetBranch("rawevent");
+  if (!fBranch) {
     Error("AliRawReaderRoot", "no raw data branch found");
     return;
   }
 
   fEvent = new AliRawEvent;
-  branch->SetAddress(&fEvent);
-  if (branch->GetEntry(eventNumber) <= 0) {
-    Error("AliRawReaderRoot", "no event with number %d found", eventNumber);
-    return;
+  fBranch->SetAddress(&fEvent);
+  if (fEventIndex >= 0) {
+    if (fBranch->GetEntry(fEventIndex) <= 0) {
+      Error("AliRawReaderRoot", "no event with number %d found", fEventIndex);
+      return;
+    }
   }
-  
-  fSubEventIndex = 0;
-  fSubEvent = NULL;
-  fRawData = NULL;
-  fMiniHeader = NULL;
-
-  fCount = 0;
-  fPosition = fEnd = NULL;
 }
 
-AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event)
+AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event) :
+  fFile(NULL),
+  fBranch(NULL),
+  fEventIndex(-1),
+  fEvent(event),
+  fSubEventIndex(0),
+  fSubEvent(NULL),
+  fEquipmentIndex(0),
+  fEquipment(NULL),
+  fRawData(NULL),
+  fPosition(NULL),
+  fEnd(NULL)
 {
 // create an object to read digits from the given raw event
 
-  fFile = NULL;
-  fEvent = event;
-  
-  fSubEventIndex = 0;
-  fSubEvent = NULL;
-  fRawData = NULL;
-  fMiniHeader = NULL;
-
-  fCount = 0;
-  fPosition = fEnd = NULL;
 }
 
 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
-  AliRawReader(rawReader)
+  AliRawReader(rawReader),
+  fFile(NULL),
+  fBranch(NULL),
+  fEventIndex(rawReader.fEventIndex),
+  fEvent(NULL),
+  fSubEventIndex(rawReader.fSubEventIndex),
+  fSubEvent(NULL),
+  fEquipmentIndex(rawReader.fEquipmentIndex),
+  fEquipment(NULL),
+  fRawData(NULL),
+  fPosition(NULL),
+  fEnd(NULL)
 {
 // copy constructor
 
-  fFile = NULL;
-  fEvent = rawReader.fEvent;
-  
-  fSubEventIndex = rawReader.fSubEventIndex;
-  fSubEvent = rawReader.fSubEvent;
-  fRawData = rawReader.fRawData;
-  fMiniHeader = rawReader.fMiniHeader;
-
-  fCount = rawReader.fCount;
-  fPosition = rawReader.fPosition;
-  fEnd = rawReader.fEnd;
+  if (rawReader.fFile) {
+    TDirectory* dir = gDirectory;
+    fFile = TFile::Open(rawReader.fFile->GetName());
+    dir->cd();
+    if (!fFile || !fFile->IsOpen()) {
+      Error("AliRawReaderRoot", "could not open file %s", 
+           rawReader.fFile->GetName());
+      return;
+    }
+    TTree* tree = (TTree*) fFile->Get("RAW");
+    if (!tree) {
+      Error("AliRawReaderRoot", "no raw data tree found");
+      return;
+    }
+    fBranch = tree->GetBranch("rawevent");
+    if (!fBranch) {
+      Error("AliRawReaderRoot", "no raw data branch found");
+      return;
+    }
+
+    fEvent = new AliRawEvent;
+    fBranch->SetAddress(&fEvent);
+    if (fEventIndex >= 0) {
+      if (fBranch->GetEntry(fEventIndex) <= 0) {
+       Error("AliRawReaderRoot", "no event with number %d found", 
+             fEventIndex);
+       return;
+      }
+    }
+  } else {
+    fEvent = rawReader.fEvent;
+  }
+
+  if (fSubEventIndex > 0) {
+    fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
+    fEquipment = fSubEvent->GetEquipment(fEquipmentIndex);
+    fRawData = fEquipment->GetRawData();
+      fCount = 0;
+    fHeader = (AliRawDataHeader*) ((UChar_t*) fRawData->GetBuffer() + 
+      ((UChar_t*) rawReader.fHeader - 
+       (UChar_t*) rawReader.fRawData->GetBuffer()));
+    fPosition = (UChar_t*) fRawData->GetBuffer() + 
+      (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
+    fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
+  }
 }
 
 AliRawReaderRoot& AliRawReaderRoot::operator = (const AliRawReaderRoot& 
@@ -170,6 +235,22 @@ const UInt_t* AliRawReaderRoot::GetAttributes() const
   return fEvent->GetHeader()->GetTypeAttribute();
 }
 
+const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
+{
+// get the type attributes from the sub event header
+
+  if (!fSubEvent) return NULL;
+  return fSubEvent->GetHeader()->GetTypeAttribute();
+}
+
+UInt_t AliRawReaderRoot::GetLDCId() const
+{
+// get the LDC Id from the event header
+
+  if (!fEvent || !fSubEvent) return 0;
+  return fSubEvent->GetHeader()->GetLDCId();
+}
+
 UInt_t AliRawReaderRoot::GetGDCId() const
 {
 // get the GDC Id from the event header
@@ -179,10 +260,51 @@ UInt_t AliRawReaderRoot::GetGDCId() const
 }
 
 
-Bool_t AliRawReaderRoot::ReadMiniHeader()
+Int_t AliRawReaderRoot::GetEquipmentSize() const
 {
-// read a mini header at the current position
-// returns kFALSE if the mini header could not be read
+// get the size of the equipment
+
+  if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
+  return fEquipment->GetEquipmentHeader()->GetEquipmentSize();
+}
+
+Int_t AliRawReaderRoot::GetEquipmentType() const
+{
+// get the type from the equipment header
+
+  if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
+  return fEquipment->GetEquipmentHeader()->GetEquipmentType();
+}
+
+Int_t AliRawReaderRoot::GetEquipmentId() const
+{
+// get the ID from the equipment header
+
+  if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return -1;
+  return fEquipment->GetEquipmentHeader()->GetId();
+}
+
+const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
+{
+// get the attributes from the equipment header
+
+  if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return NULL;
+  return fEquipment->GetEquipmentHeader()->GetTypeAttribute();
+}
+
+Int_t AliRawReaderRoot::GetEquipmentElementSize() const
+{
+// get the basic element size from the equipment header
+
+  if (!fEvent || !fEquipment || !fEquipment->GetEquipmentHeader()) return 0;
+  return fEquipment->GetEquipmentHeader()->GetBasicSizeType();
+}
+
+
+Bool_t AliRawReaderRoot::ReadHeader()
+{
+// read a data header at the current position
+// returns kFALSE if the data header could not be read
 
   fErrorCode = 0;
   if (!fEvent) return kFALSE;
@@ -191,60 +313,70 @@ Bool_t AliRawReaderRoot::ReadMiniHeader()
     // skip payload (if event was not selected)
     if (fCount > 0) fPosition += fCount;
 
-    // get the first or the next sub event if at the end of a sub event
-    if (!fSubEvent || (fPosition >= fEnd)) {
+    // get the first or the next equipment if at the end of an equipment
+    if (!fEquipment || (fPosition >= fEnd)) {
 
-      // check for end of event data
-      if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
-      fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
+      // get the first or the next sub event if at the end of a sub event
+      if (!fSubEvent || (fEquipmentIndex >= fSubEvent->GetNEquipments())) {
 
-      // check the magic word of the sub event
-      if (!fSubEvent->GetHeader()->IsValid()) {
-       Error("ReadMiniHeader", "wrong magic number in sub event!");
-       fSubEvent->GetHeader()->Dump();
-       fErrorCode = kErrMagic;
-       return kFALSE;
+       // check for end of event data
+       if (fSubEventIndex >= fEvent->GetNSubEvents()) return kFALSE;
+       fSubEvent = fEvent->GetSubEvent(fSubEventIndex++);
+
+       // check the magic word of the sub event
+       if (!fSubEvent->GetHeader()->IsValid()) {
+         Error("ReadHeader", "wrong magic number in sub event!");
+         fSubEvent->GetHeader()->Dump();
+         fErrorCode = kErrMagic;
+         return kFALSE;
+       }
+
+       fEquipmentIndex = 0;
+       fEquipment = NULL;
+       fRawData = NULL;
       }
 
-      fRawData = fSubEvent->GetRawData();
+      // get the next equipment and raw data
       fCount = 0;
+      fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
+      if (!fEquipment) continue;
+      fRawData = fEquipment->GetRawData();
+      if (!fRawData) {
+       fPosition = fEnd;
+       continue;
+      }
       fPosition = (UChar_t*) fRawData->GetBuffer();
       fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
     }
 
-    // continue with the next sub event if no data left in the payload
+    // continue with the next equipment if no data left in the payload
     if (fPosition >= fEnd) continue;
 
-    // check that there are enough bytes left for the mini header
-    if (fPosition + sizeof(AliMiniHeader) > fEnd) {
-      Error("ReadMiniHeader", "could not read mini header data!");
-      Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition);
-      fSubEvent->GetHeader()->Dump();
+    // check that there are enough bytes left for the data header
+    if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
+      Error("ReadHeader", "could not read data header!");
+      Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
+      fEquipment->GetEquipmentHeader()->Dump();
       fCount = 0;
       fPosition = fEnd;
-      fErrorCode = kErrNoMiniHeader;
+      fErrorCode = kErrNoDataHeader;
       continue;
     }
 
-    // "read" and check the mini header
-    fMiniHeader = (AliMiniHeader*) fPosition;
-    fPosition += sizeof(AliMiniHeader);
-    if (!CheckMiniHeader()) {
-      Error("ReadMiniHeader", "wrong magic word in mini header!");
-      Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition);
-      fSubEvent->GetHeader()->Dump();
-      fCount = 0;
-      fPosition = fEnd;
-      fErrorCode = kErrMiniMagic;
-      continue;
+    // "read" the data header
+    fHeader = (AliRawDataHeader*) fPosition;
+    fPosition += sizeof(AliRawDataHeader);
+    if (fHeader->fSize != 0xFFFFFFFF) {
+      fCount = fHeader->fSize - sizeof(AliRawDataHeader);
+    } else {
+      fCount = fEnd - fPosition;
     }
-    fCount = fMiniHeader->fSize;
 
-    // check consistency of data size in the mini header and in the sub event
+    // check consistency of data size in the header and in the sub event
     if (fPosition + fCount > fEnd) {  
-      Error("ReadMiniHeader", "size in mini header exceeds event size!");
-      Warning("ReadMiniHeader", "skipping %d bytes", fEnd - fPosition);
-      fSubEvent->GetHeader()->Dump();
+      Error("ReadHeader", "size in data header exceeds event size!");
+      Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
+      fEquipment->GetEquipmentHeader()->Dump();
       fCount = 0;
       fPosition = fEnd;
       fErrorCode = kErrSize;
@@ -263,7 +395,7 @@ Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
 
   fErrorCode = 0;
   while (fCount == 0) {
-    if (!ReadMiniHeader()) return kFALSE;
+    if (!ReadHeader()) return kFALSE;
   }
   data = fPosition;
   fPosition += fCount;  
@@ -295,8 +427,10 @@ Bool_t AliRawReaderRoot::Reset()
 
   fSubEventIndex = 0;
   fSubEvent = NULL;
+  fEquipmentIndex = 0;
+  fEquipment = NULL;
   fRawData = NULL;
-  fMiniHeader = NULL;
+  fHeader = NULL;
 
   fCount = 0;
   fPosition = fEnd = NULL;
@@ -304,6 +438,38 @@ Bool_t AliRawReaderRoot::Reset()
 }
 
 
+Bool_t AliRawReaderRoot::NextEvent()
+{
+// go to the next event in the root file
+
+  if (!fFile) return kFALSE;
+
+  do {
+    if (fBranch->GetEntry(fEventIndex+1) <= 0) {
+      delete fEvent;
+      fEvent = new AliRawEvent;
+      fBranch->SetAddress(&fEvent);
+      return kFALSE;
+    }
+    fEventIndex++;
+  } while (!IsEventSelected());
+  return Reset();
+}
+
+Bool_t AliRawReaderRoot::RewindEvents()
+{
+// go back to the beginning of the root file
+
+  if (!fFile) return kFALSE;
+
+  fEventIndex = -1;
+  delete fEvent;
+  fEvent = new AliRawEvent;
+  fBranch->SetAddress(&fEvent);
+  return Reset();
+}
+
+
 Int_t AliRawReaderRoot::CheckData() const
 {
 // check the consistency of the data
@@ -312,38 +478,54 @@ Int_t AliRawReaderRoot::CheckData() const
 
   AliRawEvent* subEvent = NULL;
   Int_t subEventIndex = 0;
+  AliRawEquipment* equipment = NULL;
+  Int_t equipmentIndex = 0;
   UChar_t* position = 0;
   UChar_t* end = 0;
+  Int_t result = 0;
 
   while (kTRUE) {
-    // get the first or the next sub event if at the end of a sub event
-    if (!subEvent || (position >= end)) {
+    // get the first or the next sub event if at the end of an equipment
+    if (!subEvent || (equipmentIndex >= subEvent->GetNEquipments())) {
 
       // check for end of event data
-      if (subEventIndex >= fEvent->GetNSubEvents()) return 0;
+      if (subEventIndex >= fEvent->GetNSubEvents()) return result;
       subEvent = fEvent->GetSubEvent(subEventIndex++);
 
       // check the magic word of the sub event
-      if (!fSubEvent->GetHeader()->IsValid()) return kErrMagic;
+      if (!fSubEvent->GetHeader()->IsValid()) {
+       result |= kErrMagic;
+       return result;
+      }
 
-      AliRawData* rawData = subEvent->GetRawData();
-      position = (UChar_t*) rawData->GetBuffer();
-      end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
+      equipmentIndex = 0;
     }
 
+    // get the next equipment and raw data
+    equipment = subEvent->GetEquipment(equipmentIndex++);
+    if (!equipment) continue;
+    AliRawData* rawData = equipment->GetRawData();
+    if (!rawData) continue;
+    position = (UChar_t*) rawData->GetBuffer();
+    end = ((UChar_t*) rawData->GetBuffer()) + rawData->GetSize();
+
     // continue with the next sub event if no data left in the payload
     if (position >= end) continue;
 
-    // check that there are enough bytes left for the mini header
-    if (position + sizeof(AliMiniHeader) > end) return kErrNoMiniHeader;
-
-    // "read" and check the mini header
-    AliMiniHeader* miniHeader = (AliMiniHeader*) position;
-    position += sizeof(AliMiniHeader);
-    if (!CheckMiniHeader(miniHeader)) return kErrMiniMagic;
+    // check that there are enough bytes left for the data header
+    if (position + sizeof(AliRawDataHeader) > end) {
+      result |= kErrNoDataHeader;
+      continue;
+    }
 
-    // check consistency of data size in the mini header and in the sub event
-    if (position + miniHeader->fSize > end) return kErrSize;
-    position += miniHeader->fSize;
+    // check consistency of data size in the header and in the equipment
+    AliRawDataHeader* header = (AliRawDataHeader*) position;
+    if (header->fSize != 0xFFFFFFFF) {
+      if (position + header->fSize > end) {
+       result |= kErrSize;
+      }
+    }
   };
+
+  return result;
 }