]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RAW/AliRawReaderMemory.cxx
update toy emcal trigger
[u/mrichter/AliRoot.git] / RAW / AliRawReaderMemory.cxx
index 710368232f7b56d24c71cf36447946d91c4c88e3..7076c00e159dd44b32521fe7f3848a23ba2c2493 100644 (file)
@@ -29,22 +29,21 @@ ClassImp(AliRawReaderMemory)
 
 
 AliRawReaderMemory::AliRawReaderMemory() :
-  fBuffer(NULL),
-  fBufferSize(0),
   fPosition(0),
-  fEquipmentId(-1)
+  fBuffers(),
+  fCurrent(0)
 {
 // create an object to read digits from
 // the given memory location
 }
 
 AliRawReaderMemory::AliRawReaderMemory(UChar_t* memory, UInt_t size) :
-  fBuffer(memory),
-  fBufferSize(size),
   fPosition(0),
-  fEquipmentId(-1)
+  fBuffers(),
+  fCurrent(0)
 {
 // create an object to read digits from the given memory
+  fBuffers.push_back(AliRRMBuffer(memory, size, -1));
 }
 
 AliRawReaderMemory::~AliRawReaderMemory()
@@ -68,39 +67,78 @@ Bool_t AliRawReaderMemory::ReadHeader()
 // read a data header at the current buffer position
 // returns kFALSE if the mini header could not be read
 
-  if (fEquipmentId == -1)
-  {
-    Warning("ReadHeader", "The equipment ID is not set for the DDL memory buffer.");
-  }
+  Bool_t result=kFALSE;
+  if (fCurrent>=fBuffers.size()) return kFALSE;
 
-  if (!fBuffer) return kFALSE;
   do {
+  result=kFALSE;
+  do {
+    if (fBuffers[fCurrent].GetEquipmentId() == -1)
+      {
+       Warning("ReadHeader", "The equipment ID is not set for the DDL memory buffer.");
+      }
+    if (!fBuffers[fCurrent].GetBuffer()) break;
+
     // Check if we would not read past the end of the buffer.
-    if ( fPosition+fCount+sizeof(AliRawDataHeader) > fBufferSize ) return kFALSE;
-    
-    fHeader = reinterpret_cast<AliRawDataHeader*>(fBuffer+fPosition+fCount);
+    if ( fPosition+fCount >= fBuffers[fCurrent].GetBufferSize() ) break;
+
+    fHeader = reinterpret_cast<AliRawDataHeader*>(fBuffers[fCurrent].GetBuffer()+fPosition+fCount);
+    fHeaderV3 = reinterpret_cast<AliRawDataHeaderV3*>(fBuffers[fCurrent].GetBuffer()+fPosition+fCount);
+
+    //Access to version and size is uniform for V2 and V3 
+    UChar_t version = fHeader->GetVersion();
+    UInt_t size = fHeader->fSize;
+    Int_t headerSize = 0;
+
+    if(version == 3) {
+      fHeader=NULL;
+      headerSize = sizeof(AliRawDataHeaderV3);
+    } else if(version == 2) {
+      fHeaderV3=NULL;
+      headerSize = sizeof(AliRawDataHeader);
+    } else {
+      Error("ReadHeader", "Wrong raw data header version: %d. Expected: 2 or 3.", version);
+      return kFALSE;
+    }
     
     // Check that the header is sane, that is the size does not go past the buffer.
     // Otherwise try again at the next word location.
-    if (fHeader->fSize == 0 or fPosition+fCount+fHeader->fSize > fBufferSize) {
-      if (fPosition + sizeof(UInt_t) <= fBufferSize) {
-        fPosition += sizeof(UInt_t);
-        continue;
+    while (1) {
+      if ( ( (size == 0) ||
+          ((Int_t)fPosition + fCount + (Int_t)size > (Int_t)fBuffers[fCurrent].GetBufferSize() ) )
+         && size != 0xFFFFFFFF) {
+
+       if (fPosition + sizeof(UInt_t) <= fBuffers[fCurrent].GetBufferSize()) {
+         fPosition += sizeof(UInt_t);
+         continue;
+       } else {
+         Error("ReadHeader", "Could not find a valid DDL header!");
+         return kFALSE;
+       }
       } else {
-        Error("ReadHeader", "Could not find a valid DDL header!");
-        return kFALSE;
+       fPosition += fCount + headerSize;
       }
-    } else {
-      fPosition += fCount + sizeof(AliRawDataHeader);
+      break;
     }
 
-    if (fHeader->fSize != 0xFFFFFFFF) {
-      fCount = fHeader->fSize - sizeof(AliRawDataHeader);
+    if (size != 0xFFFFFFFF) {
+      fCount = (Int_t)size - headerSize;
     } else {
-      fCount = fBufferSize - fPosition - sizeof(AliRawDataHeader);
+      fCount = fBuffers[fCurrent].GetBufferSize() - headerSize;
     }
-  } while (!IsSelected());
+  } while (!(result=IsSelected()) && OpenNextBuffer());
+  } while (!result && OpenNextBuffer());
+
+  return result;
+}
 
+Bool_t AliRawReaderMemory::OpenNextBuffer()
+{
+  // increment to next buffer
+  fPosition=0;
+  fCount=0;
+  if (fCurrent>=fBuffers.size()) return kFALSE;
+  if (++fCurrent>=fBuffers.size()) return kFALSE;
   return kTRUE;
 }
 
@@ -112,22 +150,36 @@ Bool_t AliRawReaderMemory::ReadNextData(UChar_t*& data)
   while (fCount == 0) {
     if (!ReadHeader()) return kFALSE;
   }
+
+  if(fCount < 0){
+    Error("ReadNextData","Cannot read data, payload is negative");
+    return kFALSE;
+  }
+
   UInt_t currentPosition = fPosition;
   fPosition += fCount;
   fCount = 0;
 
-  data = fBuffer+currentPosition;
+  if(fBuffers[fCurrent].GetBufferSize()<currentPosition){
+    Error("ReadNextData","Current position exceeds buffersize.");
+    return kFALSE;
+  }
+  data = fBuffers[fCurrent].GetBuffer()+currentPosition;
   return kTRUE;
 }
 
 Bool_t AliRawReaderMemory::ReadNext(UChar_t* data, Int_t size)
 {
 // reads the next block of data at the current buffer position
+// but does not shift to the next equipment. The next equipment
+// must be activated by calling ReadHeader
 // returns kFALSE if the data could not be read
 
-  if ( fBufferSize-fPosition < (UInt_t)size ) return kFALSE;
+  
+  if (fCurrent>=fBuffers.size()) return kFALSE;
+  if ( fBuffers[fCurrent].GetBufferSize()-fPosition < (UInt_t)size ) return kFALSE;
 
-  memcpy( data, fBuffer+fPosition, size );
+  memcpy( data, fBuffers[fCurrent].GetBuffer()+fPosition, size );
   fCount -= size;
   fPosition += size;
   return kTRUE;
@@ -139,8 +191,10 @@ Bool_t AliRawReaderMemory::Reset()
 // reset the current position in the buffer to the beginning of the curevent
 
   fHeader = NULL;
+  fHeaderV3 = NULL;
   fCount = 0;
   fPosition = 0;
+  fCurrent=0;
   return kTRUE;
 }
 
