]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/BASE/AliHLTComponentHandler.cxx
adding common functionality for the magnetic field to the component interface
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTComponentHandler.cxx
index c8394280843915f60cd149977723057df58ae746..e9b6e5aa8141227ad071ed8a2ea2a7294cd47b54 100644 (file)
     @date   
     @brief  Implementation of HLT component handler. */
 
+// see header file for class documentation
+// or
+// refer to README to build package
+// or
+// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
+
 #if __GNUC__>= 3
 using namespace std;
 #endif
@@ -48,29 +54,36 @@ AliHLTComponentHandler::AliHLTComponentHandler()
   fScheduleList(),
   fLibraryList(),
   fEnvironment(),
-  fOwnedComponents()
+  fOwnedComponents(),
+  fLibraryMode(kDynamic),
+  fRunDesc(kAliHLTVoidRunDesc),
+  fRunType(NULL)
 {
   // see header file for class documentation
   // or
   // refer to README to build package
   // or
   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
-  memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
+  memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
+  fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
   AddStandardComponents();
 }
 
-AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
+AliHLTComponentHandler::AliHLTComponentHandler(AliHLTAnalysisEnvironment* pEnv)
   :
   AliHLTLogging(),
   fComponentList(),
   fScheduleList(),
   fLibraryList(),
   fEnvironment(),
-  fOwnedComponents()
+  fOwnedComponents(),
+  fLibraryMode(kDynamic),
+  fRunDesc(kAliHLTVoidRunDesc),
+  fRunType(NULL)
 {
   // see header file for class documentation
   if (pEnv) {
-    memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
+    memcpy(&fEnvironment, pEnv, sizeof(AliHLTAnalysisEnvironment));
     if (pEnv->fLoggingFunc) {
       // the AliHLTLogging::Init method also sets the stream output
       // and notification handler to AliLog. This should only be done
@@ -79,7 +92,8 @@ AliHLTComponentHandler::AliHLTComponentHandler(AliHLTComponentEnvironment* pEnv)
       AliHLTLogging::Init(pEnv->fLoggingFunc);
     }
   }  else {
-    memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
+    memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
+    fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
   }
   //#ifndef __DEBUG
   //SetLocalLoggingLevel(kHLTLogError);
@@ -95,6 +109,30 @@ AliHLTComponentHandler::~AliHLTComponentHandler()
   // see header file for class documentation
   DeleteOwnedComponents();
   UnloadLibraries();
+  if (fRunType) delete [] fRunType;
+  fRunType=NULL;
+}
+
+AliHLTComponentHandler* AliHLTComponentHandler::fgpInstance=NULL;
+int AliHLTComponentHandler::fgNofInstances=0;
+
+AliHLTComponentHandler* AliHLTComponentHandler::CreateHandler()
+{
+  // see header file for class documentation
+  if (!fgpInstance) fgpInstance=new AliHLTComponentHandler;
+  fgNofInstances++;
+  return fgpInstance;
+}
+
+int AliHLTComponentHandler::Destroy()
+{
+  // see header file for class documentation
+  int nofInstances=0;
+  if (fgpInstance==this) {
+    nofInstances=fgNofInstances--;
+  }
+  if (nofInstances==0) delete this;
+  return nofInstances;
 }
 
 int AliHLTComponentHandler::AnnounceVersion()
@@ -102,15 +140,15 @@ int AliHLTComponentHandler::AnnounceVersion()
   // see header file for class documentation
   int iResult=0;
 #ifdef PACKAGE_STRING
-  void HLTbaseCompileInfo( char*& date, char*& time);
-  char* date="";
-  char* time="";
+  extern void HLTbaseCompileInfo( const char*& date, const char*& time);
+  const char* date="";
+  const char* time="";
   HLTbaseCompileInfo(date, time);
   if (!date) date="unknown";
   if (!time) time="unknown";
-  HLTInfo("%s build on %s (%s)", PACKAGE_STRING, date, time);
+  HLTImportant("%s build on %s (%s)", PACKAGE_STRING, date, time);
 #else
-  HLTInfo("ALICE High Level Trigger build on %s (%s) (embedded AliRoot build)", __DATE__, __TIME__);
+  HLTImportant("ALICE High Level Trigger build on %s (%s) (embedded AliRoot build)", __DATE__, __TIME__);
 #endif
   return iResult;
 }
@@ -173,7 +211,22 @@ Int_t AliHLTComponentHandler::ScheduleRegister(AliHLTComponent* pSample)
   return iResult;
 }
 
