]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RAW/AliRawReaderRoot.cxx
fix coding convention violations
[u/mrichter/AliRoot.git] / RAW / AliRawReaderRoot.cxx
index 7c87d5379f10f7e454884f50d358e934e62d855d..4f881e20d2836836e20b3e894b158f91dff62f8b 100644 (file)
 //
 ///////////////////////////////////////////////////////////////////////////////
 
+#include <TFile.h>
+#include <TTree.h>
 #include "AliRawReaderRoot.h"
 #include "AliRawEvent.h"
+#include "AliRawEventHeader.h"
+#include "AliRawEquipmentHeader.h"
+#include "AliRawData.h"
 
 
 ClassImp(AliRawReaderRoot)
@@ -32,7 +37,10 @@ AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber)
 // create an object to read digits from the given input file for the
 // event with the given number
 
+  fEvent = NULL;
+  TDirectory* dir = gDirectory;
   fFile = TFile::Open(fileName);
+  dir->cd();
   if (!fFile || !fFile->IsOpen()) {
     Error("AliRawReaderRoot", "could not open file %s", fileName);
     return;
@@ -58,7 +66,7 @@ AliRawReaderRoot::AliRawReaderRoot(const char* fileName, Int_t eventNumber)
   fSubEventIndex = 0;
   fSubEvent = NULL;
   fRawData = NULL;
-  fMiniHeader = NULL;
+  fHeader = NULL;
 
   fCount = 0;
   fPosition = fEnd = NULL;
@@ -74,7 +82,7 @@ AliRawReaderRoot::AliRawReaderRoot(AliRawEvent* event)
   fSubEventIndex = 0;
   fSubEvent = NULL;
   fRawData = NULL;
-  fMiniHeader = NULL;
+  fHeader = NULL;
 
   fCount = 0;
   fPosition = fEnd = NULL;
@@ -91,7 +99,7 @@ AliRawReaderRoot::AliRawReaderRoot(const AliRawReaderRoot& rawReader) :
   fSubEventIndex = rawReader.fSubEventIndex;
   fSubEvent = rawReader.fSubEvent;
   fRawData = rawReader.fRawData;
-  fMiniHeader = rawReader.fMiniHeader;
+  fHeader = rawReader.fHeader;
 
   fCount = rawReader.fCount;
   fPosition = rawReader.fPosition;
@@ -168,6 +176,14 @@ const UInt_t* AliRawReaderRoot::GetAttributes() const
   return fEvent->GetHeader()->GetTypeAttribute();
 }
 
+UInt_t AliRawReaderRoot::GetLDCId() const
+{
+// get the LDC Id from the event header
+
+  if (!fEvent || !fEvent->GetSubEvent(fSubEventIndex)) return 0;
+  return fEvent->GetSubEvent(fSubEventIndex)->GetHeader()->GetLDCId();
+}
+
 UInt_t AliRawReaderRoot::GetGDCId() const
 {
 // get the GDC Id from the event header
@@ -177,36 +193,116 @@ UInt_t AliRawReaderRoot::GetGDCId() const
 }
 
 
