]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/BASE/AliHLTComponent.cxx
adding a member for the active CTP trigger mask to the CTP data object and setting...
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTComponent.cxx
index 8f138354b7d89df8f6ffa36e47f94878ead9f38e..e3e37ff72ad5156de68d77a75fb9033f01ca2dba 100644 (file)
@@ -30,15 +30,25 @@ using namespace std;
 #include "AliHLTComponent.h"
 #include "AliHLTComponentHandler.h"
 #include "AliHLTMessage.h"
+#include "AliHLTCTPData.h"
 #include "TString.h"
 #include "TMath.h"
 #include "TObjArray.h"
 #include "TObjectTable.h"
 #include "TClass.h"
 #include "TStopwatch.h"
+#include "TFormula.h"
 #include "AliHLTMemoryFile.h"
 #include "AliHLTMisc.h"
 #include <cassert>
+#include <ctime>
+#include <stdint.h>
+
+/**
+ * default compression level for ROOT objects
+ */
+#define ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION 5
+#define ALIHLTCOMPONENT_STATTIME_SCALER 1000000
 
 /** ROOT macro for the implementation of ROOT specific class methods */
 ClassImp(AliHLTComponent);
@@ -77,7 +87,13 @@ AliHLTComponent::AliHLTComponent()
   fChainIdCrc(0),
   fpBenchmark(NULL),
   fRequireSteeringBlocks(false),
-  fEventType(gkAliEventTypeUnknown)
+  fEventType(gkAliEventTypeUnknown),
+  fComponentArgs(),
+  fEventDoneData(NULL),
+  fEventDoneDataSize(0),
+  fCompressionLevel(ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION)
+  , fLastObjectSize(0)
+  , fpCTPData(NULL)
 {
   // see header file for class documentation
   // or
@@ -116,6 +132,14 @@ AliHLTComponent::~AliHLTComponent()
     delete fpRunDesc;
     fpRunDesc=NULL;
   }
+  if (fEventDoneData)
+    delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
+  fEventDoneData=NULL;
+
+  if (fpCTPData) {
+    delete fpCTPData;
+  }
+  fpCTPData=NULL;
 }
 
 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
@@ -137,10 +161,10 @@ int AliHLTComponent::UnsetGlobalComponentHandler()
   return SetGlobalComponentHandler(NULL,1);
 }
 
