]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RAW/AliRawReaderRoot.cxx
2D Centrality files
[u/mrichter/AliRoot.git] / RAW / AliRawReaderRoot.cxx
index 2e96838cc9e019d5915c8a881657a1cd6bb062b6..1fde2b8018387b002b2f991c708449121463234b 100644 (file)
@@ -21,7 +21,7 @@
 ///
 /// The root file is expected to contain a tree of name "RAW" with
 /// a branch of name "rawevent" which contains objects of type
-/// AliRawEvent.
+/// AliRawVEvent.
 /// 
 /// 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 "AliRawVEvent.h"
 #include "AliRawEventHeaderBase.h"
-#include "AliRawEquipment.h"
+#include "AliRawVEquipment.h"
 #include "AliRawEquipmentHeader.h"
 #include "AliRawData.h"
 
 
 ClassImp(AliRawReaderRoot)
 
+AliRawReaderRoot::AliRawReaderRoot() :
+  fFile(NULL),
+  fBranch(NULL),
+  fEventIndex(-1),
+  fEvent(NULL),
+  fEventHeader(NULL),
+  fSubEventIndex(0),
+  fSubEvent(NULL),
+  fEquipmentIndex(0),
+  fEquipment(NULL),
+  fRawData(NULL),
+  fPosition(NULL),
+  fEnd(NULL)
+{
+// default constructor
+
+}
 
 AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
   fFile(NULL),
   fBranch(NULL),
   fEventIndex(eventNumber),
   fEvent(NULL),
+  fEventHeader(NULL),
   fSubEventIndex(0),
   fSubEvent(NULL),
   fEquipmentIndex(0),
@@ -62,34 +80,39 @@ AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber) :
   dir->cd();
   if (!fFile || !fFile->IsOpen()) {
     Error("AliRawReaderRoot", "could not open file %s", fileName);
+    fIsValid = kFALSE;
     return;
   }
   TTree* tree = (TTree*) fFile->Get("RAW");
   if (!tree) {
     Error("AliRawReaderRoot", "no raw data tree found");
+    fIsValid = kFALSE;
     return;
   }
   fBranch = tree->GetBranch("rawevent");
   if (!fBranch) {
     Error("AliRawReaderRoot", "no raw data branch found");
+    fIsValid = kFALSE;
     return;
   }
 
-  fEvent = new AliRawEvent;
   fBranch->SetAddress(&fEvent);
   if (fEventIndex >= 0) {
     if (fBranch->GetEntry(fEventIndex) <= 0) {
       Error("AliRawReaderRoot", "no event with number %d found", fEventIndex);
+      fIsValid = kFALSE;
       return;
     }
+    fEventHeader = fEvent->GetHeader();
   }
 }
 
-AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event) :
+AliRawReaderRoot::AliRawReaderRoot(AliRawVEvent* event) :
   fFile(NULL),
   fBranch(NULL),
   fEventIndex(-1),
   fEvent(event),
+  fEventHeader(event->GetHeader()),
   fSubEventIndex(0),
   fSubEvent(NULL),
   fEquipmentIndex(0),
@@ -99,7 +122,7 @@ AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event) :
   fEnd(NULL)
 {
 // create an object to read digits from the given raw event
-
+  if (!fEvent) fIsValid = kFALSE;
 }
 
 AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
@@ -108,6 +131,7 @@ AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
   fBranch(NULL),
   fEventIndex(rawReader.fEventIndex),
   fEvent(NULL),
+  fEventHeader(NULL),
   fSubEventIndex(rawReader.fSubEventIndex),
   fSubEvent(NULL),
   fEquipmentIndex(rawReader.fEquipmentIndex),
@@ -125,30 +149,35 @@ AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
     if (!fFile || !fFile->IsOpen()) {
       Error("AliRawReaderRoot", "could not open file %s", 
            rawReader.fFile->GetName());
+      fIsValid = kFALSE;
       return;
     }
     TTree* tree = (TTree*) fFile->Get("RAW");
     if (!tree) {
       Error("AliRawReaderRoot", "no raw data tree found");
+      fIsValid = kFALSE;
       return;
     }
     fBranch = tree->GetBranch("rawevent");
     if (!fBranch) {
       Error("AliRawReaderRoot", "no raw data branch found");
+      fIsValid = kFALSE;
       return;
     }
 
