]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RAW/AliRawReaderDate.cxx
ALIROOT-5628 AliRawReaderDate cannot decode CDH v3
[u/mrichter/AliRoot.git] / RAW / AliRawReaderDate.cxx
index 0a07ae290b8b108c0923c3373b91183684dc6723..9df44b55b2a30d46afa3be3663e6927820d95d9a 100644 (file)
@@ -34,18 +34,18 @@ ClassImp(AliRawReaderDate)
 
 AliRawReaderDate::AliRawReaderDate(
 #ifdef ALI_DATE
-                                  void* event
+                                  void* event, Bool_t owner
 #else
-                                  void* /* event */
+                                  void* /* event */, Bool_t owner
 #endif
                                   ) :
-  fRequireHeader(kTRUE),
   fFile(NULL),
   fEvent(NULL),
   fSubEvent(NULL),
   fEquipment(NULL),
   fPosition(NULL),
-  fEnd(NULL)
+  fEnd(NULL),
+  fOwner(owner)
 {
 // create an object to read digits from the given date event
 
@@ -64,13 +64,13 @@ AliRawReaderDate::AliRawReaderDate(
                                   Int_t /*eventNumber*/
 #endif
                                   ) :
-  fRequireHeader(kTRUE),
   fFile(NULL),
   fEvent(NULL),
   fSubEvent(NULL),
   fEquipment(NULL),
   fPosition(NULL),
-  fEnd(NULL)
+  fEnd(NULL),
+  fOwner(kTRUE)
 {
 // create an object to read digits from the given date event
 
@@ -78,6 +78,7 @@ AliRawReaderDate::AliRawReaderDate(
   fFile = fopen(fileName, "rb");
   if (!fFile) {
     Error("AliRawReaderDate", "could not open file %s", fileName);
+    fIsValid = kFALSE;
     return;
   }
   if (eventNumber < 0) return;
@@ -87,7 +88,7 @@ AliRawReaderDate::AliRawReaderDate(
   while (fread(&header, 1, headerSize, fFile) == headerSize) {
     if (eventNumber == 0) {
       UChar_t* buffer = new UChar_t[header.eventSize];
-      fseek(fFile, -headerSize, SEEK_CUR);
+      fseek(fFile, -(long)headerSize, SEEK_CUR);
       if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) break;
       fEvent = (eventHeaderStruct*) buffer;
       break;
@@ -103,13 +104,13 @@ AliRawReaderDate::AliRawReaderDate(
 
 AliRawReaderDate::AliRawReaderDate(const AliRawReaderDate& rawReader) :
   AliRawReader(rawReader),
-  fRequireHeader(rawReader.fRequireHeader),
   fFile(rawReader.fFile),
   fEvent(rawReader.fEvent),
   fSubEvent(rawReader.fSubEvent),
   fEquipment(rawReader.fEquipment),
   fPosition(rawReader.fPosition),
-  fEnd(rawReader.fEnd)
+  fEnd(rawReader.fEnd),
+  fOwner(rawReader.fOwner)
 
 {
 // copy constructor
@@ -131,8 +132,8 @@ AliRawReaderDate::~AliRawReaderDate()
 // destructor
 
 #ifdef ALI_DATE
+  if (fEvent && fOwner) delete[] fEvent;
   if (fFile) {
-    delete[] fEvent;
     fclose(fFile);
   }
 #endif
@@ -247,17 +248,28 @@ UInt_t AliRawReaderDate::GetGDCId() const
 #endif
 }
 
+UInt_t AliRawReaderDate::GetTimestamp() const
+{
+// get the timestamp from the event header
+
+#ifdef ALI_DATE
+  if (!fEvent) return 0;
+  return fEvent->eventTimestamp;
+#else
+  return 0;
+#endif
+}
 
 Int_t AliRawReaderDate::GetEquipmentSize() const
 {
-// get the size of the equipment (without header)
+// get the size of the equipment (including the header)
 
 #ifdef ALI_DATE
   if (!fEquipment) return 0;
   if (fSubEvent->eventVersion <= 0x00030001) {
-    return fEquipment->equipmentSize;
+    return fEquipment->equipmentSize + sizeof(equipmentHeaderStruct);
   } else {
-    return fEquipment->equipmentSize - sizeof(equipmentHeaderStruct);
+    return fEquipment->equipmentSize;
   }
 #else
   return 0;
@@ -312,6 +324,16 @@ Int_t AliRawReaderDate::GetEquipmentElementSize() const
 #endif
 }
 
+Int_t AliRawReaderDate::GetEquipmentHeaderSize() const
+{
+  // Get the equipment header size
+  // 28 bytes by default
+#ifdef ALI_DATE
+  return sizeof(equipmentHeaderStruct);
+#else
+  return 0;
+#endif
+}
 
 Bool_t AliRawReaderDate::ReadHeader()
 {
@@ -405,7 +427,34 @@ Bool_t AliRawReaderDate::ReadHeader()
 
       // "read" the data header
       fHeader = (AliRawDataHeader*) fPosition;
-      fPosition += sizeof(AliRawDataHeader);
+      // Now check the version of the header
+      UChar_t version = 2;
+      if (fHeader) version=fHeader->GetVersion();
+
+      if (version==2) {
+       if ((fPosition + fHeader->fSize) != fEnd) {
+         if ((fHeader->fSize != 0xFFFFFFFF) &&
+             (fEquipment->equipmentId != 4352))
+           Warning("ReadHeader",
+                   "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
+                   fHeader->fSize, fEnd - fPosition);
+         fHeader->fSize = fEnd - fPosition;
+       }
+       fPosition += sizeof(AliRawDataHeader);
+       fHeaderV3 = 0;
+      } else if (version==3) {
+       fHeaderV3 = (AliRawDataHeaderV3*) fPosition;
+       if ((fPosition + fHeaderV3->fSize) != fEnd) {
+         if ((fHeaderV3->fSize != 0xFFFFFFFF) &&
+             (fEquipment->equipmentId != 4352))
+           Warning("ReadHeader",
+                   "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
+                   fHeaderV3->fSize, fEnd - fPosition);
+         fHeaderV3->fSize = fEnd - fPosition;
+       }
+       fPosition += sizeof(AliRawDataHeaderV3);
+       fHeader = 0;
+      }
     }
 
     if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
@@ -425,6 +474,23 @@ Bool_t AliRawReaderDate::ReadHeader()
        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 %d bytes\n"
+               " run: %d  event: %d %d  LDC: %d  GDC: %d\n", 
+               fEnd - fPosition, fSubEvent->eventRunNb, 
+               fSubEvent->eventId[0], fSubEvent->eventId[1],
+               fSubEvent->eventLdcId, fSubEvent->eventGdcId);
+       fCount = 0;
+       fPosition = fEnd;
+       fErrorCode = kErrSize;
+       continue;
+      }
+
     } else {
       fCount = fEnd - fPosition;
     }
@@ -480,6 +546,8 @@ Bool_t AliRawReaderDate::Reset()
 #endif
   fCount = 0;
   fPosition = fEnd = NULL;
+  fHeader=NULL;
+  fHeaderV3=NULL;
   return kTRUE;
 }
 
@@ -489,7 +557,14 @@ Bool_t AliRawReaderDate::NextEvent()
 // go to the next event in the date file
 
 #ifdef ALI_DATE
-  if (!fFile) return kFALSE;
+  if (!fFile) {
+    if (fEventNumber < 0 && fEvent) {
+      fEventNumber++;
+      return kTRUE;
+    }
+    else
+      return kFALSE;
+  }
 
   Reset();
   eventHeaderStruct header;
@@ -503,13 +578,14 @@ Bool_t AliRawReaderDate::NextEvent()
       continue;
     }
     UChar_t* buffer = new UChar_t[header.eventSize];
-    fseek(fFile, -headerSize, SEEK_CUR);
+    fseek(fFile, -(long)headerSize, SEEK_CUR);
     if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) {
       Error("NextEvent", "could not read event from file");
       delete[] buffer;
       break;
     }
     fEvent = (eventHeaderStruct*) buffer;