-int AliHLTComponent::Init(const AliHLTAnalysisEnvironment* comenv, void* environParam, int argc, const char** argv )
+int AliHLTComponent::SetComponentEnvironment(const AliHLTAnalysisEnvironment* comenv, void* environParam)
 {
   // see header file for function documentation
-  HLTLogKeyword(GetComponentID());
+  HLTLogKeyword(fChainId.c_str());
   int iResult=0;
   if (comenv) {
     memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
@@ -148,6 +172,18 @@ int AliHLTComponent::Init(const AliHLTAnalysisEnvironment* comenv, void* environ
     fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
     fEnvironment.fParam=environParam;
   }
+  return iResult;
+}
+
+int AliHLTComponent::Init(const AliHLTAnalysisEnvironment* comenv, void* environParam, int argc, const char** argv )
+{
+  // see header file for function documentation
+  HLTLogKeyword(fChainId.c_str());
+  int iResult=0;
+  if (comenv) {
+    SetComponentEnvironment(comenv, environParam);
+  }
+  fComponentArgs="";
   const char** pArguments=NULL;
   int iNofChildArgs=0;
   TString argument="";
@@ -156,26 +192,39 @@ int AliHLTComponent::Init(const AliHLTAnalysisEnvironment* comenv, void* environ
     pArguments=new const char*[argc];
     if (pArguments) {
       for (int i=0; i<argc && iResult>=0; i++) {
+       if (fComponentArgs.size()>0) fComponentArgs+=" ";
+       fComponentArgs+=argv[i];
        argument=argv[i];
        if (argument.IsNull()) continue;
 
        // benchmark
-       if (argument.CompareTo("benchmark")==0) {
+       if (argument.CompareTo("-benchmark")==0) {
 
-         // loglevel
-       } else if (argument.CompareTo("loglevel")==0) {
-         if ((bMissingParam=(++i>=argc))) break;
-         TString parameter(argv[i]);
+         // -loglevel=
+       } else if (argument.BeginsWith("-loglevel=")) {
+         TString parameter=argument.ReplaceAll("-loglevel=", "");
          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);
+           unsigned int loglevel=kHLTLogNone;
+           sscanf(parameter.Data(),"%x", &loglevel);
+           SetLocalLoggingLevel((AliHLTComponentLogSeverity)loglevel);
          } else {
            HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
            iResult=-EINVAL;
          }
+         // -object-compression=
+       } else if (argument.BeginsWith("-object-compression=")) {
+         argument.ReplaceAll("-object-compression=", "");
+         if (argument.IsDigit()) {
+           fCompressionLevel=argument.Atoi();
+           if (fCompressionLevel<0 || fCompressionLevel>9) {
+             HLTWarning("invalid compression level %d, setting to default %d", fCompressionLevel, ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION);
+             fCompressionLevel=ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION;
+           }
+         } else {
+           HLTError("wrong parameter for argument -object-compression, number expected");
+         }
        } else {
          pArguments[iNofChildArgs++]=argv[i];
        }
@@ -213,9 +262,6 @@ int AliHLTComponent::Init(const AliHLTAnalysisEnvironment* comenv, void* environ
 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
   // benchmarking stopwatch for the component statistics
   fpBenchmark=new TStopwatch;
-  if (fpBenchmark) {
-    fpBenchmark->Start();
-  }
 #endif
 
   return iResult;
@@ -224,15 +270,26 @@ int AliHLTComponent::Init(const AliHLTAnalysisEnvironment* comenv, void* environ
 int AliHLTComponent::Deinit()
 {
   // see header file for function documentation
-  HLTLogKeyword(GetComponentID());
+  HLTLogKeyword(fChainId.c_str());
   int iResult=0;
   iResult=DoDeinit();
   if (fpRunDesc) {
-    HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
+    // TODO: the warning should be kept, but the condition is wrong since the
+    // AliHLTRunDesc is set before the SOR event in the SetRunDescription
+    // method. A couple of state flags should be defined but that is a bit more
+    // work to do. For the moment disable the warning (2009-07-01)
+    // 2009-09-08: now, the info is not cleared in the ProcessEvent, because it
+    // might be needed by components during the event processing.
+    //HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
     AliHLTRunDesc* pRunDesc=fpRunDesc;
     fpRunDesc=NULL;
     delete pRunDesc;
   }
+  if (fpCTPData) {
+    delete fpCTPData;
+  }
+  fpCTPData=NULL;
+
   fEventCount=0;
   return iResult;
 }
@@ -241,11 +298,14 @@ int AliHLTComponent::InitCDB(const char* cdbPath, AliHLTComponentHandler* pHandl
 {
   // see header file for function documentation
   int iResult=0;
+  HLTInfo("Using CDB: %s", cdbPath);
   if (pHandler) {
   // I have to think about separating the library handling from the
-  // component handler. Requiring the component hanlder here is not
+  // component handler. Requiring the component handler here is not
   // the cleanest solution.
-  // We presume the library already to be loaded
+  // We presume the library already to be loaded, which is the case
+  // because it is loaded in the initialization of the logging functionality
+  //
   // find the symbol
   AliHLTMiscInitCDB_t pFunc=(AliHLTMiscInitCDB_t)pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_INIT_CDB);
   if (pFunc) {
@@ -338,6 +398,100 @@ int AliHLTComponent::SetComponentDescription(const char* desc)
   return iResult;
 }
 
+int AliHLTComponent::ConfigureFromArgumentString(int argc, const char** argv)
+{
+  // see header file for function documentation
+  int iResult=0;
+  vector<const char*> array;
+  TObjArray choppedArguments;
+  TString argument="";
+  int i=0;
+  for (i=0; i<argc && iResult>=0; i++) {
+    argument=argv[i];
+    if (argument.IsNull()) continue;
+    TObjArray* pTokens=argument.Tokenize(" ");
+    if (pTokens) {
+      if (pTokens->GetEntriesFast()>0) {
+       for (int n=0; n<pTokens->GetEntriesFast(); n++) {
+         choppedArguments.AddLast(pTokens->At(n));
+         TString data=((TObjString*)pTokens->At(n))->GetString();
+         if (!data.IsNull()) {
+           array.push_back(data.Data());
+         }
+       }
+       pTokens->SetOwner(kFALSE);
+      }
+      delete pTokens;
+    }
+  }
+
+  for (i=0; (unsigned)i<array.size() && iResult>=0;) {
+    int result=ScanConfigurationArgument(array.size()-i, &array[i]);
+    if (result==0) {
+      HLTWarning("unknown component argument %s", array[i]);
+      i++;
+    } else if (result>0) {
+      i+=result;
+    } else {
+      iResult=result;
+      if (iResult==-EINVAL) {
+       HLTError("unknown argument %s", array[i]);
+      } else if (iResult==-EPROTO) {
+       HLTError("missing/wrong parameter for argument %s (%s)", array[i], (array.size()>(unsigned)i+1)?array[i+1]:"missing");
+      } else {
+       HLTError("scan of argument %s failed (%d)", array[i], iResult);
+      }
+    }
+  }
+
+  return iResult;
+}
+
+int AliHLTComponent::ConfigureFromCDBTObjString(const char* entries)
+{
+  // see header file for function documentation
+  int iResult=0;
+  TString arguments;
+  TString confEntries=entries;
+  TObjArray* pTokens=confEntries.Tokenize(" ");
+  if (pTokens) {
+    for (int n=0; n<pTokens->GetEntriesFast(); n++) {
+      const char* path=((TObjString*)pTokens->At(n))->GetString().Data();
+      const char* chainId=GetChainId();
+      HLTInfo("configure from entry %s, chain id %s", path, (chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
+      TObject* pOCDBObject = LoadAndExtractOCDBObject(path);
+      if (pOCDBObject) {
+       TObjString* pString=dynamic_cast<TObjString*>(pOCDBObject);
+       if (pString) {
+         HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
+         arguments+=pString->GetString().Data();
+         arguments+=" ";
+       } else {
+         HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
+         iResult=-EINVAL;
+       }
+      } else {
+       HLTError("can not fetch object \"%s\" from OCDB", path);
+       iResult=-ENOENT;
+      }
+    }
+    delete pTokens;
+  }
+  if (iResult>=0 && !arguments.IsNull())  {
+    const char* array=arguments.Data();
+    iResult=ConfigureFromArgumentString(1, &array);
+  }
+  return iResult;
+}
+
+TObject* AliHLTComponent::LoadAndExtractOCDBObject(const char* path, int version, int subVersion)
+{
+  // see header file for function documentation
+  AliCDBEntry* pEntry=AliHLTMisc::Instance().LoadOCDBEntry(path, GetRunNo(), version, subVersion);
+  if (!pEntry) return NULL;
+  return AliHLTMisc::Instance().ExtractObject(pEntry);
+}
+
 int AliHLTComponent::DoInit( int /*argc*/, const char** /*argv*/)
 {
   // default implementation, childs can overload
@@ -366,6 +520,14 @@ int AliHLTComponent::ReadPreprocessorValues(const char* /*modules*/)
   return 0;
 }
 
+int AliHLTComponent::ScanConfigurationArgument(int /*argc*/, const char** /*argv*/)
+{
+  // default implementation, childs can overload
+  HLTLogKeyword("dummy");
+  HLTWarning("The function needs to be implemented by the component");
+  return 0;
+}
+
 int AliHLTComponent::StartOfRun()
 {
   // default implementation, childs can overload
@@ -510,6 +672,53 @@ int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventD
   return -ENOSYS;
 }
 
+int AliHLTComponent::ReserveEventDoneData( unsigned long size )
+{
+  // see header file for function documentation
+  int iResult=0;
+
+  unsigned long capacity=fEventDoneDataSize;
+  if (fEventDoneData) capacity-=sizeof(AliHLTComponentEventDoneData)+fEventDoneData->fDataSize;
+  if (size>capacity) {
+    unsigned long newSize=sizeof(AliHLTComponentEventDoneData)+size+(fEventDoneDataSize-capacity);
+    AliHLTComponentEventDoneData* newEDD = reinterpret_cast<AliHLTComponentEventDoneData*>( new AliHLTUInt8_t[newSize] );
+    if (!newEDD)
+      return -ENOMEM;
+    newEDD->fStructSize = sizeof(AliHLTComponentEventDoneData);
+    newEDD->fDataSize = 0;
+    newEDD->fData = reinterpret_cast<AliHLTUInt8_t*>(newEDD)+newEDD->fStructSize;
+    if (fEventDoneData) {
+      memcpy( newEDD->fData, fEventDoneData->fData, fEventDoneData->fDataSize );
+      newEDD->fDataSize = fEventDoneData->fDataSize;
+      delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
+    }
+    fEventDoneData = newEDD;
+    fEventDoneDataSize = newSize;
+  }
+  return iResult;
+
+}
+
+int AliHLTComponent::PushEventDoneData( AliHLTUInt32_t eddDataWord )
+{
+  if (!fEventDoneData)
+    return -ENOMEM;
+  if (fEventDoneData->fDataSize+sizeof(AliHLTUInt32_t)>fEventDoneDataSize)
+    return -ENOSPC;
+  *reinterpret_cast<AliHLTUInt32_t*>((reinterpret_cast<AliHLTUInt8_t*>(fEventDoneData->fData)+fEventDoneData->fDataSize)) = eddDataWord;
+  fEventDoneData->fDataSize += sizeof(AliHLTUInt32_t);
+  return 0;
+}
+
+void AliHLTComponent::ReleaseEventDoneData()
+{
+  if (fEventDoneData)
+    delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
+  fEventDoneData = NULL;
+  fEventDoneDataSize = 0;
+}
+
+
 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList) 
 {
   // see header file for function documentation
@@ -518,7 +727,7 @@ int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTCom
     AliHLTComponentDataTypeList itypes;
     AliHLTComponentDataTypeList otypes;
     otypes.push_back(GetOutputDataType());
-    if (otypes[0]==kAliHLTMultipleDataType) {
+    if (MatchExactly(otypes[0],kAliHLTMultipleDataType)) {
       otypes.clear();
       int count=0;
       if ((count=GetOutputDataTypes(otypes))>0) {
@@ -537,7 +746,7 @@ int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTCom
       }
       
       AliHLTComponentDataTypeList::iterator itype=itypes.begin();
-      for (;itype!=itypes.end() && (*itype)!=(*otype); itype++);
+      for ( ; itype!=itypes.end() && (*itype)!=(*otype) ; itype++) {/* empty body */};
       //if (itype!=itypes.end()) PrintDataTypeContent(*itype, "consumer \'%s\'");
       if (itype!=itypes.end()) {
        if (tgtList) tgtList->push_back(*otype);
@@ -677,6 +886,15 @@ int AliHLTComponent::GetNumberOfInputBlocks() const
   return 0;
 }
 
+AliHLTEventID_t AliHLTComponent::GetEventId() const
+{
+  // see header file for function documentation
+  if (fpInputBlocks!=NULL) {
+    return fCurrentEventData.fEventID;
+  }
+  return 0;
+}
+
 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
                                                    const char* classname,
                                                    int bForce)
@@ -812,7 +1030,17 @@ int AliHLTComponent::CleanupInputObjects()
     // (CreateInputObject), and written to a TFile afterwards, the
     // TFile::Close calls ROOOT's garbage collection. No clue why the
     // object ended up in the key list and needs to be deleted
-    if (pObj && gObjectTable->PtrIsValid(pObj)) delete pObj;
+    //
+    // Matthias 09.11.2008 follow up
+    // This approach doesn't actually work in all cases: the object table
+    // can be switched off globally, the flag needs to be checked here as
+    // well in order to avoid memory leaks.
+    // This means we have to find another solution for the problem if it
+    // pops up again.
+    if (pObj &&
+       (!TObject::GetObjectStat() || gObjectTable->PtrIsValid(pObj))) {
+      delete pObj;
+    }
   }
   delete array;
   return 0;
@@ -988,16 +1216,37 @@ int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& d
   // see header file for function documentation
   ALIHLTCOMPONENT_BASE_STOPWATCH();
   int iResult=0;
+  fLastObjectSize=0;
   if (pObject) {
     AliHLTMessage msg(kMESS_OBJECT);
+    msg.SetCompressionLevel(fCompressionLevel);
     msg.WriteObject(pObject);
     Int_t iMsgLength=msg.Length();
     if (iMsgLength>0) {
+      // Matthias Sep 2008
+      // NOTE: AliHLTMessage does implement it's own SetLength method
+      // which is not architecture independent. The original SetLength
+      // stores the size always in network byte order.
+      // I'm trying to remember the rational for that, might be that
+      // it was just some lack of knowledge. Want to change this, but
+      // has to be done carefullt to be backward compatible.
       msg.SetLength(); // sets the length to the first (reserved) word
-      iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec, pHeader, headerSize);
+
+      // does nothing if the level is 0
+      msg.Compress();
+
+      char *mbuf = msg.Buffer();
+      if (msg.CompBuffer()) {
+       msg.SetLength(); // set once more to have to byte order
+       mbuf = msg.CompBuffer();
+       iMsgLength = msg.CompLength();
+      }
+      assert(mbuf!=NULL);
+      iResult=InsertOutputBlock(mbuf, iMsgLength, dt, spec, pHeader, headerSize);
       if (iResult>=0) {
-       HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
+       HLTDebug("object %s (%p) size %d compression %d inserted to output", pObject->ClassName(), pObject, iMsgLength, msg.GetCompressionLevel());
       }
+      fLastObjectSize=iMsgLength;
     } else {
       HLTError("object serialization failed for object %p", pObject);
       iResult=-ENOMSG;
@@ -1018,16 +1267,16 @@ int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dt
   return PushBack(pObject, dt, spec, pHeader, headerSize);
 }
 
-int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
-                             void* pHeader, int headerSize)
+int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
+                             const void* pHeader, int headerSize)
 {
   // see header file for function documentation
   ALIHLTCOMPONENT_BASE_STOPWATCH();
   return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
 }
 
-int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
-                             void* pHeader, int headerSize)
+int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
+                             const void* pHeader, int headerSize)
 {
   // see header file for function documentation
   ALIHLTCOMPONENT_BASE_STOPWATCH();
@@ -1036,47 +1285,54 @@ int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const
   return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
 }
 
-int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
-                             void* pHeader, int iHeaderSize)
+int AliHLTComponent::InsertOutputBlock(const void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
+                                      const void* pHeader, int iHeaderSize)
 {
   // see header file for function documentation
   int iResult=0;
   int iBlkSize = iBufferSize + iHeaderSize;
-  if (pBuffer) {
+
+  if ((pBuffer!=NULL && iBufferSize>0) || (pHeader!=NULL && iHeaderSize>0)) {
     if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
       AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
-      AliHLTComponentBlockData bd;
-      FillBlockData( bd );
-      bd.fOffset        = fOutputBufferFilled;
-      bd.fSize          = iBlkSize;
-      bd.fDataType      = dt;
-      bd.fSpecification = spec;
+
+      // copy header if provided but skip if the header is the target location
+      // in that case it has already been copied
       if (pHeader!=NULL && pHeader!=pTgt) {
        memcpy(pTgt, pHeader, iHeaderSize);
       }
 
       pTgt += (AliHLTUInt8_t) iHeaderSize;
 
+      // copy buffer if provided but skip if buffer is the target location
+      // in that case it has already been copied
       if (pBuffer!=NULL && pBuffer!=pTgt) {
        memcpy(pTgt, pBuffer, iBufferSize);
        
        //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer); 
        //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
       }
-      fOutputBufferFilled+=bd.fSize;
-      fOutputBlocks.push_back( bd );
       //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
     } else {
       if (fpOutputBuffer) {
-       HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iBlkSize);
+       HLTError("too little space in output buffer: %d of %d, required %d", fOutputBufferSize-fOutputBufferFilled, fOutputBufferSize, iBlkSize);
       } else {
        HLTError("output buffer not available");
       }
       iResult=-ENOSPC;
     }
-  } else {
-    iResult=-EINVAL;
   }
+  if (iResult>=0) {
+    AliHLTComponentBlockData bd;
+    FillBlockData( bd );
+    bd.fOffset        = fOutputBufferFilled;
+    bd.fSize          = iBlkSize;
+    bd.fDataType      = dt;
+    bd.fSpecification = spec;
+    fOutputBlocks.push_back( bd );
+    fOutputBufferFilled+=bd.fSize;
+  }
+
   return iResult;
 }
 
@@ -1222,12 +1478,39 @@ int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
   return iResult;
 }
 
-int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData /*edd*/)
+int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
 {
   // see header file for function documentation
-  int iResult=-ENOSYS;
-  //#warning  function not yet implemented
-  HLTWarning("function not yet implemented");
+  int iResult=0;
+
+  AliHLTComponentEventDoneData* newEDD = NULL;
+  
+  unsigned long newSize=edd.fDataSize;
+  if (fEventDoneData)
+    newSize += fEventDoneData->fDataSize;
+
+  if (newSize>fEventDoneDataSize) {
+    newEDD = reinterpret_cast<AliHLTComponentEventDoneData*>( new AliHLTUInt8_t[ sizeof(AliHLTComponentEventDoneData)+newSize ] );
+    if (!newEDD)
+      return -ENOMEM;
+    newEDD->fStructSize = sizeof(AliHLTComponentEventDoneData);
+    newEDD->fDataSize = newSize;
+    newEDD->fData = reinterpret_cast<AliHLTUInt8_t*>(newEDD)+newEDD->fStructSize;
+    unsigned long long offset = 0;
+    if (fEventDoneData) {
+      memcpy( newEDD->fData, fEventDoneData->fData, fEventDoneData->fDataSize );
+      offset += fEventDoneData->fDataSize;
+    }
+    memcpy( reinterpret_cast<AliHLTUInt8_t*>(newEDD->fData)+offset, edd.fData, edd.fDataSize );
+    if (fEventDoneData)
+      delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
+    fEventDoneData = newEDD;
+    fEventDoneDataSize = newSize;
+  }
+  else {
+    memcpy( reinterpret_cast<AliHLTUInt8_t*>(fEventDoneData->fData)+fEventDoneData->fDataSize, edd.fData, edd.fDataSize );
+    fEventDoneData->fDataSize += edd.fDataSize;
+  }
   return iResult;
 }
 
@@ -1241,7 +1524,7 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
                                   AliHLTComponentEventDoneData*& edd )
 {
   // see header file for function documentation
-  HLTLogKeyword(GetComponentID());
+  HLTLogKeyword(fChainId.c_str());
   ALIHLTCOMPONENT_BASE_STOPWATCH();
   int iResult=0;
   fCurrentEvent=evtData.fEventID;
@@ -1255,16 +1538,25 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
   fOutputBlocks.clear();
   outputBlockCnt=0;
   outputBlocks=NULL;
+
+  AliHLTComponentBlockDataList forwardedBlocks;
+
+  // optional component statistics
   AliHLTComponentStatisticsList compStats;
+  bool bAddComponentTableEntry=false;
+  vector<AliHLTUInt32_t> parentComponentTables;
 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
   AliHLTComponentStatistics outputStat;
   memset(&outputStat, 0, sizeof(AliHLTComponentStatistics));
+  outputStat.fStructSize=sizeof(AliHLTComponentStatistics);
   outputStat.fId=fChainIdCrc;
-  compStats.push_back(outputStat);
   if (fpBenchmark) {
+    fpBenchmark->Stop();
+    outputStat.fComponentCycleTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
     fpBenchmark->Reset();
     fpBenchmark->Start();
   }
+  compStats.push_back(outputStat);
 #endif
 
   // data processing is skipped
@@ -1287,6 +1579,7 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
     int indexUpdtDCSEvent=-1;
     int indexSOREvent=-1;
     int indexEOREvent=-1;
+    int indexECSParamBlock=-1;
     for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
       if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
        indexSOREvent=i;
@@ -1334,6 +1627,11 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
            compStats.push_back(*pStat);
          }
        }
