]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/rec/AliRawReaderHLT.cxx
CMake: removing qpythia from the depedencies
[u/mrichter/AliRoot.git] / HLT / rec / AliRawReaderHLT.cxx
index 5b9177651dc66db1abd5a800c6f199a79a38b1cc..5982f97fa773e0ef2b131cbbbd802285cb9155b0 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/** @file   AliRawReaderHLT.cxx
-    @author Matthias Richter
-    @date   
-    @brief  AliRawReader implementation which replaces original input of
-            detectors with the appropriate HLT output.                    */
-
-// see header file for class documentation
-// or
-// refer to README to build package
-// or
-// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
+/// @file   AliRawReaderHLT.cxx
+/// @author Matthias Richter
+/// @date   
+/// @brief  AliRawReader implementation which replaces original input of
+///         detectors with the appropriate HLT output.
 
 #include "AliRawReaderHLT.h"
 #include "AliHLTOUTRawReader.h"
 #include "AliHLTModuleAgent.h"
 #include "AliHLTOUTHandler.h"
 #include "AliHLTOUTHandlerEquId.h"
+#include "AliHLTSystem.h"
+#include "AliHLTPluginBase.h"
+#include "AliHLTCDHWrapper.h"
 #include "AliLog.h"
 #include "AliDAQ.h"            // RAW, for detector names and equipment ids
 #include "TObjString.h"
@@ -46,14 +43,18 @@ AliRawReaderHLT::AliRawReaderHLT(AliRawReader* pRawreader, const char* options)
   AliRawReader(),
   fpParentReader(pRawreader),
   fOptions(),
+  fSystemOptions(),
   fpData(NULL),
   fDataSize(0),
   fOffset(0),
+  fPosition(0),
   fEquipmentId(-1),
   fbHaveHLTData(false),
   fDetectors(),
   fpHLTOUT(NULL),
