]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/BASE/AliHLTSystem.cxx
Adding the new detector MFT (Antonio Uras)
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTSystem.cxx
index 804a3258a00053ce79a35b64e6eca49634e134b8..1508db209f9c1522796a31a9e3e29177a347a0fb 100644 (file)
@@ -1,5 +1,4 @@
 // $Id$
-
 //**************************************************************************
 //* This file is property of and copyright by the ALICE HLT Project        * 
 //* ALICE Experiment at CERN, All rights reserved.                         *
@@ -33,15 +32,21 @@ using namespace std;
 #include "AliHLTComponent.h"
 #include "AliHLTConfiguration.h"
 #include "AliHLTConfigurationHandler.h"
+#include "AliHLTOnlineConfiguration.h"
 #include "AliHLTTask.h"
 #include "AliHLTModuleAgent.h"
 #include "AliHLTOfflineInterface.h"
 #include "AliHLTDataSource.h"
 #include "AliHLTOUT.h"
 #include "AliHLTOUTHandler.h"
+#include "AliHLTOUTTask.h"
+#include "AliHLTControlTask.h"
+#include "AliHLTDataBuffer.h"
+#include "AliHLTMisc.h"
 #include <TObjArray.h>
 #include <TObjString.h>
 #include <TStopwatch.h>
+#include <TList.h>
 //#include <TSystem.h>
 #include <TROOT.h>
 //#include <TInterpreter.h>
@@ -52,9 +57,15 @@ const char* AliHLTSystem::fgkHLTDefaultLibs[]= {
   "libAliHLTRCU.so", 
   "libAliHLTTPC.so", 
   //  "libAliHLTSample.so",
-  //"libAliHLTPHOS.so",
-  //"libAliHLTMUON.so",
+  "libAliHLTCalo.so",
+  "libAliHLTEMCAL.so",
+  "libAliHLTPHOS.so",
+  "libAliHLTMUON.so",
   "libAliHLTTRD.so",
+  "libAliHLTITS.so",
+  "libAliHLTVZERO.so",
+  "libAliHLTZDC.so",
+  "libAliHLTGlobal.so",
   "libAliHLTTrigger.so",
   NULL
 };
@@ -62,16 +73,28 @@ const char* AliHLTSystem::fgkHLTDefaultLibs[]= {
 /** ROOT macro for the implementation of ROOT specific class methods */
 ClassImp(AliHLTSystem)
 
-AliHLTSystem::AliHLTSystem(AliHLTComponentLogSeverity loglevel)
-  :
-  fpComponentHandler(new AliHLTComponentHandler()),
-  fpConfigurationHandler(new AliHLTConfigurationHandler()),
+AliHLTSystem::AliHLTSystem(AliHLTComponentLogSeverity loglevel, const char* name,
+                          AliHLTComponentHandler* pCompHandler,
+                          AliHLTConfigurationHandler* pConfHandler
+                          )
+  : fpComponentHandler(pCompHandler==NULL?AliHLTComponentHandler::CreateHandler():pCompHandler)
+  , fpConfigurationHandler(pConfHandler==NULL?AliHLTConfigurationHandler::CreateHandler():pConfHandler),
   fTaskList(),
   fState(0),
   fChains(),
   fStopwatches(new TObjArray),
   fEventCount(-1),
-  fGoodEvents(-1)
+  fGoodEvents(-1),
+  fpChainHandlers(NULL),
+  fpEsdHandlers(NULL),
+  fpProprietaryHandlers(NULL),
+  fpHLTOUTTask(NULL),
+  fpHLTOUT(NULL),
+  fHLTOUTUse(0),
+  fpControlTask(NULL),
+  fName(name)
+  , fECSParams()
+  , fUseHLTOUTComponentTypeGlobal(true)
 {
   // see header file for class documentation
   // or
@@ -79,25 +102,30 @@ AliHLTSystem::AliHLTSystem(AliHLTComponentLogSeverity loglevel)
   // or
   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
 
-  if (fgNofInstances++>0)
-    HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
+  if (fgNofInstances++>0) {
+    // July 2008: multiple instances are now allowed
+    // AliHLTSystem is used in multiple instances for the kChain HLTOUT handler
+    //HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
+  }
 
   SetGlobalLoggingLevel(loglevel);
   SetFrameworkLog(loglevel);
   if (fpComponentHandler) {
-    AliHLTComponentEnvironment env;
-    memset(&env, 0, sizeof(AliHLTComponentEnvironment));
+    AliHLTAnalysisEnvironment env;
+    memset(&env, 0, sizeof(AliHLTAnalysisEnvironment));
+    env.fStructSize=sizeof(AliHLTAnalysisEnvironment);
     env.fAllocMemoryFunc=AliHLTSystem::AllocMemory;
+    env.fGetEventDoneDataFunc=AliHLTSystem::AllocEventDoneData;
     env.fLoggingFunc=NULL;
     fpComponentHandler->SetEnvironment(&env);
     InitAliLogFunc(fpComponentHandler);
+    if (fgNofInstances==1) {
     fpComponentHandler->AnnounceVersion();
+    }
   } else {
     HLTFatal("can not create Component Handler");
   }
-  if (fpConfigurationHandler) {
-    AliHLTConfiguration::GlobalInit(fpConfigurationHandler);
-  } else {
+  if (fpConfigurationHandler==NULL) {
     HLTFatal("can not create Configuration Handler");
   }
 }
@@ -106,65 +134,24 @@ AliHLTSystem::~AliHLTSystem()
 {
   // see header file for class documentation
   fgNofInstances--;
+  CleanupHLTOUTHandlers();
   CleanTaskList();
-  AliHLTConfiguration::GlobalDeinit(fpConfigurationHandler);
   if (fpConfigurationHandler) {
-    delete fpConfigurationHandler;
+    fpConfigurationHandler->Destroy();
   }
   fpConfigurationHandler=NULL;
   
   if (fpComponentHandler) {
-    delete fpComponentHandler;
+    fpComponentHandler->Destroy();
   }
   fpComponentHandler=NULL;
-}
-
-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;
-  }
-  return iResult;
-}
+  delete fStopwatches;
 
-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;
-  }
-  return iResult;
+  // note: fpHLTOUTTask and fpControlTask are deleted by
+  // CleanTaskList
 }
 
-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;
-  }
-  return iResult;
-}
+int AliHLTSystem::fgNofInstances=0;
 
 int AliHLTSystem::BuildTaskList(const char* id)
 {
@@ -201,7 +188,8 @@ int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
       }
       // task for this configuration exists, terminate
       pTask=NULL;