+      } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentTable) {
+       forwardedBlocks.push_back(fpInputBlocks[i]);
+       parentComponentTables.push_back(fpInputBlocks[i].fSpecification);
+      } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeECSParam) {
+       indexECSParamBlock=i;
       } else {
        // the processing function is called if there is at least one
        // non-steering data block. Steering blocks are not filtered out
@@ -1348,6 +1646,7 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
 
     if (indexSOREvent>=0) {
       // start of run
+      bAddComponentTableEntry=true;
       if (fpRunDesc==NULL) {
        fpRunDesc=new AliHLTRunDesc;
        if (fpRunDesc) *fpRunDesc=kAliHLTVoidRunDesc;
@@ -1366,8 +1665,29 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
       } else {
        iResult=-ENOMEM;
       }
+
+      if (indexECSParamBlock>=0) {
+       if (fpInputBlocks[indexECSParamBlock].fSize>0) {
+         const char* param=reinterpret_cast<const char*>(fpInputBlocks[indexECSParamBlock].fPtr);
+         TString paramString;
+         if (param[fpInputBlocks[indexECSParamBlock].fSize-1]!=0) {
+           HLTWarning("ECS parameter string not terminated");
+           paramString.Insert(0, param, fpInputBlocks[indexECSParamBlock].fSize);
+           paramString+="";
+         } else {
+           paramString=param;
+         }
+         ScanECSParam(paramString.Data());
+       } else {
+         HLTWarning("empty ECS parameter received");
+       }
+      } else {
+       // TODO: later on we might throw a warning here since the CTP trigger classes
+       // should be mandatory
+      }
     }
     if (indexEOREvent>=0) {
+      bAddComponentTableEntry=true;
       if (fpRunDesc!=NULL) {
        if (fpRunDesc) {
          AliHLTRunDesc rundesc;
@@ -1378,9 +1698,10 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
              HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
            }
          }
-         AliHLTRunDesc* pRunDesc=fpRunDesc;
-         fpRunDesc=NULL;
-         delete pRunDesc;
+         // we do not unload the fpRunDesc struct here in order to have the run information
+         // available during the event processing
+         // https://savannah.cern.ch/bugs/?39711
+         // the info will be cleared in DeInit
        }
       } else {
        HLTWarning("did not receive SOR, ignoring EOR");
@@ -1418,6 +1739,13 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
   // for the private blocks
   if (fRequireSteeringBlocks) bSkipDataProcessing=0;
 
+  if (fpCTPData) {
+    // set the active triggers for this event
+    fpCTPData->SetTriggers(trigData);
+    // increment CTP trigger counters if available
+    if (IsDataEvent()) fpCTPData->Increment(trigData);
+  }
+
   AliHLTComponentBlockDataList blockData;
   if (iResult>=0 && !bSkipDataProcessing)
   { // dont delete, sets the scope for the stopwatch guard
@@ -1450,10 +1778,17 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
          HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
          iResult=-EFAULT;
        } else {
-         if (compStats.size()>0) {
+         if (compStats.size()>0 && IsDataEvent()) {
            int offset=AddComponentStatistics(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, compStats);
            if (offset>0) fOutputBufferFilled+=offset;
          }
+         if (bAddComponentTableEntry) {
+           int offset=AddComponentTableEntry(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, parentComponentTables);
+           if (offset>0) size+=offset;
+         }
+         if (forwardedBlocks.size()>0) {
+           fOutputBlocks.insert(fOutputBlocks.end(), forwardedBlocks.begin(), forwardedBlocks.end());
+         }
          iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
          size=fOutputBufferFilled;
        }
@@ -1464,6 +1799,13 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
        int offset=AddComponentStatistics(blockData, fpOutputBuffer, fOutputBufferSize, size, compStats);
        if (offset>0) size+=offset;
       }
+      if (bAddComponentTableEntry) {
+       int offset=AddComponentTableEntry(blockData, fpOutputBuffer, fOutputBufferSize, size, parentComponentTables);
+       if (offset>0) size+=offset;
+      }
+      if (forwardedBlocks.size()>0) {
+       blockData.insert(blockData.end(), forwardedBlocks.begin(), forwardedBlocks.end());
+      }
       iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
     }
     if (iResult<0) {
@@ -1482,6 +1824,12 @@ int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
     // no output blocks, set size to 0
     size=0;
   }
