]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/BASE/AliHLTSystem.cxx
Minor cleanup of code.
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTSystem.cxx
index cba349299065eaf0d41eb71973b096db8206da4e..510b8f42a691ab2e0938f5c7e3be6375eb0e1c4e 100644 (file)
@@ -1,20 +1,20 @@
 // $Id$
 
-/**************************************************************************
- * This file is property of and copyright by the ALICE HLT Project        * 
- * ALICE Experiment at CERN, All rights reserved.                         *
- *                                                                        *
- * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
- *                  for The ALICE HLT Project.                            *
- *                                                                        *
- * Permission to use, copy, modify and distribute this software and its   *
- * documentation strictly for non-commercial purposes is hereby granted   *
- * without fee, provided that the above copyright notice appears in all   *
- * copies and that both the copyright notice and this permission notice   *
- * appear in the supporting documentation. The authors make no claims     *
- * about the suitability of this software for any purpose. It is          *
- * provided "as is" without express or implied warranty.                  *
- **************************************************************************/
+//**************************************************************************
+//* This file is property of and copyright by the ALICE HLT Project        * 
+//* ALICE Experiment at CERN, All rights reserved.                         *
+//*                                                                        *
+//* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
+//*                  for The ALICE HLT Project.                            *
+//*                                                                        *
+//* Permission to use, copy, modify and distribute this software and its   *
+//* documentation strictly for non-commercial purposes is hereby granted   *
+//* without fee, provided that the above copyright notice appears in all   *
+//* copies and that both the copyright notice and this permission notice   *
+//* appear in the supporting documentation. The authors make no claims     *
+//* about the suitability of this software for any purpose. It is          *
+//* provided "as is" without express or implied warranty.                  *
+//**************************************************************************
 
 /** @file   AliHLTSystem.cxx
     @author Matthias Richter
@@ -36,20 +36,26 @@ using namespace std;
 #include "AliHLTTask.h"
 #include "AliHLTModuleAgent.h"
 #include "AliHLTOfflineInterface.h"
+#include "AliHLTDataSource.h"
+#include "AliHLTOUT.h"
+#include "AliHLTOUTHandler.h"
 #include <TObjArray.h>
 #include <TObjString.h>
 #include <TStopwatch.h>
+//#include <TSystem.h>
 #include <TROOT.h>
-#include <TInterpreter.h>
+//#include <TInterpreter.h>
 
 /** HLT default component libraries */