-    } else if (pConf->SourcesResolved(1)!=1) {
+    // check first if the configuration has all sources resolved, try to extract otherwise
+    } else if (pConf->SourcesResolved()!=1 && pConf->ExtractSources()!=1) {
        HLTError("configuration \"%s\" has unresolved sources, aborting ...", pConf->GetName());
        iResult=-ENOLINK;
     } else {
@@ -264,11 +252,14 @@ int AliHLTSystem::CleanTaskList()
 {
   // see header file for class documentation
   int iResult=0;
+  fpHLTOUTTask=NULL;
+  fpControlTask=NULL;
   TObjLink* lnk=NULL;
   while ((lnk=fTaskList.LastLink())!=NULL) {
     delete (lnk->GetObject());
     fTaskList.Remove(lnk);
   }
+
   return iResult;
 }
 
@@ -276,9 +267,24 @@ int AliHLTSystem::InsertTask(AliHLTTask* pTask)
 {
   // see header file for class documentation
   int iResult=0;
-  TObjLink *lnk = NULL;
-  if ((iResult=pTask->CheckDependencies())>0)
-    lnk=fTaskList.FirstLink();
+  if (fpControlTask==NULL) {
+    fpControlTask=new AliHLTControlTask;
+    if (!fpControlTask) return -ENOMEM;
+    fTaskList.AddFirst(fpControlTask);
+  }
+  TObjLink *controlLnk=NULL;
+  TObjLink *lnk = fTaskList.FirstLink();
+  assert(!lnk || lnk->GetObject()==fpControlTask || fpControlTask==NULL);
+  if (lnk && lnk->GetObject()==fpControlTask) {
+    if (pTask->GetConf() && pTask->GetConf()->GetFirstSource()==NULL) {
+      pTask->SetDependency(fpControlTask);
+      fpControlTask->SetTarget(pTask);
+    }
+    controlLnk=lnk;
+    lnk=lnk->Next();
+  }
+  if ((iResult=pTask->CheckDependencies())<=0)
+    lnk=NULL;
   while (lnk && iResult>0) {
     AliHLTTask* pCurr = (AliHLTTask*)lnk->GetObject();
     //HLTDebug("checking  \"%s\"", pCurr->GetName());
@@ -299,6 +305,8 @@ int AliHLTSystem::InsertTask(AliHLTTask* pTask)
   if (iResult==0) {
       if (lnk) {
        fTaskList.AddAfter(lnk, pTask);
+      } else if (controlLnk) {
+       fTaskList.AddAfter(controlLnk, pTask);
       } else {
        fTaskList.AddFirst(pTask);
       }
@@ -339,7 +347,9 @@ void AliHLTSystem::PrintTaskList()
   }
 }
 
-int AliHLTSystem::Run(Int_t iNofEvents, int bStop)
+int AliHLTSystem::Run(Int_t iNofEvents, int bStop, AliHLTUInt64_t trgMask,
+                     AliHLTUInt32_t timestamp, AliHLTUInt32_t eventtype,
+                     AliHLTUInt32_t participatingDetectors)
 {
   // see header file for class documentation
   int iResult=0;
@@ -350,10 +360,24 @@ int AliHLTSystem::Run(Int_t iNofEvents, int bStop)
       if (fEventCount==0) {
        InitBenchmarking(fStopwatches);
       } else {
-       ResumeBenchmarking(fStopwatches);    
+       // Matthias Oct 11 2008 this is a bug
+       // By resuming the stopwatches at this point, all continued counting, but the
+       // starting and stopping is controlled by the AliHLTStopwatchGuard
+       //ResumeBenchmarking(fStopwatches);    
       }
       for (int i=fEventCount; i<fEventCount+iNofEvents && iResult>=0; i++) {
-       if ((iResult=ProcessTasks(i))>=0) {
+       if (fpHLTOUTTask) {
+         if (iNofEvents>1 && i==fEventCount) {
+           HLTWarning("can not add more than one event to the HLTOUT, skipping all but last block");
+         }
+         // reset and prepare for new data
+         fpHLTOUTTask->Reset();
+       }
+       if (eventtype == 0) {
+         eventtype = gkAliEventTypeData;
+         participatingDetectors = 0x0;
+       }
+       if ((iResult=ProcessTasks(i, trgMask, timestamp, eventtype, participatingDetectors))>=0) {
          fGoodEvents++;
          iCount++;
        } else {
@@ -362,6 +386,7 @@ int AliHLTSystem::Run(Int_t iNofEvents, int bStop)
          // currently ignored 
          iResult=0;
        }
+       AliHLTDataBuffer::SetGlobalEventCount(iCount);
       }
       fEventCount+=iNofEvents;
       if (bStop) StopTasks();
@@ -375,6 +400,7 @@ int AliHLTSystem::Run(Int_t iNofEvents, int bStop)
     iResult=0; // do not propagate the error
   }
   ClearStatusFlags(kRunning);
+  AliHLTDataBuffer::PrintStatistics();
   return iResult;
 }
 
@@ -385,7 +411,7 @@ int AliHLTSystem::InitTasks()
   TObjLink *lnk=fTaskList.FirstLink();
 
   if (lnk==NULL) {
-    HLTWarning("Task list is empty, skipping HLT");
+    HLTInfo("Task list is empty, skipping HLT");
     return -126 /*ENOKEY*/;
   }
   while (lnk && iResult>=0) {
@@ -557,26 +583,32 @@ int AliHLTSystem::StartTasks()
     fEventCount=0;
     fGoodEvents=0;
     if ((iResult=SendControlEvent(kAliHLTDataTypeSOR))<0) {
-      HLTError("can not send SOR event");
+      HLTError("can not send SOR event: error %d", iResult);
     }
   }
   return iResult;
 }
 
-int AliHLTSystem::ProcessTasks(Int_t eventNo)
+int AliHLTSystem::ProcessTasks(Int_t eventNo, AliHLTUInt64_t trgMask,
+         AliHLTUInt32_t timestamp, AliHLTUInt32_t eventtype,
+         AliHLTUInt32_t participatingDetectors)
 {
   // see header file for class documentation
   int iResult=0;
   HLTDebug("processing event no %d", eventNo);
   TObjLink *lnk=fTaskList.FirstLink();
-  while (lnk && iResult>=0) {
+  while (lnk) {
     TObject* obj=lnk->GetObject();
     if (obj) {
       AliHLTTask* pTask=(AliHLTTask*)obj;
-      iResult=pTask->ProcessTask(eventNo);
+      if (iResult>=0) {
+      iResult=pTask->ProcessTask(eventNo, eventtype, trgMask, timestamp, participatingDetectors);
 //       ProcInfo_t ProcInfo;
 //       gSystem->GetProcInfo(&ProcInfo);
 //       HLTInfo("task %s processed (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
+      } else {
+       pTask->SubscribeSourcesAndSkip();
+      }
     } else {
     }
     lnk = lnk->Next();
@@ -599,6 +631,13 @@ int AliHLTSystem::StopTasks()
   if ((iResult=SendControlEvent(kAliHLTDataTypeEOR))<0) {
     HLTError("can not send EOR event");
   }
+
+  // cleanup blocks from the last event. This is a bit awkward. All output
+  // blocks from the chains need to be stored in the HLTOUT task. Though,
+  // we do not know, whether HLTOUT is going to be processed or not.
+  if (fpHLTOUTTask)
+    fpHLTOUTTask->Reset();
+
   TObjLink *lnk=fTaskList.FirstLink();
   while (lnk) {
     TObject* obj=lnk->GetObject();
@@ -614,6 +653,9 @@ int AliHLTSystem::StopTasks()
     lnk = lnk->Next();
   }
   PrintBenchmarking(fStopwatches, 1 /*clean*/);
+  if (fEventCount!=fGoodEvents) {
+    HLTError("%d out of %d event(s) failed", fEventCount-fGoodEvents, fEventCount);
+  }
   ClearStatusFlags(kStarted);
   return iResult;
 }
@@ -621,26 +663,54 @@ int AliHLTSystem::StopTasks()
 int AliHLTSystem::SendControlEvent(AliHLTComponentDataType dt)
 {
   // see header file for class documentation
+  int iResult=0;
 
-  // disabled for the moment
-  return 0;
+  AliHLTComponentBlockDataList controlBlocks;
+  AliHLTComponentBlockData bd;
 
-  int iResult=0;
+  // run decriptor block of type kAliHLTDataTypeSOR/kAliHLTDataTypeEOR 
+  AliHLTComponent::FillBlockData(bd);
   AliHLTRunDesc runDesc;
   memset(&runDesc, 0, sizeof(AliHLTRunDesc));
   runDesc.fStructSize=sizeof(AliHLTRunDesc);
-  AliHLTDataSource::AliSpecialEventGuard g(&runDesc, dt, kAliHLTVoidDataSpec);
+  runDesc.fRunNo=AliHLTMisc::Instance().GetCDBRunNo();
+  bd.fPtr=&runDesc;
+  bd.fSize=sizeof(AliHLTRunDesc);
+  bd.fDataType=dt;
+  bd.fSpecification=kAliHLTVoidDataSpec;
+  controlBlocks.push_back(bd);
+
+  // ECS parameter of type kAliHLTDataTypeECSParam
+  if (fECSParams.IsNull())
+    fECSParams="CTP_TRIGGER_CLASS=00:DUMMY-TRIGGER-ALL:00-01-02-03-04-05-06-07-08-09-10-11-12-13-14-15-16-17";
+  AliHLTComponent::FillBlockData(bd);
+  bd.fPtr=(void*)fECSParams.Data();
+  bd.fSize=fECSParams.Length()+1;
+  bd.fDataType=kAliHLTDataTypeECSParam;
+  bd.fSpecification=kAliHLTVoidDataSpec;
+  controlBlocks.push_back(bd);  
+
+  AliHLTControlTask::AliHLTControlEventGuard g(fpControlTask, controlBlocks);
   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);
+      AliHLTUInt32_t eventType=gkAliEventTypeUnknown;
+      if (dt==kAliHLTDataTypeSOR) eventType=gkAliEventTypeStartOfRun;
+      else if (dt==kAliHLTDataTypeEOR) eventType=gkAliEventTypeEndOfRun;
+      else HLTWarning("unknown control event %s", AliHLTComponent::DataType2Text(dt).c_str());
+      iResult=pTask->ProcessTask(-1, eventType, 0, 0);
     } else {
     }
     lnk = lnk->Next();
   }
+
+  // control events are not supposed to go into the HLTOUT
+  if (fpHLTOUTTask)
+    fpHLTOUTTask->Reset();
+
   HLTDebug("event %s done (%d)", AliHLTComponent::DataType2Text(dt).c_str(), iResult);
   return iResult;
 }