-Bool_t AliRawReaderRoot::ReadMiniHeader()
+Int_t AliRawReaderRoot::GetEquipmentSize() const
+{
+// get the size of the equipment
+
+  if (!fEvent || !fEvent->GetEquipmentHeader()) return 0;
+  return fEvent->GetEquipmentHeader()->GetEquipmentSize();
+}
+
+Int_t AliRawReaderRoot::GetEquipmentType() const
+{
+// get the type from the equipment header
+
+  if (!fEvent || !fEvent->GetEquipmentHeader()) return -1;
+  return fEvent->GetEquipmentHeader()->GetEquipmentType();
+}
+
+Int_t AliRawReaderRoot::GetEquipmentId() const
+{
+// get the ID from the equipment header
+
+  if (!fEvent || !fEvent->GetEquipmentHeader()) return -1;
+  return fEvent->GetEquipmentHeader()->GetId();
+}
+
+const UInt_t* AliRawReaderRoot::GetEquipmentAttributes() const
+{
+// get the attributes from the equipment header
+
+  if (!fEvent || !fEvent->GetEquipmentHeader()) return NULL;
+  return fEvent->GetEquipmentHeader()->GetTypeAttribute();
+}
+
+Int_t AliRawReaderRoot::GetEquipmentElementSize() const
 {
-// read a mini header at the current position
-// returns kFALSE if the mini header could not be read
+// get the basic element size from the equipment header
 
+  if (!fEvent || !fEvent->GetEquipmentHeader()) return 0;
+  return fEvent->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;
+
   do {
-    if (fCount > 0) fPosition += fCount;      // skip payload if event was not selected
-    if (!fSubEvent || (fPosition >= fEnd)) {  // new sub event
+    // 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)) {
+
+      // 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;
+      }
+
       fRawData = fSubEvent->GetRawData();
       fCount = 0;
       fPosition = (UChar_t*) fRawData->GetBuffer();
       fEnd = ((UChar_t*) fRawData->GetBuffer()) + fRawData->GetSize();
     }
-    if (fPosition >= fEnd) continue;          // no data left in the payload
-    if (fPosition + sizeof(AliMiniHeader) > fEnd) {
-      Error("ReadMiniHeader", "could not read data!");
-      return kFALSE;
+
+    // continue with the next sub event if no data left in the payload
+    if (fPosition >= fEnd) continue;
+
+    // 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);
+      fSubEvent->GetHeader()->Dump();
+      fCount = 0;
+      fPosition = fEnd;
+      fErrorCode = kErrNoDataHeader;
+      continue;
     }
-    fMiniHeader = (AliMiniHeader*) fPosition;
-    fPosition += sizeof(AliMiniHeader);
-    CheckMiniHeader();
-    fCount = fMiniHeader->fSize;
-    if (fPosition + fCount > fEnd) {  // check data size in mini header and sub event
-      Error("ReadMiniHeader", "size in mini header exceeds event size!");
-      fMiniHeader->fSize = fCount = fEnd - fPosition;
+
+    // "read" the data header
+    fHeader = (AliRawDataHeader*) fPosition;
+    fPosition += sizeof(AliRawDataHeader);
+    if (fHeader->fSize != 0xFFFFFFFF) {
+      fCount = fHeader->fSize - sizeof(AliRawDataHeader);
+    } else {
+      fCount = fEnd - fPosition;
+    }
+
+    // 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);
+      fSubEvent->GetHeader()->Dump();
+      fCount = 0;
+      fPosition = fEnd;
+      fErrorCode = kErrSize;
+      continue;
     }
+
   } while (!IsSelected());
+
   return kTRUE;
 }
 
@@ -215,8 +311,9 @@ Bool_t AliRawReaderRoot::ReadNextData(UChar_t*& data)
 // reads the next payload at the current position
 // returns kFALSE if the data could not be read
 
+  fErrorCode = 0;
   while (fCount == 0) {
-    if (!ReadMiniHeader()) return kFALSE;
+    if (!ReadHeader()) return kFALSE;
   }
   data = fPosition;
   fPosition += fCount;  
@@ -229,8 +326,10 @@ Bool_t AliRawReaderRoot::ReadNext(UChar_t* data, Int_t size)
 // reads the next block of data at the current position
 // returns kFALSE if the data could not be read
 
+  fErrorCode = 0;
   if (fPosition + size > fEnd) {
     Error("ReadNext", "could not read data!");
+    fErrorCode = kErrOutOfBounds;
     return kFALSE;
   }
   memcpy(data, fPosition, size);
@@ -247,10 +346,68 @@ Bool_t AliRawReaderRoot::Reset()
   fSubEventIndex = 0;
   fSubEvent = NULL;
   fRawData = NULL;
-  fMiniHeader = NULL;
+  fHeader = NULL;
 
   fCount = 0;
   fPosition = fEnd = NULL;
   return kTRUE;
 }
 
+
+Int_t AliRawReaderRoot::CheckData() const
+{
+// check the consistency of the data
+
+  if (!fEvent) return 0;
+
+  AliRawEvent* subEvent = NULL;
+  Int_t subEventIndex = 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)) {
+
+      // check for end of event data
+      if (subEventIndex >= fEvent->GetNSubEvents()) return result;
+      subEvent = fEvent->GetSubEvent(subEventIndex++);
+
+      // check the magic word of the sub event
+      if (!fSubEvent->GetHeader()->IsValid()) {
+       result |= kErrMagic;
+       return result;
+      }
+
+      AliRawData* rawData = subEvent->GetRawData();
+      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 data header
+    if (position + sizeof(AliRawDataHeader) > end) {
+      result |= kErrNoDataHeader;
+      position = end;
+      continue;
+    }
+
+    // check consistency of data size in the header and in the sub event
+    AliRawDataHeader* header = (AliRawDataHeader*) position;
+    if (fHeader->fSize != 0xFFFFFFFF) {
+      if (position + header->fSize > end) {
+       result |= kErrSize;
+       position = end;
+      } else {
+       position += header->fSize;
+      }
+    } else {
+      position = end;
+    }
+  };
+
+  return result;
+}