-    fEvent = new AliRawEvent;
     fBranch->SetAddress(&fEvent);
     if (fEventIndex >= 0) {
       if (fBranch->GetEntry(fEventIndex) <= 0) {
        Error("AliRawReaderRoot", "no event with number %d found", 
              fEventIndex);
+       fIsValid = kFALSE;
        return;
       }
+      fEventHeader = fEvent->GetHeader();
     }
   } else {
     fEvent = rawReader.fEvent;
+    fEventHeader = rawReader.fEventHeader;
   }
 
   if (fSubEventIndex > 0) {
@@ -190,56 +219,55 @@ const AliRawEventHeaderBase* AliRawReaderRoot::GetEventHeader() const
 {
   // Get the even header
   // Return NULL in case of failure
-  if (!fEvent) return NULL;
-  return fEvent->GetHeader();
+  return fEventHeader;
 }
 
 UInt_t AliRawReaderRoot::GetType() const
 {
 // get the type from the event header
 
-  if (!fEvent) return 0;
-  return fEvent->GetHeader()->Get("Type");
+  if (!fEventHeader) return 0;
+  return fEventHeader->Get("Type");
 }
 
 UInt_t AliRawReaderRoot::GetRunNumber() const
 {
 // get the run number from the event header
 
-  if (!fEvent) return 0;
-  return fEvent->GetHeader()->Get("RunNb");
+  if (!fEventHeader) return 0;
+  return fEventHeader->Get("RunNb");
 }
 
 const UInt_t* AliRawReaderRoot::GetEventId() const
 {
 // get the event id from the event header
 
-  if (!fEvent) return NULL;
-  return fEvent->GetHeader()->GetP("Id");
+  if (!fEventHeader) return NULL;
+  return fEventHeader->GetP("Id");
 }
 
 const UInt_t* AliRawReaderRoot::GetTriggerPattern() const
 {
 // get the trigger pattern from the event header
 
-  if (!fEvent) return NULL;
-  return fEvent->GetHeader()->GetP("TriggerPattern");
+  if (!fEventHeader) return NULL;
+  return fEventHeader->GetP("TriggerPattern");
 }
 
 const UInt_t* AliRawReaderRoot::GetDetectorPattern() const
 {
 // get the detector pattern from the event header
 
-  if (!fEvent) return NULL;
-  return fEvent->GetHeader()->GetP("DetectorPattern");
+  if (!fEventHeader) return NULL;
+  return fEventHeader->GetP("DetectorPattern");
 }
 
 const UInt_t* AliRawReaderRoot::GetAttributes() const
 {
 // get the type attributes from the event header
 
-  if (!fEvent) return NULL;
-  return fEvent->GetHeader()->GetP("TypeAttribute");
+  if (!fEventHeader) return NULL;
+  return fEventHeader->GetP("TypeAttribute");
 }
 
 const UInt_t* AliRawReaderRoot::GetSubEventAttributes() const
@@ -262,14 +290,14 @@ UInt_t AliRawReaderRoot::GetGDCId() const
 {
 // get the GDC Id from the event header
 
-  if (!fEvent) return 0;
-  return fEvent->GetHeader()->Get("GdcId");
+  if (!fEventHeader) return 0;
+  return fEventHeader->Get("GdcId");
 }
 
 UInt_t AliRawReaderRoot::GetTimestamp() const
 {
-  if (!fEvent) return 0;
-  return fEvent->GetHeader()->Get("Timestamp");
+  if (!fEventHeader) return 0;
+  return fEventHeader->Get("Timestamp");
 }
 
 Int_t AliRawReaderRoot::GetEquipmentSize() const
@@ -321,18 +349,10 @@ Int_t AliRawReaderRoot::GetEquipmentHeaderSize() const
 }
 
 // _________________________________________________________________________
-UInt_t AliRawReaderRoot::SwapWord(UInt_t x) const
-{
-   // Swap the endianess of the integer value 'x'
-
-   return (((x & 0x000000ffU) << 24) | ((x & 0x0000ff00U) <<  8) |
-           ((x & 0x00ff0000U) >>  8) | ((x & 0xff000000U) >> 24));
-}
-
 void AliRawReaderRoot::SwapData(const void* inbuf, const void* outbuf, UInt_t size) {
   // The method swaps the contents of the
   // raw-data event header
-  UInt_t  intCount = (size+sizeof(UInt_t)-1)/sizeof(UInt_t);
+  UInt_t  intCount = (size+3)/sizeof(UInt_t);
 
   UInt_t* buf = (UInt_t*) inbuf;    // temporary integers buffer
   for (UInt_t i=0; i<intCount; i++, buf++) {
@@ -382,6 +402,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()) {
@@ -404,7 +428,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;
@@ -419,9 +443,10 @@ Bool_t AliRawReaderRoot::ReadHeader()
       fHeader=fHeaderSwapped;
 #endif
       if ((fPosition + fHeader->fSize) != fEnd) {
-       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);
+       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);
@@ -433,7 +458,7 @@ 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;
@@ -476,60 +501,7 @@ Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
     fErrorCode = kErrOutOfBounds;
     return kFALSE;
   }
