]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RAW/AliRawReaderDate.cxx
update toy emcal trigger
[u/mrichter/AliRoot.git] / RAW / AliRawReaderDate.cxx
index d459352fef5f4f42685697548eafe0da53205df4..effd34af411c98b0f638a1291714805d57add487 100644 (file)
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "AliRawReaderDate.h"
-#ifdef ALI_DATE
 #include "event.h"
-#endif
 
 ClassImp(AliRawReaderDate)
 
 
-AliRawReaderDate::AliRawReaderDate(
-#ifdef ALI_DATE
-                                  void* event
-#else
-                                  void* /* event */
-#endif
-                                  ) :
-  fRequireHeader(kTRUE),
+AliRawReaderDate::AliRawReaderDate(void* event, Bool_t owner) :
+  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
 
-#ifdef ALI_DATE
   fEvent = (eventHeaderStruct*) event;
-#else
-  Fatal("AliRawReaderDate", "this class was compiled without DATE");
-#endif
 }
 
-AliRawReaderDate::AliRawReaderDate(
-#ifdef ALI_DATE
-                                  const char* fileName, Int_t eventNumber
-#else
-                                  const char* /*fileName*/, 
-                                  Int_t /*eventNumber*/
-#endif
-                                  ) :
-  fRequireHeader(kTRUE),
+AliRawReaderDate::AliRawReaderDate(const char* fileName, Int_t eventNumber) :
+  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");
-#endif
-}
-
-AliRawReaderDate::AliRawReaderDate(const AliRawReaderDate& rawReader) :
-  AliRawReader(rawReader),
-  fRequireHeader(rawReader.fRequireHeader),
-  fEvent(rawReader.fEvent),
-  fSubEvent(rawReader.fSubEvent),
-  fEquipment(rawReader.fEquipment),
-  fIsOwner(kFALSE),
-  fPosition(rawReader.fPosition),
-  fEnd(rawReader.fEnd)
-
-{
-// copy constructor
 
 }
 
-AliRawReaderDate& AliRawReaderDate::operator = (const AliRawReaderDate& 
-                                               rawReader)
-{
-// assignment operator
-
-  this->~AliRawReaderDate();
-  new(this) AliRawReaderDate(rawReader);
-  return *this;
-}
-
 AliRawReaderDate::~AliRawReaderDate()
 {
 // destructor
 
-#ifdef ALI_DATE
-  if (fIsOwner) delete[] fEvent;
-#endif
+  if (fEvent && fOwner) delete[] fEvent;
+  if (fFile) {
+    fclose(fFile);
+  }
 }
 
 
@@ -140,159 +94,132 @@ UInt_t AliRawReaderDate::GetType() const
 {
 // get the type from the event header
 
-#ifdef ALI_DATE
   if (!fEvent) return 0;
   return fEvent->eventType;
-#else
-  return 0;
-#endif
 }
 
 UInt_t AliRawReaderDate::GetRunNumber() const
 {
 // get the run number from the event header
 
-#ifdef ALI_DATE
   if (!fEvent) return 0;
   return fEvent->eventRunNb;
-#else
-  return 0;
-#endif
 }
 
 const UInt_t* AliRawReaderDate::GetEventId() const
 {
 // get the event id from the event header
 
-#ifdef ALI_DATE
   if (!fEvent) return NULL;
   return fEvent->eventId;
-#else
-  return NULL;
-#endif
 }
 
 const UInt_t* AliRawReaderDate::GetTriggerPattern() const
 {
 // get the trigger pattern from the event header
 
-#ifdef ALI_DATE
   if (!fEvent) return NULL;
   return fEvent->eventTriggerPattern;
-#else
-  return NULL;
-#endif
 }
 
 const UInt_t* AliRawReaderDate::GetDetectorPattern() const
 {
 // get the detector pattern from the event header
 
-#ifdef ALI_DATE
   if (!fEvent) return NULL;
   return fEvent->eventDetectorPattern;
-#else
-  return NULL;
-#endif
 }
 
 const UInt_t* AliRawReaderDate::GetAttributes() const
 {
 // get the type attributes from the event header
 
-#ifdef ALI_DATE
   if (!fEvent) return NULL;
   return fEvent->eventTypeAttribute;
-#else
-  return NULL;
-#endif
+}
+
+const UInt_t* AliRawReaderDate::GetSubEventAttributes() const
+{
+// get the type attributes from the sub event header
+
+  if (!fSubEvent) return NULL;
+  return fSubEvent->eventTypeAttribute;
 }
 
 UInt_t AliRawReaderDate::GetLDCId() const
 {
 // get the LDC Id from the event header
 
-#ifdef ALI_DATE
   if (!fSubEvent) return 0;
   return fSubEvent->eventLdcId;
-#else
-  return 0;
-#endif
 }
 
 UInt_t AliRawReaderDate::GetGDCId() const
 {
 // get the GDC Id from the event header
 
-#ifdef ALI_DATE
   if (!fEvent) return 0;
   return fEvent->eventGdcId;
-#else
-  return 0;
-#endif
 }
 
