]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/BASE/AliHLTComponent.cxx
bugfix in high level interface: GetFirst/NextObject/Block
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTComponent.cxx
index 7bd5211001827a407b986dc7b57cdb546f1ea7ab..3113f1fe872ec94966be1455552f47c367d2e477 100644 (file)
 using namespace std;
 #endif
 
-#include "AliHLTStdIncludes.h"
+//#include "AliHLTStdIncludes.h"
 #include "AliHLTComponent.h"
 #include "AliHLTComponentHandler.h"
 #include "AliHLTMessage.h"
 #include "TString.h"
 #include "TObjArray.h"
 #include "TClass.h"
+#include "TStopwatch.h"
 
 /** ROOT macro for the implementation of ROOT specific class methods */
-ClassImp(AliHLTComponent)
+ClassImp(AliHLTComponent);
+
+/** stopwatch macro using the stopwatch guard */
+#define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
+//#define ALIHLTCOMPONENT_STOPWATCH(type) 
+
+/** stopwatch macro for operations of the base class */
+#define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
+/** stopwatch macro for operations of the detector algorithm (DA) */
+#define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
 
 AliHLTComponent::AliHLTComponent()
   :
@@ -51,7 +61,8 @@ AliHLTComponent::AliHLTComponent()
   fpOutputBuffer(NULL),
   fOutputBufferSize(0),
   fOutputBufferFilled(0),
-  fOutputBlocks()
+  fOutputBlocks(),
+  fpStopwatches(new TObjArray(kSWTypeCount))
 {
   // see header file for class documentation
   // or
@@ -61,6 +72,7 @@ AliHLTComponent::AliHLTComponent()
   memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
   if (fgpComponentHandler)
     fgpComponentHandler->ScheduleRegister(this);
+  SetLocalLoggingLevel(kHLTLogDefault);
 }
 
 AliHLTComponent::AliHLTComponent(const AliHLTComponent&)
@@ -79,7 +91,8 @@ AliHLTComponent::AliHLTComponent(const AliHLTComponent&)
   fpOutputBuffer(NULL),
   fOutputBufferSize(0),
   fOutputBufferFilled(0),
-  fOutputBlocks()
+  fOutputBlocks(),
+  fpStopwatches(NULL)
 {
   // see header file for class documentation
   HLTFatal("copy constructor untested");
@@ -95,6 +108,9 @@ AliHLTComponent& AliHLTComponent::operator=(const AliHLTComponent&)
 AliHLTComponent::~AliHLTComponent()
 {
   // see header file for function documentation
+  CleanupInputObjects();
+  if (fpStopwatches!=NULL) delete fpStopwatches;
+  fpStopwatches=NULL;
 }
 
 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
@@ -124,8 +140,51 @@ int AliHLTComponent::Init( AliHLTComponentEnvironment* environ, void* environPar
     memcpy(&fEnvironment, environ, sizeof(AliHLTComponentEnvironment));
     fEnvironment.fParam=environParam;
   }
-  iResult=DoInit(argc, argv);
+  const char** pArguments=NULL;
+  int iNofChildArgs=0;
+  TString argument="";
+  int bMissingParam=0;
+  if (argc>0) {
+    pArguments=new const char*[argc];
+    if (pArguments) {
+      for (int i=0; i<argc && iResult>=0; i++) {
+       argument=argv[i];
+       if (argument.IsNull()) continue;
+
+       // benchmark
+       if (argument.CompareTo("benchmark")==0) {
+
+         // loglevel
+       } else if (argument.CompareTo("loglevel")==0) {
+         if ((bMissingParam=(++i>=argc))) break;
+         TString parameter(argv[i]);
+         parameter.Remove(TString::kLeading, ' '); // remove all blanks
+         if (parameter.BeginsWith("0x") &&
+             parameter.Replace(0,2,"",0).IsHex()) {
+           AliHLTComponentLogSeverity loglevel=kHLTLogNone;
+           sscanf(parameter.Data(),"%x", (unsigned int*)&loglevel);
+           SetLocalLoggingLevel(loglevel);
+         } else {
+           HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
+           iResult=-EINVAL;
+         }
+       } else {
+         pArguments[iNofChildArgs++]=argv[i];
+       }
+      }
+    } else {
+      iResult=-ENOMEM;
+    }
+  }
+  if (bMissingParam) {
+    HLTError("missing parameter for argument %s", argument.Data());
+    iResult=-EINVAL;
+  }
+  if (iResult>=0) {
+    iResult=DoInit(iNofChildArgs, pArguments);
+  }
   if (iResult>=0) fEventCount=0;
+  if (pArguments) delete [] pArguments;
   return iResult;
 }
 