+    fEventNumber++;
     return kTRUE;
   };
 
@@ -523,9 +599,10 @@ Bool_t AliRawReaderDate::RewindEvents()
 {
 // go back to the beginning of the date file
 
-  if (!fFile) return kFALSE;
+  if (fFile)
+    fseek(fFile, 0, SEEK_SET);
 
-  fseek(fFile, 0, SEEK_SET);
+  fEventNumber = -1;
   return Reset();
 }
 
@@ -575,27 +652,51 @@ Int_t AliRawReaderDate::CheckData() const
     // continue with the next sub event if no data left in the payload
     if (position >= end) continue;
 
+    if (fRequireHeader) {
     // check that there are enough bytes left for the data header
-    if (position + sizeof(AliRawDataHeader) > end) {
-      result |= kErrNoDataHeader;
-      position = end;
-      continue;
-    }
+      if (position + sizeof(AliRawDataHeader) > end) {
+       result |= kErrNoDataHeader;
+       position = end;
+       continue;
+      }
 
-    // check consistency of data size in the data header and in the sub event
-    AliRawDataHeader* header = (AliRawDataHeader*) position;
-    if (header->fSize != 0xFFFFFFFF) {
-      if (position + header->fSize > end) {
+      // check consistency of data size in the data header and in the sub event
+      AliRawDataHeader* header = (AliRawDataHeader*) position;
+      if ((position + header->fSize) != end) {
+       if (header->fSize != 0xFFFFFFFF)
+         Warning("ReadHeader",
+                 "raw data size found in the header is wrong (%d != %d)! Using the equipment size instead !",
+                 header->fSize, end - position);
+       header->fSize = end - position;
        result |= kErrSize;
-       position = end;
-      } else {
-       position += header->fSize;
       }
-    } else {
-      position = end;
     }
+    position = end;
   };
 
 #endif
   return 0;
 }
+
+AliRawReader* AliRawReaderDate::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)
+
+#ifdef ALI_DATE
+  if (fEvent) {
+    UInt_t evSize = fEvent->eventSize;
+    if (evSize) {
+      UChar_t *newEvent = new UChar_t[evSize];
+      memcpy(newEvent,fEvent,evSize);
+      return new AliRawReaderDate((void *)newEvent,kTRUE);
+    }
+  }
+#else
+  Fatal("AliRawReaderDate", "this class was compiled without DATE");
+#endif
+  return NULL;
+}