+UInt_t AliRawReaderDate::GetTimestamp() const
+{
+// get the timestamp from the event header
+
+  if (!fEvent) return 0;
+  return fEvent->eventTimestamp;
+}
 
 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;
-#else
-  return 0;
-#endif
+  if (fSubEvent->eventVersion <= 0x00030001) {
+    return fEquipment->equipmentSize + sizeof(equipmentHeaderStruct);
+  } else {
+    return fEquipment->equipmentSize;
+  }
 }
 
 Int_t AliRawReaderDate::GetEquipmentType() const
 {
 // get the type from the equipment header
 
-#ifdef ALI_DATE
   if (!fEquipment) return -1;
   return fEquipment->equipmentType;
-#else
-  return 0;
-#endif
 }
 
 Int_t AliRawReaderDate::GetEquipmentId() const
 {
 // get the ID from the equipment header
 
-#ifdef ALI_DATE
   if (!fEquipment) return -1;
   return fEquipment->equipmentId;
-#else
-  return 0;
-#endif
 }
 
 const UInt_t* AliRawReaderDate::GetEquipmentAttributes() const
 {
 // get the attributes from the equipment header
 
-#ifdef ALI_DATE
   if (!fEquipment) return NULL;
   return fEquipment->equipmentTypeAttribute;
-#else
-  return 0;
-#endif
 }
 
 Int_t AliRawReaderDate::GetEquipmentElementSize() const
 {
 // get the basic element size from the equipment header
 
-#ifdef ALI_DATE
   if (!fEquipment) return 0;
   return fEquipment->equipmentBasicElementSize;
-#else
-  return 0;
-#endif
 }
 
+Int_t AliRawReaderDate::GetEquipmentHeaderSize() const
+{
+  // Get the equipment header size
+  // 28 bytes by default
+  return sizeof(equipmentHeaderStruct);
+}
 
 Bool_t AliRawReaderDate::ReadHeader()
 {
@@ -301,7 +228,7 @@ Bool_t AliRawReaderDate::ReadHeader()
 
   fErrorCode = 0;
 
-#ifdef ALI_DATE
+  fHeader = NULL;
   if (!fEvent) return kFALSE;
   // check whether there are sub events
   if (fEvent->eventSize <= fEvent->eventHeadSize) return kFALSE;
@@ -312,6 +239,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 || 
@@ -319,7 +247,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 {
@@ -338,6 +269,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);
 
@@ -347,18 +285,21 @@ 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;
 
-    fHeader = NULL;
     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"
+       Warning("ReadHeader", "skipping %ld bytes\n"
                " run: %d  event: %d %d  LDC: %d  GDC: %d\n", 
                fEnd - fPosition, fSubEvent->eventRunNb, 
                fSubEvent->eventId[0], fSubEvent->eventId[1],
@@ -371,7 +312,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 != %ld)! 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 != %ld)! Using the equipment size instead !",
+                   fHeaderV3->fSize, fEnd - fPosition);
+         fHeaderV3->fSize = fEnd - fPosition;
+       }
+       fPosition += sizeof(AliRawDataHeaderV3);
+       fHeader = 0;
+      }
     }
 
     if (fHeader && (fHeader->fSize != 0xFFFFFFFF)) {
@@ -380,7 +348,24 @@ Bool_t AliRawReaderDate::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\n"
+       Warning("ReadHeader", "skipping %ld 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 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\n"
                " run: %d  event: %d %d  LDC: %d  GDC: %d\n", 
                fEnd - fPosition, fSubEvent->eventRunNb, 
                fSubEvent->eventId[0], fSubEvent->eventId[1],
@@ -395,12 +380,9 @@ Bool_t AliRawReaderDate::ReadHeader()
       fCount = fEnd - fPosition;
     }
 
-  } while (!IsSelected());
+  } while (!fEquipment || !IsSelected());
 
   return kTRUE;
-#else
-  return kFALSE;
-#endif
 }
 
 Bool_t AliRawReaderDate::ReadNextData(UChar_t*& data)
@@ -440,20 +422,73 @@ Bool_t AliRawReaderDate::Reset()
 {
 // reset the current position to the beginning of the event
 
-#ifdef ALI_DATE
   fSubEvent = NULL;
-#endif
+  fEquipment = NULL;
   fCount = 0;
   fPosition = fEnd = NULL;
+  fHeader=NULL;
+  fHeaderV3=NULL;
   return kTRUE;
 }
 
 
+Bool_t AliRawReaderDate::NextEvent()
+{
+// go to the next event in the date file
+
+  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;
+
+  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
 
-#ifdef ALI_DATE
   if (!fEvent) return 0;
   // check whether there are sub events
   if (fEvent->eventSize <= fEvent->eventHeadSize) return 0;
@@ -469,7 +504,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 {
@@ -491,27 +529,46 @@ 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 (fHeader->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 != %ld)! 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)
+
+  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);
+    }
+  }
+  return NULL;
+}