@@ -649,18 +719,19 @@ int AliHLTSystem::DeinitTasks()
 {
   // see header file for class documentation
   int iResult=0;
-  TObjLink *lnk=fTaskList.FirstLink();
-  while (lnk && iResult>=0) {
+  TObjLink *lnk=fTaskList.LastLink();
+  while (lnk) {
     TObject* obj=lnk->GetObject();
     if (obj) {
       AliHLTTask* pTask=(AliHLTTask*)obj;
-      iResult=pTask->Deinit();
+      int localRes=pTask->Deinit();
+      if (iResult>=0) iResult=localRes;
 //       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();
+    lnk = lnk->Prev();
   }
   fEventCount=-1;
   fGoodEvents=-1;
@@ -668,6 +739,44 @@ int AliHLTSystem::DeinitTasks()
   return iResult;
 }
 
+int AliHLTSystem::CleanupHLTOUTHandlers()
+{
+  // see header file for class documentation
+  if (fpChainHandlers) {
+    AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpChainHandlers);
+    fpChainHandlers=NULL;
+    if (pHandlers) {
+      AliHLTOUT::InvalidateBlocks(*pHandlers);
+      AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
+    }
+    assert(pHandlers->size()==0);
+    delete pHandlers;
+  }
+
+  if (fpEsdHandlers) {
+    AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpEsdHandlers);
+    fpEsdHandlers=NULL;
+    if (pHandlers) {
+      AliHLTOUT::InvalidateBlocks(*pHandlers);
+      AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
+    }
+    assert(pHandlers->size()==0);
+    delete pHandlers;
+  }
+
+  if (fpProprietaryHandlers) {
+    AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpProprietaryHandlers);
+    fpProprietaryHandlers=NULL;
+    if (pHandlers) {
+      AliHLTOUT::InvalidateBlocks(*pHandlers);
+      AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
+    }
+    assert(pHandlers->size()==0);
+    delete pHandlers;
+  }
+  return 0;
+}
+
 void* AliHLTSystem::AllocMemory( void* /*param*/, unsigned long size )
 {
   // see header file for class documentation
@@ -682,6 +791,21 @@ void* AliHLTSystem::AllocMemory( void* /*param*/, unsigned long size )
   return p;
 }
 