@@ -165,11 +219,107 @@ Bool_t AliRawReaderMemory::RewindEvents()
 
 Bool_t AliRawReaderMemory::SetMemory( UChar_t* memory, ULong_t size )
 {
-  fBuffer = memory;
-  fBufferSize = size;
+  // SetMemory function kept for backward compatibility, only allowed
+  // if no blocks have been added so far 
+  if (!memory || size<=0) return kFALSE;
+  if (fBuffers.size()>1 || (fBuffers.size()==1 && fPosition==0 && fCurrent==0)) {
+    Error("SetMemory","can not SetMemory for multiple buffers, use AddBuffer(...)");
+    return kFALSE;
+  }
+  if (fBuffers.size()==1) fBuffers.pop_back();
+  fBuffers.push_back(AliRRMBuffer(memory, size, -1));
+  fCurrent=0;
   fHeader = NULL;
+  fHeaderV3 = NULL;
   fCount = 0;
   fPosition = 0;
-  return (fBuffer && fBufferSize>0) ? kTRUE : kFALSE;
+  return kTRUE;
+}
+
+void  AliRawReaderMemory::SetEquipmentID(Int_t id)
+{
+  // SetMemory function kept for backward compatibility, only allowed
+  // if no blocks have been added so far, set equipment id of the first
+  // buffer
+  if (fBuffers.size()>1) {
+    Error("SetEquipmentID", "can not SetEquipmentID for multiple buffers, use AddBuffer(...)");
+    return;
+  }
+  if (fBuffers.size()==0 || fCurrent>=fBuffers.size()) {
+    Error("SetEquipmentID", "no block available to set equipment id");
+    return;    
+  }
+  fBuffers[fCurrent].SetEquipmentId(id);
 }
 
+Int_t AliRawReaderMemory::GetEquipmentSize() const
+{
+  // get the size of the equipment, that is payload + CDH
+  if (fCurrent>=fBuffers.size()) return 0;
+  return fBuffers[fCurrent].GetBufferSize();
+}
+
+Int_t AliRawReaderMemory::GetEquipmentId() const
+{
+  // get the current equipment id
+  if (fCurrent>=fBuffers.size()) return -1;
+  return fBuffers[fCurrent].GetEquipmentId();
+}
+
+Bool_t AliRawReaderMemory::AddBuffer(UChar_t* memory, ULong_t size, Int_t equipmentId )
+{
+  // Add a buffer to the list
+  if (!memory || size<=0 || equipmentId<0 ) return kFALSE;
+  fBuffers.push_back(AliRRMBuffer(memory, size, equipmentId));
+  return kTRUE;
+}
+
+void AliRawReaderMemory::ClearBuffers()
+{
+  // Clear the buffer list
+  fBuffers.clear();
+  Reset();
+}
+
+AliRawReaderMemory::AliRRMBuffer::AliRRMBuffer()
+  :
+  fBuffer(NULL),
+  fBufferSize(0),
+  fEquipmentId(-1)
+{
+  // ctor
+}
+
+AliRawReaderMemory::AliRRMBuffer::AliRRMBuffer(UChar_t* pBuffer, UInt_t bufferSize, Int_t equipmentId)
+  :
+  fBuffer(pBuffer),
+  fBufferSize(bufferSize),
+  fEquipmentId(equipmentId)
+{
+  // ctor
+}
+
+AliRawReaderMemory::AliRRMBuffer::~AliRRMBuffer()
+{
+  // dtor
+}
+
+AliRawReaderMemory::AliRRMBuffer::AliRRMBuffer(const AliRRMBuffer& src)
+  :
+  fBuffer(src.fBuffer),
+  fBufferSize(src.fBufferSize),
+  fEquipmentId(src.fEquipmentId)
+{
+  // copy ctor, there are no buffers allocated internally, pointers
+  // are just copied
+}
+
+AliRawReaderMemory::AliRRMBuffer& AliRawReaderMemory::AliRRMBuffer::operator=(const AliRRMBuffer& src)
+{
+  // assignment op
+  if(&src == this) return *this;
+  fBuffer=src.fBuffer;
+  fBufferSize=src.fBufferSize;
+  fEquipmentId=src.fEquipmentId;
+  return *this;
+}