-  fpDataHandler(NULL)
+  fbReadFirst(true),
+  fpDataHandler(NULL),
+  fpPluginBase(new AliHLTPluginBase)
 {
   // see header file for class documentation
   // or
@@ -67,13 +68,9 @@ AliRawReaderHLT::AliRawReaderHLT(AliRawReader* pRawreader, const char* options)
 AliRawReaderHLT::~AliRawReaderHLT()
 {
   // see header file for class documentation
-  if (fpHLTOUT) {
-    if (fpDataHandler) fpDataHandler->ReleaseProcessedData(fpData, fDataSize);
-    else fpHLTOUT->ReleaseDataBuffer(fpData);
-    fpDataHandler=NULL;
-    delete fpHLTOUT;
-    fpHLTOUT=NULL;
-  }
+  ReleaseHLTData();
+  if (fpPluginBase) delete fpPluginBase;
+  fpPluginBase=NULL;
 }
 
 UInt_t AliRawReaderHLT::GetType() const
@@ -144,7 +141,6 @@ const UInt_t* AliRawReaderHLT::GetEquipmentAttributes() const
 
 Int_t    AliRawReaderHLT::GetEquipmentElementSize() const
 {
-  // see header file for class documentation
   // don't know what it really means, bu the AliRawReaderFile
   // just sets it to 0
   // do the same if we have a valid equipment data set from
@@ -155,8 +151,6 @@ Int_t    AliRawReaderHLT::GetEquipmentElementSize() const
 
 Int_t    AliRawReaderHLT::GetEquipmentHeaderSize() const
 {
-  // see header file for class documentation
-
   // equipment header means the additional data header?
   // if we have a valid equipment data set from the HLT stream
   // there is no additional header
@@ -166,8 +160,11 @@ Int_t    AliRawReaderHLT::GetEquipmentHeaderSize() const
 
 Int_t    AliRawReaderHLT::GetEquipmentSize() const
 {
-  // see header file for class documentation
-  if (fEquipmentId>=0) return fDataSize+sizeof(AliRawDataHeader);
+  // return the equipment size, that's including the CDH
+  // fDataSize has been set to the full size of the block if it is from HLTOUT
+  // if the data block is from the parent rawreader it is only the pointer
+  // to the payload and the size of the CDH must be added
+  if (fEquipmentId>=0) return fDataSize + (fbHaveHLTData?0:GetEquipmentHeaderSize());
   return fpParentReader->GetEquipmentSize();
 }
 
@@ -179,7 +176,7 @@ Int_t    AliRawReaderHLT::GetEquipmentType() const
 
 Int_t    AliRawReaderHLT::GetEquipmentId() const
 {
-  // see header file for class documentation
+  // id of current equipment
   Int_t id=-1;
   if (fEquipmentId>=0) id=fEquipmentId;
   else id=fpParentReader->GetEquipmentId();
@@ -188,53 +185,95 @@ Int_t    AliRawReaderHLT::GetEquipmentId() const
 
 Bool_t   AliRawReaderHLT::ReadHeader()
 {
-  // see header file for class documentation
-  Bool_t result=fpParentReader->ReadHeader();
-  fHeader=const_cast<AliRawDataHeader*>(fpParentReader->GetDataHeader());
+  // read the header of the next equipment
+  // depending on whether there is data redirected from HLT for the current event
+  // first the data in the HLTOUT is tried
+  Bool_t result=kFALSE;
+  Bool_t firstParentCycle=fbHaveHLTData;
+  while ((fbHaveHLTData=(fbHaveHLTData && ReadNextHLTData()))) {
+    // all internal data variables set
+    assert(fpData!=NULL);
+    AliHLTCDHWrapper cdh((void*)fpData);
+    if(cdh.GetVersion()==2){
+      fHeader=reinterpret_cast<AliRawDataHeader*>(const_cast<AliHLTUInt8_t*>(fpData));
+      fHeaderV3=NULL;
+    } else {
+      fHeader=NULL;
+      fHeaderV3=reinterpret_cast<AliRawDataHeaderV3*>(const_cast<AliHLTUInt8_t*>(fpData));
+    }
+    fOffset=cdh.GetHeaderSize();
+    fPosition=fOffset;
+    if ((result=IsSelected())) break;
+  }
+  firstParentCycle&=!fbHaveHLTData; // true if it just changed from true to false
+  while (!result) {
+    if (firstParentCycle) {
+      firstParentCycle=kFALSE;
+      // reset and set the selection back to the original one
+      fpParentReader->Reset();
+      fpParentReader->SelectEquipment(fSelectEquipmentType, fSelectMinEquipmentId, fSelectMaxEquipmentId);
+    }
+
+    if (!(result=fpParentReader->ReadHeader())) {
+      fHeader=NULL;
+      fHeaderV3=NULL;
+      break;
+    }
+    fHeader=const_cast<AliRawDataHeader*>(fpParentReader->GetDataHeader());
+    fHeaderV3=const_cast<AliRawDataHeaderV3*>(fpParentReader->GetDataHeaderV3());
+    fDataSize=fpParentReader->GetDataSize();
+    fPosition=0;
+    fpData=NULL;
+
+    // filter out all equipment ids which should be taken from the HLT stream
+    int id=fpParentReader->GetEquipmentId();
+    if ((result=!IsHLTInput(id))) break;
+  }
   return result;
 }
 
 Bool_t   AliRawReaderHLT::ReadNextData(UChar_t*& data)
 {
-  // see header file for class documentation
+  // read data from equipment, return pointer to data, GetDataSize
+  // provides the data size
+  return ReadNextData(data, kTRUE);
+}
 
+Bool_t   AliRawReaderHLT::ReadNextData(UChar_t*& data, Bool_t readHeader)
+{
   // this function is the backbone of the ReadNext functions, it gets the
   // whole data block either from the HLT stream or the parent raw reader.
   // Each call of ReadNextData directly jumps to the next data set.
-  Bool_t result=kFALSE;
-  if (fbHaveHLTData&=ReadNextHLTData()) {
-    // all internal data variables set
-    assert(fpData!=NULL);
-    data=const_cast<AliHLTUInt8_t*>(fpData);
-    result=kTRUE;
-  }
-  if (!result) {
-    // no data in the HLT stream, read real data
-    //AliInfo(Form("read from parent reader: min=%d max=%d", fSelectMinEquipmentId, fSelectMaxEquipmentId));
-
-    // first set the selection back to the original one
-    fpParentReader->SelectEquipment(fSelectEquipmentType, fSelectMinEquipmentId, fSelectMaxEquipmentId);
-
-    // read data
-    while (result=fpParentReader->ReadNextData(data)) {
-      // continue if the Equipment Id is supposed to be replaced by the HLT stream
-      // in that case we do not want to read it from the parent raw reader
-      if (!IsHLTInput(fpParentReader->GetEquipmentId())) break;
-    }
-
-    // set the header of this reader from the parent reader.
-    // This is necessary because of a few base class methods working directly
-    // on the header
-    fHeader=const_cast<AliRawDataHeader*>(fpParentReader->GetDataHeader());
-    if (result) {
-      fpData=data;
-      fDataSize=fpParentReader->GetDataSize();
+  Bool_t result=kTRUE;
+
+  // read new header if data already read
+  if (fPosition<fDataSize || (result=(readHeader && ReadHeader()))) {
+    if (fbHaveHLTData && fpHLTOUT!=NULL) {
+      // all internal data variables set
+      result=kTRUE;
+      AliHLTCDHWrapper cdh((void*) fpData);
+      data=const_cast<AliHLTUInt8_t*>(fpData+cdh.GetHeaderSize());
+      // fpData includes the CDH, set offset behind CDH
+      fOffset=cdh.GetHeaderSize();
     } else {
-      fpData=NULL;
-      fDataSize=0;
+      // no data in the HLT stream, read real data
+      //AliInfo(Form("read from parent reader: min=%d max=%d", fSelectMinEquipmentId, fSelectMaxEquipmentId));
+
+      // read data
+      result=fpParentReader->ReadNextData(data);
+      if (result) {
+       fpData=data;
+       fDataSize=fpParentReader->GetDataSize();
+       // fpData is without CDH
+       fOffset=0;
+      } else {
+       fpData=NULL;
+       fDataSize=0;
+      }
+
+      fEquipmentId=-1;
     }
-    fOffset=0;
-    fEquipmentId=-1;
+    fPosition=fDataSize;
   }
   return result;
 }
@@ -250,7 +289,7 @@ Bool_t   AliRawReaderHLT::ReadNextInt(UInt_t& data)
       fOffset+=iCopy;
       return kTRUE;
     }
-  } while (ReadNextData(dummy));
+  } while (ReadNextData(dummy, kTRUE));
   return kFALSE;
 }
 
@@ -265,7 +304,7 @@ Bool_t   AliRawReaderHLT::ReadNextShort(UShort_t& data)
       fOffset+=iCopy;
       return kTRUE;
     }
-  } while (ReadNextData(dummy));
+  } while (ReadNextData(dummy, kTRUE));
   return kFALSE;
 }
 