-const char* kHLTDefaultLibs[]= {
+const char* AliHLTSystem::fgkHLTDefaultLibs[]= {
   "libAliHLTUtil.so", 
+  "libAliHLTRCU.so", 
   "libAliHLTTPC.so", 
   //  "libAliHLTSample.so",
-  "libAliHLTPHOS.so",
+  //"libAliHLTPHOS.so",
   //"libAliHLTMUON.so",
   "libAliHLTTRD.so",
+  "libAliHLTTrigger.so",
   NULL
 };
 
@@ -77,6 +83,7 @@ AliHLTSystem::AliHLTSystem()
     HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
 
   SetGlobalLoggingLevel(kHLTLogDefault);
+  SetFrameworkLog(kHLTLogDefault);
   if (fpComponentHandler) {
     AliHLTComponentEnvironment env;
     memset(&env, 0, sizeof(AliHLTComponentEnvironment));
@@ -117,8 +124,11 @@ int AliHLTSystem::fgNofInstances=0;
 int AliHLTSystem::AddConfiguration(AliHLTConfiguration* pConf)
 {
   // see header file for class documentation
+  HLTLogKeyword("configuration handling");
   int iResult=0;
   if (pConf) {
+    HLTError("function not yet implemented");
+    iResult=-ENOSYS;
   } else {
     iResult=-EINVAL;
   }
@@ -128,10 +138,13 @@ int AliHLTSystem::AddConfiguration(AliHLTConfiguration* pConf)
 int AliHLTSystem::InsertConfiguration(AliHLTConfiguration* pConf, AliHLTConfiguration* pPrec)
 {
   // see header file for class documentation
+  HLTLogKeyword("configuration handling");
   int iResult=0;
   if (pConf) {
     if (pPrec) {
       // find the position
+      HLTError("function not yet implemented");
+      iResult=-ENOSYS;
     }
   } else {
     iResult=-EINVAL;
@@ -142,8 +155,11 @@ int AliHLTSystem::InsertConfiguration(AliHLTConfiguration* pConf, AliHLTConfigur
 int AliHLTSystem::DeleteConfiguration(AliHLTConfiguration* pConf)
 {
   // see header file for class documentation
+  HLTLogKeyword("configuration handling");
   int iResult=0;
   if (pConf) {
+    HLTError("function not yet implemented");
+    iResult=-ENOSYS;
   } else {
     iResult=-EINVAL;
   }
@@ -180,7 +196,7 @@ int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
     AliHLTTask* pTask=NULL;
     if ((pTask=FindTask(pConf->GetName()))!=NULL) {
       if (pTask->GetConf()!=pConf) {
-       HLTError("configuration missmatch, there is already a task with configuration name \"%s\", but it is different. Most likely configuration %p is not registered properly", pConf->GetName(), pConf);
+       HLTError("configuration mismatch, there is already a task with configuration name \"%s\", but it is different. Most likely configuration %p is not registered properly", pConf->GetName(), pConf);
        iResult=-EEXIST;
       }
       // task for this configuration exists, terminate
@@ -192,6 +208,8 @@ int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
       pTask=new AliHLTTask(pConf);
       if (pTask==NULL) {
        iResult=-ENOMEM;
+      } else {
+       pTask->SetLocalLoggingLevel(GetLocalLoggingLevel());
       }
     }
     static int iterationLevel=0;
@@ -353,7 +371,7 @@ int AliHLTSystem::Run(Int_t iNofEvents, int bStop)
   }
   if (iResult>=0) {
     iResult=iCount;
-  } else  if (iResult==-ENOKEY) {
+  } else  if (iResult==-126 /*ENOKEY*/) {
     iResult=0; // do not propagate the error
   }
   ClearStatusFlags(kRunning);
@@ -367,14 +385,17 @@ int AliHLTSystem::InitTasks()
   TObjLink *lnk=fTaskList.FirstLink();
 
   if (lnk==NULL) {
-    HLTWarning("Task list is empty, aborting ...");
-    return -ENOKEY;
+    HLTWarning("Task list is empty, skipping HLT");
+    return -126 /*ENOKEY*/;
   }
   while (lnk && iResult>=0) {
     TObject* obj=lnk->GetObject();
     if (obj) {
       AliHLTTask* pTask=(AliHLTTask*)obj;
       iResult=pTask->Init(NULL, fpComponentHandler);
+//       ProcInfo_t ProcInfo;
+//       gSystem->GetProcInfo(&ProcInfo);
+//       HLTInfo("task %s initialized (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
     } else {
     }
     lnk = lnk->Next();
@@ -496,6 +517,9 @@ int AliHLTSystem::StartTasks()
     if (obj) {
       AliHLTTask* pTask=(AliHLTTask*)obj;
       iResult=pTask->StartRun();
+//       ProcInfo_t ProcInfo;
+//       gSystem->GetProcInfo(&ProcInfo);
+//       HLTInfo("task %s started (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
     } else {
     }
     lnk = lnk->Next();
@@ -505,6 +529,9 @@ int AliHLTSystem::StartTasks()
   } else {
     fEventCount=0;
     fGoodEvents=0;
+    if ((iResult=SendControlEvent(kAliHLTDataTypeSOR))<0) {
+      HLTError("can not send SOR event");
+    }
   }
   return iResult;
 }
@@ -520,14 +547,16 @@ int AliHLTSystem::ProcessTasks(Int_t eventNo)
     if (obj) {
       AliHLTTask* pTask=(AliHLTTask*)obj;
       iResult=pTask->ProcessTask(eventNo);
-      HLTDebug("task %s finnished (%d)", pTask->GetName(), iResult);
+//       ProcInfo_t ProcInfo;
+//       gSystem->GetProcInfo(&ProcInfo);
+//       HLTInfo("task %s processed (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
     } else {
     }
     lnk = lnk->Next();
   }
 
   if (iResult>=0) {
-    HLTInfo("Event %d successfully finished (%d)", eventNo, iResult);
+    HLTImportant("Event %d successfully finished (%d)", eventNo, iResult);
     iResult=0;
   } else {
     HLTError("Processing of event %d failed (%d)", eventNo, iResult);
@@ -540,12 +569,19 @@ int AliHLTSystem::StopTasks()
 {
   // see header file for class documentation
   int iResult=0;
+  if ((iResult=SendControlEvent(kAliHLTDataTypeEOR))<0) {
+    HLTError("can not send EOR event");
+  }
   TObjLink *lnk=fTaskList.FirstLink();
-  while (lnk && iResult>=0) {
+  while (lnk) {
     TObject* obj=lnk->GetObject();
     if (obj) {
       AliHLTTask* pTask=(AliHLTTask*)obj;
-      iResult=pTask->EndRun();
+      int locResult=pTask->EndRun();
+      if (iResult>=0 && locResult<0) iResult=locResult;
+//       ProcInfo_t ProcInfo;
+//       gSystem->GetProcInfo(&ProcInfo);
+//       HLTInfo("task %s stopped (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
     } else {
     }
     lnk = lnk->Next();
@@ -554,6 +590,33 @@ int AliHLTSystem::StopTasks()
   return iResult;
 }
 
+int AliHLTSystem::SendControlEvent(AliHLTComponentDataType dt)
+{
+  // see header file for class documentation
+
+  // disabled for the moment
+  return 0;
+
+  int iResult=0;
+  AliHLTRunDesc runDesc;
+  memset(&runDesc, 0, sizeof(AliHLTRunDesc));
+  runDesc.fStructSize=sizeof(AliHLTRunDesc);
+  AliHLTDataSource::AliSpecialEventGuard g(&runDesc, dt, kAliHLTVoidDataSpec);
+  HLTDebug("sending event %s, run descriptor %p", AliHLTComponent::DataType2Text(dt).c_str(), &runDesc);
+  TObjLink *lnk=fTaskList.FirstLink();
+  while (lnk && iResult>=0) {
+    TObject* obj=lnk->GetObject();
+    if (obj) {
+      AliHLTTask* pTask=(AliHLTTask*)obj;
+      iResult=pTask->ProcessTask(-1);
+    } else {
+    }
+    lnk = lnk->Next();
+  }
+  HLTDebug("event %s done (%d)", AliHLTComponent::DataType2Text(dt).c_str(), iResult);
+  return iResult;
+}
+
 int AliHLTSystem::DeinitTasks()
 {
   // see header file for class documentation
@@ -564,6 +627,9 @@ int AliHLTSystem::DeinitTasks()
     if (obj) {
       AliHLTTask* pTask=(AliHLTTask*)obj;
       iResult=pTask->Deinit();
+//       ProcInfo_t ProcInfo;
+//       gSystem->GetProcInfo(&ProcInfo);
+//       HLTInfo("task %s cleaned (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
     } else {
     }
     lnk = lnk->Next();
@@ -633,6 +699,109 @@ int AliHLTSystem::FillESD(int eventNo, AliRunLoader* runLoader, AliESDEvent* esd
   return iResult;
 }
 
+int AliHLTSystem::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd)
+{
+  // see header file for class documentation
+  int iResult=0;
+  if (!pHLTOUT) return -EINVAL;
+
+  HLTDebug("processing %d HLT data blocks", pHLTOUT->GetNofDataBlocks());
+  AliHLTOUT::AliHLTOUTHandlerListEntryVector esdHandlers;
+  for (iResult=pHLTOUT->SelectFirstDataBlock();
+       iResult>=0;
+       iResult=pHLTOUT->SelectNextDataBlock()) {
+    AliHLTComponentDataType dt=kAliHLTVoidDataType;
+    AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
+    pHLTOUT->GetDataBlockDescription(dt, spec);
+    AliHLTOUTHandler* pHandler=pHLTOUT->GetHandler();
+    AliHLTModuleAgent::AliHLTOUTHandlerType handlerType=pHLTOUT->GetDataBlockHandlerType();
+    if (!pHandler && (dt==kAliHLTDataTypeESDObject || dt==kAliHLTDataTypeESDTree)) {
+      handlerType=AliHLTModuleAgent::kEsd;
+    }
+    const char* pMsg="invalid";
+    switch (handlerType) {
+    case AliHLTModuleAgent::kEsd:
+      {
+       if (pHandler) {
+         // preprocess and write later
+         AliHLTOUT::AliHLTOUTLockGuard g(pHLTOUT);
+         pHandler->ProcessData(pHLTOUT);
+         pHLTOUT->InsertHandler(esdHandlers, pHLTOUT->GetDataBlockHandlerDesc());
+       } else {
+         // write directly
+         const AliHLTUInt8_t* pBuffer=NULL;
+         AliHLTUInt32_t size=0;
+         if (pHLTOUT->GetDataBuffer(pBuffer, size)>=0) {
+           pHLTOUT->WriteESD(pBuffer, size, dt);
+           pHLTOUT->ReleaseDataBuffer(pBuffer);
+         }
+       }
+      }
+      break;
+    case AliHLTModuleAgent::kRawReader:
+      // handled in the AliRawReaderHLT
+      break;
+    case AliHLTModuleAgent::kRawStream:
+      HLTWarning("HLTOUT handler type 'kRawStream' not yet implemented: agent %s, data type %s, specification %#x",
+                pMsg, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"invalid",
+                AliHLTComponent::DataType2Text(dt).c_str(), spec);
+      break;
+    case AliHLTModuleAgent::kChain:
+      HLTWarning("HLTOUT handler type 'kChain' not yet implemented: agent %s, data type %s, specification %#x",
+                pMsg, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"invalid",
+                AliHLTComponent::DataType2Text(dt).c_str(), spec);
+      break;
+    case AliHLTModuleAgent::kProprietary:
+      HLTDebug("processing proprietary data: agent %s, data type %s, specification %#x",
+                pMsg, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"invalid",
+                AliHLTComponent::DataType2Text(dt).c_str(), spec);
+      if (pHandler) {
+       AliHLTOUT::AliHLTOUTLockGuard g(pHLTOUT);
+       int res=pHandler->ProcessData(pHLTOUT);
+       if (res<0) {
+         HLTWarning("processing proprietary data failed (%d): agent %s, data type %s, specification %#x",
+                    res, pMsg, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"invalid",
+                    AliHLTComponent::DataType2Text(dt).c_str(), spec);
+       }
+      }
+      break;
+    case AliHLTModuleAgent::kUnknownOutput:
+      pMsg="unknown";
+      // fall trough intended
+    default:
+      HLTWarning("%s handler type: agent %s, data type %s, specification %#x, ... skipping data block",
+                pMsg, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"invalid",
+                AliHLTComponent::DataType2Text(dt).c_str(), spec);
+    }
+  }
+  // TODO: the return value of SelectFirst/NextDataBlock must be
+  // changed in order to avoid this check
+  if (iResult==-ENOENT) iResult=0;
+
+  AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator esdHandler;
+  // write all postponed esd data blocks
+  // first come first serve: the ESD of the first handler is also filled into
+  // the main ESD. Has to be changed later.
+  AliESDEvent* pMasterESD=esd;
+  for (esdHandler=esdHandlers.begin(); esdHandler!=esdHandlers.end() && iResult>=0; esdHandler++) {
+    AliHLTOUTHandler* pHandler=*esdHandler;
+    const AliHLTUInt8_t* pBuffer=NULL;
+    AliHLTUInt32_t size=0;
+    if ((size=pHandler->GetProcessedData(pBuffer))>0) {
+      AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*esdHandler;
+      AliHLTComponentDataType dt=desc;
+      pHLTOUT->WriteESD(pBuffer, size, dt);
+      if (pMasterESD) {
+       pHLTOUT->WriteESD(pBuffer, size, dt, pMasterESD);
+       pMasterESD=NULL;
+      }
+      pHandler->ReleaseProcessedData(pBuffer, size);
+    }
+  }
+
+  return iResult;
+}
+
 int AliHLTSystem::LoadComponentLibraries(const char* libraries)
 {
   // see header file for class documentation
@@ -679,7 +848,7 @@ int AliHLTSystem::Configure(AliRawReader* rawReader, AliRunLoader* runloader)
     HLTError("HLT system in running state, can not configure");
     return -EBUSY;
   }
-  ClearStatusFlags(kConfigurationLoaded|kTaskListCreated);
+  ClearStatusFlags(kTaskListCreated);
   if (CheckFilter(kHLTLogDebug))
     AliHLTModuleAgent::PrintStatus();
   if (CheckStatus(kConfigurationLoaded)==0) {
@@ -709,6 +878,7 @@ int AliHLTSystem::ScanOptions(const char* options)
   // see header file for class documentation
   int iResult=0;
   if (options) {
+    //AliHLTComponentHandler::TLibraryMode libMode=AliHLTComponentHandler::kDynamic;
     TString libs("");
     TString alloptions(options);
     TObjArray* pTokens=alloptions.Tokenize(" ");
@@ -728,6 +898,18 @@ int AliHLTSystem::ScanOptions(const char* options)
          } else {
            HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
          }
+       } else if (token.Contains("frameworklog=")) {
+         TString param=token.ReplaceAll("frameworklog=", "");
+         if (param.IsDigit()) {
+           SetFrameworkLog((AliHLTComponentLogSeverity)param.Atoi());
+         } else if (param.BeginsWith("0x") &&
+                    param.Replace(0,2,"",0).IsHex()) {
+           int severity=0;
+           sscanf(param.Data(),"%x", &severity);
+           SetFrameworkLog((AliHLTComponentLogSeverity)severity);
+         } else {
+           HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
+         }
        } else if (token.Contains("alilog=off")) {
          SwitchAliLog(0);
        } else if (token.Contains("config=")) {
@@ -743,6 +925,18 @@ int AliHLTSystem::ScanOptions(const char* options)
        } else if (token.Contains("chains=")) {
          TString param=token.ReplaceAll("chains=", "");
          fChains=param.ReplaceAll(",", " ");
+       } else if (token.Contains("libmode=")) {
+         TString param=token.ReplaceAll("libmode=", "");
+         param.ReplaceAll(",", " ");
+         if (fpComponentHandler) {
+           if (param.CompareTo("static")==0) {
+             fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kStatic);
+           } else if (param.CompareTo("dynamic")==0) {
+             fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kDynamic);
+           } else {
+             HLTWarning("wrong argument for option \'libmode=\', use \'static\' or \'dynamic\'");
+           }
+         }
        } else if (token.BeginsWith("lib") && token.EndsWith(".so")) {
          libs+=token;
          libs+=" ";
@@ -755,7 +949,7 @@ int AliHLTSystem::ScanOptions(const char* options)
 
     if (iResult>=0) {
       if (libs.IsNull()) {
-       const char** deflib=kHLTDefaultLibs;
+       const char** deflib=fgkHLTDefaultLibs;
        while (*deflib) {
          libs+=*deflib++;
          libs+=" ";
@@ -827,7 +1021,7 @@ int AliHLTSystem::BuildTaskListsFromReconstructionChains(AliRawReader* rawReader
   TString chains;
   if (fChains.Length()>0) {
     chains=fChains;
-    HLTInfo("custom reconstruction chain: %s", chains.Data());
+    HLTImportant("custom reconstruction chain: %s", chains.Data());
   } else {
     AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
     while ((pAgent || fChains.Length()>0) && iResult>=0) {
@@ -926,3 +1120,11 @@ void* AliHLTSystem::FindDynamicSymbol(const char* library, const char* symbol)
   if (fpComponentHandler==NULL) return NULL;
   return fpComponentHandler->FindSymbol(library, symbol);
 }
+
+void AliHLTSystem::SetFrameworkLog(AliHLTComponentLogSeverity level) 
+{
+  // see header file for class documentation
+  SetLocalLoggingLevel(level);
+  if (fpComponentHandler) fpComponentHandler->SetLocalLoggingLevel(level);
+  if (fpConfigurationHandler) fpConfigurationHandler->SetLocalLoggingLevel(level);
+}