+int AliHLTSystem::AllocEventDoneData( void* /*param*/, AliHLTEventID_t /*eventID*/, unsigned long size, AliHLTComponentEventDoneData** edd )
+{
+  // see header file for class documentation
+  unsigned long blocksize=sizeof(AliHLTComponentEventDoneData)+size;
+  void* block=AllocMemory(NULL, blocksize);
+  if (!block) return -ENOMEM;
+  memset(block, 0, blocksize);
+  *edd=reinterpret_cast<AliHLTComponentEventDoneData*>(block);
+  (*edd)->fStructSize=sizeof(AliHLTComponentEventDoneData);
+  (*edd)->fDataSize=size;
+  (*edd)->fData=reinterpret_cast<AliHLTUInt8_t*>(block)+sizeof(AliHLTComponentEventDoneData);
+  
+  return 0;
+}
+
 int AliHLTSystem::Reconstruct(int nofEvents, AliRunLoader* runLoader, 
                              AliRawReader* rawReader)
 {
@@ -695,13 +819,49 @@ int AliHLTSystem::Reconstruct(int nofEvents, AliRunLoader* runLoader,
        if (!CheckStatus(kError)) {
        StopTasks();
        DeinitTasks();
+       CleanupHLTOUTHandlers();
        }
       } else {
       if ((iResult=AliHLTOfflineInterface::SetParamsToComponents(runLoader, rawReader))>=0) {
+       AliHLTUInt64_t trgMask=0x1;
+       AliHLTUInt32_t timestamp=0;
+       AliHLTUInt32_t eventtype=0;
+       if (runLoader==NULL) {
+         // this is a quick workaround for the case of simulation
+         // the trigger framework is still under development, secondly, AliHLTSimulation
+         // does not yet add the emulated ECS parameters, so no CTP trigger is known in the HLT
+         // AliHLTTask will initialize one dummy CTP trigger class with bit 0, that's why the
+         // default trigger mask is 0x1
+         trgMask=AliHLTMisc::Instance().GetTriggerMask(rawReader);
+
+         // get the timestamp and type of the event from the raw reader
+         // this is currently only meaningfull for reconstruction (runloader==NULL)
+         timestamp=AliHLTMisc::Instance().GetTimeStamp(rawReader);
+         eventtype=AliHLTMisc::Instance().GetEventType(rawReader);
+       }
        // the system always remains started after event processing, a specific
        // call with nofEvents==0 is needed to execute the stop sequence
-       if ((iResult=Run(nofEvents, 0))<0) SetStatusFlags(kError);
+       if ((iResult=Run(nofEvents, 0, trgMask, timestamp, eventtype))<0) SetStatusFlags(kError);
+      }
       }
+
+      // add the current HLTOUT task to the collection
+      if (fpHLTOUTTask) {
+       AliHLTOUT* pTask=dynamic_cast<AliHLTOUT*>(fpHLTOUTTask);
+       if (pTask && (iResult=pTask->Init())>=0) {
+         if (pTask->GetNofDataBlocks()>0) {
+           AliHLTOUT* pHLTOUT=RequestHLTOUT();
+           if (pHLTOUT) {
+             pHLTOUT->AddSubCollection(pTask);
+             ReleaseHLTOUT(pHLTOUT);
+           } else {
+             HLTWarning("no HLTOUT instance available, output blocks of the chain are ignored");
+           }
+         }
+       } else {
+         HLTWarning("can not initialize HLTOUT sub collection %s for reconstruction chain (%d), data blocks are lost", pTask?fpHLTOUTTask->GetName():"nil", iResult);
+         iResult=0;
+       }
       }
     } else {
       HLTError("wrong state %#x, required flags %#x", GetStatusFlags(), kReady);
@@ -732,17 +892,31 @@ 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;
-
-  // first come first serve: the ESD of the first handler is also filled into
-  // the main ESD. Has to be changed later.
-  // currently, merging to the provided ESDs crashes at the level of the
-  // TTree::Fill in AliReconstruction, furthermore, the wrong ESD is passed
-  // by the framework
-  AliESDEvent* pMasterESD=NULL;
-  pMasterESD=esd;
+
+  //
+  // process all kChain handlers first
+  //
+  if ((iResult=ProcessHLTOUTkChain(pHLTOUT))<0) {
+    HLTWarning("Processing of kChain-type data blocks failed with error code %d", iResult);
+    iResult=0;
+  } 
+
+  if (!fpEsdHandlers)
+    fpEsdHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
+  if (!fpProprietaryHandlers)
+    fpProprietaryHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
+
+  AliHLTOUT::AliHLTOUTHandlerListEntryVector* pEsdHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpEsdHandlers);
+  AliHLTOUT::AliHLTOUTHandlerListEntryVector* pProprietaryHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpProprietaryHandlers);
+  if (!pEsdHandlers || !pProprietaryHandlers) return -ENOMEM;
+
+  // invalidate all blocks
+  AliHLTOUT::InvalidateBlocks(*pEsdHandlers);
+  AliHLTOUT::InvalidateBlocks(*pProprietaryHandlers);
+
+  AliHLTComponentDataTypeList esdBlocks;
+
   for (iResult=pHLTOUT->SelectFirstDataBlock();
        iResult>=0;
        iResult=pHLTOUT->SelectNextDataBlock()) {
@@ -751,6 +925,8 @@ int AliHLTSystem::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd)
     pHLTOUT->GetDataBlockDescription(dt, spec);
     AliHLTOUTHandler* pHandler=pHLTOUT->GetHandler();
     AliHLTModuleAgent::AliHLTOUTHandlerType handlerType=pHLTOUT->GetDataBlockHandlerType();
+
+    // default handling for ESD data blocks does not require an explicite handler
     if (!pHandler && (dt==kAliHLTDataTypeESDObject || dt==kAliHLTDataTypeESDTree)) {
       handlerType=AliHLTModuleAgent::kEsd;
     }
@@ -759,22 +935,26 @@ int AliHLTSystem::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd)
     case AliHLTModuleAgent::kEsd:
       {
        if (pHandler) {
-         // preprocess and write later
-         AliHLTOUT::AliHLTOUTLockGuard g(pHLTOUT);
-         pHandler->ProcessData(pHLTOUT);
-         pHLTOUT->InsertHandler(esdHandlers, pHLTOUT->GetDataBlockHandlerDesc());
+         // schedule for later processing
+         pHLTOUT->InsertHandler(*pEsdHandlers, pHLTOUT->GetDataBlockHandlerDesc());
        } else {
+         AliHLTComponentDataTypeList::iterator element=esdBlocks.begin();
+         for (; element!=esdBlocks.end(); element++) {
+           if (*element==dt) {
+             HLTWarning("multiple ESDs of identical data type %s, please add appropriate handler to merge ESDs", AliHLTComponent::DataType2Text(dt).c_str());
+             break;
+           }
+         }
+         if (element==esdBlocks.end()) esdBlocks.push_back(dt);
+
          // write directly
          const AliHLTUInt8_t* pBuffer=NULL;
          AliHLTUInt32_t size=0;
          if (pHLTOUT->GetDataBuffer(pBuffer, size)>=0) {
-           pHLTOUT->WriteESD(pBuffer, size, dt);
-           if (pMasterESD) {
-             pHLTOUT->WriteESD(pBuffer, size, dt, pMasterESD);
-             pMasterESD=NULL;
-           }
+           pHLTOUT->WriteESD(pBuffer, size, dt, esd);
            pHLTOUT->ReleaseDataBuffer(pBuffer);
          }
+         pHLTOUT->MarkDataBlockProcessed();
        }
       }
       break;