+
+  // reset the internal EventData struct
+  FillEventData(fCurrentEventData);
+
+  // reset the active triggers
+  if (fpCTPData) fpCTPData->SetTriggers(0);
   return iResult;
 }
 
@@ -1493,13 +1841,38 @@ int  AliHLTComponent::AddComponentStatistics(AliHLTComponentBlockDataList& block
 {
   // see header file for function documentation
   int iResult=0;
+#if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
   if (stats.size()==0) return -ENOENT;
+  // check if there is space for at least one entry
+  if (offset+sizeof(AliHLTComponentStatistics)>bufferSize) return 0;
   stats[0].fTotalOutputSize=offset;
   stats[0].fOutputBlockCount=blocks.size();
   if (fpBenchmark) {
-    stats[0].fTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*1000000);
-    stats[0].fCTime=(AliHLTUInt32_t)(fpBenchmark->CpuTime()*1000000);
+    fpBenchmark->Stop();
+    stats[0].fTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
+    stats[0].fCTime=(AliHLTUInt32_t)(fpBenchmark->CpuTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
+    fpBenchmark->Continue();
   }
+  if (offset+stats.size()*sizeof(AliHLTComponentStatistics)>bufferSize) {
+    AliHLTUInt32_t removedLevel=0;
+    do {
+      // remove all entries of the level of the last entry
+      removedLevel=stats.back().fLevel;
+      AliHLTComponentStatisticsList::iterator element=stats.begin();
+      element++;
+      while (element!=stats.end()) {
+       if (element->fLevel<=removedLevel) {
+         element=stats.erase(element);
+       } else {
+         element++;
+       }
+      }
+    } while (stats.size()>1 && 
+            (offset+stats.size()*sizeof(AliHLTComponentStatistics)>bufferSize));
+  }
+  assert(stats.size()>0);
+  if (stats.size()==0) return 0;
+
   if (offset+stats.size()*sizeof(AliHLTComponentStatistics)<=bufferSize) {
     AliHLTComponentBlockData bd;
     FillBlockData( bd );
@@ -1530,6 +1903,83 @@ int  AliHLTComponent::AddComponentStatistics(AliHLTComponentBlockDataList& block
     blocks.push_back(bd);
     iResult=bd.fSize;
   }
+#else
+  if (blocks.size() && buffer && bufferSize && offset && stats.size()) {
+    // get rid of warning
+  }
+#endif
+  return iResult;
+}
+
+int  AliHLTComponent::AddComponentTableEntry(AliHLTComponentBlockDataList& blocks, 
+                                            AliHLTUInt8_t* buffer,
+                                            AliHLTUInt32_t bufferSize,
+                                            AliHLTUInt32_t offset,
+                                            const vector<AliHLTUInt32_t>& parents) const
+{
+  // see header file for function documentation
+  int iResult=0;
+#if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
+  // the payload consists of the AliHLTComponentTableEntry struct,
+  // followed by a an array of 32bit crc chain ids and the component
+  // description string
+  unsigned int payloadSize=sizeof(AliHLTComponentTableEntry);
+  payloadSize+=parents.size()*sizeof(AliHLTUInt32_t);
+
+  // the component description has the following format:
+  // chain-id{component-id:arguments}
+  const char* componentId=const_cast<AliHLTComponent*>(this)->GetComponentID();
+  unsigned int descriptionSize=fChainId.size()+1;
+  descriptionSize+=2; // the '{}' around the component id
+  descriptionSize+=strlen(componentId);
+  descriptionSize+=1; // the ':' between component id and arguments
+  descriptionSize+=fComponentArgs.size();
+
+  payloadSize+=descriptionSize;
+  if (buffer && (offset+payloadSize<=bufferSize)) {
+    AliHLTUInt8_t* pTgt=buffer+offset;
+    memset(pTgt, 0, payloadSize);
+
+    // write entry
+    AliHLTComponentTableEntry* pEntry=reinterpret_cast<AliHLTComponentTableEntry*>(pTgt);
+    pEntry->fStructSize=sizeof(AliHLTComponentTableEntry);
+    pEntry->fNofParents=parents.size();
+    pEntry->fSizeDescription=descriptionSize;
+    pTgt=pEntry->fBuffer;
+
+    // write array of parents
+    if (parents.size()>0) {
+      unsigned int copy=parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type);
+      memcpy(pTgt, &parents[0], parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type));
+      pTgt+=copy;
+    }
+
+    // write component description
+    memcpy(pTgt, fChainId.c_str(), fChainId.size());
+    pTgt+=fChainId.size();
+    *pTgt++='{';
+    memcpy(pTgt, componentId, strlen(componentId));
+    pTgt+=strlen(componentId);
+    *pTgt++=':';
+    memcpy(pTgt, fComponentArgs.c_str(), fComponentArgs.size());
+    pTgt+=fComponentArgs.size();
+    *pTgt++='}';
+    *pTgt++=0;
+
+    AliHLTComponentBlockData bd;
+    FillBlockData( bd );
+    bd.fOffset        = offset;
+    bd.fSize          = payloadSize;
+    bd.fDataType      = kAliHLTDataTypeComponentTable;
+    bd.fSpecification = fChainIdCrc;
+    blocks.push_back(bd);
+    iResult=bd.fSize;
+  }
+#else
+  if (blocks.size() && buffer && bufferSize && offset && parents.size()) {
+    // get rid of warning
+  }
+ #endif
   return iResult;
 }
 
