]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RAW/AliRawReaderRoot.cxx
Merge branch 'master' into TPCdev
[u/mrichter/AliRoot.git] / RAW / AliRawReaderRoot.cxx
index ec519f9f186290c413c2d7832f755bf6c79f3512..c849d7c653935005309bb5993dfb6e4b736b57f8 100644 (file)
@@ -30,6 +30,8 @@
 
 #include <TFile.h>
 #include <TTree.h>
+#include <TTreeIndex.h>
+#include <TGrid.h>
 #include "AliRawReaderRoot.h"
 #include "AliRawVEvent.h"
 #include "AliRawEventHeaderBase.h"
@@ -39,6 +41,8 @@
 
 
 ClassImp(AliRawReaderRoot)
+Bool_t AliRawReaderRoot::fgUseOrder = kFALSE;
+
 
 AliRawReaderRoot::AliRawReaderRoot() :
   fFile(NULL),
@@ -52,7 +56,8 @@ AliRawReaderRoot::AliRawReaderRoot() :
   fEquipment(NULL),
   fRawData(NULL),
   fPosition(NULL),
-  fEnd(NULL)
+  fEnd(NULL),
+  fIndex(0x0)
 {
 // default constructor
 
@@ -70,12 +75,15 @@ AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
   fEquipment(NULL),
   fRawData(NULL),
   fPosition(NULL),
-  fEnd(NULL)
+  fEnd(NULL),
+  fIndex(0x0)
 {
 // create an object to read digits from the given input file for the
 // event with the given number
 
   TDirectory* dir = gDirectory;
+  TString flStr = fileName;
+  if (flStr.BeginsWith("alien://") && !gGrid) TGrid::Connect("alien://");
   fFile = TFile::Open(fileName);
   dir->cd();
   if (!fFile || !fFile->IsOpen()) {
@@ -119,7 +127,8 @@ AliRawReaderRoot::AliRawReaderRoot(AliRawVEvent* event) :
   fEquipment(NULL),
   fRawData(NULL),
   fPosition(NULL),
-  fEnd(NULL)
+  fEnd(NULL),
+  fIndex(0x0)
 {
 // create an object to read digits from the given raw event
   if (!fEvent) fIsValid = kFALSE;
@@ -138,12 +147,13 @@ AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
   fEquipment(NULL),
   fRawData(NULL),
   fPosition(NULL),
-  fEnd(NULL)
+  fEnd(NULL),
+  fIndex(0x0)
 {
 // copy constructor
 
   if (rawReader.fFile) {
-    TDirectory* dir = gDirectory;
+    TDirectory* dir = gDirectory;    
     fFile = TFile::Open(rawReader.fFile->GetName());
     dir->cd();
     if (!fFile || !fFile->IsOpen()) {
@@ -184,10 +194,19 @@ AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
     fSubEvent = fEvent->GetSubEvent(fSubEventIndex-1);
     fEquipment = fSubEvent->GetEquipment(fEquipmentIndex);
     fRawData = fEquipment->GetRawData();
-      fCount = 0;
+    fCount = 0;
     fHeader = (AliRawDataHeader*) ((UChar_t*) fRawData->GetBuffer() + 
       ((UChar_t*) rawReader.fHeader - 
        (UChar_t*) rawReader.fRawData->GetBuffer()));
+    // Now check the version of the header
+    UChar_t version = 2;
+    if (fHeader) version = fHeader->GetVersion();
+    if (version==3) {
+      fHeader = NULL;
+      fHeaderV3 = (AliRawDataHeaderV3*) ((UChar_t*) fRawData->GetBuffer() + 
+      ((UChar_t*) rawReader.fHeaderV3 - 
+       (UChar_t*) rawReader.fRawData->GetBuffer()));
+    }
     fPosition = (UChar_t*) fRawData->GetBuffer() + 
       (rawReader.fPosition - (UChar_t*) rawReader.fRawData->GetBuffer());
     fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
@@ -402,6 +421,10 @@ Bool_t AliRawReaderRoot::ReadHeader()
 
       // get the next equipment and raw data
       fCount = 0;
+      if (fEquipmentIndex >= fSubEvent->GetNEquipments()) {
+       fEquipment = NULL;
+       continue;
+      }
       fEquipment = fSubEvent->GetEquipment(fEquipmentIndex++);
       if (!fEquipment) continue;
       if (!IsSelected()) {
@@ -424,7 +447,7 @@ Bool_t AliRawReaderRoot::ReadHeader()
       // 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);
+       Warning("ReadHeader", "skipping %ld bytes", fEnd - fPosition);
        fEquipment->GetEquipmentHeader()->Dump();
        fCount = 0;
        fPosition = fEnd;
@@ -434,18 +457,39 @@ Bool_t AliRawReaderRoot::ReadHeader()
 
       // "read" the data header
       fHeader = (AliRawDataHeader*) fPosition;
+      // Now check the version of the header
+      UChar_t version = 2;
+      if (fHeader) version=fHeader->GetVersion();
+      if (version==2) {
+ #ifndef R__BYTESWAP
+       SwapData((void*) fHeader, (void*) fHeaderSwapped, sizeof(AliRawDataHeader));
+       fHeader=fHeaderSwapped;
+#endif
+       if ((fPosition + fHeader->fSize) != fEnd) {
+         if (fHeader->fSize != 0xFFFFFFFF)
+           Warning("ReadHeader",
+                   "Equipment %d : raw data size found in the header is wrong (%d != %ld)! Using the equipment size instead !",
+                   fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
+         fHeader->fSize = fEnd - fPosition;
+       }
+       fPosition += sizeof(AliRawDataHeader);
+       fHeaderV3 = 0;
+      } else if (version==3) {
+       fHeaderV3 = (AliRawDataHeaderV3*) fPosition;
 #ifndef R__BYTESWAP
-      SwapData((void*) fHeader, (void*) fHeaderSwapped, sizeof(AliRawDataHeader));
-      fHeader=fHeaderSwapped;
+       SwapData((void*) fHeaderV3, (void*) fHeaderSwapped, sizeof(AliRawDataHeaderV3));
+       fHeaderV3=fHeaderSwappedV3;
 #endif
-      if ((fPosition + fHeader->fSize) != fEnd) {
-       if (fHeader->fSize != 0xFFFFFFFF)
-         Warning("ReadHeader",
-                 "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
-                 fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
-       fHeader->fSize = fEnd - fPosition;
+       if ((fPosition + fHeaderV3->fSize) != fEnd) {
+         if (fHeaderV3->fSize != 0xFFFFFFFF)
+           Warning("ReadHeader",
+                   "Equipment %d : raw data size found in the header is wrong (%d != %ld)! Using the equipment size instead !",
+                   fEquipment->GetEquipmentHeader()->GetId(),fHeader->fSize, fEnd - fPosition);
+         fHeaderV3->fSize = fEnd - fPosition;
+       }
+       fPosition += sizeof(AliRawDataHeaderV3);
+       fHeader = 0;
       }
-      fPosition += sizeof(AliRawDataHeader);
     }
 
     if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
@@ -454,7 +498,20 @@ Bool_t AliRawReaderRoot::ReadHeader()
       // check consistency of data size in the header and in the sub event
       if (fPosition + fCount > fEnd) {  
        Error("ReadHeader", "size in data header exceeds event size!");
-       Warning("ReadHeader", "skipping %d bytes", fEnd - fPosition);
+       Warning("ReadHeader", "skipping %ld bytes", fEnd - fPosition);
+       fEquipment->GetEquipmentHeader()->Dump();
+       fCount = 0;
+       fPosition = fEnd;
+       fErrorCode = kErrSize;
+       continue;
+      }
+    } else if (fHeaderV3 && (fHeaderV3->fSize != 0xFFFFFFFF)) {
+      fCount = fHeaderV3->fSize - sizeof(AliRawDataHeaderV3);
+
+      // check consistency of data size in the header and in the sub event
+      if (fPosition + fCount > fEnd) {  
+       Error("ReadHeader", "size in data header exceeds event size!");
+       Warning("ReadHeader", "skipping %ld bytes", fEnd - fPosition);
        fEquipment->GetEquipmentHeader()->Dump();
        fCount = 0;
        fPosition = fEnd;
@@ -515,6 +572,7 @@ Bool_t AliRawReaderRoot::Reset()
   fEquipment = NULL;
   fRawData = NULL;
   fHeader = NULL;
+  fHeaderV3 = NULL;
 
   fCount = 0;
   fPosition = fEnd = NULL;
@@ -528,12 +586,20 @@ Bool_t AliRawReaderRoot::NextEvent()
 
   if (!fBranch) return kFALSE;
 
+  // check if it uses order or not
+  if (fgUseOrder && !fIndex) MakeIndex();
+
   do {
     delete fEvent;
     fEvent = NULL;
     fEventHeader = NULL;
     fBranch->SetAddress(&fEvent);
-    if (fBranch->GetEntry(fEventIndex+1) <= 0)
+    Int_t entryToGet = fEventIndex + 1;
+    if (fgUseOrder 
+       && fIndex 
+       && entryToGet<fBranch->GetEntries()
+       && entryToGet>-1 ) entryToGet = fIndex[entryToGet];
+    if (fBranch->GetEntry(entryToGet) <= 0)
       return kFALSE;
     fEventHeader = fEvent->GetHeader();
     fEventIndex++;
@@ -565,11 +631,19 @@ Bool_t  AliRawReaderRoot::GotoEvent(Int_t event)
 
   if (!fBranch) return kFALSE;
 
+  // check if it uses order or not
+  if (fgUseOrder && !fIndex) MakeIndex();
+
   delete fEvent;
   fEvent = NULL;
   fEventHeader = NULL;
   fBranch->SetAddress(&fEvent);
-  if (fBranch->GetEntry(event) <= 0)
+  Int_t entryToGet = event;
+  if (fgUseOrder 
+      && fIndex 
+      && entryToGet<fBranch->GetEntries()
+      && entryToGet>-1 ) entryToGet = fIndex[entryToGet];
+  if (fBranch->GetEntry(entryToGet) <= 0)
     return kFALSE;
   fEventHeader = fEvent->GetHeader();
   fEventIndex = event;
@@ -619,6 +693,10 @@ Int_t AliRawReaderRoot::CheckData() const
     }
 
     // get the next equipment and raw data
+    if (equipmentIndex >= subEvent->GetNEquipments()) {
+      equipment = NULL;
+      continue;
+    }
     equipment = subEvent->GetEquipment(equipmentIndex++);
     if (!equipment) continue;
     AliRawData* rawData = equipment->GetRawData();
@@ -641,7 +719,7 @@ Int_t AliRawReaderRoot::CheckData() const
       if ((position + header->fSize) != end) {
        if (header->fSize != 0xFFFFFFFF)
          Warning("ReadHeader",
-                 "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
+                 "Equipment %d : raw data size found in the header is wrong (%d != %ld)! Using the equipment size instead !",
                  equipment->GetEquipmentHeader()->GetId(),header->fSize, end - position);
        header->fSize = end - position;
        result |= kErrSize;
@@ -652,3 +730,44 @@ Int_t AliRawReaderRoot::CheckData() const
 
   return result;
 }
+
+AliRawReader* AliRawReaderRoot::CloneSingleEvent() const
+{
+  // Clones the current event and
+  // creates raw-reader for the cloned event
+  // Can be used in order to make asynchronious
+  // access to the current raw data within
+  // several threads (online event display/reco)
+
+  if (fEvent) {
+    // Root formatted raw data
+    AliRawVEvent *gdcRootEvent = (AliRawVEvent*)fEvent->Clone();
+    for (Int_t ldcCounter=0; ldcCounter < gdcRootEvent->GetNSubEvents(); ldcCounter++) {
+      AliRawVEvent *ldcRootEvent = gdcRootEvent->GetSubEvent(ldcCounter);
+      AliRawVEvent *subEvent = fEvent->GetSubEvent(ldcCounter);
+      for (Int_t eqCounter=0; eqCounter < ldcRootEvent->GetNEquipments(); eqCounter++) {
+       AliRawVEquipment *equipment=ldcRootEvent->GetEquipment(eqCounter);
+       AliRawVEquipment *eq = subEvent->GetEquipment(eqCounter);
+       equipment->CloneRawData(eq->GetRawData());
+      }
+    }
+    // Reset original event and newly
+    // produced one
+    gdcRootEvent->GetSubEvent(-1);
+    fEvent->GetSubEvent(-1);
+    return new AliRawReaderRoot(gdcRootEvent);
+  }
+  return NULL;
+}
+
+void AliRawReaderRoot::MakeIndex() {
+  // Make index
+  if (fBranch) {
+    TTree * rawTree = fBranch->GetTree();
+    if (rawTree) {
+      rawTree->BuildIndex("-fEvtHdrs[0].fSize"); // Minus sign to get largest first
+      TTreeIndex * treeInd = (TTreeIndex*)rawTree->GetTreeIndex();
+      if (treeInd) fIndex = treeInd->GetIndex();
+    }
+  }
+}