-int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component )
+int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvParam, int argc, const char** argv, AliHLTComponent*& component)
+{
+  // see header file for class documentation
+  int iResult=CreateComponent(componentID, component);
+  if (iResult>=0 && component) {
+       HLTDebug("component \"%s\" created (%p)", componentID, component);
+       if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
+         HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
+         delete component;
+         component=NULL;
+       }
+  }
+  return iResult;
+}
+
+int AliHLTComponentHandler::CreateComponent(const char* componentID, AliHLTComponent*& component )
 {
   // see header file for class documentation
   int iResult=0;
@@ -183,11 +236,6 @@ int AliHLTComponentHandler::CreateComponent(const char* componentID, void* pEnvP
       component=pSample->Spawn();
       if (component) {
        HLTDebug("component \"%s\" created (%p)", componentID, component);
-       if ((iResult=component->Init(&fEnvironment, pEnvParam, argc, argv))!=0) {
-         HLTError("Initialization of component \"%s\" failed with error %d", componentID, iResult);
-         delete component;
-         component=NULL;
-       }
       } else {
        HLTError("can not spawn component \"%s\"", componentID);
        iResult=-ENOENT;
@@ -207,7 +255,7 @@ Int_t AliHLTComponentHandler::FindComponentIndex(const char* componentID)
   // see header file for class documentation
   Int_t iResult=0;
   if (componentID) {
-    vector<AliHLTComponent*>::iterator element=fComponentList.begin();
+    AliHLTComponentPList::iterator element=fComponentList.begin();
     while (element!=fComponentList.end() && iResult>=0) {
       if (strcmp(componentID, (*element)->GetComponentID())==0) {
        break;
@@ -248,7 +296,7 @@ Int_t AliHLTComponentHandler::InsertComponent(AliHLTComponent* pSample)
 void AliHLTComponentHandler::List() 
 {
   // see header file for class documentation
-  vector<AliHLTComponent*>::iterator element=fComponentList.begin();
+  AliHLTComponentPList::iterator element=fComponentList.begin();
   int index=0;
   while (element!=fComponentList.end()) {
     HLTInfo("%d. %s", index++, (*element++)->GetComponentID());
@@ -270,11 +318,13 @@ int AliHLTComponentHandler::HasOutputData( const char* componentID)
   return iResult;
 }
 
-void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv) 
+void AliHLTComponentHandler::SetEnvironment(AliHLTAnalysisEnvironment* pEnv) 
 {
   // see header file for class documentation
   if (pEnv) {
-    memcpy(&fEnvironment, pEnv, sizeof(AliHLTComponentEnvironment));
+    memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
+    memcpy(&fEnvironment, pEnv, pEnv->fStructSize<sizeof(AliHLTAnalysisEnvironment)?pEnv->fStructSize:sizeof(AliHLTAnalysisEnvironment));
+    fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
     if (fEnvironment.fLoggingFunc) {
       // the AliHLTLogging::Init method also sets the stream output
       // and notification handler to AliLog. This should only be done
@@ -285,43 +335,61 @@ void AliHLTComponentHandler::SetEnvironment(AliHLTComponentEnvironment* pEnv)
   }
 }
 
+AliHLTComponentHandler::TLibraryMode AliHLTComponentHandler::SetLibraryMode(TLibraryMode mode)
+{
+  // see header file for class documentation
+  TLibraryMode old=fLibraryMode;
+  fLibraryMode=mode;
+  return old;
+}
+
 int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateAgents)
 {
   // see header file for class documentation
   int iResult=0;
   if (libraryPath) {
-    // first activate all agents which are already loaded
-    if (bActivateAgents) ActivateAgents();
+    TString libName=libraryPath;
+    int slash=-1;
+    while ((slash=libName.Index("/"))>=0) {
+      libName=libName.Remove(0, slash+1);
+    }
+    libName.ReplaceAll(".so","");
 
     // set the global component handler for static component registration
     AliHLTComponent::SetGlobalComponentHandler(this);
 
     AliHLTLibHandle hLib;
+    AliHLTLibHandle* phSearch=FindLibrary(libraryPath);
     const char* loadtype="";
 #ifdef HAVE_DLFCN_H
     // use interface to the dynamic linking loader
-    try {
+
+    // exeption does not help in Root context, the Root exeption
+    // handler always catches the exeption before. Have to find out
+    // how exeptions can be used in Root
+    /*try*/ {
       hLib.fHandle=dlopen(libraryPath, RTLD_NOW);
       loadtype="dlopen";
     }
+    /*
     catch (...) {
       // error message printed further down
       loadtype="dlopen exeption";
     }
+    */
 #else
     // use ROOT dynamic loader
     // check if the library was already loaded, as Load returns
     // 'failure' if the library was already loaded
-    try {
-    AliHLTLibHandle* pLib=FindLibrary(libraryPath);
-    if (pLib) {
-       int* pRootHandle=reinterpret_cast<int*>(pLib->fHandle);
+    /*try*/ {
+    if (phSearch) {
+       int* pRootHandle=reinterpret_cast<int*>(phSearch->fHandle);
        (*pRootHandle)++;
        HLTDebug("instance %d of library %s loaded", (*pRootHandle), libraryPath);
        hLib.fHandle=pRootHandle;
     }
     
-    if (hLib.fHandle==NULL && gSystem->Load(libraryPath)==0) {
+    if (hLib.fHandle==NULL && gSystem->Load(libraryPath)>=0) {
       int* pRootHandle=new int;
       if (pRootHandle) *pRootHandle=1;
       hLib.fHandle=pRootHandle;
@@ -329,27 +397,31 @@ int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateA
     }
     loadtype="gSystem";
     }
+    /*
     catch (...) {
       // error message printed further down
       loadtype="gSystem exeption";
     }
+    */
 #endif //HAVE_DLFCN_H
     if (hLib.fHandle!=NULL) {
       // create TString object to store library path and use pointer as handle 
       hLib.fName=new TString(libraryPath);
-      HLTInfo("library %s loaded (%s)", libraryPath, loadtype);
+      hLib.fMode=fLibraryMode;
       fLibraryList.insert(fLibraryList.begin(), hLib);
-      typedef void (*CompileInfo)( char*& date, char*& time);
+      if (!phSearch) {
+      typedef void (*CompileInfo)(const char*& date, const char*& time);
       CompileInfo fctInfo=(CompileInfo)FindSymbol(libraryPath, "CompileInfo");
+      const char* date="";
+      const char* time="";
+      const char* buildOn="";
       if (fctInfo) {
-       char* date="";
-       char* time="";
+       buildOn=" build on ";
        (*fctInfo)(date, time);
        if (!date) date="unknown";
        if (!time) time="unknown";
-       HLTInfo("build on %s (%s)", date, time);
-      } else {
-       HLTInfo("no build info available (possible AliRoot embedded build)");
+      }
+      HLTImportant("using %s plugin%s%s %s (%s%s)", libraryPath, buildOn, date, time, hLib.fMode==kStatic?"persistent, ":"", loadtype);
       }
 
       // static registration of components when library is loaded
@@ -371,7 +443,7 @@ int AliHLTComponentHandler::LoadLibrary( const char* libraryPath, int bActivateA
     if (iResult>=0) {
       // alternative dynamic registration by library agents
       // !!! has to be done after UnsetGlobalComponentHandler
-      if (bActivateAgents) ActivateAgents();
+      if (bActivateAgents) ActivateAgents(libName.Data());
     }
 
   } else {
@@ -407,13 +479,20 @@ int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandl
   int iResult=0;
   fgAliLoggingFunc=NULL;
   TString* pName=reinterpret_cast<TString*>(handle.fName);
+  if (handle.fMode!=kStatic) {
 #ifdef HAVE_DLFCN_H
-  try {
+  // exeption does not help in Root context, the Root exeption
+  // handler always catches the exeption before. Have to find out
+  // how exeptions can be used in Root
+
+  /*try*/ {
     dlclose(handle.fHandle);
   }
+  /*
   catch (...) {
     HLTError("exeption caught during dlclose of library %s", pName!=NULL?pName->Data():"");
   }
+  */
 #else
   int* pCount=reinterpret_cast<int*>(handle.fHandle);
   if (--(*pCount)==0) {
@@ -443,14 +522,17 @@ int AliHLTComponentHandler::UnloadLibrary(AliHLTComponentHandler::AliHLTLibHandl
     delete pCount;
   }
 #endif //HAVE_DLFCN_H
-  handle.fName=NULL;
-  handle.fHandle=NULL;
   if (pName) {
     HLTDebug("unload library %s", pName->Data());
-    delete pName;
   } else {
     HLTWarning("missing name for unloaded library");
   }
+  }
+  handle.fName=NULL;
+  handle.fHandle=NULL;
+  if (pName) {
+    delete pName;
+  }
   pName=NULL;
   return iResult;
 }
@@ -468,17 +550,17 @@ int AliHLTComponentHandler::UnloadLibraries()
   return iResult;
 }
 
-void* AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol)
+void (*AliHLTComponentHandler::FindSymbol(const char* library, const char* symbol))()
 {
   // see header file for class documentation
   AliHLTLibHandle* hLib=FindLibrary(library);
   if (hLib==NULL) return NULL;
-  void* pFunc=NULL;
+  void (*pFunc)()=NULL;
 #ifdef HAVE_DLFCN_H
-  pFunc=dlsym(hLib->fHandle, symbol);
+  pFunc=(void (*)())dlsym(hLib->fHandle, symbol);
 #else
   TString* name=reinterpret_cast<TString*>(hLib->fName);
-  pFunc=gSystem->DynFindSymbol(name->Data(), symbol);
+  pFunc=(void (*)())gSystem->DynFindSymbol(name->Data(), symbol);
 #endif
   return pFunc;
 }
@@ -513,7 +595,7 @@ int AliHLTComponentHandler::RegisterScheduledComponents()
 {
   // see header file for class documentation
   int iResult=0;
-  vector<AliHLTComponent*>::iterator element=fScheduleList.begin();
+  AliHLTComponentPList::iterator element=fScheduleList.begin();
   int iLocalResult=0;
   while (element!=fScheduleList.end()) {
     iLocalResult=RegisterComponent(*element);
@@ -524,45 +606,85 @@ int AliHLTComponentHandler::RegisterScheduledComponents()
   return iResult;
 }
 
-int AliHLTComponentHandler::ActivateAgents(const AliHLTModuleAgent** blackList, int size)
+int AliHLTComponentHandler::ActivateAgents(const char* library, const char* blackList)
 {
   // see header file for class documentation
   int iResult=0;
-  AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
-  while (pAgent && iResult>=0) {
-    if (blackList) {
-      int i=0;
-      for (; i<size; i++) {
-       if (blackList[i]==pAgent) break;
+  vector<AliHLTModuleAgent*> agents;
+  for (AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent(); 
+       pAgent && iResult>=0;
+       pAgent=AliHLTModuleAgent::GetNextAgent()) {
+
+    // check if we found the agent for the specified library
+    if (library) {
+      TString check="libAliHLT"; check+=pAgent->GetModuleId();
+      if (check.CompareTo(library)==0) {
+       agents.clear();
+       agents.push_back(pAgent);
+       break;
       }
-      if (i<size) {
-       // this agent was in the list
-       pAgent=AliHLTModuleAgent::GetNextAgent();
-       continue;
+    }
+
+    // check if the current agent is in the black list
+    if (blackList) {
+      const char* found=strstr(blackList, pAgent->GetModuleId());
+      if (found) {
+       found+=strlen(pAgent->GetModuleId());
+       // skip this agent as it is in the blacklist
+       if (*found==0 or *found==' ') continue;
       }
     }
+    agents.push_back(pAgent);
+  }
 
-    pAgent->ActivateComponentHandler(this);
-    pAgent=AliHLTModuleAgent::GetNextAgent();
+  for (vector<AliHLTModuleAgent*>::iterator element=agents.begin();
+       element!=agents.end(); element++) {
+    (*element)->ActivateComponentHandler(this);
   }
-  return iResult;
+
+  return agents.size();
 }
 
 int AliHLTComponentHandler::DeleteOwnedComponents()
 {
   // see header file for class documentation
   int iResult=0;
-  vector<AliHLTComponent*>::iterator element=fOwnedComponents.begin();
+  AliHLTComponentPList::iterator element=fOwnedComponents.begin();
   while (element!=fOwnedComponents.end()) {
     //DeregisterComponent((*element)->GetComponentID());
-    try {
+    // exeption does not help in Root context, the Root exeption
+    // handler always catches the exeption before. Have to find out
+    // how exeptions can be used in Root
+    /*try*/ {
       delete *element;
     }
+    /*
     catch (...) {
       HLTError("delete managed sample %p", *element);
     }
+    */
     fOwnedComponents.erase(element);
     element=fOwnedComponents.begin();
   }
   return iResult;
 }
+
+int AliHLTComponentHandler::SetRunDescription(const AliHLTRunDesc* desc, const char* runType)
+{
+  // see header file for class documentation
+  if (!desc) return -EINVAL;
+  if (desc->fStructSize!=sizeof(AliHLTRunDesc)) {
+    HLTError("invalid size of RunDesc struct (%ul)", desc->fStructSize);
+    return -EINVAL;
+  }
+
+  memcpy(&fRunDesc, desc, sizeof(AliHLTRunDesc));
+  if (runType) {
+    if (fRunType) delete [] fRunType;
+    fRunType=new char[sizeof(runType)+1];
+    if (fRunType) {
+      strcpy(fRunType, runType);
+    }
+  }
+  return 0;
+}