@@ -1657,6 +2107,34 @@ AliHLTUInt32_t AliHLTComponent::GetRunType() const
   return fpRunDesc->fRunType;
 }
 
+AliHLTUInt32_t    AliHLTComponent::GetTimeStamp() const
+{
+  // see header file for function documentation
+  if (fCurrentEventData.fEventCreation_s) {
+    return  fCurrentEventData.fEventCreation_s;
+  }
+  // using the actual UTC if the time stamp was not set by the framework
+  return static_cast<AliHLTUInt32_t>(time(NULL));
+}
+
+AliHLTUInt32_t    AliHLTComponent::GetPeriodNumber() const
+{
+  // see header file for function documentation
+  return (GetEventId()>>36)&0xfffffff;
+}
+
+AliHLTUInt32_t    AliHLTComponent::GetOrbitNumber() const
+{
+  // see header file for function documentation
+  return (GetEventId()>>12)&0xffffff;
+}
+
+AliHLTUInt16_t    AliHLTComponent::GetBunchCrossNumber() const
+{
+  // see header file for function documentation
+  return GetEventId()&0xfff;
+}
+
 bool AliHLTComponent::IsDataEvent(AliHLTUInt32_t* pTgt)
 {
   // see header file for function documentation
@@ -1816,3 +2294,153 @@ AliHLTUInt32_t AliHLTComponent::CalculateChecksum(const AliHLTUInt8_t* buffer, i
    */
   return (remainder);
 }
+
+int AliHLTComponent::ExtractComponentTableEntry(const AliHLTUInt8_t* pBuffer, AliHLTUInt32_t size,
+                                               string& retChainId, string& retCompId, string& retCompArgs,
+                                               vector<AliHLTUInt32_t>& parents)
+{
+  // see header file for function documentation
+  retChainId.clear();
+  retCompId.clear();
+  retCompArgs.clear();
+  parents.clear();
+  if (!pBuffer || size==0) return 0;
+
+  const AliHLTComponentTableEntry* pEntry=reinterpret_cast<const AliHLTComponentTableEntry*>(pBuffer);
+  if (size<8/* the initial size of the structure*/ ||
+      pEntry==NULL || pEntry->fStructSize<8) return -ENOMSG;
+  const AliHLTUInt32_t* pParents=reinterpret_cast<const AliHLTUInt32_t*>(pEntry->fBuffer);
+  const AliHLTUInt8_t* pEnd=pBuffer+size;
+
+  if (pParents+pEntry->fNofParents>=reinterpret_cast<const AliHLTUInt32_t*>(pEnd)) return -ENODEV;
+  for (unsigned int i=0; i<pEntry->fNofParents; i++, pParents++) {
+    parents.push_back(*pParents);
+  }
+
+  const char* pDescription=reinterpret_cast<const char*>(pParents);
+  if (pDescription+pEntry->fSizeDescription>=reinterpret_cast<const char*>(pEnd) ||
+      *(pDescription+pEntry->fSizeDescription)!=0) {
+    return -EBADF;
+  }
+
+  TString descriptor=reinterpret_cast<const char*>(pDescription);
+  TString chainId;
+  TString compId;
+  TString compArgs;
+  TObjArray* pTokens=descriptor.Tokenize("{");
+  if (pTokens) {
+    int n=0;
+    if (pTokens->GetEntries()>n) {
+      retChainId=((TObjString*)pTokens->At(n++))->GetString();
+    }
+    if (pTokens->GetEntries()>n) {
+      compId=((TObjString*)pTokens->At(n++))->GetString();
+    }
+    delete pTokens;
+  }
+  if (!compId.IsNull() && (pTokens=compId.Tokenize(":"))!=NULL) {
+    int n=0;
+    if (pTokens->GetEntries()>n) {
+      compId=((TObjString*)pTokens->At(n++))->GetString();
+    }
+    if (pTokens->GetEntries()>n) {
+      compArgs=((TObjString*)pTokens->At(n++))->GetString();
+    }
+    delete pTokens;
+  }
+  compId.ReplaceAll("}", "");
+  compArgs.ReplaceAll("}", "");
+
+  retCompId=compId;
+  retCompArgs=compArgs;
+
+  if (retChainId.size()==0) return -ENODATA;
+
+  return 1;
+}
+
+int AliHLTComponent::LoggingVarargs(AliHLTComponentLogSeverity severity, 
+                                   const char* originClass, const char* originFunc,
+                                   const char* file, int line, ... ) const
+{
+  // see header file for function documentation
+  int iResult=0;
+
+  va_list args;
+  va_start(args, line);
+
+  // logging function needs to be const in order to be called from const member functions
+  // without problems. But at this point we face the problem with virtual members which
+  // are not necessarily const.
+  AliHLTComponent* nonconst=const_cast<AliHLTComponent*>(this);
+  AliHLTLogging::SetLogString("%s (%s, %p): ", 
+                             fChainId[0]!=0?fChainId.c_str():nonconst->GetComponentID(),
+                             nonconst->GetComponentID(), this);
+  iResult=SendMessage(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args, true /*append*/));
+  va_end(args);
+
+  return iResult;
+}
+
+int AliHLTComponent::ScanECSParam(const char* ecsParam)
+{
+  // see header file for function documentation
+
+  // format of the parameter string from ECS
+  // <command>;<parameterkey>=<parametervalue>;<parameterkey>=<parametervalue>;...
+  // search for a subset of the parameterkeys
+  int iResult=0;
+  TString string=ecsParam;
+  TObjArray* parameter=string.Tokenize(";");
+  if (parameter) {
+    for (int i=0; i<parameter->GetEntries(); i++) {
+      TString entry=((TObjString*)parameter->At(i))->GetString();
+      HLTDebug("scanning ECS entry: %s", entry.Data());
+      TObjArray* entryParams=entry.Tokenize("=");
+      if (entryParams) {
+       if (entryParams->GetEntries()>1) {
+         if ((((TObjString*)entryParams->At(0))->GetString()).CompareTo("CTP_TRIGGER_CLASS")==0) {
+           int result=InitCTPTriggerClasses((((TObjString*)entryParams->At(1))->GetString()).Data());
+           if (iResult>=0 && result<0) iResult=result;
+         } else {
+           // TODO: scan the other parameters
+           // e.g. consistency check of run number
+         }
+       }
+       delete entryParams;
+      }
+    }
+    delete parameter;
+  }
+
+  return iResult;
+}
+
+int AliHLTComponent::SetupCTPData()
+{
+  // see header file for function documentation
+  if (fpCTPData) delete fpCTPData;
+  fpCTPData=new AliHLTCTPData;
+  if (!fpCTPData) return -ENOMEM;
+  return 0;
+}
+
+int AliHLTComponent::InitCTPTriggerClasses(const char* ctpString)
+{
+  // see header file for function documentation
+  if (!fpCTPData) return 0; // silently accept as the component has to announce that it want's the CTP info
+  return fpCTPData->InitCTPTriggerClasses(ctpString);
+}
+
+bool AliHLTComponent::EvaluateCTPTriggerClass(const char* expression, AliHLTComponentTriggerData& trigData) const
+{
+  // see header file for function documentation
+  if (!fpCTPData) {
+    static bool bWarningThrown=false;
+    if (!bWarningThrown) HLTError("Trigger classes not initialized, use SetupCTPData from DoInit()");
+    bWarningThrown=true;
+    return false;
+  }
+
+  return fpCTPData->EvaluateCTPTriggerClass(expression, trigData);
+}