-#ifndef R__BYTESWAP
-  // relative position in the raw data buffer
-  UInt_t pos  = (UInt_t) (fPosition-(UChar_t*)fRawData->GetBuffer());  
-
-  UInt_t gapL = pos%sizeof(UInt_t);
-  UInt_t gapR = (pos+size)%sizeof(UInt_t);
-  if ( gapR > 0 ) gapR = sizeof(UInt_t) - gapR;
-
-  UChar_t* firstWord = fPosition - gapL;          // pointer to the begin of the 1st word
-  UChar_t* lastWord  = fPosition + size + gapR;   // pointer to the begin of the 1st word following the buffer and not included
-
-  // Loop through the all words and write each of them swapped
-  UInt_t   bytesWritten = 0;
-  while (firstWord < lastWord) 
-  {
-      UInt_t* value   = (UInt_t*) firstWord;
-      UInt_t valueInv = SwapWord( *value );
-
-      if ( (gapL+size)<=sizeof(UInt_t) ) 
-      {
-         // invert only byte(s) within the 1st (and unique) word 
-         memcpy((UInt_t*)(data+bytesWritten), &valueInv+gapL, size);
-         bytesWritten += size;
-      }
-      else 
-      {
-         if ( gapL>0 ) 
-         {
-            // 1st word unaligned
-            memcpy((UInt_t*)(data+bytesWritten), &valueInv+gapL, sizeof(UInt_t)-gapL);
-            bytesWritten += sizeof(UInt_t) - gapL;
-         }
-         else 
-         {
-            if ( gapR>0 ) 
-            {
-               // last word unaligned
-               memcpy((UInt_t*)(data+bytesWritten), &valueInv, sizeof(UInt_t)-gapR);
-               bytesWritten += sizeof(UInt_t) - gapR;
-            }
-            else
-            { 
-               // no unalignements
-               memcpy((UInt_t*)(data+bytesWritten), &valueInv, sizeof(UInt_t));
-               bytesWritten += sizeof(UInt_t);
-            }            
-         }
-      }
-
-      firstWord += sizeof(UInt_t);
-  }
-#else
   memcpy(data, fPosition, size);
-#endif
 
   fPosition += size;
   fCount -= size;
@@ -562,10 +534,12 @@ Bool_t AliRawReaderRoot::NextEvent()
 
   do {
     delete fEvent;
-    fEvent = new AliRawEvent;
+    fEvent = NULL;
+    fEventHeader = NULL;
     fBranch->SetAddress(&fEvent);
     if (fBranch->GetEntry(fEventIndex+1) <= 0)
       return kFALSE;
+    fEventHeader = fEvent->GetHeader();
     fEventIndex++;
   } while (!IsEventSelected());
   fEventNumber++;
@@ -580,12 +554,42 @@ Bool_t AliRawReaderRoot::RewindEvents()
 
   fEventIndex = -1;
   delete fEvent;
-  fEvent = new AliRawEvent;
+  fEvent = NULL;
+  fEventHeader = NULL;
   fBranch->SetAddress(&fEvent);
   fEventNumber = -1;
   return Reset();
 }
 
+Bool_t  AliRawReaderRoot::GotoEvent(Int_t event)
+{
+  // go to a particular event
+  // Uses the absolute event index inside the
+  // raw-data file
+
+  if (!fBranch) return kFALSE;
+
+  delete fEvent;
+  fEvent = NULL;
+  fEventHeader = NULL;
+  fBranch->SetAddress(&fEvent);
+  if (fBranch->GetEntry(event) <= 0)
+    return kFALSE;
+  fEventHeader = fEvent->GetHeader();
+  fEventIndex = event;
+  fEventNumber++;
+  return Reset();
+}
+
+Int_t AliRawReaderRoot::GetNumberOfEvents() const
+{
+  // Get the total number of events in
+  // the raw-data tree
+
+  if (!fBranch) return -1;
+
+  return fBranch->GetEntries();
+}
 
 Int_t AliRawReaderRoot::CheckData() const
 {
@@ -593,9 +597,9 @@ Int_t AliRawReaderRoot::CheckData() const
 
   if (!fEvent) return 0;
 
-  AliRawEvent* subEvent = NULL;
+  AliRawVEvent* subEvent = NULL;
   Int_t subEventIndex = 0;
-  AliRawEquipment* equipment = NULL;
+  AliRawVEquipment* equipment = NULL;
   Int_t equipmentIndex = 0;
   UChar_t* position = 0;
   UChar_t* end = 0;
@@ -619,6 +623,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();
@@ -639,9 +647,10 @@ Int_t AliRawReaderRoot::CheckData() const
       // check consistency of data size in the header and in the equipment
       AliRawDataHeader* header = (AliRawDataHeader*) position;
       if ((position + header->fSize) != end) {
-       Warning("ReadHeader",
-               "Equipment %d : raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
-               equipment->GetEquipmentHeader()->GetId(),header->fSize, end - position);
+       if (header->fSize != 0xFFFFFFFF)
+         Warning("ReadHeader",
+                 "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;
       }
@@ -651,3 +660,32 @@ 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;
+}