@@ -280,7 +319,7 @@ Bool_t   AliRawReaderHLT::ReadNextChar(UChar_t& data)
       fOffset+=iCopy;
       return kTRUE;
     }
-  } while (ReadNextData(dummy));
+  } while (ReadNextData(dummy, kTRUE));
   return kFALSE;
 }
 
@@ -297,38 +336,25 @@ Bool_t   AliRawReaderHLT::ReadNext(UChar_t* data, Int_t size)
       fOffset+=iCopy;
       return kTRUE;
     }
-  } while (ReadNextData(dummy));
+    // By convention, the ReadNextData function stays in the
+    // current block and does not switch to the next one
+    // automatically -> kFALSE
+  } while (ReadNextData(dummy, kFALSE));
   return kFALSE;
 }
 
 Bool_t   AliRawReaderHLT::Reset()
 {
   // see header file for class documentation
+  ReleaseHLTData(false/* keep HLTOUT instance */);
   Bool_t result=fpParentReader->Reset();
-  fpData=NULL;
-  fDataSize=0;
-  fOffset=0;
   fEquipmentId=-1;
-  if (fbHaveHLTData=(fDetectors.size()>0)) {
-    vector<int>::iterator detector=fDetectors.begin();
-    for (; detector!=fDetectors.end(); detector++) {
-      int ddlOffset=AliDAQ::DdlIDOffset(*detector);
-      int nofDDLs=AliDAQ::NumberOfDdls(*detector);
-      if ((fSelectMinEquipmentId>=0 && fSelectMinEquipmentId>ddlOffset+nofDDLs) ||
-         (fSelectMinEquipmentId>=0 && fSelectMaxEquipmentId<ddlOffset))
-       continue;
-      break;
-    }
-    fbHaveHLTData=detector!=fDetectors.end();
-  }
 
-  if (fpHLTOUT) {
-    if (fpDataHandler) fpDataHandler->ReleaseProcessedData(fpData, fDataSize);
-    else fpHLTOUT->ReleaseDataBuffer(fpData);
-    fpDataHandler=NULL;
-    delete fpHLTOUT;
-    fpHLTOUT=NULL;
-  }
+  // check if redirection is enabled for at least one detector in the selected range
+  fbHaveHLTData=EvaluateSelection();
+
+  // start reading HLTOUT data blocks from the beginning
+  fbReadFirst=true;
 
   return result;
 }