@@ -143,12 +202,14 @@ int AliHLTComponent::DoInit( int argc, const char** argv )
   if (argc==0 && argv==NULL) {
     // this is currently just to get rid of the warning "unused parameter"
   }
+  fEventCount=0;
   return 0;
 }
 
 int AliHLTComponent::DoDeinit()
 {
   // see header file for function documentation
+  fEventCount=0;
   return 0;
 }
 
@@ -265,6 +326,7 @@ int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, vector<Al
 
 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format) const
 {
+  // see header file for function documentation
   const char* fmt="publisher \'%s\'";
   if (format) fmt=format;
   HLTMessage(fmt, (DataType2Text(dt)).c_str());
@@ -372,7 +434,7 @@ int AliHLTComponent::IncrementEventCounter()
   return fEventCount;
 }
 
-int AliHLTComponent::GetNumberOfInputBlocks()
+int AliHLTComponent::GetNumberOfInputBlocks() const
 {
   // see header file for function documentation
   if (fpInputBlocks!=NULL) {
@@ -386,11 +448,12 @@ const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataTyp
                                                    int bForce)
 {
   // see header file for function documentation
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
   fSearchDataType=dt;
   if (classname) fClassName=classname;
   else fClassName.clear();
-  int idx=FindInputBlock(fSearchDataType, 0);
-  //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
+  int idx=FindInputBlock(fSearchDataType, 0, 1);
+  HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
   TObject* pObj=NULL;
   if (idx>=0) {
     if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
@@ -407,6 +470,7 @@ const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
                                                    int         bForce)
 {
   // see header file for function documentation
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
   AliHLTComponentDataType dt;
   SetDataType(dt, dtID, dtOrigin);
   return GetFirstInputObject(dt, classname, bForce);
@@ -415,7 +479,8 @@ const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
 {
   // see header file for function documentation
-  int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
+  int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
   //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
   TObject* pObj=NULL;
   if (idx>=0) {
@@ -426,13 +491,18 @@ const TObject* AliHLTComponent::GetNextInputObject(int bForce)
   return pObj;
 }
 
-int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx)
+int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
 {
   // see header file for function documentation
   int iResult=-ENOENT;
   if (fpInputBlocks!=NULL) {
     int idx=startIdx<0?0:startIdx;
     for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
+      if (bObject!=0) {
+       if (fpInputBlocks[idx].fPtr==NULL) continue;
+       AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
+       if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
+      }
       if (dt == kAliHLTAnyDataType || fpInputBlocks[idx].fDataType == dt) {
        iResult=idx;
       }
@@ -450,15 +520,16 @@ TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
       if (fpInputBlocks[idx].fPtr) {
        AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
        if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
-         //HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
+         HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
          AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
-         pObj=msg.ReadObject(msg.GetClass());
-         if (pObj && msg.GetClass()) {
-           //HLTDebug("object %p type %s created", pObj, msg.GetClass()->GetName());
+         TClass* objclass=msg.GetClass();
+         pObj=msg.ReadObject(objclass);
+         if (pObj && objclass) {
+           HLTDebug("object %p type %s created", pObj, objclass->GetName());
          } else {
          }
-       } else {
-         //    } else if (bForce!=0) {
+         //} else {
+               } else if (bForce!=0) {
          HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
        }
       } else {
@@ -495,9 +566,24 @@ TObject* AliHLTComponent::GetInputObject(int idx, const char* classname, int bFo
   return pObj;
 }
 
+int AliHLTComponent::CleanupInputObjects()
+{
+  // see header file for function documentation
+  if (!fpInputObjects) return 0;
+  TObjArray* array=fpInputObjects;
+  fpInputObjects=NULL;
+  for (int i=0; i<array->GetEntries(); i++) {
+    TObject* pObj=array->At(i);
+    if (pObj) delete pObj;
+  }
+  delete array;
+  return 0;
+}
+
 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
 {
   // see header file for function documentation
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
   AliHLTComponentDataType dt=kAliHLTVoidDataType;
   int idx=fCurrentInputBlock;
   if (pObject) {
@@ -519,6 +605,7 @@ AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
 {
   // see header file for function documentation
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
   AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
   int idx=fCurrentInputBlock;
   if (pObject) {
@@ -540,6 +627,7 @@ AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
 {
   // see header file for function documentation
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
   fSearchDataType=dt;
   fClassName.clear();
   int idx=FindInputBlock(fSearchDataType, 0);
@@ -547,6 +635,7 @@ const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLT
   if (idx>=0) {
     // check for fpInputBlocks pointer done in FindInputBlock
     pBlock=&fpInputBlocks[idx];
+    fCurrentInputBlock=idx;
   }
   return pBlock;
 }
@@ -555,6 +644,7 @@ const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char*
                                                                    const char* dtOrigin)
 {
   // see header file for function documentation
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
   AliHLTComponentDataType dt;
   SetDataType(dt, dtID, dtOrigin);
   return GetFirstInputBlock(dt);
@@ -563,16 +653,18 @@ const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char*
 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
 {
   // see header file for function documentation
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
   int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
   const AliHLTComponentBlockData* pBlock=NULL;
   if (idx>=0) {
     // check for fpInputBlocks pointer done in FindInputBlock
     pBlock=&fpInputBlocks[idx];
+    fCurrentInputBlock=idx;
   }
   return pBlock;
 }
 
-int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock)
+int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
 {
   // see header file for function documentation
   int iResult=-ENOENT;
@@ -591,6 +683,7 @@ int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock)
 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
 {
   // see header file for function documentation
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
   AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
   int idx=fCurrentInputBlock;
   if (pBlock) {
@@ -609,6 +702,7 @@ AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData*
 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
 {
   // see header file for function documentation
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
   int iResult=0;
   if (pObject) {
     AliHLTMessage msg(kMESS_OBJECT);
@@ -618,7 +712,7 @@ int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& d
       msg.SetLength(); // sets the length to the first (reserved) word
       iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec);
       if (iResult>=0) {
-       //HLTDebug("object %s (%p) inserted to output", pObject->ClassName(), pObject);
+       HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
       }
     } else {
       HLTError("object serialization failed for object %p", pObject);
@@ -633,6 +727,7 @@ int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& d
 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec)
 {
   // see header file for function documentation
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
   AliHLTComponentDataType dt;
   SetDataType(dt, dtID, dtOrigin);
   return PushBack(pObject, dt, spec);
@@ -641,12 +736,14 @@ int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dt
 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
 {
   // see header file for function documentation
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
   return InsertOutputBlock(pBuffer, iSize, dt, spec);
 }
 
 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec)
 {
   // see header file for function documentation
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
   AliHLTComponentDataType dt;
   SetDataType(dt, dtID, dtOrigin);
   return PushBack(pBuffer, iSize, dt, spec);
@@ -657,7 +754,7 @@ int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iSize, const AliHLTCom
   // see header file for function documentation
   int iResult=0;
   if (pBuffer) {
-    if (fpOutputBuffer && (fOutputBufferSize-fOutputBufferFilled)) {
+    if (fpOutputBuffer && iSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
       AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
       AliHLTComponentBlockData bd;
       FillBlockData( bd );
@@ -688,6 +785,15 @@ int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iSize, const AliHLTCom
   return iResult;
 }
 
+int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
+{
+  // see header file for function documentation
+  if (!pObject) return -EINVAL;
+    AliHLTMessage msg(kMESS_OBJECT);
+    msg.WriteObject(pObject);
+    return msg.Length();  
+}
+
 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
 {
   // see header file for function documentation
@@ -707,6 +813,7 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
                                   AliHLTComponentEventDoneData*& edd )
 {
   // see header file for function documentation
+  ALIHLTCOMPONENT_BASE_STOPWATCH();
   int iResult=0;
   fCurrentEvent=evtData.fEventID;
   fCurrentEventData=evtData;
@@ -719,7 +826,10 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
   fOutputBlocks.clear();
   
   vector<AliHLTComponentBlockData> blockData;
-  iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
+  { // dont delete, sets the scope for the stopwatch guard
+    ALIHLTCOMPONENT_DA_STOPWATCH();
+    iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
+  } // end of the scope of the stopwatch guard
   if (iResult>=0) {
     if (fOutputBlocks.size()>0) {
       //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
@@ -741,7 +851,105 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
     outputBlockCnt=0;
     outputBlocks=NULL;
   }
+  CleanupInputObjects();
   IncrementEventCounter();
   return iResult;
 }
 
+AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
+  :
+  fpStopwatch(NULL),
+  fpPrec(NULL)
+{
+  // standard constructor (not for use)
+}
+
+AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
+  :
+  fpStopwatch(pStopwatch),
+  fpPrec(NULL)
+{
+  // constructor
+
+  // check for already existing guard
+  if (fgpCurrent) fpPrec=fgpCurrent;
+  fgpCurrent=this;
+
+  // stop the preceeding guard if it controls a different stopwatch
+  int bStart=1;
+  if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
+
+  // start the stopwatch if the current guard controls a different one
+  if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
+}
+
+AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(AliHLTStopwatchGuard&)
+  :
+  fpStopwatch(NULL),
+  fpPrec(NULL)
+{
+  // copy constructor (not for use)
+}
+
+AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
+
+AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
+{
+  // destructor
+
+  // resume the preceeding guard if it controls a different stopwatch
+  int bStop=1;
+  if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
+
+  // stop the stopwatch if the current guard controls a different one
+  if (fpStopwatch && bStop==1) fpStopwatch->Stop();
+
+  // resume to the preceeding guard
+  fgpCurrent=fpPrec;
+}
+
+int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
+{
+  // see header file for function documentation
+  if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
+  return fpStopwatch!=pSucc?1:0;
+}
+
+int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
+{
+  // see header file for function documentation
+  if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
+  return fpStopwatch!=pSucc?1:0;
+}
+
+int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type) 
+{
+  // see header file for function documentation
+  int iResult=0;
+  if (pSW!=NULL && type<kSWTypeCount) {
+    if (fpStopwatches) {
+      TObject* pObj=fpStopwatches->At((int)type);
+      if (pSW==NULL        // explicit reset
+         || pObj==NULL) { // explicit set
+       fpStopwatches->AddAt(pSW, (int)type);
+      } else if (pObj!=pSW) {
+       HLTWarning("stopwatch %d already set, reset first", (int)type);
+       iResult=-EBUSY;
+      }
+    }
+  } else {
+    iResult=-EINVAL;
+  }
+  return iResult;
+}
+
+int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
+{
+  // see header file for function documentation
+  if (pStopwatches==NULL) return -EINVAL;
+
+  int iResult=0;
+  for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
+    SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
+  return iResult;
+}