@@ -783,24 +963,25 @@ int AliHLTSystem::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd)
       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",
+                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",
+      HLTWarning("HLTOUT handler type 'kChain' has already been processed: agent %s, data type %s, specification %#x\n"
+                "New block of this type added by the chain? Skipping data block ...",
+                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",
+                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",
+                    res, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
                     AliHLTComponent::DataType2Text(dt).c_str(), spec);
        }
       }
@@ -810,7 +991,7 @@ int AliHLTSystem::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd)
       // 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",
+                pMsg, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
                 AliHLTComponent::DataType2Text(dt).c_str(), spec);
     }
   }
@@ -818,24 +999,84 @@ int AliHLTSystem::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd)
   // changed in order to avoid this check
   if (iResult==-ENOENT) iResult=0;
 
-  AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator esdHandler;
-  // write all postponed esd data blocks
-  for (esdHandler=esdHandlers.begin(); esdHandler!=esdHandlers.end() && iResult>=0; esdHandler++) {
-    AliHLTOUTHandler* pHandler=*esdHandler;
+  AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator handler;
+
+  // process and write all esd data blocks
+  for (handler=pEsdHandlers->begin(); handler!=pEsdHandlers->end() && iResult>=0; handler++) {
+    AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));            
+    AliHLTOUTHandler* pHandler=*handler;
     const AliHLTUInt8_t* pBuffer=NULL;
     AliHLTUInt32_t size=0;
+    pHandler->ProcessData(pHLTOUT);
     if ((size=pHandler->GetProcessedData(pBuffer))>0) {
-      AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*esdHandler;
+      AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*handler;
       AliHLTComponentDataType dt=desc;
-      pHLTOUT->WriteESD(pBuffer, size, dt);
-      if (pMasterESD) {
-       pHLTOUT->WriteESD(pBuffer, size, dt, pMasterESD);
-       pMasterESD=NULL;
-      }
+      pHLTOUT->WriteESD(pBuffer, size, dt, esd);
+      pHandler->ReleaseProcessedData(pBuffer, size);
+    }
+    pHLTOUT->MarkDataBlocksProcessed(&(*handler));
+  }
+
+  // process all kProprietary data blocks
+  for (handler=pProprietaryHandlers->begin(); handler!=pProprietaryHandlers->end() && iResult>=0; handler++) {
+    AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));            
+    AliHLTOUTHandler* pHandler=*handler;
+    const AliHLTUInt8_t* pBuffer=NULL;
+    AliHLTUInt32_t size=0;
+    pHandler->ProcessData(pHLTOUT);
+    if ((size=pHandler->GetProcessedData(pBuffer))>0) {
+      HLTWarning("data produced by kProprietary handler ignored");
       pHandler->ReleaseProcessedData(pBuffer, size);
     }
+    pHLTOUT->MarkDataBlocksProcessed(&(*handler));
   }
 
+  // remove all empty handlers form the list (handlers which did not get a block this time)
+  AliHLTOUT::RemoveEmptyDuplicateHandlers(*pEsdHandlers);
+  AliHLTOUT::RemoveEmptyDuplicateHandlers(*pProprietaryHandlers);
+
+  return iResult;
+}
+
+int AliHLTSystem::ProcessHLTOUTkChain(AliHLTOUT* pHLTOUT)
+{
+  // see header file for class documentation
+  int iResult=0;
+  if (!pHLTOUT) return -EINVAL;
+
+  if (!fpChainHandlers)
+    fpChainHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
+
+  AliHLTOUT::AliHLTOUTHandlerListEntryVector* pChainHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpChainHandlers);
+  if (!pChainHandlers) return -ENOMEM;
+
+  // invalidate all blocks
+  AliHLTOUT::InvalidateBlocks(*pChainHandlers);
+
+  // fill the list
+  pHLTOUT->FillHandlerList(*pChainHandlers, AliHLTModuleAgent::kChain);
+
+  // process all defined chain handlers
+  AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator chainHandler;
+  for (chainHandler=pChainHandlers->begin(); chainHandler!=pChainHandlers->end() && iResult>=0; chainHandler++) {
+    if (chainHandler->IsEmpty()) continue;
+    AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*chainHandler));       
+    AliHLTOUTHandler* pHandler=*chainHandler;
+    const AliHLTUInt8_t* pBuffer=NULL;
+    AliHLTUInt32_t size=0;
+    pHandler->ProcessData(pHLTOUT);
+    if ((size=pHandler->GetProcessedData(pBuffer))>0) {
+      AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*chainHandler;
+      //AliHLTComponentDataType dt=desc;
+
+      pHandler->ReleaseProcessedData(pBuffer, size);
+    }
+    pHLTOUT->MarkDataBlocksProcessed(&(*chainHandler));
+  }
+
+  // remove all empty handlers form the list (handlers which did not get a block this time)
+  AliHLTOUT::RemoveEmptyDuplicateHandlers(*pChainHandlers);
+
   return iResult;
 }
 
@@ -848,9 +1089,9 @@ int AliHLTSystem::LoadComponentLibraries(const char* libraries)
       TString libs(libraries);
       TObjArray* pTokens=libs.Tokenize(" ");
       if (pTokens) {
-       int iEntries=pTokens->GetEntries();
+       int iEntries=pTokens->GetEntriesFast();
        for (int i=0; i<iEntries && iResult>=0; i++) {
-         iResult=fpComponentHandler->LoadLibrary((((TObjString*)pTokens->At(i))->GetString()).Data());
+         iResult=fpComponentHandler->LoadLibrary((((TObjString*)pTokens->At(i))->String()).Data());
        }
        delete pTokens;
       }