@@ -336,6 +362,9 @@ Bool_t   AliRawReaderHLT::Reset()
 Bool_t   AliRawReaderHLT::NextEvent()
 {
   // see header file for class documentation
+
+  ReleaseHLTData();
+
   Bool_t result=fpParentReader->NextEvent();
   if (result) {
     fEventNumber++;
@@ -357,6 +386,7 @@ void AliRawReaderHLT::Select(Int_t detectorID, Int_t minDDLID, Int_t maxDDLID)
   // see header file for class documentation
   AliRawReader::Select(detectorID, minDDLID, maxDDLID);
   fpParentReader->Select(detectorID, minDDLID, maxDDLID);
+  fbHaveHLTData=EvaluateSelection();
 }
 
 // most likely we do not need this method since the base class directly forwards
@@ -373,8 +403,9 @@ void AliRawReaderHLT::SelectEquipment(Int_t equipmentType, Int_t minEquipmentId,
   // see header file for class documentation
 
   //AliInfo(Form("equipmentType=%d, minEquipmentId=%d, maxEquipmentId=%d", equipmentType, minEquipmentId, maxEquipmentId));
-  AliRawReader::Select(equipmentType, minEquipmentId, maxEquipmentId);
-  fpParentReader->Select(equipmentType, minEquipmentId, maxEquipmentId);
+  AliRawReader::SelectEquipment(equipmentType, minEquipmentId, maxEquipmentId);
+  fpParentReader->SelectEquipment(equipmentType, minEquipmentId, maxEquipmentId);
+  fbHaveHLTData=EvaluateSelection();
 }
 
 void AliRawReaderHLT::SkipInvalid(Bool_t skip)
@@ -385,6 +416,7 @@ void AliRawReaderHLT::SkipInvalid(Bool_t skip)
   fpParentReader->SkipInvalid(skip);
 }
 
+/*
 void AliRawReaderHLT::SelectEvents(Int_t type)
 {
   // see header file for class documentation
@@ -393,6 +425,7 @@ void AliRawReaderHLT::SelectEvents(Int_t type)
   AliRawReader::SelectEvents(type);
   fpParentReader->SelectEvents(type);
 }
+*/
 
 int AliRawReaderHLT::ScanOptions(const char* options)
 {
@@ -401,6 +434,7 @@ int AliRawReaderHLT::ScanOptions(const char* options)
   TString optString(options);
   TString argument;
   TString parameter;
+  TString detectors;
   TObjArray* pTokens=optString.Tokenize(" ");
   if (pTokens) {
     int iEntries=pTokens->GetEntries();
@@ -413,11 +447,19 @@ int AliRawReaderHLT::ScanOptions(const char* options)
       int detId=AliDAQ::DetectorID(argument.Data());
       if (detId>=0) {
        fDetectors.push_back(detId);
+       if (!detectors.IsNull()) detectors+=" ";
+       detectors+=argument;
+      } else {
+       if (!fSystemOptions.IsNull()) fSystemOptions+=" ";
+       fSystemOptions+=argument;
       }
     }
     delete pTokens;
   }
 
+  if (iResult>=0 && !detectors.IsNull()) {
+    AliInfo(Form("running reconstruction from HLT data: %s", detectors.Data()));
+  }
   return iResult;
 }
 
