]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RAW/AliRawReaderDate.cxx
Cdf adapted to the common debug option
[u/mrichter/AliRoot.git] / RAW / AliRawReaderDate.cxx
index 608222e855edbac2bc1aa4f0fd1e4721d32f246d..8553e19aace7034412331a7cbee98c95f3184880 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
+/* $Id$ */
+
 ///////////////////////////////////////////////////////////////////////////////
-//
-// This is a class for reading a raw data from a date event and providing
-// information about digits
-//
+///
+/// This is a class for reading raw data from a date file or event.
+///
+/// The AliRawReaderDate is constructed either with a pointer to a
+/// date event or with a file name and an event number.
+///
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "AliRawReaderDate.h"
@@ -30,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
                                   ) :
-  fRequireMiniHeader(kTRUE),
+  fFile(NULL),
   fEvent(NULL),
   fSubEvent(NULL),
   fEquipment(NULL),
-  fIsOwner(kFALSE),
   fPosition(NULL),
-  fEnd(NULL)
+  fEnd(NULL),
+  fOwner(owner)
 {
 // create an object to read digits from the given date event
 
@@ -60,37 +64,38 @@ AliRawReaderDate::AliRawReaderDate(
                                   Int_t /*eventNumber*/
 #endif
                                   ) :
-  fRequireMiniHeader(kTRUE),
+  fFile(NULL),
   fEvent(NULL),
   fSubEvent(NULL),
   fEquipment(NULL),
-  fIsOwner(kFALSE),
   fPosition(NULL),
-  fEnd(NULL)
+  fEnd(NULL),
+  fOwner(kTRUE)
 {
 // create an object to read digits from the given date event
 
 #ifdef ALI_DATE
-  FILE* file = fopen(fileName, "rb");
-  if (!file) {
+  fFile = fopen(fileName, "rb");
+  if (!fFile) {
     Error("AliRawReaderDate", "could not open file %s", fileName);
+    fIsValid = kFALSE;
     return;
   }
+  if (eventNumber < 0) return;
+
   eventHeaderStruct header;
   UInt_t headerSize = sizeof(eventHeaderStruct);
-  while (fread(&header, 1, headerSize, file) == headerSize) {
+  while (fread(&header, 1, headerSize, fFile) == headerSize) {
     if (eventNumber == 0) {
       UChar_t* buffer = new UChar_t[header.eventSize];
-      fseek(file, -headerSize, SEEK_CUR);
-      if (fread(buffer, 1, header.eventSize, file) != header.eventSize) break;
+      fseek(fFile, -(long)headerSize, SEEK_CUR);
+      if (fread(buffer, 1, header.eventSize, fFile) != header.eventSize) break;
       fEvent = (eventHeaderStruct*) buffer;
-      fIsOwner = kTRUE;
       break;
     }
-    fseek(file, header.eventSize-headerSize, SEEK_CUR);
+    fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
     eventNumber--;
   }
-  fclose(file);
 
 #else
   Fatal("AliRawReaderDate", "this class was compiled without DATE");
@@ -99,26 +104,26 @@ AliRawReaderDate::AliRawReaderDate(
 
 AliRawReaderDate::AliRawReaderDate(const AliRawReaderDate& rawReader) :
   AliRawReader(rawReader),
-  fRequireMiniHeader(rawReader.fRequireMiniHeader),
+  fFile(rawReader.fFile),
   fEvent(rawReader.fEvent),
   fSubEvent(rawReader.fSubEvent),
   fEquipment(rawReader.fEquipment),
-  fIsOwner(kFALSE),
   fPosition(rawReader.fPosition),
-  fEnd(rawReader.fEnd)
+  fEnd(rawReader.fEnd),
+  fOwner(rawReader.fOwner)
 
 {
 // copy constructor
 
+  Fatal("AliRawReaderDate", "copy constructor not implemented");
 }
 
 AliRawReaderDate& AliRawReaderDate::operator = (const AliRawReaderDate& 
-                                               rawReader)
+                                               /*rawReader*/)
 {
 // assignment operator
 
-  this->~AliRawReaderDate();
-  new(this) AliRawReaderDate(rawReader);
+  Fatal("operator =", "assignment operator not implemented");
   return *this;
 }
 
@@ -127,7 +132,10 @@ AliRawReaderDate::~AliRawReaderDate()
 // destructor
 
 #ifdef ALI_DATE
-  if (fIsOwner) delete[] fEvent;
+  if (fEvent && fOwner) delete[] fEvent;
+  if (fFile) {
+    fclose(fFile);
+  }
 #endif
 }
 
@@ -204,6 +212,18 @@ const UInt_t* AliRawReaderDate::GetAttributes() const
 #endif
 }
 
+const UInt_t* AliRawReaderDate::GetSubEventAttributes() const
+{
+// get the type attributes from the sub event header
+
+#ifdef ALI_DATE
+  if (!fSubEvent) return NULL;
+  return fSubEvent->eventTypeAttribute;
+#else
+  return NULL;
+#endif
+}
+
 UInt_t AliRawReaderDate::GetLDCId() const
 {
 // get the LDC Id from the event header
@@ -228,14 +248,29 @@ 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
+// get the size of the equipment (including the header)
 
 #ifdef ALI_DATE
   if (!fEquipment) return 0;
-  return fEquipment->equipmentSize;
+  if (fSubEvent->eventVersion <= 0x00030001) {
+    return fEquipment->equipmentSize + sizeof(equipmentHeaderStruct);
+  } else {
+    return fEquipment->equipmentSize;
+  }
 #else
   return 0;
 #endif
@@ -289,15 +324,26 @@ 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()
 {
-// read a mini header at the current position
-// returns kFALSE if the mini header could not be read
+// read a data header at the current position
+// returns kFALSE if the data header could not be read
 
   fErrorCode = 0;
 
 #ifdef ALI_DATE
+  fHeader = NULL;
   if (!fEvent) return kFALSE;
   // check whether there are sub events
   if (fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
@@ -308,6 +354,7 @@ Bool_t AliRawReaderDate::ReadHeader()
 
     // get the first or the next equipment if at the end of an equipment
     if (!fEquipment || (fPosition >= fEnd)) {
+      fEquipment = NULL;
 
       // get the first or the next sub event if at the end of a sub event
       if (!fSubEvent || 
@@ -315,7 +362,10 @@ Bool_t AliRawReaderDate::ReadHeader()
 
        // check for end of event data
        if (fPosition >= ((UChar_t*)fEvent)+fEvent->eventSize) return kFALSE;
-       if (fSubEvent) {
+        if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute, 
+                                   ATTR_SUPER_EVENT)) {
+         fSubEvent = fEvent;   // no super event
+       } else if (fSubEvent) {
          fSubEvent = (eventHeaderStruct*) (((UChar_t*)fSubEvent) + 
                                            fSubEvent->eventSize);
        } else {
@@ -334,6 +384,13 @@ Bool_t AliRawReaderDate::ReadHeader()
          return kFALSE;
        }
 
+       // continue if no data in the subevent
+       if (fSubEvent->eventSize == fSubEvent->eventHeadSize) {
+         fPosition = fEnd = ((UChar_t*)fSubEvent) + fSubEvent->eventSize;
+         fCount = 0;
+         continue;
+       }
+
        fEquipment = (equipmentHeaderStruct*)
          (((UChar_t*)fSubEvent) + fSubEvent->eventHeadSize);
 
@@ -343,17 +400,20 @@ Bool_t AliRawReaderDate::ReadHeader()
 
       fCount = 0;
       fPosition = ((UChar_t*)fEquipment) + sizeof(equipmentHeaderStruct);
-      fEnd = fPosition + fEquipment->equipmentSize;
+      if (fSubEvent->eventVersion <= 0x00030001) {
+        fEnd = fPosition + fEquipment->equipmentSize;
+      } else {
+        fEnd = ((UChar_t*)fEquipment) + fEquipment->equipmentSize;
+      }
     }
 
     // continue with the next sub event if no data left in the payload
     if (fPosition >= fEnd) continue;
 
-    fMiniHeader = NULL;
-    if (fRequireMiniHeader) {
-      // check that there are enough bytes left for the mini header
-      if (fPosition + sizeof(AliMiniHeader) > fEnd) {
-       Error("ReadHeader", "could not read mini header data!");
+    if (fRequireHeader) {
+      // check that there are enough bytes left for the data header
+      if (fPosition + sizeof(AliRawDataHeader) > fEnd) {
+       Error("ReadHeader", "could not read data header data!");
        Warning("ReadHeader", "skipping %d bytes\n"
                " run: %d  event: %d %d  LDC: %d  GDC: %d\n", 
                fEnd - fPosition, fSubEvent->eventRunNb, 
@@ -361,41 +421,29 @@ Bool_t AliRawReaderDate::ReadHeader()
                fSubEvent->eventLdcId, fSubEvent->eventGdcId);
        fCount = 0;
        fPosition = fEnd;
-       fErrorCode = kErrNoMiniHeader;
+       fErrorCode = kErrNoDataHeader;
        continue;
       }
 
-      // "read" and check the mini header
-      fMiniHeader = (AliMiniHeader*) fPosition;
-      fPosition += sizeof(AliMiniHeader);
-      if (!CheckMiniHeader()) {
-       Error("ReadHeader", "wrong magic word in mini header!");
-       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 = kErrMiniMagic;
-       continue;
-      }
-
-    } else {  // if mini header not required
-      // assume there is a mini header if the magic word is correct
-      if ((fPosition + sizeof(AliMiniHeader) <= fEnd) &&
-         CheckMiniHeader((AliMiniHeader*) fPosition)) {
-       fMiniHeader = (AliMiniHeader*) fPosition;
-       fPosition += sizeof(AliMiniHeader);
+      // "read" the data header
+      fHeader = (AliRawDataHeader*) fPosition;
+      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);
     }
 
-    if (fMiniHeader) {
-      fCount = fMiniHeader->fSize;
+    if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
+      fCount = fHeader->fSize - sizeof(AliRawDataHeader);
 
-      // 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("ReadHeader", "size in mini header exceeds event size!");
+       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, 
@@ -411,7 +459,7 @@ Bool_t AliRawReaderDate::ReadHeader()
       fCount = fEnd - fPosition;
     }
 
-  } while (!IsSelected());
+  } while (!fEquipment || !IsSelected());
 
   return kTRUE;
 #else
@@ -458,6 +506,7 @@ Bool_t AliRawReaderDate::Reset()
 
 #ifdef ALI_DATE
   fSubEvent = NULL;
+  fEquipment = NULL;
 #endif
   fCount = 0;
   fPosition = fEnd = NULL;
@@ -465,6 +514,61 @@ Bool_t AliRawReaderDate::Reset()
 }
 
 
+Bool_t AliRawReaderDate::NextEvent()
+{
+// go to the next event in the date file
+
+#ifdef ALI_DATE
+  if (!fFile) {
+    if (fEventNumber < 0 && fEvent) {
+      fEventNumber++;
+      return kTRUE;
+    }
+    else
+      return kFALSE;
+  }
+
+  Reset();
+  eventHeaderStruct header;
+  UInt_t headerSize = sizeof(eventHeaderStruct);
+  if (fEvent) delete[] fEvent;
+  fEvent = &header;
+
+  while (fread(&header, 1, headerSize, fFile) == headerSize) {
+    if (!IsEventSelected()) {
+      fseek(fFile, header.eventSize-headerSize, SEEK_CUR);
+      continue;
+    }
+    UChar_t* buffer = new UChar_t[header.eventSize];
+    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;
+  };
+
+  fEvent = NULL;
+#endif
+
+  return kFALSE;
+}
+
+Bool_t AliRawReaderDate::RewindEvents()
+{
+// go back to the beginning of the date file
+
+  if (fFile)
+    fseek(fFile, 0, SEEK_SET);
+
+  fEventNumber = -1;
+  return Reset();
+}
+
+
 Int_t AliRawReaderDate::CheckData() const
 {
 // check the consistency of the data
@@ -485,7 +589,10 @@ Int_t AliRawReaderDate::CheckData() const
 
       // check for end of event data
       if (position >= ((UChar_t*)fEvent)+fEvent->eventSize) return result;
-      if (subEvent) {
+      if (!TEST_SYSTEM_ATTRIBUTE(fEvent->eventTypeAttribute, 
+                                 ATTR_SUPER_EVENT)) {
+        subEvent = fEvent;   // no super event
+      } else if (subEvent) {
        subEvent = (eventHeaderStruct*) (((UChar_t*)subEvent) + 
                                         subEvent->eventSize);
       } else {
@@ -507,27 +614,51 @@ Int_t AliRawReaderDate::CheckData() const
     // 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) {
-      result |= kErrNoMiniHeader;
-      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;
+      }
 
-    // "read" and check the mini header
-    AliMiniHeader* miniHeader = (AliMiniHeader*) position;
-    position += sizeof(AliMiniHeader);
-    if (!CheckMiniHeader(miniHeader)){
-      result |= kErrMiniMagic;
-      position = end;
-      continue;
+      // 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;
+      }
     }
-
-    // check consistency of data size in the mini header and in the sub event
-    if (position + miniHeader->fSize > end) result |= kErrSize;
-    position += miniHeader->fSize;
+    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;
+}