@@ -892,7 +1133,7 @@ int AliHLTSystem::Configure(AliRawReader* rawReader, AliRunLoader* runloader)
     iResult=LoadConfigurations(rawReader, runloader);
   } else {
     if (fChains.Length()==0) {
-      HLTError("custom configuration(s) specified, but no configuration to run in local reconstruction, use \'localrec=<conf>\' option");
+      HLTError("custom configuration(s) specified, but no configuration to run in local reconstruction, use \'chains=<chain,...>\' option");
       iResult=-ENOENT;
     }
   }
@@ -917,12 +1158,13 @@ int AliHLTSystem::ScanOptions(const char* options)
   if (options) {
     //AliHLTComponentHandler::TLibraryMode libMode=AliHLTComponentHandler::kDynamic;
     TString libs("");
+    TString excludelibs("");
     TString alloptions(options);
     TObjArray* pTokens=alloptions.Tokenize(" ");
     if (pTokens) {
-      int iEntries=pTokens->GetEntries();
+      int iEntries=pTokens->GetEntriesFast();
       for (int i=0; i<iEntries; i++) {
-       TString token=(((TObjString*)pTokens->At(i))->GetString());
+       TString token=(((TObjString*)pTokens->At(i))->String());
        if (token.Contains("loglevel=")) {
          TString param=token.ReplaceAll("loglevel=", "");
          if (param.IsDigit()) {
@@ -949,19 +1191,58 @@ int AliHLTSystem::ScanOptions(const char* options)
          }
        } else if (token.Contains("alilog=off")) {
          SwitchAliLog(0);
-       } else if (token.Contains("config=")) {
-         TString param=token.ReplaceAll("config=", "");
-         Int_t error=0;
-         gROOT->Macro(param.Data(), &error);
-         if (error==0) {
-           SetStatusFlags(kConfigurationLoaded);
+       } else if (token.Contains("config=") || token.Contains("run-online-config")) {
+         if (!CheckStatus(kConfigurationLoaded)) {
+           Int_t error=0;
+           AliHLTOnlineConfiguration* pConf = NULL;
+           if (token.Contains("run-online-config")) {
+             AliCDBEntry* pEntry=AliHLTMisc::Instance().LoadOCDBEntry("HLT/Calib/OnlineConfig");
+             if (pEntry) {
+               TObject* pObject=AliHLTMisc::Instance().ExtractObject(pEntry);
+               if (pObject && pObject->IsA() == AliHLTOnlineConfiguration::Class())
+                 pConf = (AliHLTOnlineConfiguration*)pObject;
+             }
+           }
+           if (token.Contains("config=")) {
+             TString param=token.ReplaceAll("config=", "");
+             if (token.EndsWith(".xml", TString::kIgnoreCase)) {
+               Int_t filesize = 0;
+               pConf = new AliHLTOnlineConfiguration;
+               filesize = pConf->LoadConfiguration(param.Data());
+               if (filesize <= 0) {
+                 HLTError("cannot load config \'%s\'", param.Data());
+                 iResult=-EBADF;
+               }
+             } else {
+               gROOT->Macro(param.Data(), &error);
+               if (error==0) {
+                 SetStatusFlags(kConfigurationLoaded);
+               } else {
+                 HLTError("cannot execute macro \'%s\'", param.Data());
+                 iResult=-EBADF;
+               }
+             }
+           }
+           if (pConf) {
+               error = pConf->Parse();
+               if (error==0) {
+                 fChains = pConf->GetDefaultChains();
+                 libs = pConf->GetComponentLibraries();
+                 libs += " ";
+                 SetStatusFlags(kConfigurationLoaded);
+               } else {
+                 HLTError("cannot parse online configuration");
+                 iResult=-EBADF;
+               }
+           }
+           delete pConf; pConf=NULL;
          } else {
-           HLTError("can not execute macro \'%s\'", param.Data());
-           iResult=-EBADF;
+           HLTWarning("HLT options has both a config file and run-online-config set");
          }
        } else if (token.Contains("chains=")) {
          TString param=token.ReplaceAll("chains=", "");
          fChains=param.ReplaceAll(",", " ");
+         if (fChains.IsNull()) fChains=" "; // disable all chains
        } else if (token.Contains("libmode=")) {
          TString param=token.ReplaceAll("libmode=", "");
          param.ReplaceAll(",", " ");
@@ -974,9 +1255,21 @@ int AliHLTSystem::ScanOptions(const char* options)
              HLTWarning("wrong argument for option \'libmode=\', use \'static\' or \'dynamic\'");
            }
          }
+       } else if (token.BeginsWith("ECS=")) {
+         fECSParams=token.ReplaceAll("ECS=", "");
+       } else if (token.BeginsWith("hltout-mode=")) {
+         // The actual parameter for argument 'hltout-mode' is treated in AliSimulation.
+         // For AliHLTSystem the occurrence with parameter 'split' signals the use of the
+         // separated HLTOUTComponents for digit and raw data. All others indicate
+         // HLTOUTComponent type 'global' where the data generation is steered from global
+         // flags
+         fUseHLTOUTComponentTypeGlobal=token.CompareTo("hltout-mode=split")!=0;
        } else if (token.BeginsWith("lib") && token.EndsWith(".so")) {
          libs+=token;
          libs+=" ";
+       } else if (token.BeginsWith("!lib") && token.EndsWith(".so")) {
+         excludelibs+=token;
+         excludelibs+=" ";
        } else {
          HLTWarning("unknown option \'%s\'", token.Data());
        }
@@ -987,8 +1280,9 @@ int AliHLTSystem::ScanOptions(const char* options)
     if (iResult>=0) {
       if (libs.IsNull()) {
        const char** deflib=fgkHLTDefaultLibs;
-       while (*deflib) {
-         libs+=*deflib++;
+       for (;*deflib; deflib++) {
+         if (excludelibs.Contains(*deflib)) continue;
+         libs+=*deflib;
          libs+=" ";
        }
       }
@@ -1023,19 +1317,75 @@ int AliHLTSystem::LoadConfigurations(AliRawReader* rawReader, AliRunLoader* runl
     return -EBUSY;
   }
   int iResult=0;
-  AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
-  while (pAgent && iResult>=0) {
+  AliHLTModuleAgent* pAgent=NULL;
+
+  // first check for the required libraries and load those
+  TString extralibs;
+  for (pAgent=AliHLTModuleAgent::GetFirstAgent(); 
+       pAgent && iResult>=0;
+       pAgent=AliHLTModuleAgent::GetNextAgent()) {
     const char* deplibs=pAgent->GetRequiredComponentLibraries();
     if (deplibs) {
-      HLTDebug("load libraries \'%s\' for agent %s (%p)", deplibs, pAgent->GetName(), pAgent);
-      iResult=LoadComponentLibraries(deplibs);
+      HLTDebug("required libraries \'%s\' for agent %s (%p)", deplibs, pAgent->GetName(), pAgent);
+      extralibs+=" ";
+      extralibs+=deplibs;
     }
-    if (iResult>=0) {
+  }
+  if (iResult>=0) {
+    iResult=LoadComponentLibraries(extralibs.Data());
+  }
+
+  // in order to register the configurations in the correct sequence
+  // all agents need to be ordered with respect to the required
+  // libraries. Ordering relies on the naming convention
+  // libAliHLT<Module>.so
+  TList agents;
+  for (pAgent=AliHLTModuleAgent::GetFirstAgent(); 
+       pAgent && iResult>=0;
+       pAgent=AliHLTModuleAgent::GetNextAgent()) {
+    AliHLTModuleAgent* pPrevDep=NULL;
+    TString dependencies=pAgent->GetRequiredComponentLibraries();
+    TObjArray* pTokens=dependencies.Tokenize(" ");
+    if (pTokens) {
+      for (int n=0; n<pTokens->GetEntriesFast(); n++) {
+       TString module=((TObjString*)pTokens->At(n))->String();
+       HLTDebug("  checking %s", module.Data());
+       module.ReplaceAll("libAliHLT", "");
+       module.ReplaceAll(".so", "");
+       
+       for (AliHLTModuleAgent* pCurrent=dynamic_cast<AliHLTModuleAgent*>(pPrevDep==NULL?agents.First():agents.After(pPrevDep));
+            pCurrent!=NULL; pCurrent=dynamic_cast<AliHLTModuleAgent*>(agents.After(pCurrent))) {
+         HLTDebug("    checking %s == %s", module.Data(), pCurrent->GetModuleId());
+
+         if (module.CompareTo(pCurrent->GetModuleId())==0) {
+           pPrevDep=pCurrent;
+           break;
+         }
+       }
+      }
+      delete pTokens;
+    }
+
+    if (pPrevDep) {
+      // insert right after the last dependency
+      agents.AddAfter(pPrevDep, pAgent);
+      HLTDebug("insert %s after %s", pAgent->GetModuleId(), pPrevDep->GetModuleId());
+    } else {
+      // insert at the beginning
+      agents.AddFirst(pAgent);
+      HLTDebug("insert %s at beginning", pAgent->GetModuleId());
+    }
+  }
+
+  // now we load the configurations
+  if (agents.GetEntries()) {
+    TIter next(&agents);
+    while ((pAgent = dynamic_cast<AliHLTModuleAgent*>(next()))) {
       HLTDebug("load configurations for agent %s (%p)", pAgent->GetName(), pAgent);
       pAgent->CreateConfigurations(fpConfigurationHandler, rawReader, runloader);
-      pAgent=AliHLTModuleAgent::GetNextAgent();
     }
   }
+
   return iResult;
 }
 
@@ -1060,31 +1410,33 @@ int AliHLTSystem::BuildTaskListsFromReconstructionChains(AliRawReader* rawReader
     chains=fChains;
     HLTImportant("custom reconstruction chain: %s", chains.Data());
   } else {
-    AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
-    while ((pAgent || fChains.Length()>0) && iResult>=0) {
+    for (AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
+        pAgent && iResult>=0;
+        pAgent=AliHLTModuleAgent::GetNextAgent()) {
       const char* agentchains=pAgent->GetReconstructionChains(rawReader, runloader);
       if (agentchains) {
-       if (!chains.IsNull()) chains+="";
+       if (!chains.IsNull()) chains+=" ";
        chains+=agentchains;
        HLTInfo("reconstruction chains for agent %s (%p): %s", pAgent->GetName(), pAgent, agentchains);
       }
-      pAgent=AliHLTModuleAgent::GetNextAgent();
     }
   }
 
   // build task list for chains
   TObjArray* pTokens=chains.Tokenize(" ");
   if (pTokens) {
-    int iEntries=pTokens->GetEntries();
+    int iEntries=pTokens->GetEntriesFast();
     for (int i=0; i<iEntries && iResult>=0; i++) {
-      const char* pCID=((TObjString*)pTokens->At(i))->GetString().Data();
+      const char* pCID=((TObjString*)pTokens->At(i))->String().Data();
       AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(pCID);
       if (pConf) {
        iResult=BuildTaskList(pConf);
-       if (runloader) {
+       if (true) { // condition was deprecated but kept for sake of svn diff
+         // bHaveOutput variable has to be set for both running modes
+         // AliHLTSimulation and AliHLTReconstruction
          assert(fpComponentHandler!=NULL);
          TString cid=pConf->GetComponentID();
-         if (cid.CompareTo("HLTOUT")==0) {
+         if (runloader!=NULL && cid.CompareTo("HLTOUT")==0) {
            // remove from the input of a global HLTOUT configuration
            chains.ReplaceAll(pCID, "");
          } else if (bHaveOutput==0) {
@@ -1107,9 +1459,34 @@ int AliHLTSystem::BuildTaskListsFromReconstructionChains(AliRawReader* rawReader
     if (bHaveOutput) {
       // there are components in the chain which produce data which need to be
       // piped to an HLTOUT
+
+      // add the SchemaEvolutionComponent which analyzes the ROOT objects in
+      // the output stream
+      if (fpComponentHandler->FindComponentIndex("ROOTSchemaEvolutionComponent")>=0 ||
+         fpComponentHandler->LoadLibrary("libAliHLTUtil.so")>=0) {
+       AliHLTConfiguration schemaevo("_schemaevolution_", "ROOTSchemaEvolutionComponent", 
+                                     chains.Data(), "-file=HLT.StreamerInfo.root");
+       iResult=BuildTaskList("_schemaevolution_");
+      } else {
+       HLTWarning("can not load libAliHLTUtil.so and ROOTSchemaEvolutionComponent");
+      }
+
+      // add the HLTOUT component
       if (fpComponentHandler->FindComponentIndex("HLTOUT")>=0 ||
-         LoadComponentLibraries("libHLTsim.so")>=0) {
-       AliHLTConfiguration globalout("_globalout_", "HLTOUT", chains.Data(), NULL);
+         fpComponentHandler->LoadLibrary("libHLTsim.so")>=0) {
+       // for the default HLTOUTComponent type 'global' the data generation is steered
+       // by global flags from AliSimulation. This allows for emulation of the old
+       // AliHLTSimulation behavior where only one chain is run on either digits or
+       // simulated raw data and the HLT digits and raw files have been generated
+       // depending on the configuration
+       const char* HLTOUTComponentId="HLTOUT";
+       if (!fUseHLTOUTComponentTypeGlobal) {
+         // choose the type of output depending  on the availability of
+         // the raw reader
+         if (rawReader) HLTOUTComponentId="HLTOUTraw";
+         else HLTOUTComponentId="HLTOUTdigits";
+       }
+       AliHLTConfiguration globalout("_globalout_", HLTOUTComponentId, chains.Data(), NULL);
        iResult=BuildTaskList("_globalout_");
       } else {
        HLTError("can not load libHLTsim.so and HLTOUT component");
@@ -1118,11 +1495,95 @@ int AliHLTSystem::BuildTaskListsFromReconstructionChains(AliRawReader* rawReader
     }
   }
 
+  // build HLTOUT task for reconstruction
+  // Matthias 08.07.2008 the rawReader is never set when running embedded into
+  // AliReconstruction. The system is configured during AliHLTReconstructor::Init
+  // where the RawReader is not available. It is available in the first invocation
+  // of Reconstruct.
+  // 
+  // That means that policy is slightly changed:
+  // - if the run loader is available -> AliSimulation
+  // - no run loader available -> AliReconstruction
+  if (iResult>=0 && !runloader) {
+    if (bHaveOutput) {
+      // there are components in the chain which produce data which need to be
+      // piped to an HLTOUT sub-collection
+      if (!fpHLTOUTTask) {
+       iResult=AddHLTOUTTask(chains.Data());
+      }
+    }
+  }
+
   if (iResult>=0) SetStatusFlags(kTaskListCreated);
 
   return iResult;
 }
 
+int AliHLTSystem::AddHLTOUTTask(const char* hltoutchains)
+{
+  // see header file for class documentation
+  int iResult=0;
+  if (!hltoutchains || hltoutchains[0]==0) return 0;
+
+  // check chains for output
+  TString chains=hltoutchains;
+  TObjArray* pTokens=chains.Tokenize(" ");
+  if (pTokens) {
+    int iEntries=pTokens->GetEntriesFast();
+    for (int i=0; i<iEntries && iResult>=0; i++) {
+      const char* token=((TObjString*)pTokens->At(i))->String().Data();
+      AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(token);
+      if (pConf) {
+       TString cid=pConf->GetComponentID();
+       if (fpComponentHandler->HasOutputData(cid.Data())) {
+         continue;
+       }
+      } else {
+       HLTWarning("can not find configuration %s", token);
+      }
+      // remove from the list of hltout chains
+      chains.ReplaceAll(token, "");
+    }
+    delete pTokens;
+  }
+
+  // do not create the HLTOUT task if none of the chains have output
+  if (chains.IsNull()) return 0;
+
+  // indicate the task to be available
+  iResult=1;
+
+  if (fpHLTOUTTask) {
+    if (strcmp(chains.Data(), fpHLTOUTTask->GetSourceChains())==0) {
+      HLTWarning("HLTOUT task already added for chains \"%s\" %p", chains.Data(), fpHLTOUTTask);
+    } else {
+      HLTError("HLTOUT task already added for chains \"%s\" %p, ignoring new chains  \"%s\"",
+              fpHLTOUTTask->GetSourceChains(), fpHLTOUTTask, chains.Data());
+    }
+    return iResult;
+  }
+
+  fpHLTOUTTask=new AliHLTOUTTask(chains);
+  if (fpHLTOUTTask) {
+    if (fpHLTOUTTask->GetConf() && 
+       (fpHLTOUTTask->GetConf()->SourcesResolved()>0 ||
+        fpHLTOUTTask->GetConf()->ExtractSources()>0)) {
+      iResult=InsertTask(fpHLTOUTTask);
+    } else {
+      HLTError("HLTOUT task (%s) sources not resolved", fpHLTOUTTask->GetName());
+      iResult=-ENOENT;
+    }
+
+    if (iResult<0) {
+      delete fpHLTOUTTask;
+    }
+
+  } else {
+    iResult=-ENOMEM;
+  }
+  return iResult;
+}
+
 int AliHLTSystem::CheckStatus(int flag)
 {
   // see header file for class documentation
@@ -1151,7 +1612,7 @@ int AliHLTSystem::ClearStatusFlags(int flags)
   return fState;
 }
 
-void* AliHLTSystem::FindDynamicSymbol(const char* library, const char* symbol)
+AliHLTfctVoid AliHLTSystem::FindDynamicSymbol(const char* library, const char* symbol)
 {
   // see header file for class documentation
   if (fpComponentHandler==NULL) return NULL;
@@ -1165,3 +1626,64 @@ void AliHLTSystem::SetFrameworkLog(AliHLTComponentLogSeverity level)
   if (fpComponentHandler) fpComponentHandler->SetLocalLoggingLevel(level);
   if (fpConfigurationHandler) fpConfigurationHandler->SetLocalLoggingLevel(level);
 }
+
+int AliHLTSystem::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);
+
+  if (!fName.IsNull())
+    AliHLTLogging::SetLogString(this, " (%p)", "%s_pfmt_: ", fName.Data());
+  iResult=SendMessage(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args, !fName.IsNull() /*append if non empty*/));
+  va_end(args);
+
+  return iResult;
+}
+
+int AliHLTSystem::InitHLTOUT(AliHLTOUT* instance)
+{
+  // Init the HLTOUT instance for the current event.
+  // The instance can be used by other classes to get hold on the data
+  // from HLTOUT.
+  if (!instance) return -EINVAL;
+  if (fpHLTOUT && fpHLTOUT!=instance) return -EBUSY;
+  fpHLTOUT=instance;
+  return 0;
+}
+
+int AliHLTSystem::InvalidateHLTOUT(AliHLTOUT** target)
+{
+  // Clear the HLTOUT instance.
+  int iResult=0;
+  if (fHLTOUTUse>0) {
+    HLTWarning("HLTOUT instance still in use, potential problem due to invalid pointer ahead");
+    fHLTOUTUse=0;
+    iResult=-EBUSY;
+  }
+  if (target) *target=fpHLTOUT;
+  fpHLTOUT=NULL;
+  return iResult;
+}
+
+AliHLTOUT* AliHLTSystem::RequestHLTOUT()
+{
+  // Get the HLTOUT instance.
+  // User method for processing classes. To be released after use.
+  if (!fpHLTOUT) return NULL;
+  fHLTOUTUse++;
+  return fpHLTOUT;
+}
+
+int AliHLTSystem::ReleaseHLTOUT(const AliHLTOUT* instance)
+{
+  // Release the HLTOUT instance after use.
+  if (!instance) return -EINVAL;
+  if (instance!=fpHLTOUT) return -ENOENT;
+  fHLTOUTUse--;
+  return 0;
+}