@@ -425,23 +467,31 @@ Bool_t   AliRawReaderHLT::ReadNextHLTData()
 {
   // see header file for class documentation
   bool result=kTRUE;
-  if (!fpHLTOUT) {
+  if (fbReadFirst || !fpHLTOUT) {
+    if (!fpHLTOUT) {
     fpHLTOUT=new AliHLTOUTRawReader(fpParentReader);
-    if (result=(fpHLTOUT!=NULL)) {
-      if (result=(fpHLTOUT->Init()>=0)) {
-       result=fpHLTOUT->SelectFirstDataBlock(kAliHLTAnyDataType, kAliHLTVoidDataSpec,
-                                             AliHLTModuleAgent::kRawReader)>=0;
+    if ((result=(fpHLTOUT!=NULL))) {
+      if (!fpPluginBase) {
+       AliFatal("internal data error: can not get AliHLTSystem instance from plugin");
+       return false;
+      }
+      AliHLTSystem* pSystem=fpPluginBase->GetInstance();
+      if (pSystem) {
+       pSystem->ScanOptions(fSystemOptions.Data());
       }
+      if ((result=(fpHLTOUT->Init())>=0)) {
+      }
+    }
     }
+    if (result) {
+      result=fpHLTOUT->SelectFirstDataBlock(kAliHLTAnyDataType, kAliHLTVoidDataSpec,
+                                           AliHLTModuleAgent::kRawReader)>=0;
+    }
+    fbReadFirst=false;
   } else {
     // first release the data buffer
-    if (fpDataHandler) fpDataHandler->ReleaseProcessedData(fpData, fDataSize);
-    else fpHLTOUT->ReleaseDataBuffer(fpData);
-    fpDataHandler=NULL;
-    if (!(result=fpHLTOUT->SelectNextDataBlock()>=0)) {
-      delete fpHLTOUT;
-      fpHLTOUT=NULL;
-    }
+    ReleaseHLTData(false /* keep HLTOUT instance */);
+    result=fpHLTOUT->SelectNextDataBlock()>=0;
   }
   if (result) {
     AliHLTComponentDataType dt=kAliHLTVoidDataType;
@@ -451,12 +501,13 @@ Bool_t   AliRawReaderHLT::ReadNextHLTData()
     AliHLTOUTHandler* pHandler=fpHLTOUT->GetHandler();
     if (pHandler) {
       if (dynamic_cast<AliHLTOUTHandlerEquId*>(pHandler)!=NULL) {
-       AliHLTOUT::AliHLTOUTLockGuard g(fpHLTOUT);
+       AliHLTOUT::AliHLTOUTSelectionGuard g(fpHLTOUT);
        fEquipmentId=pHandler->ProcessData(fpHLTOUT);
        fpData=NULL;
        fDataSize=pHandler->GetProcessedData(fpData);
        if (!fpData) {
          result=fpHLTOUT->GetDataBuffer(fpData, size)>=0;
+         fpDataHandler=NULL;
          AliDebug(AliLog::kDebug, Form("forward data block from HLTOUT stream to equipment %d", fEquipmentId));
          fDataSize=(int)size;
        } else {
@@ -464,6 +515,7 @@ Bool_t   AliRawReaderHLT::ReadNextHLTData()
          fpDataHandler=pHandler;
          AliDebug(AliLog::kDebug, Form("forward decoded data block provided by handler to equipment %d", fEquipmentId));
        }
+       return kTRUE;
       } else {
        AliError(Form("handler is not of type AliHLTOUTHandlerEquId for block %x data type %s spec %#x; data block skipped",
                      fpHLTOUT->GetDataBlockIndex(), AliHLTComponent::DataType2Text(dt).c_str(), spec));
@@ -473,12 +525,9 @@ Bool_t   AliRawReaderHLT::ReadNextHLTData()
                      fpHLTOUT->GetDataBlockIndex(), AliHLTComponent::DataType2Text(dt).c_str(), spec));
     }
   } else {
-    fpData=NULL;
-    fDataSize=0;
-    fOffset=0;
-    fEquipmentId=-1;
+    ReleaseHLTData(false /* keep HLTOUT instance */);
   }
-  return false;
+  return kFALSE;
 }
 
 Bool_t AliRawReaderHLT::IsHLTInput(int ddlid)
@@ -494,6 +543,46 @@ Bool_t AliRawReaderHLT::IsHLTInput(int ddlid)
   return kFALSE;
 }
 
+int AliRawReaderHLT::ReleaseHLTData(bool bReleaseHLTOUT)
+{
+  // see header file for class documentation
+  if (fpHLTOUT) {
+    if (fpDataHandler) fpDataHandler->ReleaseProcessedData(fpData, fDataSize);
+    else fpHLTOUT->ReleaseDataBuffer(fpData);
+    fpDataHandler=NULL;
+    if (bReleaseHLTOUT) {
+      delete fpHLTOUT;
+      fpHLTOUT=NULL;
+    }
+  }
+
+  fpData=NULL;
+  fDataSize=0;
+  fOffset=0;
+  fPosition=0;
+  fEquipmentId=-1;
+
+  return 0;
+}
+
+Bool_t AliRawReaderHLT::EvaluateSelection()
+{
+  // see header file for class documentation
+  Bool_t bHaveHLTData=kFALSE;
+  if ((bHaveHLTData=(fDetectors.size())>0)) {
+    vector<int>::iterator detector=fDetectors.begin();
+    for (; detector!=fDetectors.end(); detector++) {
+      int ddlOffset=AliDAQ::DdlIDOffset(*detector);
+      int nofDDLs=AliDAQ::NumberOfDdls(*detector);
+      if ((fSelectMinEquipmentId<0 || fSelectMinEquipmentId<ddlOffset+nofDDLs) &&
+         (fSelectMaxEquipmentId<0 || fSelectMaxEquipmentId>=ddlOffset))
+       break;
+    }
+    bHaveHLTData=detector!=fDetectors.end();
+  }
+  return bHaveHLTData;
+}
+
 AliRawReader* AliRawReaderHLTCreateInstance(AliRawReader* pParentReader, const char* options)
 {
   // see header file for class documentation