]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTSystem.cxx
Minor bug fixes
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTSystem.cxx
1 // $Id$
2
3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project        * 
5 //* ALICE Experiment at CERN, All rights reserved.                         *
6 //*                                                                        *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8 //*                  for The ALICE HLT Project.                            *
9 //*                                                                        *
10 //* Permission to use, copy, modify and distribute this software and its   *
11 //* documentation strictly for non-commercial purposes is hereby granted   *
12 //* without fee, provided that the above copyright notice appears in all   *
13 //* copies and that both the copyright notice and this permission notice   *
14 //* appear in the supporting documentation. The authors make no claims     *
15 //* about the suitability of this software for any purpose. It is          *
16 //* provided "as is" without express or implied warranty.                  *
17 //**************************************************************************
18
19 /** @file   AliHLTSystem.cxx
20     @author Matthias Richter
21     @date   
22     @brief  Implementation of HLT module management.
23 */
24
25 #if __GNUC__>= 3
26 using namespace std;
27 #endif
28
29 #include <cassert>
30 #include "AliHLTStdIncludes.h"
31 #include "AliHLTSystem.h"
32 #include "AliHLTComponentHandler.h"
33 #include "AliHLTComponent.h"
34 #include "AliHLTConfiguration.h"
35 #include "AliHLTConfigurationHandler.h"
36 #include "AliHLTTask.h"
37 #include "AliHLTModuleAgent.h"
38 #include "AliHLTOfflineInterface.h"
39 #include "AliHLTDataSource.h"
40 #include "AliHLTOUT.h"
41 #include "AliHLTOUTHandler.h"
42 #include "AliHLTOUTTask.h"
43 #include <TObjArray.h>
44 #include <TObjString.h>
45 #include <TStopwatch.h>
46 //#include <TSystem.h>
47 #include <TROOT.h>
48 //#include <TInterpreter.h>
49
50 /** HLT default component libraries */
51 const char* AliHLTSystem::fgkHLTDefaultLibs[]= {
52   "libAliHLTUtil.so", 
53   "libAliHLTRCU.so", 
54   "libAliHLTTPC.so", 
55   //  "libAliHLTSample.so",
56   //"libAliHLTPHOS.so",
57   "libAliHLTMUON.so",
58   "libAliHLTTRD.so",
59   "libAliHLTTrigger.so",
60   NULL
61 };
62
63 /** ROOT macro for the implementation of ROOT specific class methods */
64 ClassImp(AliHLTSystem)
65
66 AliHLTSystem::AliHLTSystem(AliHLTComponentLogSeverity loglevel)
67   :
68   fpComponentHandler(AliHLTComponentHandler::CreateHandler()),
69   fpConfigurationHandler(AliHLTConfigurationHandler::CreateHandler()),
70   fTaskList(),
71   fState(0),
72   fChains(),
73   fStopwatches(new TObjArray),
74   fEventCount(-1),
75   fGoodEvents(-1),
76   fpChainHandlers(NULL),
77   fpEsdHandlers(NULL),
78   fpProprietaryHandlers(NULL),
79   fpHLTOUTTask(NULL)
80 {
81   // see header file for class documentation
82   // or
83   // refer to README to build package
84   // or
85   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
86
87   if (fgNofInstances++>0) {
88     // July 2008: multiple instances are now allowed
89     // AliHLTSystem is used in multiple instances for the kChain HLTOUT handler
90     //HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
91   }
92
93   SetGlobalLoggingLevel(loglevel);
94   SetFrameworkLog(loglevel);
95   if (fpComponentHandler) {
96     AliHLTAnalysisEnvironment env;
97     memset(&env, 0, sizeof(AliHLTAnalysisEnvironment));
98     env.fStructSize=sizeof(AliHLTAnalysisEnvironment);
99     env.fAllocMemoryFunc=AliHLTSystem::AllocMemory;
100     env.fLoggingFunc=NULL;
101     fpComponentHandler->SetEnvironment(&env);
102     InitAliLogFunc(fpComponentHandler);
103     if (fgNofInstances==1) {
104     fpComponentHandler->AnnounceVersion();
105     }
106   } else {
107     HLTFatal("can not create Component Handler");
108   }
109   if (fpConfigurationHandler) {
110     AliHLTConfiguration::GlobalInit(fpConfigurationHandler);
111   } else {
112     HLTFatal("can not create Configuration Handler");
113   }
114 }
115
116 AliHLTSystem::~AliHLTSystem()
117 {
118   // see header file for class documentation
119   fgNofInstances--;
120   CleanTaskList();
121   AliHLTConfiguration::GlobalDeinit(fpConfigurationHandler);
122   if (fpConfigurationHandler) {
123     fpConfigurationHandler->Destroy();
124   }
125   fpConfigurationHandler=NULL;
126   
127   if (fpComponentHandler) {
128     fpComponentHandler->Destroy();
129   }
130   fpComponentHandler=NULL;
131 }
132
133 int AliHLTSystem::fgNofInstances=0;
134
135 int AliHLTSystem::AddConfiguration(AliHLTConfiguration* pConf)
136 {
137   // see header file for class documentation
138   HLTLogKeyword("configuration handling");
139   int iResult=0;
140   if (pConf) {
141     HLTError("function not yet implemented");
142     iResult=-ENOSYS;
143   } else {
144     iResult=-EINVAL;
145   }
146   return iResult;
147 }
148
149 int AliHLTSystem::InsertConfiguration(AliHLTConfiguration* pConf, AliHLTConfiguration* pPrec)
150 {
151   // see header file for class documentation
152   HLTLogKeyword("configuration handling");
153   int iResult=0;
154   if (pConf) {
155     if (pPrec) {
156       // find the position
157       HLTError("function not yet implemented");
158       iResult=-ENOSYS;
159     }
160   } else {
161     iResult=-EINVAL;
162   }
163   return iResult;
164 }
165
166 int AliHLTSystem::DeleteConfiguration(AliHLTConfiguration* pConf)
167 {
168   // see header file for class documentation
169   HLTLogKeyword("configuration handling");
170   int iResult=0;
171   if (pConf) {
172     HLTError("function not yet implemented");
173     iResult=-ENOSYS;
174   } else {
175     iResult=-EINVAL;
176   }
177   return iResult;
178 }
179
180 int AliHLTSystem::BuildTaskList(const char* id)
181 {
182   // see header file for class documentation
183   int iResult=0;
184   if (id) {
185     if (fpConfigurationHandler) {
186       AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(id);
187       if (pConf) {
188         iResult=BuildTaskList(pConf);
189       } else {
190         HLTError("unknown configuration \"%s\"", id);
191         iResult=-EEXIST;
192       }
193     } else {
194       iResult=-EFAULT;
195     }
196   } else {
197     iResult=-EINVAL;
198   }
199   return iResult;
200 }
201
202 int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
203 {
204   // see header file for class documentation
205   int iResult=0;
206   if (pConf) {
207     AliHLTTask* pTask=NULL;
208     if ((pTask=FindTask(pConf->GetName()))!=NULL) {
209       if (pTask->GetConf()!=pConf) {
210         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);
211         iResult=-EEXIST;
212       }
213       // task for this configuration exists, terminate
214       pTask=NULL;
215     } else if (pConf->SourcesResolved(1)!=1) {
216         HLTError("configuration \"%s\" has unresolved sources, aborting ...", pConf->GetName());
217         iResult=-ENOLINK;
218     } else {
219       pTask=new AliHLTTask(pConf);
220       if (pTask==NULL) {
221         iResult=-ENOMEM;
222       } else {
223         pTask->SetLocalLoggingLevel(GetLocalLoggingLevel());
224       }
225     }
226     static int iterationLevel=0;
227     if (pTask && iResult>=0) {
228       // check for circular dependencies
229       if ((iResult=pConf->FollowDependency(pConf->GetName()))>0) {
230         HLTError("detected circular dependency for configuration \"%s\"", pTask->GetName());
231         pTask->PrintDependencyTree(pTask->GetName(), 1/*use the configuration list*/);
232         HLTError("aborted ...");
233         iResult=-ELOOP;
234       }
235       if (iResult>=0) {
236         // check whether all dependencies are already in the task list
237         // create the missing ones
238         // this step is an iterative process which calls this function again for the missing
239         // configurations, in order to avoid the currently processed task to be created
240         // again it is added to the list temporarily and removed afterwards
241         // This is of high importance to preserve the order of the tasks. Furthermore, the
242         // InsertTask method has to be used in order to set all the cross links right 
243         fTaskList.Add(pTask);
244         AliHLTConfiguration* pDep=pConf->GetFirstSource();
245         while (pDep!=NULL && iResult>=0) {
246           HLTDebug("iteration %d: checking dependency %s (%p)", iterationLevel, pDep->GetName(), pDep);
247           if (FindTask(pDep->GetName())==NULL) {
248             HLTDebug("iteration %d: building task list for configuration %s (%p)", iterationLevel, pDep->GetName(), pDep);
249             iterationLevel++;
250             iResult=BuildTaskList(pDep);
251             iterationLevel--;
252           }
253           pDep=pConf->GetNextSource();
254         }
255         // remove the temporarily added task
256         fTaskList.Remove(pTask);
257
258         // insert the task and set the cross-links
259         if (iResult>=0) {
260           HLTDebug("iteration %d: inserting task %s (%p)", iterationLevel, pTask->GetName(), pTask);
261           iResult=InsertTask(pTask);
262         }
263       } else {
264         delete pTask;
265         pTask=NULL;
266       }
267     }
268   } else {
269     iResult=-EINVAL;
270   }
271   return iResult;
272 }
273
274 int AliHLTSystem::CleanTaskList()
275 {
276   // see header file for class documentation
277   int iResult=0;
278   TObjLink* lnk=NULL;
279   while ((lnk=fTaskList.LastLink())!=NULL) {
280     delete (lnk->GetObject());
281     fTaskList.Remove(lnk);
282   }
283   return iResult;
284 }
285
286 int AliHLTSystem::InsertTask(AliHLTTask* pTask)
287 {
288   // see header file for class documentation
289   int iResult=0;
290   TObjLink *lnk = NULL;
291   if ((iResult=pTask->CheckDependencies())>0)
292     lnk=fTaskList.FirstLink();
293   while (lnk && iResult>0) {
294     AliHLTTask* pCurr = (AliHLTTask*)lnk->GetObject();
295     //HLTDebug("checking  \"%s\"", pCurr->GetName());
296     iResult=pTask->Depends(pCurr);
297     if (iResult>0) {
298       iResult=pTask->SetDependency(pCurr);
299       pCurr->SetTarget(pTask);
300       HLTDebug("set dependency  \"%s\" for configuration \"%s\"", pCurr->GetName(), pTask->GetName());
301     }
302     if (pCurr->Depends(pTask)) {
303       // circular dependency
304       HLTError("circular dependency: can not resolve dependencies for configuration \"%s\"", pTask->GetName());
305       iResult=-ELOOP;
306     } else if ((iResult=pTask->CheckDependencies())>0) {
307       lnk = lnk->Next();
308     }
309   }
310   if (iResult==0) {
311       if (lnk) {
312         fTaskList.AddAfter(lnk, pTask);
313       } else {
314         fTaskList.AddFirst(pTask);
315       }
316       HLTDebug("task \"%s\" (%p) inserted (size %d)", pTask->GetName(), pTask, sizeof(AliHLTTask));
317   } else if (iResult>0) {
318     HLTError("can not resolve dependencies for configuration \"%s\" (%d unresolved)", pTask->GetName(), iResult);
319     iResult=-ENOLINK;
320   }
321   return iResult;
322 }
323
324 AliHLTTask* AliHLTSystem::FindTask(const char* id)
325 {
326   // see header file for class documentation
327   AliHLTTask* pTask=NULL;
328   if (id) {
329     pTask=dynamic_cast<AliHLTTask*>(fTaskList.FindObject(id)); 
330   }
331   return pTask;
332 }
333
334 void AliHLTSystem::PrintTaskList()
335 {
336   // see header file for class documentation
337   HLTLogKeyword("task list");
338   TObjLink *lnk = NULL;
339   HLTMessage("Task List");
340   lnk=fTaskList.FirstLink();
341   while (lnk) {
342     TObject* obj=lnk->GetObject();
343     if (obj) {
344       HLTMessage("  %s - status:", obj->GetName());
345       AliHLTTask* pTask=(AliHLTTask*)obj;
346       pTask->PrintStatus();
347     } else {
348     }
349     lnk = lnk->Next();
350   }
351 }
352
353 int AliHLTSystem::Run(Int_t iNofEvents, int bStop)
354 {
355   // see header file for class documentation
356   int iResult=0;
357   int iCount=0;
358   SetStatusFlags(kRunning);
359   if (fEventCount>=0 || (iResult=InitTasks())>=0) {
360     if (fEventCount>=0 || (iResult=StartTasks())>=0) {
361       if (fEventCount==0) {
362         InitBenchmarking(fStopwatches);
363       } else {
364         ResumeBenchmarking(fStopwatches);    
365       }
366       for (int i=fEventCount; i<fEventCount+iNofEvents && iResult>=0; i++) {
367         if (fpHLTOUTTask) {
368           if (iNofEvents>1 && i==fEventCount) {
369             HLTWarning("can not add more than one event to the HLTOUT, skipping all but last block");
370           }
371           // reset and prepare for new data
372           fpHLTOUTTask->Reset();
373         }
374         if ((iResult=ProcessTasks(i))>=0) {
375           fGoodEvents++;
376           iCount++;
377         } else {
378           // TODO: define different running modes to either ignore errors in
379           // event processing or not
380           // currently ignored 
381           iResult=0;
382         }
383       }
384       fEventCount+=iNofEvents;
385       if (bStop) StopTasks();
386       else PauseBenchmarking(fStopwatches);
387     }
388     if (bStop) DeinitTasks();
389   }
390   if (iResult>=0) {
391     iResult=iCount;
392   } else  if (iResult==-126 /*ENOKEY*/) {
393     iResult=0; // do not propagate the error
394   }
395   ClearStatusFlags(kRunning);
396   return iResult;
397 }
398
399 int AliHLTSystem::InitTasks()
400 {
401   // see header file for class documentation
402   int iResult=0;
403   TObjLink *lnk=fTaskList.FirstLink();
404
405   if (lnk==NULL) {
406     HLTWarning("Task list is empty, skipping HLT");
407     return -126 /*ENOKEY*/;
408   }
409   while (lnk && iResult>=0) {
410     TObject* obj=lnk->GetObject();
411     if (obj) {
412       AliHLTTask* pTask=(AliHLTTask*)obj;
413       iResult=pTask->Init(NULL, fpComponentHandler);
414 //       ProcInfo_t ProcInfo;
415 //       gSystem->GetProcInfo(&ProcInfo);
416 //       HLTInfo("task %s initialized (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
417     } else {
418     }
419     lnk = lnk->Next();
420   }
421   if (iResult<0) {
422     HLTError("can not initialize task list, error %d", iResult);
423   }
424
425   return iResult;
426 }
427
428 int AliHLTSystem::InitBenchmarking(TObjArray* pStopwatches)
429 {
430   // see header file for class documentation
431   int iResult=0;
432   if (pStopwatches==NULL) return 0;
433
434   for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
435     TStopwatch* pStopwatch= new TStopwatch;
436     if (pStopwatch) {
437       pStopwatch->Reset();
438       pStopwatches->AddAt(pStopwatch, i);
439     } else {
440       iResult=-ENOMEM;
441       break;
442     }
443   }
444
445   TObjLink *lnk=fTaskList.FirstLink();
446   while (lnk && iResult>=0) {
447     TObject* obj=lnk->GetObject();
448     if (obj) {
449       AliHLTTask* pTask=(AliHLTTask*)obj;
450       AliHLTComponent* pComp=NULL;
451       if (iResult>=0 && (pComp=pTask->GetComponent())!=NULL) {
452         switch (pComp->GetComponentType()) {
453         case AliHLTComponent::kProcessor:
454           pComp->SetStopwatches(pStopwatches);
455           break;
456         case AliHLTComponent::kSource:
457           {
458             // this switch determines whether the time consumption of the
459             // AliHLTComponent base methods should be counted to the input
460             // stopwatch or base stopwatch.
461             //int inputBase=(int)AliHLTComponent::kSWBase;
462             int inputBase=(int)AliHLTComponent::kSWInput;
463             pComp->SetStopwatch(pStopwatches->At(inputBase), AliHLTComponent::kSWBase);
464             pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWInput), AliHLTComponent::kSWDA);
465           }
466           break;
467         case AliHLTComponent::kSink:
468           {
469             // this switch determines whether the time consumption of the
470             // AliHLTComponent base methods should be counted to the output
471             // stopwatch or base stopwatch.
472             //int outputBase=(int)AliHLTComponent::kSWBase;
473             int outputBase=(int)AliHLTComponent::kSWOutput;
474             pComp->SetStopwatch(pStopwatches->At(outputBase), AliHLTComponent::kSWBase);
475             pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWOutput), AliHLTComponent::kSWDA);
476           }
477           break;
478         default:
479           HLTWarning("unknown component type %d", (int)pComp->GetComponentType());
480         }
481       }
482     } else {
483     }
484     lnk = lnk->Next();
485   }
486   return iResult;
487 }
488
489 int AliHLTSystem::PauseBenchmarking(TObjArray* pStopwatches) const
490 {
491   // see header file for class documentation
492   if (pStopwatches==NULL) return 0;
493
494   for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
495     if (!pStopwatches->At(i)) continue;
496     TStopwatch* pSw=dynamic_cast<TStopwatch*>(pStopwatches->At(i));
497     if (pSw) pSw->Stop();
498   }
499   return 0;
500 }
501
502 int AliHLTSystem::ResumeBenchmarking(TObjArray* pStopwatches) const
503 {
504   // see header file for class documentation
505   if (pStopwatches==NULL) return 0;
506
507   for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
508     if (!pStopwatches->At(i)) continue;
509     TStopwatch* pSw=dynamic_cast<TStopwatch*>(pStopwatches->At(i));
510     if (pSw) pSw->Continue();
511   }
512   return 0;
513 }
514
515 int AliHLTSystem::PrintBenchmarking(TObjArray* pStopwatches, int bClean) const
516 {
517   // see header file for class documentation
518   int iInitialized=1;
519   if (pStopwatches==NULL) return 0;
520
521   for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
522     if (!dynamic_cast<TStopwatch*>(pStopwatches->At(i))) {
523       iInitialized=0;
524       break;
525     }
526   }
527
528   if (iInitialized!=0) {
529     HLTImportant("HLT statistics:\n"
530             "    base:              R:%.3fs C:%.3fs\n"
531             "    input:             R:%.3fs C:%.3fs\n"
532             "    output:            R:%.3fs C:%.3fs\n"
533             "    event processing : R:%.3fs C:%.3fs"
534             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->RealTime()
535             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->CpuTime()
536             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->RealTime()
537             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->CpuTime()
538             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->RealTime()
539             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->CpuTime()
540             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->RealTime()
541             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->CpuTime()
542             );
543   }
544
545   if (bClean) {
546     for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
547       TObject* pObj=pStopwatches->RemoveAt(i);
548       if (pObj) delete pObj;
549     }
550   }
551   return 0;
552 }
553
554 int AliHLTSystem::StartTasks()
555 {
556   // see header file for class documentation
557   int iResult=0;
558   TObjLink *lnk=fTaskList.FirstLink();
559   while (lnk && iResult>=0) {
560     TObject* obj=lnk->GetObject();
561     if (obj) {
562       AliHLTTask* pTask=(AliHLTTask*)obj;
563       iResult=pTask->StartRun();
564 //       ProcInfo_t ProcInfo;
565 //       gSystem->GetProcInfo(&ProcInfo);
566 //       HLTInfo("task %s started (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
567     } else {
568     }
569     lnk = lnk->Next();
570   }
571   if (iResult<0) {
572     HLTError("can not start task list, error %d", iResult);
573   } else {
574     SetStatusFlags(kStarted);
575     fEventCount=0;
576     fGoodEvents=0;
577     if ((iResult=SendControlEvent(kAliHLTDataTypeSOR))<0) {
578       HLTError("can not send SOR event");
579     }
580   }
581   return iResult;
582 }
583
584 int AliHLTSystem::ProcessTasks(Int_t eventNo)
585 {
586   // see header file for class documentation
587   int iResult=0;
588   HLTDebug("processing event no %d", eventNo);
589   TObjLink *lnk=fTaskList.FirstLink();
590   while (lnk && iResult>=0) {
591     TObject* obj=lnk->GetObject();
592     if (obj) {
593       AliHLTTask* pTask=(AliHLTTask*)obj;
594       iResult=pTask->ProcessTask(eventNo);
595 //       ProcInfo_t ProcInfo;
596 //       gSystem->GetProcInfo(&ProcInfo);
597 //       HLTInfo("task %s processed (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
598     } else {
599     }
600     lnk = lnk->Next();
601   }
602
603   if (iResult>=0) {
604     HLTImportant("Event %d successfully finished (%d)", eventNo, iResult);
605     iResult=0;
606   } else {
607     HLTError("Processing of event %d failed (%d)", eventNo, iResult);
608   }
609
610   return iResult;
611 }
612
613 int AliHLTSystem::StopTasks()
614 {
615   // see header file for class documentation
616   int iResult=0;
617   if ((iResult=SendControlEvent(kAliHLTDataTypeEOR))<0) {
618     HLTError("can not send EOR event");
619   }
620   TObjLink *lnk=fTaskList.FirstLink();
621   while (lnk) {
622     TObject* obj=lnk->GetObject();
623     if (obj) {
624       AliHLTTask* pTask=(AliHLTTask*)obj;
625       int locResult=pTask->EndRun();
626       if (iResult>=0 && locResult<0) iResult=locResult;
627 //       ProcInfo_t ProcInfo;
628 //       gSystem->GetProcInfo(&ProcInfo);
629 //       HLTInfo("task %s stopped (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
630     } else {
631     }
632     lnk = lnk->Next();
633   }
634   PrintBenchmarking(fStopwatches, 1 /*clean*/);
635   ClearStatusFlags(kStarted);
636   return iResult;
637 }
638
639 int AliHLTSystem::SendControlEvent(AliHLTComponentDataType dt)
640 {
641   // see header file for class documentation
642
643   // disabled for the moment
644   return 0;
645
646   int iResult=0;
647   AliHLTRunDesc runDesc;
648   memset(&runDesc, 0, sizeof(AliHLTRunDesc));
649   runDesc.fStructSize=sizeof(AliHLTRunDesc);
650   AliHLTDataSource::AliSpecialEventGuard g(&runDesc, dt, kAliHLTVoidDataSpec);
651   HLTDebug("sending event %s, run descriptor %p", AliHLTComponent::DataType2Text(dt).c_str(), &runDesc);
652   TObjLink *lnk=fTaskList.FirstLink();
653   while (lnk && iResult>=0) {
654     TObject* obj=lnk->GetObject();
655     if (obj) {
656       AliHLTTask* pTask=(AliHLTTask*)obj;
657       iResult=pTask->ProcessTask(-1);
658     } else {
659     }
660     lnk = lnk->Next();
661   }
662   HLTDebug("event %s done (%d)", AliHLTComponent::DataType2Text(dt).c_str(), iResult);
663   return iResult;
664 }
665
666 int AliHLTSystem::DeinitTasks()
667 {
668   // see header file for class documentation
669   int iResult=0;
670   TObjLink *lnk=fTaskList.FirstLink();
671   while (lnk && iResult>=0) {
672     TObject* obj=lnk->GetObject();
673     if (obj) {
674       AliHLTTask* pTask=(AliHLTTask*)obj;
675       iResult=pTask->Deinit();
676 //       ProcInfo_t ProcInfo;
677 //       gSystem->GetProcInfo(&ProcInfo);
678 //       HLTInfo("task %s cleaned (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
679     } else {
680     }
681     lnk = lnk->Next();
682   }
683   fEventCount=-1;
684   fGoodEvents=-1;
685
686   return iResult;
687 }
688
689 void* AliHLTSystem::AllocMemory( void* /*param*/, unsigned long size )
690 {
691   // see header file for class documentation
692   void* p=NULL;
693   try {
694     p=(void*)new char[size];
695   }
696   catch (...) {
697     AliHLTLogging log;
698     log.LoggingVarargs(kHLTLogError, "AliHLTSystem" , "AllocMemory" , __FILE__ , __LINE__ , "exeption during memory allocation" );
699   }
700   return p;
701 }
702
703 int AliHLTSystem::Reconstruct(int nofEvents, AliRunLoader* runLoader, 
704                               AliRawReader* rawReader)
705 {
706   // see header file for class documentation
707   int iResult=0;
708   if (runLoader || rawReader || nofEvents==0) {
709     if (nofEvents>0) {HLTInfo("Run Loader %p, Raw Reader %p , %d event(s)", runLoader, rawReader, nofEvents);}
710     if (CheckStatus(kReady)) {
711       if (nofEvents==0) {
712         // special case to close the reconstruction
713         if (!CheckStatus(kError)) {
714         StopTasks();
715         DeinitTasks();
716         }
717       } else {
718       if ((iResult=AliHLTOfflineInterface::SetParamsToComponents(runLoader, rawReader))>=0) {
719         // the system always remains started after event processing, a specific
720         // call with nofEvents==0 is needed to execute the stop sequence
721         if ((iResult=Run(nofEvents, 0))<0) SetStatusFlags(kError);
722       }
723       }
724     } else {
725       HLTError("wrong state %#x, required flags %#x", GetStatusFlags(), kReady);
726     }
727   } else {
728     HLTError("missing RunLoader (%p)/RawReader (%p) instance", runLoader, rawReader);
729     iResult=-EINVAL;
730   }
731   return iResult;
732 }
733
734 int AliHLTSystem::FillESD(int eventNo, AliRunLoader* runLoader, AliESDEvent* esd)
735 {
736   // see header file for class documentation
737   int iResult=0;
738   if (runLoader || esd) {
739     HLTInfo("Event %d: Run Loader %p, ESD %p", eventNo, runLoader, esd);
740     iResult=AliHLTOfflineInterface::FillComponentESDs(eventNo, runLoader, esd);
741   } else {
742     HLTError("missing run loader/ESD instance(s)");
743     iResult=-EINVAL;
744   }
745   return iResult;
746 }
747
748 int AliHLTSystem::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd)
749 {
750   // see header file for class documentation
751   int iResult=0;
752   if (!pHLTOUT) return -EINVAL;
753   HLTDebug("processing %d HLT data blocks", pHLTOUT->GetNofDataBlocks());
754
755   // add the current HLTOUT task to the collection
756   if (fpHLTOUTTask) {
757     AliHLTOUT* pTask=dynamic_cast<AliHLTOUT*>(fpHLTOUTTask);
758     if (pTask && (iResult=pTask->Init())>=0) {
759       if (pTask->GetNofDataBlocks()>0) {
760         pHLTOUT->AddSubCollection(pTask);
761       }
762     } else {
763       HLTWarning("can not initialize HLTOUT sub collection %s for reconstruction chain (%d), data blocks are lost", pTask?fpHLTOUTTask->GetName():"nil", iResult);
764       iResult=0;
765     }
766   }
767
768   
769   //
770   // process all kChain handlers first
771   //
772   if ((iResult=ProcessHLTOUTkChain(pHLTOUT))<0) {
773     HLTWarning("Processing of kChain-type data blocks failed with error code %d", iResult);
774     iResult=0;
775   } 
776
777   if (!fpEsdHandlers)
778     fpEsdHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
779   if (!fpProprietaryHandlers)
780     fpProprietaryHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
781
782   AliHLTOUT::AliHLTOUTHandlerListEntryVector* pEsdHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpEsdHandlers);
783   AliHLTOUT::AliHLTOUTHandlerListEntryVector* pProprietaryHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpProprietaryHandlers);
784   if (!pEsdHandlers || !pProprietaryHandlers) return -ENOMEM;
785
786   // invalidate all blocks
787   AliHLTOUT::InvalidateBlocks(*pEsdHandlers);
788   AliHLTOUT::InvalidateBlocks(*pProprietaryHandlers);
789
790   // first come first serve: the ESD of the first handler is also filled into
791   // the main ESD. Has to be changed later.
792   // currently, merging to the provided ESDs crashes at the level of the
793   // TTree::Fill in AliReconstruction, furthermore, the wrong ESD is passed
794   // by the framework
795   AliESDEvent* pMasterESD=NULL;
796   pMasterESD=esd;
797
798   AliHLTComponentDataTypeList esdBlocks;
799
800   for (iResult=pHLTOUT->SelectFirstDataBlock();
801        iResult>=0;
802        iResult=pHLTOUT->SelectNextDataBlock()) {
803     AliHLTComponentDataType dt=kAliHLTVoidDataType;
804     AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
805     pHLTOUT->GetDataBlockDescription(dt, spec);
806     AliHLTOUTHandler* pHandler=pHLTOUT->GetHandler();
807     AliHLTModuleAgent::AliHLTOUTHandlerType handlerType=pHLTOUT->GetDataBlockHandlerType();
808
809     // default handling for ESD data blocks does not require an explicite handler
810     if (!pHandler && (dt==kAliHLTDataTypeESDObject || dt==kAliHLTDataTypeESDTree)) {
811       handlerType=AliHLTModuleAgent::kEsd;
812     }
813     const char* pMsg="invalid";
814     switch (handlerType) {
815     case AliHLTModuleAgent::kEsd:
816       {
817         if (pHandler) {
818           // schedule for later processing
819           pHLTOUT->InsertHandler(*pEsdHandlers, pHLTOUT->GetDataBlockHandlerDesc());
820         } else {
821           AliHLTComponentDataTypeList::iterator element=esdBlocks.begin();
822           for (; element!=esdBlocks.end(); element++) {
823             if (*element==dt) {
824               HLTWarning("multiple ESDs of identical data type %s, please add appropriate handler to merge ESDs", AliHLTComponent::DataType2Text(dt).c_str());
825               break;
826             }
827           }
828           if (element==esdBlocks.end()) esdBlocks.push_back(dt);
829
830           // write directly
831           const AliHLTUInt8_t* pBuffer=NULL;
832           AliHLTUInt32_t size=0;
833           if (pHLTOUT->GetDataBuffer(pBuffer, size)>=0) {
834             pHLTOUT->WriteESD(pBuffer, size, dt);
835             if (pMasterESD) {
836               pHLTOUT->WriteESD(pBuffer, size, dt, pMasterESD);
837               pMasterESD=NULL;
838             }
839             pHLTOUT->ReleaseDataBuffer(pBuffer);
840           }
841           pHLTOUT->MarkDataBlockProcessed();
842         }
843       }
844       break;
845     case AliHLTModuleAgent::kRawReader:
846       // handled in the AliRawReaderHLT
847       break;
848     case AliHLTModuleAgent::kRawStream:
849       HLTWarning("HLTOUT handler type 'kRawStream' not yet implemented: agent %s, data type %s, specification %#x",
850                  pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
851                  AliHLTComponent::DataType2Text(dt).c_str(), spec);
852       break;
853     case AliHLTModuleAgent::kChain:
854       HLTWarning("HLTOUT handler type 'kChain' has already been processed: agent %s, data type %s, specification %#x\n"
855                  "New block of this type added by the chain? Skipping data block ...",
856                  pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
857                  AliHLTComponent::DataType2Text(dt).c_str(), spec);
858       break;
859     case AliHLTModuleAgent::kProprietary:
860       HLTDebug("processing proprietary data: agent %s, data type %s, specification %#x",
861                  pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
862                  AliHLTComponent::DataType2Text(dt).c_str(), spec);
863       if (pHandler) {
864         AliHLTOUT::AliHLTOUTLockGuard g(pHLTOUT);
865         int res=pHandler->ProcessData(pHLTOUT);
866         if (res<0) {
867           HLTWarning("processing proprietary data failed (%d): agent %s, data type %s, specification %#x",
868                      res, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
869                      AliHLTComponent::DataType2Text(dt).c_str(), spec);
870         }
871       }
872       break;
873     case AliHLTModuleAgent::kUnknownOutput:
874       pMsg="unknown";
875       // fall trough intended
876     default:
877       HLTWarning("%s handler type: agent %s, data type %s, specification %#x, ... skipping data block",
878                  pMsg, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
879                  AliHLTComponent::DataType2Text(dt).c_str(), spec);
880     }
881   }
882   // TODO: the return value of SelectFirst/NextDataBlock must be
883   // changed in order to avoid this check
884   if (iResult==-ENOENT) iResult=0;
885
886   AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator handler;
887
888   // process and write all esd data blocks
889   for (handler=pEsdHandlers->begin(); handler!=pEsdHandlers->end() && iResult>=0; handler++) {
890     AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));     
891     AliHLTOUTHandler* pHandler=*handler;
892     const AliHLTUInt8_t* pBuffer=NULL;
893     AliHLTUInt32_t size=0;
894     pHandler->ProcessData(pHLTOUT);
895     if ((size=pHandler->GetProcessedData(pBuffer))>0) {
896       AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*handler;
897       AliHLTComponentDataType dt=desc;
898       pHLTOUT->WriteESD(pBuffer, size, dt);
899       if (pMasterESD) {
900         pHLTOUT->WriteESD(pBuffer, size, dt, pMasterESD);
901         pMasterESD=NULL;
902       }
903       pHandler->ReleaseProcessedData(pBuffer, size);
904     }
905     pHLTOUT->MarkDataBlocksProcessed(&(*handler));
906   }
907
908   // process all kProprietary data blocks
909   for (handler=pProprietaryHandlers->begin(); handler!=pProprietaryHandlers->end() && iResult>=0; handler++) {
910     AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));     
911     AliHLTOUTHandler* pHandler=*handler;
912     const AliHLTUInt8_t* pBuffer=NULL;
913     AliHLTUInt32_t size=0;
914     pHandler->ProcessData(pHLTOUT);
915     if ((size=pHandler->GetProcessedData(pBuffer))>0) {
916       HLTWarning("data produced by kProprietary handler ignored");
917       pHandler->ReleaseProcessedData(pBuffer, size);
918     }
919     pHLTOUT->MarkDataBlocksProcessed(&(*handler));
920   }
921
922   // remove all empty handlers form the list (handlers which did not get a block this time)
923   AliHLTOUT::RemoveEmptyDuplicateHandlers(*pEsdHandlers);
924   AliHLTOUT::RemoveEmptyDuplicateHandlers(*pProprietaryHandlers);
925
926   return iResult;
927 }
928
929 int AliHLTSystem::ProcessHLTOUTkChain(AliHLTOUT* pHLTOUT)
930 {
931   // see header file for class documentation
932   int iResult=0;
933   if (!pHLTOUT) return -EINVAL;
934
935   if (!fpChainHandlers)
936     fpChainHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
937
938   AliHLTOUT::AliHLTOUTHandlerListEntryVector* pChainHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpChainHandlers);
939   if (!pChainHandlers) return -ENOMEM;
940
941   // invalidate all blocks
942   AliHLTOUT::InvalidateBlocks(*pChainHandlers);
943
944   // fill the list
945   pHLTOUT->FillHandlerList(*pChainHandlers, AliHLTModuleAgent::kChain);
946
947   // process all defined chain handlers
948   AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator chainHandler;
949   for (chainHandler=pChainHandlers->begin(); chainHandler!=pChainHandlers->end() && iResult>=0; chainHandler++) {
950     if (chainHandler->IsEmpty()) continue;
951     AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*chainHandler));        
952     AliHLTOUTHandler* pHandler=*chainHandler;
953     const AliHLTUInt8_t* pBuffer=NULL;
954     AliHLTUInt32_t size=0;
955     pHandler->ProcessData(pHLTOUT);
956     if ((size=pHandler->GetProcessedData(pBuffer))>0) {
957       AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*chainHandler;
958       AliHLTComponentDataType dt=desc;
959
960       pHandler->ReleaseProcessedData(pBuffer, size);
961     }
962     pHLTOUT->MarkDataBlocksProcessed(&(*chainHandler));
963   }
964
965   // remove all empty handlers form the list (handlers which did not get a block this time)
966   AliHLTOUT::RemoveEmptyDuplicateHandlers(*pChainHandlers);
967
968   return iResult;
969 }
970
971 int AliHLTSystem::LoadComponentLibraries(const char* libraries)
972 {
973   // see header file for class documentation
974   int iResult=0;
975   if (libraries) {
976     if (fpComponentHandler) {
977       TString libs(libraries);
978       TObjArray* pTokens=libs.Tokenize(" ");
979       if (pTokens) {
980         int iEntries=pTokens->GetEntries();
981         for (int i=0; i<iEntries && iResult>=0; i++) {
982           iResult=fpComponentHandler->LoadLibrary((((TObjString*)pTokens->At(i))->GetString()).Data());
983         }
984         delete pTokens;
985       }
986       if (iResult>=0) {
987         SetStatusFlags(kLibrariesLoaded);
988       } else {
989         // lets see if we need this, probably not
990         //fpComponentHandler->UnloadLibraries();
991         ClearStatusFlags(kLibrariesLoaded);
992       }
993     } else {
994       iResult=-EFAULT;
995       HLTFatal("no component handler available");
996     }
997   } else {
998     iResult=-EINVAL;
999   }
1000   return iResult;
1001 }
1002
1003 int AliHLTSystem::Configure(AliRunLoader* runloader)
1004 {
1005   // see header file for class documentation
1006   return Configure(NULL, runloader);
1007 }
1008
1009 int AliHLTSystem::Configure(AliRawReader* rawReader, AliRunLoader* runloader)
1010 {
1011   // see header file for class documentation
1012   int iResult=0;
1013   if (CheckStatus(kRunning)) {
1014     HLTError("HLT system in running state, can not configure");
1015     return -EBUSY;
1016   }
1017   ClearStatusFlags(kTaskListCreated);
1018   if (CheckFilter(kHLTLogDebug))
1019     AliHLTModuleAgent::PrintStatus();
1020   if (CheckStatus(kConfigurationLoaded)==0) {
1021     iResult=LoadConfigurations(rawReader, runloader);
1022   } else {
1023     if (fChains.Length()==0) {
1024       HLTError("custom configuration(s) specified, but no configuration to run in local reconstruction, use \'chains=<chain,...>\' option");
1025       iResult=-ENOENT;
1026     }
1027   }
1028   if (iResult>=0) {
1029     SetStatusFlags(kConfigurationLoaded);
1030     if (CheckFilter(kHLTLogDebug))
1031       fpConfigurationHandler->PrintConfigurations();
1032     iResult=BuildTaskListsFromReconstructionChains(rawReader, runloader);
1033     if (iResult>=0) {
1034       SetStatusFlags(kTaskListCreated);
1035     }
1036   }
1037   if (iResult<0) SetStatusFlags(kError);
1038   
1039   return iResult;
1040 }
1041
1042 int AliHLTSystem::ScanOptions(const char* options)
1043 {
1044   // see header file for class documentation
1045   int iResult=0;
1046   if (options) {
1047     //AliHLTComponentHandler::TLibraryMode libMode=AliHLTComponentHandler::kDynamic;
1048     TString libs("");
1049     TString alloptions(options);
1050     TObjArray* pTokens=alloptions.Tokenize(" ");
1051     if (pTokens) {
1052       int iEntries=pTokens->GetEntries();
1053       for (int i=0; i<iEntries; i++) {
1054         TString token=(((TObjString*)pTokens->At(i))->GetString());
1055         if (token.Contains("loglevel=")) {
1056           TString param=token.ReplaceAll("loglevel=", "");
1057           if (param.IsDigit()) {
1058             SetGlobalLoggingLevel((AliHLTComponentLogSeverity)param.Atoi());
1059           } else if (param.BeginsWith("0x") &&
1060                      param.Replace(0,2,"",0).IsHex()) {
1061             int severity=0;
1062             sscanf(param.Data(),"%x", &severity);
1063             SetGlobalLoggingLevel((AliHLTComponentLogSeverity)severity);
1064           } else {
1065             HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
1066           }
1067         } else if (token.Contains("frameworklog=")) {
1068           TString param=token.ReplaceAll("frameworklog=", "");
1069           if (param.IsDigit()) {
1070             SetFrameworkLog((AliHLTComponentLogSeverity)param.Atoi());
1071           } else if (param.BeginsWith("0x") &&
1072                      param.Replace(0,2,"",0).IsHex()) {
1073             int severity=0;
1074             sscanf(param.Data(),"%x", &severity);
1075             SetFrameworkLog((AliHLTComponentLogSeverity)severity);
1076           } else {
1077             HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
1078           }
1079         } else if (token.Contains("alilog=off")) {
1080           SwitchAliLog(0);
1081         } else if (token.Contains("config=")) {
1082           TString param=token.ReplaceAll("config=", "");
1083           Int_t error=0;
1084           gROOT->Macro(param.Data(), &error);
1085           if (error==0) {
1086             SetStatusFlags(kConfigurationLoaded);
1087           } else {
1088             HLTError("can not execute macro \'%s\'", param.Data());
1089             iResult=-EBADF;
1090           }
1091         } else if (token.Contains("chains=")) {
1092           TString param=token.ReplaceAll("chains=", "");
1093           fChains=param.ReplaceAll(",", " ");
1094           if (fChains.IsNull()) fChains=" "; // disable all chains
1095         } else if (token.Contains("libmode=")) {
1096           TString param=token.ReplaceAll("libmode=", "");
1097           param.ReplaceAll(",", " ");
1098           if (fpComponentHandler) {
1099             if (param.CompareTo("static")==0) {
1100               fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kStatic);
1101             } else if (param.CompareTo("dynamic")==0) {
1102               fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kDynamic);
1103             } else {
1104               HLTWarning("wrong argument for option \'libmode=\', use \'static\' or \'dynamic\'");
1105             }
1106           }
1107         } else if (token.BeginsWith("lib") && token.EndsWith(".so")) {
1108           libs+=token;
1109           libs+=" ";
1110         } else {
1111           HLTWarning("unknown option \'%s\'", token.Data());
1112         }
1113       }
1114       delete pTokens;
1115     }
1116
1117     if (iResult>=0) {
1118       if (libs.IsNull()) {
1119         const char** deflib=fgkHLTDefaultLibs;
1120         while (*deflib) {
1121           libs+=*deflib++;
1122           libs+=" ";
1123         }
1124       }
1125       if ((!CheckStatus(AliHLTSystem::kLibrariesLoaded)) &&
1126           (LoadComponentLibraries(libs.Data())<0)) {
1127         HLTError("error while loading HLT libraries");
1128         iResult=-EFAULT;
1129       }
1130     }
1131   }
1132   return iResult;
1133 }
1134
1135 int AliHLTSystem::Reset(int bForce)
1136 {
1137   // see header file for class documentation
1138   int iResult=0;
1139   if (!bForce && CheckStatus(kRunning)) {
1140     HLTError("HLT system in running state, can not configure");
1141     return -EBUSY;
1142   }
1143   CleanTaskList();
1144   ClearStatusFlags(~kUninitialized);
1145   return iResult;
1146 }
1147
1148 int AliHLTSystem::LoadConfigurations(AliRawReader* rawReader, AliRunLoader* runloader)
1149 {
1150   // see header file for class documentation
1151   if (CheckStatus(kRunning)) {
1152     HLTError("HLT system in running state, can not configure");
1153     return -EBUSY;
1154   }
1155   int iResult=0;
1156   AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
1157   TString extralibs;
1158   while (pAgent && iResult>=0) {
1159     const char* deplibs=pAgent->GetRequiredComponentLibraries();
1160     if (deplibs) {
1161       HLTDebug("required libraries \'%s\' for agent %s (%p)", deplibs, pAgent->GetName(), pAgent);
1162       extralibs+=deplibs;
1163     }
1164     if (iResult>=0) {
1165       HLTDebug("load configurations for agent %s (%p)", pAgent->GetName(), pAgent);
1166       pAgent->CreateConfigurations(fpConfigurationHandler, rawReader, runloader);
1167       pAgent=AliHLTModuleAgent::GetNextAgent();
1168     }
1169   }
1170   if (iResult>=0) {
1171     iResult=LoadComponentLibraries(extralibs.Data());
1172   }
1173
1174   return iResult;
1175 }
1176
1177 int AliHLTSystem::BuildTaskListsFromReconstructionChains(AliRawReader* rawReader, AliRunLoader* runloader)
1178 {
1179   // see header file for class documentation
1180   if (CheckStatus(kRunning)) {
1181     HLTError("HLT system in running state, can not configure");
1182     return -EBUSY;
1183   }
1184   if (!CheckStatus(kConfigurationLoaded)) {
1185     HLTWarning("configurations not yet loaded");
1186     return 0;
1187   }
1188
1189   int iResult=0;
1190   int bHaveOutput=0;
1191
1192   // query chains
1193   TString chains;
1194   if (fChains.Length()>0) {
1195     chains=fChains;
1196     HLTImportant("custom reconstruction chain: %s", chains.Data());
1197   } else {
1198     AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
1199     while ((pAgent || fChains.Length()>0) && iResult>=0) {
1200       const char* agentchains=pAgent->GetReconstructionChains(rawReader, runloader);
1201       if (agentchains) {
1202         if (!chains.IsNull()) chains+=" ";
1203         chains+=agentchains;
1204         HLTInfo("reconstruction chains for agent %s (%p): %s", pAgent->GetName(), pAgent, agentchains);
1205       }
1206       pAgent=AliHLTModuleAgent::GetNextAgent();
1207     }
1208   }
1209
1210   // build task list for chains
1211   TObjArray* pTokens=chains.Tokenize(" ");
1212   if (pTokens) {
1213     int iEntries=pTokens->GetEntries();
1214     for (int i=0; i<iEntries && iResult>=0; i++) {
1215       const char* pCID=((TObjString*)pTokens->At(i))->GetString().Data();
1216       AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(pCID);
1217       if (pConf) {
1218         iResult=BuildTaskList(pConf);
1219         if (true) { // condition was deprecated but kept for sake of svn diff
1220           // bHaveOutput variable has to be set for both running modes
1221           // AliHLTSimulation and AliHLTReconstruction
1222           assert(fpComponentHandler!=NULL);
1223           TString cid=pConf->GetComponentID();
1224           if (runloader!=NULL && cid.CompareTo("HLTOUT")==0) {
1225             // remove from the input of a global HLTOUT configuration
1226             chains.ReplaceAll(pCID, "");
1227           } else if (bHaveOutput==0) {
1228             // check whether this configuration produces data output
1229             if ((bHaveOutput=fpComponentHandler->HasOutputData(cid.Data()))<0) {
1230               bHaveOutput=0;
1231               chains.ReplaceAll(pCID, "");
1232             }
1233           }
1234         }
1235       } else {
1236         HLTWarning("can not find configuration %s", pCID);
1237       }
1238     }
1239     delete pTokens;
1240   }
1241
1242   // build HLTOUT for simulation
1243   if (iResult>=0 && runloader) {
1244     if (bHaveOutput) {
1245       // there are components in the chain which produce data which need to be
1246       // piped to an HLTOUT
1247       if (fpComponentHandler->FindComponentIndex("HLTOUT")>=0 ||
1248           fpComponentHandler->LoadLibrary("libHLTsim.so")>=0) {
1249         AliHLTConfiguration globalout("_globalout_", "HLTOUT", chains.Data(), NULL);
1250         iResult=BuildTaskList("_globalout_");
1251       } else {
1252         HLTError("can not load libHLTsim.so and HLTOUT component");
1253         iResult=-EFAULT;
1254       }
1255     }
1256   }
1257
1258   // build HLTOUT task for reconstruction
1259   // Matthias 08.07.2008 the rawReader is never set when running embedded into
1260   // AliReconstruction. The system is configured during AliHLTReconstructor::Init
1261   // where the RawReader is not available. It is available in the first invocation
1262   // of Reconstruct.
1263   // 
1264   // That means that policy is slightly changed:
1265   // - if the run loader is available -> AliSimulation
1266   // - no run loader available -> AliReconstruction
1267   if (iResult>=0 && !runloader) {
1268     if (bHaveOutput) {
1269       // there are components in the chain which produce data which need to be
1270       // piped to an HLTOUT sub-collection
1271       if (!fpHLTOUTTask) {
1272         fpHLTOUTTask=new AliHLTOUTTask(chains.Data());
1273         if (fpHLTOUTTask) {
1274           if (fpHLTOUTTask->GetConf() && fpHLTOUTTask->GetConf()->SourcesResolved()>=0) {
1275             iResult=InsertTask(fpHLTOUTTask);
1276           } else {
1277             HLTError("HLTOUT task (%s) sources not resolved", fpHLTOUTTask->GetName());
1278             iResult=-ENOENT;
1279           }
1280         }
1281       }
1282     }
1283   }
1284
1285   if (iResult>=0) SetStatusFlags(kTaskListCreated);
1286
1287   return iResult;
1288 }
1289
1290 int AliHLTSystem::CheckStatus(int flag)
1291 {
1292   // see header file for class documentation
1293   if (flag==kUninitialized && flag==fState) return 1;
1294   if ((fState&flag)==flag) return 1;
1295   return 0;
1296 }
1297
1298 int AliHLTSystem::GetStatusFlags()
1299 {
1300   // see header file for class documentation
1301   return fState;
1302 }
1303
1304 int AliHLTSystem::SetStatusFlags(int flags)
1305 {
1306   // see header file for class documentation
1307   fState|=flags;
1308   return fState;
1309 }
1310
1311 int AliHLTSystem::ClearStatusFlags(int flags)
1312 {
1313   // see header file for class documentation
1314   fState&=~flags;
1315   return fState;
1316 }
1317
1318 void* AliHLTSystem::FindDynamicSymbol(const char* library, const char* symbol)
1319 {
1320   // see header file for class documentation
1321   if (fpComponentHandler==NULL) return NULL;
1322   return fpComponentHandler->FindSymbol(library, symbol);
1323 }
1324
1325 void AliHLTSystem::SetFrameworkLog(AliHLTComponentLogSeverity level) 
1326 {
1327   // see header file for class documentation
1328   SetLocalLoggingLevel(level);
1329   if (fpComponentHandler) fpComponentHandler->SetLocalLoggingLevel(level);
1330   if (fpConfigurationHandler) fpConfigurationHandler->SetLocalLoggingLevel(level);
1331 }