]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTSystem.cxx
minor bugfix: release HLTOUT instance after using it to avoid warning message
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTSystem.cxx
1 // $Id$
2 //**************************************************************************
3 //* This file is property of and copyright by the ALICE HLT Project        * 
4 //* ALICE Experiment at CERN, All rights reserved.                         *
5 //*                                                                        *
6 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
7 //*                  for The ALICE HLT Project.                            *
8 //*                                                                        *
9 //* Permission to use, copy, modify and distribute this software and its   *
10 //* documentation strictly for non-commercial purposes is hereby granted   *
11 //* without fee, provided that the above copyright notice appears in all   *
12 //* copies and that both the copyright notice and this permission notice   *
13 //* appear in the supporting documentation. The authors make no claims     *
14 //* about the suitability of this software for any purpose. It is          *
15 //* provided "as is" without express or implied warranty.                  *
16 //**************************************************************************
17
18 /** @file   AliHLTSystem.cxx
19     @author Matthias Richter
20     @date   
21     @brief  Implementation of HLT module management.
22 */
23
24 #if __GNUC__>= 3
25 using namespace std;
26 #endif
27
28 #include <cassert>
29 #include "AliHLTStdIncludes.h"
30 #include "AliHLTSystem.h"
31 #include "AliHLTComponentHandler.h"
32 #include "AliHLTComponent.h"
33 #include "AliHLTConfiguration.h"
34 #include "AliHLTConfigurationHandler.h"
35 #include "AliHLTOnlineConfiguration.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 "AliHLTControlTask.h"
44 #include "AliHLTDataBuffer.h"
45 #include "AliHLTMisc.h"
46 #include <TObjArray.h>
47 #include <TObjString.h>
48 #include <TStopwatch.h>
49 #include <TList.h>
50 //#include <TSystem.h>
51 #include <TROOT.h>
52 //#include <TInterpreter.h>
53
54 /** HLT default component libraries */
55 const char* AliHLTSystem::fgkHLTDefaultLibs[]= {
56   "libAliHLTUtil.so", 
57   "libAliHLTRCU.so", 
58   "libAliHLTTPC.so", 
59   //  "libAliHLTSample.so",
60   "libAliHLTCalo.so",
61   "libAliHLTEMCAL.so",
62   "libAliHLTPHOS.so",
63   "libAliHLTMUON.so",
64   "libAliHLTTRD.so",
65   "libAliHLTITS.so",
66   "libAliHLTVZERO.so",
67   "libAliHLTZDC.so",
68   "libAliHLTGlobal.so",
69   "libAliHLTTrigger.so",
70   NULL
71 };
72
73 /** ROOT macro for the implementation of ROOT specific class methods */
74 ClassImp(AliHLTSystem)
75
76 AliHLTSystem::AliHLTSystem(AliHLTComponentLogSeverity loglevel, const char* name,
77                            AliHLTComponentHandler* pCompHandler,
78                            AliHLTConfigurationHandler* pConfHandler
79                            )
80   : fpComponentHandler(pCompHandler==NULL?AliHLTComponentHandler::CreateHandler():pCompHandler)
81   , fpConfigurationHandler(pConfHandler==NULL?AliHLTConfigurationHandler::CreateHandler():pConfHandler),
82   fTaskList(),
83   fState(0),
84   fChains(),
85   fStopwatches(new TObjArray),
86   fEventCount(-1),
87   fGoodEvents(-1),
88   fpChainHandlers(NULL),
89   fpEsdHandlers(NULL),
90   fpProprietaryHandlers(NULL),
91   fpHLTOUTTask(NULL),
92   fpHLTOUT(NULL),
93   fHLTOUTUse(0),
94   fpControlTask(NULL),
95   fName(name)
96   , fECSParams()
97   , fUseHLTOUTComponentTypeGlobal(true)
98 {
99   // see header file for class documentation
100   // or
101   // refer to README to build package
102   // or
103   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
104
105   if (fgNofInstances++>0) {
106     // July 2008: multiple instances are now allowed
107     // AliHLTSystem is used in multiple instances for the kChain HLTOUT handler
108     //HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
109   }
110
111   SetGlobalLoggingLevel(loglevel);
112   SetFrameworkLog(loglevel);
113   if (fpComponentHandler) {
114     AliHLTAnalysisEnvironment env;
115     memset(&env, 0, sizeof(AliHLTAnalysisEnvironment));
116     env.fStructSize=sizeof(AliHLTAnalysisEnvironment);
117     env.fAllocMemoryFunc=AliHLTSystem::AllocMemory;
118     env.fGetEventDoneDataFunc=AliHLTSystem::AllocEventDoneData;
119     env.fLoggingFunc=NULL;
120     fpComponentHandler->SetEnvironment(&env);
121     InitAliLogFunc(fpComponentHandler);
122     if (fgNofInstances==1) {
123     fpComponentHandler->AnnounceVersion();
124     }
125   } else {
126     HLTFatal("can not create Component Handler");
127   }
128   if (fpConfigurationHandler==NULL) {
129     HLTFatal("can not create Configuration Handler");
130   }
131 }
132
133 AliHLTSystem::~AliHLTSystem()
134 {
135   // see header file for class documentation
136   fgNofInstances--;
137   CleanupHLTOUTHandlers();
138   CleanTaskList();
139   if (fpConfigurationHandler) {
140     fpConfigurationHandler->Destroy();
141   }
142   fpConfigurationHandler=NULL;
143   
144   if (fpComponentHandler) {
145     fpComponentHandler->Destroy();
146   }
147   fpComponentHandler=NULL;
148   delete fStopwatches;
149
150   // note: fpHLTOUTTask and fpControlTask are deleted by
151   // CleanTaskList
152 }
153
154 int AliHLTSystem::fgNofInstances=0;
155
156 int AliHLTSystem::BuildTaskList(const char* id)
157 {
158   // see header file for class documentation
159   int iResult=0;
160   if (id) {
161     if (fpConfigurationHandler) {
162       AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(id);
163       if (pConf) {
164         iResult=BuildTaskList(pConf);
165       } else {
166         HLTError("unknown configuration \"%s\"", id);
167         iResult=-EEXIST;
168       }
169     } else {
170       iResult=-EFAULT;
171     }
172   } else {
173     iResult=-EINVAL;
174   }
175   return iResult;
176 }
177
178 int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
179 {
180   // see header file for class documentation
181   int iResult=0;
182   if (pConf) {
183     AliHLTTask* pTask=NULL;
184     if ((pTask=FindTask(pConf->GetName()))!=NULL) {
185       if (pTask->GetConf()!=pConf) {
186         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);
187         iResult=-EEXIST;
188       }
189       // task for this configuration exists, terminate
190       pTask=NULL;
191     // check first if the configuration has all sources resolved, try to extract otherwise
192     } else if (pConf->SourcesResolved()!=1 && pConf->ExtractSources()!=1) {
193         HLTError("configuration \"%s\" has unresolved sources, aborting ...", pConf->GetName());
194         iResult=-ENOLINK;
195     } else {
196       pTask=new AliHLTTask(pConf);
197       if (pTask==NULL) {
198         iResult=-ENOMEM;
199       } else {
200         pTask->SetLocalLoggingLevel(GetLocalLoggingLevel());
201       }
202     }
203     static int iterationLevel=0;
204     if (pTask && iResult>=0) {
205       // check for circular dependencies
206       if ((iResult=pConf->FollowDependency(pConf->GetName()))>0) {
207         HLTError("detected circular dependency for configuration \"%s\"", pTask->GetName());
208         pTask->PrintDependencyTree(pTask->GetName(), 1/*use the configuration list*/);
209         HLTError("aborted ...");
210         iResult=-ELOOP;
211       }
212       if (iResult>=0) {
213         // check whether all dependencies are already in the task list
214         // create the missing ones
215         // this step is an iterative process which calls this function again for the missing
216         // configurations, in order to avoid the currently processed task to be created
217         // again it is added to the list temporarily and removed afterwards
218         // This is of high importance to preserve the order of the tasks. Furthermore, the
219         // InsertTask method has to be used in order to set all the cross links right 
220         fTaskList.Add(pTask);
221         AliHLTConfiguration* pDep=pConf->GetFirstSource();
222         while (pDep!=NULL && iResult>=0) {
223           HLTDebug("iteration %d: checking dependency %s (%p)", iterationLevel, pDep->GetName(), pDep);
224           if (FindTask(pDep->GetName())==NULL) {
225             HLTDebug("iteration %d: building task list for configuration %s (%p)", iterationLevel, pDep->GetName(), pDep);
226             iterationLevel++;
227             iResult=BuildTaskList(pDep);
228             iterationLevel--;
229           }
230           pDep=pConf->GetNextSource();
231         }
232         // remove the temporarily added task
233         fTaskList.Remove(pTask);
234
235         // insert the task and set the cross-links
236         if (iResult>=0) {
237           HLTDebug("iteration %d: inserting task %s (%p)", iterationLevel, pTask->GetName(), pTask);
238           iResult=InsertTask(pTask);
239         }
240       } else {
241         delete pTask;
242         pTask=NULL;
243       }
244     }
245   } else {
246     iResult=-EINVAL;
247   }
248   return iResult;
249 }
250
251 int AliHLTSystem::CleanTaskList()
252 {
253   // see header file for class documentation
254   int iResult=0;
255   fpHLTOUTTask=NULL;
256   fpControlTask=NULL;
257   TObjLink* lnk=NULL;
258   while ((lnk=fTaskList.LastLink())!=NULL) {
259     delete (lnk->GetObject());
260     fTaskList.Remove(lnk);
261   }
262
263   return iResult;
264 }
265
266 int AliHLTSystem::InsertTask(AliHLTTask* pTask)
267 {
268   // see header file for class documentation
269   int iResult=0;
270   if (fpControlTask==NULL) {
271     fpControlTask=new AliHLTControlTask;
272     if (!fpControlTask) return -ENOMEM;
273     fTaskList.AddFirst(fpControlTask);
274   }
275   TObjLink *controlLnk=NULL;
276   TObjLink *lnk = fTaskList.FirstLink();
277   assert(!lnk || lnk->GetObject()==fpControlTask || fpControlTask==NULL);
278   if (lnk && lnk->GetObject()==fpControlTask) {
279     if (pTask->GetConf() && pTask->GetConf()->GetFirstSource()==NULL) {
280       pTask->SetDependency(fpControlTask);
281       fpControlTask->SetTarget(pTask);
282     }
283     controlLnk=lnk;
284     lnk=lnk->Next();
285   }
286   if ((iResult=pTask->CheckDependencies())<=0)
287     lnk=NULL;
288   while (lnk && iResult>0) {
289     AliHLTTask* pCurr = (AliHLTTask*)lnk->GetObject();
290     //HLTDebug("checking  \"%s\"", pCurr->GetName());
291     iResult=pTask->Depends(pCurr);
292     if (iResult>0) {
293       iResult=pTask->SetDependency(pCurr);
294       pCurr->SetTarget(pTask);
295       HLTDebug("set dependency  \"%s\" for configuration \"%s\"", pCurr->GetName(), pTask->GetName());
296     }
297     if (pCurr->Depends(pTask)) {
298       // circular dependency
299       HLTError("circular dependency: can not resolve dependencies for configuration \"%s\"", pTask->GetName());
300       iResult=-ELOOP;
301     } else if ((iResult=pTask->CheckDependencies())>0) {
302       lnk = lnk->Next();
303     }
304   }
305   if (iResult==0) {
306       if (lnk) {
307         fTaskList.AddAfter(lnk, pTask);
308       } else if (controlLnk) {
309         fTaskList.AddAfter(controlLnk, pTask);
310       } else {
311         fTaskList.AddFirst(pTask);
312       }
313       HLTDebug("task \"%s\" (%p) inserted (size %d)", pTask->GetName(), pTask, sizeof(AliHLTTask));
314   } else if (iResult>0) {
315     HLTError("can not resolve dependencies for configuration \"%s\" (%d unresolved)", pTask->GetName(), iResult);
316     iResult=-ENOLINK;
317   }
318   return iResult;
319 }
320
321 AliHLTTask* AliHLTSystem::FindTask(const char* id)
322 {
323   // see header file for class documentation
324   AliHLTTask* pTask=NULL;
325   if (id) {
326     pTask=dynamic_cast<AliHLTTask*>(fTaskList.FindObject(id)); 
327   }
328   return pTask;
329 }
330
331 void AliHLTSystem::PrintTaskList()
332 {
333   // see header file for class documentation
334   HLTLogKeyword("task list");
335   TObjLink *lnk = NULL;
336   HLTMessage("Task List");
337   lnk=fTaskList.FirstLink();
338   while (lnk) {
339     TObject* obj=lnk->GetObject();
340     if (obj) {
341       HLTMessage("  %s - status:", obj->GetName());
342       AliHLTTask* pTask=(AliHLTTask*)obj;
343       pTask->PrintStatus();
344     } else {
345     }
346     lnk = lnk->Next();
347   }
348 }
349
350 int AliHLTSystem::Run(Int_t iNofEvents, int bStop, AliHLTUInt64_t trgMask,
351                       AliHLTUInt32_t timestamp, AliHLTUInt32_t eventtype,
352                       AliHLTUInt32_t participatingDetectors)
353 {
354   // see header file for class documentation
355   int iResult=0;
356   int iCount=0;
357   SetStatusFlags(kRunning);
358   if (fEventCount>=0 || (iResult=InitTasks())>=0) {
359     if (fEventCount>=0 || (iResult=StartTasks())>=0) {
360       if (fEventCount==0) {
361         InitBenchmarking(fStopwatches);
362       } else {
363         // Matthias Oct 11 2008 this is a bug
364         // By resuming the stopwatches at this point, all continued counting, but the
365         // starting and stopping is controlled by the AliHLTStopwatchGuard
366         //ResumeBenchmarking(fStopwatches);    
367       }
368       for (int i=fEventCount; i<fEventCount+iNofEvents && iResult>=0; i++) {
369         if (fpHLTOUTTask) {
370           if (iNofEvents>1 && i==fEventCount) {
371             HLTWarning("can not add more than one event to the HLTOUT, skipping all but last block");
372           }
373           // reset and prepare for new data
374           fpHLTOUTTask->Reset();
375         }
376         if (eventtype == 0) {
377           eventtype = gkAliEventTypeData;
378           participatingDetectors = 0x0;
379         }
380         if ((iResult=ProcessTasks(i, trgMask, timestamp, eventtype, participatingDetectors))>=0) {
381           fGoodEvents++;
382           iCount++;
383         } else {
384           // TODO: define different running modes to either ignore errors in
385           // event processing or not
386           // currently ignored 
387           iResult=0;
388         }
389         AliHLTDataBuffer::SetGlobalEventCount(iCount);
390       }
391       fEventCount+=iNofEvents;
392       if (bStop) StopTasks();
393       else PauseBenchmarking(fStopwatches);
394     }
395     if (bStop) DeinitTasks();
396   }
397   if (iResult>=0) {
398     iResult=iCount;
399   } else  if (iResult==-126 /*ENOKEY*/) {
400     iResult=0; // do not propagate the error
401   }
402   ClearStatusFlags(kRunning);
403   AliHLTDataBuffer::PrintStatistics();
404   return iResult;
405 }
406
407 int AliHLTSystem::InitTasks()
408 {
409   // see header file for class documentation
410   int iResult=0;
411   TObjLink *lnk=fTaskList.FirstLink();
412
413   if (lnk==NULL) {
414     HLTInfo("Task list is empty, skipping HLT");
415     return -126 /*ENOKEY*/;
416   }
417   while (lnk && iResult>=0) {
418     TObject* obj=lnk->GetObject();
419     if (obj) {
420       AliHLTTask* pTask=(AliHLTTask*)obj;
421       iResult=pTask->Init(NULL, fpComponentHandler);
422 //       ProcInfo_t ProcInfo;
423 //       gSystem->GetProcInfo(&ProcInfo);
424 //       HLTInfo("task %s initialized (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
425     } else {
426     }
427     lnk = lnk->Next();
428   }
429   if (iResult<0) {
430     HLTError("can not initialize task list, error %d", iResult);
431   }
432
433   return iResult;
434 }
435
436 int AliHLTSystem::InitBenchmarking(TObjArray* pStopwatches)
437 {
438   // see header file for class documentation
439   int iResult=0;
440   if (pStopwatches==NULL) return 0;
441
442   for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
443     TStopwatch* pStopwatch= new TStopwatch;
444     if (pStopwatch) {
445       pStopwatch->Reset();
446       pStopwatches->AddAt(pStopwatch, i);
447     } else {
448       iResult=-ENOMEM;
449       break;
450     }
451   }
452
453   TObjLink *lnk=fTaskList.FirstLink();
454   while (lnk && iResult>=0) {
455     TObject* obj=lnk->GetObject();
456     if (obj) {
457       AliHLTTask* pTask=(AliHLTTask*)obj;
458       AliHLTComponent* pComp=NULL;
459       if (iResult>=0 && (pComp=pTask->GetComponent())!=NULL) {
460         switch (pComp->GetComponentType()) {
461         case AliHLTComponent::kProcessor:
462           pComp->SetStopwatches(pStopwatches);
463           break;
464         case AliHLTComponent::kSource:
465           {
466             // this switch determines whether the time consumption of the
467             // AliHLTComponent base methods should be counted to the input
468             // stopwatch or base stopwatch.
469             //int inputBase=(int)AliHLTComponent::kSWBase;
470             int inputBase=(int)AliHLTComponent::kSWInput;
471             pComp->SetStopwatch(pStopwatches->At(inputBase), AliHLTComponent::kSWBase);
472             pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWInput), AliHLTComponent::kSWDA);
473           }
474           break;
475         case AliHLTComponent::kSink:
476           {
477             // this switch determines whether the time consumption of the
478             // AliHLTComponent base methods should be counted to the output
479             // stopwatch or base stopwatch.
480             //int outputBase=(int)AliHLTComponent::kSWBase;
481             int outputBase=(int)AliHLTComponent::kSWOutput;
482             pComp->SetStopwatch(pStopwatches->At(outputBase), AliHLTComponent::kSWBase);
483             pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWOutput), AliHLTComponent::kSWDA);
484           }
485           break;
486         default:
487           HLTWarning("unknown component type %d", (int)pComp->GetComponentType());
488         }
489       }
490     } else {
491     }
492     lnk = lnk->Next();
493   }
494   return iResult;
495 }
496
497 int AliHLTSystem::PauseBenchmarking(TObjArray* pStopwatches) const
498 {
499   // see header file for class documentation
500   if (pStopwatches==NULL) return 0;
501
502   for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
503     if (!pStopwatches->At(i)) continue;
504     TStopwatch* pSw=dynamic_cast<TStopwatch*>(pStopwatches->At(i));
505     if (pSw) pSw->Stop();
506   }
507   return 0;
508 }
509
510 int AliHLTSystem::ResumeBenchmarking(TObjArray* pStopwatches) const
511 {
512   // see header file for class documentation
513   if (pStopwatches==NULL) return 0;
514
515   for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
516     if (!pStopwatches->At(i)) continue;
517     TStopwatch* pSw=dynamic_cast<TStopwatch*>(pStopwatches->At(i));
518     if (pSw) pSw->Continue();
519   }
520   return 0;
521 }
522
523 int AliHLTSystem::PrintBenchmarking(TObjArray* pStopwatches, int bClean) const
524 {
525   // see header file for class documentation
526   int iInitialized=1;
527   if (pStopwatches==NULL) return 0;
528
529   for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
530     if (!dynamic_cast<TStopwatch*>(pStopwatches->At(i))) {
531       iInitialized=0;
532       break;
533     }
534   }
535
536   if (iInitialized!=0) {
537     HLTImportant("HLT statistics:\n"
538             "    base:              R:%.3fs C:%.3fs\n"
539             "    input:             R:%.3fs C:%.3fs\n"
540             "    output:            R:%.3fs C:%.3fs\n"
541             "    event processing : R:%.3fs C:%.3fs"
542             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->RealTime()
543             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->CpuTime()
544             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->RealTime()
545             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->CpuTime()
546             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->RealTime()
547             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->CpuTime()
548             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->RealTime()
549             , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->CpuTime()
550             );
551   }
552
553   if (bClean) {
554     for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
555       TObject* pObj=pStopwatches->RemoveAt(i);
556       if (pObj) delete pObj;
557     }
558   }
559   return 0;
560 }
561
562 int AliHLTSystem::StartTasks()
563 {
564   // see header file for class documentation
565   int iResult=0;
566   TObjLink *lnk=fTaskList.FirstLink();
567   while (lnk && iResult>=0) {
568     TObject* obj=lnk->GetObject();
569     if (obj) {
570       AliHLTTask* pTask=(AliHLTTask*)obj;
571       iResult=pTask->StartRun();
572 //       ProcInfo_t ProcInfo;
573 //       gSystem->GetProcInfo(&ProcInfo);
574 //       HLTInfo("task %s started (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
575     } else {
576     }
577     lnk = lnk->Next();
578   }
579   if (iResult<0) {
580     HLTError("can not start task list, error %d", iResult);
581   } else {
582     SetStatusFlags(kStarted);
583     fEventCount=0;
584     fGoodEvents=0;
585     if ((iResult=SendControlEvent(kAliHLTDataTypeSOR))<0) {
586       HLTError("can not send SOR event: error %d", iResult);
587     }
588   }
589   return iResult;
590 }
591
592 int AliHLTSystem::ProcessTasks(Int_t eventNo, AliHLTUInt64_t trgMask,
593           AliHLTUInt32_t timestamp, AliHLTUInt32_t eventtype,
594           AliHLTUInt32_t participatingDetectors)
595 {
596   // see header file for class documentation
597   int iResult=0;
598   HLTDebug("processing event no %d", eventNo);
599   TObjLink *lnk=fTaskList.FirstLink();
600   while (lnk) {
601     TObject* obj=lnk->GetObject();
602     if (obj) {
603       AliHLTTask* pTask=(AliHLTTask*)obj;
604       if (iResult>=0) {
605       iResult=pTask->ProcessTask(eventNo, eventtype, trgMask, timestamp, participatingDetectors);
606 //       ProcInfo_t ProcInfo;
607 //       gSystem->GetProcInfo(&ProcInfo);
608 //       HLTInfo("task %s processed (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
609       } else {
610         pTask->SubscribeSourcesAndSkip();
611       }
612     } else {
613     }
614     lnk = lnk->Next();
615   }
616
617   if (iResult>=0) {
618     HLTImportant("Event %d successfully finished (%d)", eventNo, iResult);
619     iResult=0;
620   } else {
621     HLTError("Processing of event %d failed (%d)", eventNo, iResult);
622   }
623
624   return iResult;
625 }
626
627 int AliHLTSystem::StopTasks()
628 {
629   // see header file for class documentation
630   int iResult=0;
631   if ((iResult=SendControlEvent(kAliHLTDataTypeEOR))<0) {
632     HLTError("can not send EOR event");
633   }
634
635   // cleanup blocks from the last event. This is a bit awkward. All output
636   // blocks from the chains need to be stored in the HLTOUT task. Though,
637   // we do not know, whether HLTOUT is going to be processed or not.
638   if (fpHLTOUTTask)
639     fpHLTOUTTask->Reset();
640
641   TObjLink *lnk=fTaskList.FirstLink();
642   while (lnk) {
643     TObject* obj=lnk->GetObject();
644     if (obj) {
645       AliHLTTask* pTask=(AliHLTTask*)obj;
646       int locResult=pTask->EndRun();
647       if (iResult>=0 && locResult<0) iResult=locResult;
648 //       ProcInfo_t ProcInfo;
649 //       gSystem->GetProcInfo(&ProcInfo);
650 //       HLTInfo("task %s stopped (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
651     } else {
652     }
653     lnk = lnk->Next();
654   }
655   PrintBenchmarking(fStopwatches, 1 /*clean*/);
656   if (fEventCount!=fGoodEvents) {
657     HLTError("%d out of %d event(s) failed", fEventCount-fGoodEvents, fEventCount);
658   }
659   ClearStatusFlags(kStarted);
660   return iResult;
661 }
662
663 int AliHLTSystem::SendControlEvent(AliHLTComponentDataType dt)
664 {
665   // see header file for class documentation
666   int iResult=0;
667
668   AliHLTComponentBlockDataList controlBlocks;
669   AliHLTComponentBlockData bd;
670
671   // run decriptor block of type kAliHLTDataTypeSOR/kAliHLTDataTypeEOR 
672   AliHLTComponent::FillBlockData(bd);
673   AliHLTRunDesc runDesc;
674   memset(&runDesc, 0, sizeof(AliHLTRunDesc));
675   runDesc.fStructSize=sizeof(AliHLTRunDesc);
676   runDesc.fRunNo=AliHLTMisc::Instance().GetCDBRunNo();
677   bd.fPtr=&runDesc;
678   bd.fSize=sizeof(AliHLTRunDesc);
679   bd.fDataType=dt;
680   bd.fSpecification=kAliHLTVoidDataSpec;
681   controlBlocks.push_back(bd);
682
683   // ECS parameter of type kAliHLTDataTypeECSParam
684   if (fECSParams.IsNull())
685     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";
686   AliHLTComponent::FillBlockData(bd);
687   bd.fPtr=(void*)fECSParams.Data();
688   bd.fSize=fECSParams.Length()+1;
689   bd.fDataType=kAliHLTDataTypeECSParam;
690   bd.fSpecification=kAliHLTVoidDataSpec;
691   controlBlocks.push_back(bd);  
692
693   AliHLTControlTask::AliHLTControlEventGuard g(fpControlTask, controlBlocks);
694   HLTDebug("sending event %s, run descriptor %p", AliHLTComponent::DataType2Text(dt).c_str(), &runDesc);
695   TObjLink *lnk=fTaskList.FirstLink();
696   while (lnk && iResult>=0) {
697     TObject* obj=lnk->GetObject();
698     if (obj) {
699       AliHLTTask* pTask=(AliHLTTask*)obj;
700       AliHLTUInt32_t eventType=gkAliEventTypeUnknown;
701       if (dt==kAliHLTDataTypeSOR) eventType=gkAliEventTypeStartOfRun;
702       else if (dt==kAliHLTDataTypeEOR) eventType=gkAliEventTypeEndOfRun;
703       else HLTWarning("unknown control event %s", AliHLTComponent::DataType2Text(dt).c_str());
704       iResult=pTask->ProcessTask(-1, eventType, 0, 0);
705     } else {
706     }
707     lnk = lnk->Next();
708   }
709
710   // control events are not supposed to go into the HLTOUT
711   if (fpHLTOUTTask)
712     fpHLTOUTTask->Reset();
713
714   HLTDebug("event %s done (%d)", AliHLTComponent::DataType2Text(dt).c_str(), iResult);
715   return iResult;
716 }
717
718 int AliHLTSystem::DeinitTasks()
719 {
720   // see header file for class documentation
721   int iResult=0;
722   TObjLink *lnk=fTaskList.LastLink();
723   while (lnk) {
724     TObject* obj=lnk->GetObject();
725     if (obj) {
726       AliHLTTask* pTask=(AliHLTTask*)obj;
727       int localRes=pTask->Deinit();
728       if (iResult>=0) iResult=localRes;
729 //       ProcInfo_t ProcInfo;
730 //       gSystem->GetProcInfo(&ProcInfo);
731 //       HLTInfo("task %s cleaned (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
732     } else {
733     }
734     lnk = lnk->Prev();
735   }
736   fEventCount=-1;
737   fGoodEvents=-1;
738
739   return iResult;
740 }
741
742 int AliHLTSystem::CleanupHLTOUTHandlers()
743 {
744   // see header file for class documentation
745   if (fpChainHandlers) {
746     AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpChainHandlers);
747     fpChainHandlers=NULL;
748     if (pHandlers) {
749       AliHLTOUT::InvalidateBlocks(*pHandlers);
750       AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
751     }
752     assert(pHandlers->size()==0);
753     delete pHandlers;
754   }
755
756   if (fpEsdHandlers) {
757     AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpEsdHandlers);
758     fpEsdHandlers=NULL;
759     if (pHandlers) {
760       AliHLTOUT::InvalidateBlocks(*pHandlers);
761       AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
762     }
763     assert(pHandlers->size()==0);
764     delete pHandlers;
765   }
766
767   if (fpProprietaryHandlers) {
768     AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpProprietaryHandlers);
769     fpProprietaryHandlers=NULL;
770     if (pHandlers) {
771       AliHLTOUT::InvalidateBlocks(*pHandlers);
772       AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
773     }
774     assert(pHandlers->size()==0);
775     delete pHandlers;
776   }
777   return 0;
778 }
779
780 void* AliHLTSystem::AllocMemory( void* /*param*/, unsigned long size )
781 {
782   // see header file for class documentation
783   void* p=NULL;
784   try {
785     p=(void*)new char[size];
786   }
787   catch (...) {
788     AliHLTLogging log;
789     log.LoggingVarargs(kHLTLogError, "AliHLTSystem" , "AllocMemory" , __FILE__ , __LINE__ , "exeption during memory allocation" );
790   }
791   return p;
792 }
793
794 int AliHLTSystem::AllocEventDoneData( void* /*param*/, AliHLTEventID_t /*eventID*/, unsigned long size, AliHLTComponentEventDoneData** edd )
795 {
796   // see header file for class documentation
797   unsigned long blocksize=sizeof(AliHLTComponentEventDoneData)+size;
798   void* block=AllocMemory(NULL, blocksize);
799   if (!block) return -ENOMEM;
800   memset(block, 0, blocksize);
801   *edd=reinterpret_cast<AliHLTComponentEventDoneData*>(block);
802   (*edd)->fStructSize=sizeof(AliHLTComponentEventDoneData);
803   (*edd)->fDataSize=size;
804   (*edd)->fData=reinterpret_cast<AliHLTUInt8_t*>(block)+sizeof(AliHLTComponentEventDoneData);
805   
806   return 0;
807 }
808
809 int AliHLTSystem::Reconstruct(int nofEvents, AliRunLoader* runLoader, 
810                               AliRawReader* rawReader)
811 {
812   // see header file for class documentation
813   int iResult=0;
814   if (runLoader || rawReader || nofEvents==0) {
815     if (nofEvents>0) {HLTInfo("Run Loader %p, Raw Reader %p , %d event(s)", runLoader, rawReader, nofEvents);}
816     if (CheckStatus(kReady)) {
817       if (nofEvents==0) {
818         // special case to close the reconstruction
819         if (!CheckStatus(kError)) {
820         StopTasks();
821         DeinitTasks();
822         CleanupHLTOUTHandlers();
823         }
824       } else {
825       if ((iResult=AliHLTOfflineInterface::SetParamsToComponents(runLoader, rawReader))>=0) {
826         AliHLTUInt64_t trgMask=0x1;
827         AliHLTUInt32_t timestamp=0;
828         AliHLTUInt32_t eventtype=0;
829         if (runLoader==NULL) {
830           // this is a quick workaround for the case of simulation
831           // the trigger framework is still under development, secondly, AliHLTSimulation
832           // does not yet add the emulated ECS parameters, so no CTP trigger is known in the HLT
833           // AliHLTTask will initialize one dummy CTP trigger class with bit 0, that's why the
834           // default trigger mask is 0x1
835           trgMask=AliHLTMisc::Instance().GetTriggerMask(rawReader);
836
837           // get the timestamp and type of the event from the raw reader
838           // this is currently only meaningfull for reconstruction (runloader==NULL)
839           timestamp=AliHLTMisc::Instance().GetTimeStamp(rawReader);
840           eventtype=AliHLTMisc::Instance().GetEventType(rawReader);
841         }
842         // the system always remains started after event processing, a specific
843         // call with nofEvents==0 is needed to execute the stop sequence
844         if ((iResult=Run(nofEvents, 0, trgMask, timestamp, eventtype))<0) SetStatusFlags(kError);
845       }
846       }
847
848       // add the current HLTOUT task to the collection
849       if (fpHLTOUTTask) {
850         AliHLTOUT* pTask=dynamic_cast<AliHLTOUT*>(fpHLTOUTTask);
851         if (pTask && (iResult=pTask->Init())>=0) {
852           if (pTask->GetNofDataBlocks()>0) {
853             AliHLTOUT* pHLTOUT=RequestHLTOUT();
854             if (pHLTOUT) {
855               pHLTOUT->AddSubCollection(pTask);
856               ReleaseHLTOUT(pHLTOUT);
857             } else {
858               HLTWarning("no HLTOUT instance available, output blocks of the chain are ignored");
859             }
860           }
861         } else {
862           HLTWarning("can not initialize HLTOUT sub collection %s for reconstruction chain (%d), data blocks are lost", pTask?fpHLTOUTTask->GetName():"nil", iResult);
863           iResult=0;
864         }
865       }
866     } else {
867       HLTError("wrong state %#x, required flags %#x", GetStatusFlags(), kReady);
868     }
869   } else {
870     HLTError("missing RunLoader (%p)/RawReader (%p) instance", runLoader, rawReader);
871     iResult=-EINVAL;
872   }
873   return iResult;
874 }
875
876 int AliHLTSystem::FillESD(int eventNo, AliRunLoader* runLoader, AliESDEvent* esd)
877 {
878   // see header file for class documentation
879   int iResult=0;
880   if (runLoader || esd) {
881     HLTInfo("Event %d: Run Loader %p, ESD %p", eventNo, runLoader, esd);
882     iResult=AliHLTOfflineInterface::FillComponentESDs(eventNo, runLoader, esd);
883   } else {
884     HLTError("missing run loader/ESD instance(s)");
885     iResult=-EINVAL;
886   }
887   return iResult;
888 }
889
890 int AliHLTSystem::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd)
891 {
892   // see header file for class documentation
893   int iResult=0;
894   if (!pHLTOUT) return -EINVAL;
895   HLTDebug("processing %d HLT data blocks", pHLTOUT->GetNofDataBlocks());
896
897   //
898   // process all kChain handlers first
899   //
900   if ((iResult=ProcessHLTOUTkChain(pHLTOUT))<0) {
901     HLTWarning("Processing of kChain-type data blocks failed with error code %d", iResult);
902     iResult=0;
903   } 
904
905   if (!fpEsdHandlers)
906     fpEsdHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
907   if (!fpProprietaryHandlers)
908     fpProprietaryHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
909
910   AliHLTOUT::AliHLTOUTHandlerListEntryVector* pEsdHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpEsdHandlers);
911   AliHLTOUT::AliHLTOUTHandlerListEntryVector* pProprietaryHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpProprietaryHandlers);
912   if (!pEsdHandlers || !pProprietaryHandlers) return -ENOMEM;
913
914   // invalidate all blocks
915   AliHLTOUT::InvalidateBlocks(*pEsdHandlers);
916   AliHLTOUT::InvalidateBlocks(*pProprietaryHandlers);
917
918   AliHLTComponentDataTypeList esdBlocks;
919
920   for (iResult=pHLTOUT->SelectFirstDataBlock();
921        iResult>=0;
922        iResult=pHLTOUT->SelectNextDataBlock()) {
923     AliHLTComponentDataType dt=kAliHLTVoidDataType;
924     AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
925     pHLTOUT->GetDataBlockDescription(dt, spec);
926     AliHLTOUTHandler* pHandler=pHLTOUT->GetHandler();
927     AliHLTModuleAgent::AliHLTOUTHandlerType handlerType=pHLTOUT->GetDataBlockHandlerType();
928
929     // default handling for ESD data blocks does not require an explicite handler
930     if (!pHandler && (dt==kAliHLTDataTypeESDObject || dt==kAliHLTDataTypeESDTree)) {
931       handlerType=AliHLTModuleAgent::kEsd;
932     }
933     const char* pMsg="invalid";
934     switch (handlerType) {
935     case AliHLTModuleAgent::kEsd:
936       {
937         if (pHandler) {
938           // schedule for later processing
939           pHLTOUT->InsertHandler(*pEsdHandlers, pHLTOUT->GetDataBlockHandlerDesc());
940         } else {
941           AliHLTComponentDataTypeList::iterator element=esdBlocks.begin();
942           for (; element!=esdBlocks.end(); element++) {
943             if (*element==dt) {
944               HLTWarning("multiple ESDs of identical data type %s, please add appropriate handler to merge ESDs", AliHLTComponent::DataType2Text(dt).c_str());
945               break;
946             }
947           }
948           if (element==esdBlocks.end()) esdBlocks.push_back(dt);
949
950           // write directly
951           const AliHLTUInt8_t* pBuffer=NULL;
952           AliHLTUInt32_t size=0;
953           if (pHLTOUT->GetDataBuffer(pBuffer, size)>=0) {
954             pHLTOUT->WriteESD(pBuffer, size, dt, esd);
955             pHLTOUT->ReleaseDataBuffer(pBuffer);
956           }
957           pHLTOUT->MarkDataBlockProcessed();
958         }
959       }
960       break;
961     case AliHLTModuleAgent::kRawReader:
962       // handled in the AliRawReaderHLT
963       break;
964     case AliHLTModuleAgent::kRawStream:
965       HLTWarning("HLTOUT handler type 'kRawStream' not yet implemented: agent %s, data type %s, specification %#x",
966                  pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
967                  AliHLTComponent::DataType2Text(dt).c_str(), spec);
968       break;
969     case AliHLTModuleAgent::kChain:
970       HLTWarning("HLTOUT handler type 'kChain' has already been processed: agent %s, data type %s, specification %#x\n"
971                  "New block of this type added by the chain? Skipping data block ...",
972                  pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
973                  AliHLTComponent::DataType2Text(dt).c_str(), spec);
974       break;
975     case AliHLTModuleAgent::kProprietary:
976       HLTDebug("processing proprietary data: agent %s, data type %s, specification %#x",
977                  pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
978                  AliHLTComponent::DataType2Text(dt).c_str(), spec);
979       if (pHandler) {
980         AliHLTOUT::AliHLTOUTLockGuard g(pHLTOUT);
981         int res=pHandler->ProcessData(pHLTOUT);
982         if (res<0) {
983           HLTWarning("processing proprietary data failed (%d): agent %s, data type %s, specification %#x",
984                      res, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
985                      AliHLTComponent::DataType2Text(dt).c_str(), spec);
986         }
987       }
988       break;
989     case AliHLTModuleAgent::kUnknownOutput:
990       pMsg="unknown";
991       // fall trough intended
992     default:
993       HLTWarning("%s handler type: agent %s, data type %s, specification %#x, ... skipping data block",
994                  pMsg, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
995                  AliHLTComponent::DataType2Text(dt).c_str(), spec);
996     }
997   }
998   // TODO: the return value of SelectFirst/NextDataBlock must be
999   // changed in order to avoid this check
1000   if (iResult==-ENOENT) iResult=0;
1001
1002   AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator handler;
1003
1004   // process and write all esd data blocks
1005   for (handler=pEsdHandlers->begin(); handler!=pEsdHandlers->end() && iResult>=0; handler++) {
1006     AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));     
1007     AliHLTOUTHandler* pHandler=*handler;
1008     const AliHLTUInt8_t* pBuffer=NULL;
1009     AliHLTUInt32_t size=0;
1010     pHandler->ProcessData(pHLTOUT);
1011     if ((size=pHandler->GetProcessedData(pBuffer))>0) {
1012       AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*handler;
1013       AliHLTComponentDataType dt=desc;
1014       pHLTOUT->WriteESD(pBuffer, size, dt, esd);
1015       pHandler->ReleaseProcessedData(pBuffer, size);
1016     }
1017     pHLTOUT->MarkDataBlocksProcessed(&(*handler));
1018   }
1019
1020   // process all kProprietary data blocks
1021   for (handler=pProprietaryHandlers->begin(); handler!=pProprietaryHandlers->end() && iResult>=0; handler++) {
1022     AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));     
1023     AliHLTOUTHandler* pHandler=*handler;
1024     const AliHLTUInt8_t* pBuffer=NULL;
1025     AliHLTUInt32_t size=0;
1026     pHandler->ProcessData(pHLTOUT);
1027     if ((size=pHandler->GetProcessedData(pBuffer))>0) {
1028       HLTWarning("data produced by kProprietary handler ignored");
1029       pHandler->ReleaseProcessedData(pBuffer, size);
1030     }
1031     pHLTOUT->MarkDataBlocksProcessed(&(*handler));
1032   }
1033
1034   // remove all empty handlers form the list (handlers which did not get a block this time)
1035   AliHLTOUT::RemoveEmptyDuplicateHandlers(*pEsdHandlers);
1036   AliHLTOUT::RemoveEmptyDuplicateHandlers(*pProprietaryHandlers);
1037
1038   return iResult;
1039 }
1040
1041 int AliHLTSystem::ProcessHLTOUTkChain(AliHLTOUT* pHLTOUT)
1042 {
1043   // see header file for class documentation
1044   int iResult=0;
1045   if (!pHLTOUT) return -EINVAL;
1046
1047   if (!fpChainHandlers)
1048     fpChainHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
1049
1050   AliHLTOUT::AliHLTOUTHandlerListEntryVector* pChainHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpChainHandlers);
1051   if (!pChainHandlers) return -ENOMEM;
1052
1053   // invalidate all blocks
1054   AliHLTOUT::InvalidateBlocks(*pChainHandlers);
1055
1056   // fill the list
1057   pHLTOUT->FillHandlerList(*pChainHandlers, AliHLTModuleAgent::kChain);
1058
1059   // process all defined chain handlers
1060   AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator chainHandler;
1061   for (chainHandler=pChainHandlers->begin(); chainHandler!=pChainHandlers->end() && iResult>=0; chainHandler++) {
1062     if (chainHandler->IsEmpty()) continue;
1063     AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*chainHandler));        
1064     AliHLTOUTHandler* pHandler=*chainHandler;
1065     const AliHLTUInt8_t* pBuffer=NULL;
1066     AliHLTUInt32_t size=0;
1067     pHandler->ProcessData(pHLTOUT);
1068     if ((size=pHandler->GetProcessedData(pBuffer))>0) {
1069       AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*chainHandler;
1070       //AliHLTComponentDataType dt=desc;
1071
1072       pHandler->ReleaseProcessedData(pBuffer, size);
1073     }
1074     pHLTOUT->MarkDataBlocksProcessed(&(*chainHandler));
1075   }
1076
1077   // remove all empty handlers form the list (handlers which did not get a block this time)
1078   AliHLTOUT::RemoveEmptyDuplicateHandlers(*pChainHandlers);
1079
1080   return iResult;
1081 }
1082
1083 int AliHLTSystem::LoadComponentLibraries(const char* libraries)
1084 {
1085   // see header file for class documentation
1086   int iResult=0;
1087   if (libraries) {
1088     if (fpComponentHandler) {
1089       TString libs(libraries);
1090       TObjArray* pTokens=libs.Tokenize(" ");
1091       if (pTokens) {
1092         int iEntries=pTokens->GetEntriesFast();
1093         for (int i=0; i<iEntries && iResult>=0; i++) {
1094           iResult=fpComponentHandler->LoadLibrary((((TObjString*)pTokens->At(i))->GetString()).Data());
1095         }
1096         delete pTokens;
1097       }
1098       if (iResult>=0) {
1099         SetStatusFlags(kLibrariesLoaded);
1100       } else {
1101         // lets see if we need this, probably not
1102         //fpComponentHandler->UnloadLibraries();
1103         ClearStatusFlags(kLibrariesLoaded);
1104       }
1105     } else {
1106       iResult=-EFAULT;
1107       HLTFatal("no component handler available");
1108     }
1109   } else {
1110     iResult=-EINVAL;
1111   }
1112   return iResult;
1113 }
1114
1115 int AliHLTSystem::Configure(AliRunLoader* runloader)
1116 {
1117   // see header file for class documentation
1118   return Configure(NULL, runloader);
1119 }
1120
1121 int AliHLTSystem::Configure(AliRawReader* rawReader, AliRunLoader* runloader)
1122 {
1123   // see header file for class documentation
1124   int iResult=0;
1125   if (CheckStatus(kRunning)) {
1126     HLTError("HLT system in running state, can not configure");
1127     return -EBUSY;
1128   }
1129   ClearStatusFlags(kTaskListCreated);
1130   if (CheckFilter(kHLTLogDebug))
1131     AliHLTModuleAgent::PrintStatus();
1132   if (CheckStatus(kConfigurationLoaded)==0) {
1133     iResult=LoadConfigurations(rawReader, runloader);
1134   } else {
1135     if (fChains.Length()==0) {
1136       HLTError("custom configuration(s) specified, but no configuration to run in local reconstruction, use \'chains=<chain,...>\' option");
1137       iResult=-ENOENT;
1138     }
1139   }
1140   if (iResult>=0) {
1141     SetStatusFlags(kConfigurationLoaded);
1142     if (CheckFilter(kHLTLogDebug))
1143       fpConfigurationHandler->PrintConfigurations();
1144     iResult=BuildTaskListsFromReconstructionChains(rawReader, runloader);
1145     if (iResult>=0) {
1146       SetStatusFlags(kTaskListCreated);
1147     }
1148   }
1149   if (iResult<0) SetStatusFlags(kError);
1150   
1151   return iResult;
1152 }
1153
1154 int AliHLTSystem::ScanOptions(const char* options)
1155 {
1156   // see header file for class documentation
1157   int iResult=0;
1158   if (options) {
1159     //AliHLTComponentHandler::TLibraryMode libMode=AliHLTComponentHandler::kDynamic;
1160     TString libs("");
1161     TString excludelibs("");
1162     TString alloptions(options);
1163     TObjArray* pTokens=alloptions.Tokenize(" ");
1164     if (pTokens) {
1165       int iEntries=pTokens->GetEntriesFast();
1166       for (int i=0; i<iEntries; i++) {
1167         TString token=(((TObjString*)pTokens->At(i))->GetString());
1168         if (token.Contains("loglevel=")) {
1169           TString param=token.ReplaceAll("loglevel=", "");
1170           if (param.IsDigit()) {
1171             SetGlobalLoggingLevel((AliHLTComponentLogSeverity)param.Atoi());
1172           } else if (param.BeginsWith("0x") &&
1173                      param.Replace(0,2,"",0).IsHex()) {
1174             int severity=0;
1175             sscanf(param.Data(),"%x", &severity);
1176             SetGlobalLoggingLevel((AliHLTComponentLogSeverity)severity);
1177           } else {
1178             HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
1179           }
1180         } else if (token.Contains("frameworklog=")) {
1181           TString param=token.ReplaceAll("frameworklog=", "");
1182           if (param.IsDigit()) {
1183             SetFrameworkLog((AliHLTComponentLogSeverity)param.Atoi());
1184           } else if (param.BeginsWith("0x") &&
1185                      param.Replace(0,2,"",0).IsHex()) {
1186             int severity=0;
1187             sscanf(param.Data(),"%x", &severity);
1188             SetFrameworkLog((AliHLTComponentLogSeverity)severity);
1189           } else {
1190             HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
1191           }
1192         } else if (token.Contains("alilog=off")) {
1193           SwitchAliLog(0);
1194         } else if (token.Contains("config=") || token.Contains("run-online-config")) {
1195           if (!CheckStatus(kConfigurationLoaded)) {
1196             Int_t error=0;
1197             AliHLTOnlineConfiguration* pConf = NULL;
1198             if (token.Contains("run-online-config")) {
1199               AliCDBEntry* pEntry=AliHLTMisc::Instance().LoadOCDBEntry("HLT/Calib/OnlineConfig");
1200               if (pEntry) {
1201                 TObject* pObject=AliHLTMisc::Instance().ExtractObject(pEntry);
1202                 if (pObject && pObject->IsA() == AliHLTOnlineConfiguration::Class())
1203                   pConf = (AliHLTOnlineConfiguration*)pObject;
1204               }
1205             }
1206             if (token.Contains("config=")) {
1207               TString param=token.ReplaceAll("config=", "");
1208               if (token.EndsWith(".xml", TString::kIgnoreCase)) {
1209                 Int_t filesize = 0;
1210                 pConf = new AliHLTOnlineConfiguration;
1211                 filesize = pConf->LoadConfiguration(param.Data());
1212                 if (filesize <= 0) {
1213                   HLTError("cannot load config \'%s\'", param.Data());
1214                   iResult=-EBADF;
1215                 }
1216               } else {
1217                 gROOT->Macro(param.Data(), &error);
1218                 if (error==0) {
1219                   SetStatusFlags(kConfigurationLoaded);
1220                 } else {
1221                   HLTError("cannot execute macro \'%s\'", param.Data());
1222                   iResult=-EBADF;
1223                 }
1224               }
1225             }
1226             if (pConf) {
1227                 error = pConf->Parse();
1228                 if (error==0) {
1229                   fChains = pConf->GetDefaultChains();
1230                   libs = pConf->GetComponentLibraries();
1231                   libs += " ";
1232                   SetStatusFlags(kConfigurationLoaded);
1233                 } else {
1234                   HLTError("cannot parse online configuration");
1235                   iResult=-EBADF;
1236                 }
1237             }
1238             delete pConf; pConf=NULL;
1239           } else {
1240             HLTWarning("HLT options has both a config file and run-online-config set");
1241           }
1242         } else if (token.Contains("chains=")) {
1243           TString param=token.ReplaceAll("chains=", "");
1244           fChains=param.ReplaceAll(",", " ");
1245           if (fChains.IsNull()) fChains=" "; // disable all chains
1246         } else if (token.Contains("libmode=")) {
1247           TString param=token.ReplaceAll("libmode=", "");
1248           param.ReplaceAll(",", " ");
1249           if (fpComponentHandler) {
1250             if (param.CompareTo("static")==0) {
1251               fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kStatic);
1252             } else if (param.CompareTo("dynamic")==0) {
1253               fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kDynamic);
1254             } else {
1255               HLTWarning("wrong argument for option \'libmode=\', use \'static\' or \'dynamic\'");
1256             }
1257           }
1258         } else if (token.BeginsWith("ECS=")) {
1259           fECSParams=token.ReplaceAll("ECS=", "");
1260         } else if (token.BeginsWith("hltout-mode=")) {
1261           // The actual parameter for argument 'hltout-mode' is treated in AliSimulation.
1262           // For AliHLTSystem the occurrence with parameter 'split' signals the use of the
1263           // separated HLTOUTComponents for digit and raw data. All others indicate
1264           // HLTOUTComponent type 'global' where the data generation is steered from global
1265           // flags
1266           fUseHLTOUTComponentTypeGlobal=token.CompareTo("hltout-mode=split")==1;
1267         } else if (token.BeginsWith("lib") && token.EndsWith(".so")) {
1268           libs+=token;
1269           libs+=" ";
1270         } else if (token.BeginsWith("!lib") && token.EndsWith(".so")) {
1271           excludelibs+=token;
1272           excludelibs+=" ";
1273         } else {
1274           HLTWarning("unknown option \'%s\'", token.Data());
1275         }
1276       }
1277       delete pTokens;
1278     }
1279
1280     if (iResult>=0) {
1281       if (libs.IsNull()) {
1282         const char** deflib=fgkHLTDefaultLibs;
1283         for (;*deflib; deflib++) {
1284           if (excludelibs.Contains(*deflib)) continue;
1285           libs+=*deflib;
1286           libs+=" ";
1287         }
1288       }
1289       if ((!CheckStatus(AliHLTSystem::kLibrariesLoaded)) &&
1290           (LoadComponentLibraries(libs.Data())<0)) {
1291         HLTError("error while loading HLT libraries");
1292         iResult=-EFAULT;
1293       }
1294     }
1295   }
1296   return iResult;
1297 }
1298
1299 int AliHLTSystem::Reset(int bForce)
1300 {
1301   // see header file for class documentation
1302   int iResult=0;
1303   if (!bForce && CheckStatus(kRunning)) {
1304     HLTError("HLT system in running state, can not configure");
1305     return -EBUSY;
1306   }
1307   CleanTaskList();
1308   ClearStatusFlags(~kUninitialized);
1309   return iResult;
1310 }
1311
1312 int AliHLTSystem::LoadConfigurations(AliRawReader* rawReader, AliRunLoader* runloader)
1313 {
1314   // see header file for class documentation
1315   if (CheckStatus(kRunning)) {
1316     HLTError("HLT system in running state, can not configure");
1317     return -EBUSY;
1318   }
1319   int iResult=0;
1320   AliHLTModuleAgent* pAgent=NULL;
1321
1322   // first check for the required libraries and load those
1323   TString extralibs;
1324   for (pAgent=AliHLTModuleAgent::GetFirstAgent(); 
1325        pAgent && iResult>=0;
1326        pAgent=AliHLTModuleAgent::GetNextAgent()) {
1327     const char* deplibs=pAgent->GetRequiredComponentLibraries();
1328     if (deplibs) {
1329       HLTDebug("required libraries \'%s\' for agent %s (%p)", deplibs, pAgent->GetName(), pAgent);
1330       extralibs+=" ";
1331       extralibs+=deplibs;
1332     }
1333   }
1334   if (iResult>=0) {
1335     iResult=LoadComponentLibraries(extralibs.Data());
1336   }
1337
1338   // in order to register the configurations in the correct sequence
1339   // all agents need to be ordered with respect to the required
1340   // libraries. Ordering relies on the naming convention
1341   // libAliHLT<Module>.so
1342   TList agents;
1343   for (pAgent=AliHLTModuleAgent::GetFirstAgent(); 
1344        pAgent && iResult>=0;
1345        pAgent=AliHLTModuleAgent::GetNextAgent()) {
1346     AliHLTModuleAgent* pPrevDep=NULL;
1347     TString dependencies=pAgent->GetRequiredComponentLibraries();
1348     TObjArray* pTokens=dependencies.Tokenize(" ");
1349     if (pTokens) {
1350       for (int n=0; n<pTokens->GetEntriesFast(); n++) {
1351         TString module=((TObjString*)pTokens->At(n))->GetString();
1352         HLTDebug("  checking %s", module.Data());
1353         module.ReplaceAll("libAliHLT", "");
1354         module.ReplaceAll(".so", "");
1355         
1356         for (AliHLTModuleAgent* pCurrent=dynamic_cast<AliHLTModuleAgent*>(pPrevDep==NULL?agents.First():agents.After(pPrevDep));
1357              pCurrent!=NULL; pCurrent=dynamic_cast<AliHLTModuleAgent*>(agents.After(pCurrent))) {
1358           HLTDebug("    checking %s == %s", module.Data(), pCurrent->GetModuleId());
1359
1360           if (module.CompareTo(pCurrent->GetModuleId())==0) {
1361             pPrevDep=pCurrent;
1362             break;
1363           }
1364         }
1365       }
1366       delete pTokens;
1367     }
1368
1369     if (pPrevDep) {
1370       // insert right after the last dependency
1371       agents.AddAfter(pPrevDep, pAgent);
1372       HLTDebug("insert %s after %s", pAgent->GetModuleId(), pPrevDep->GetModuleId());
1373     } else {
1374       // insert at the beginning
1375       agents.AddFirst(pAgent);
1376       HLTDebug("insert %s at beginning", pAgent->GetModuleId());
1377     }
1378   }
1379
1380   // now we load the configurations
1381   if (agents.GetEntries()) {
1382     TIter next(&agents);
1383     while ((pAgent = dynamic_cast<AliHLTModuleAgent*>(next()))) {
1384       HLTDebug("load configurations for agent %s (%p)", pAgent->GetName(), pAgent);
1385       pAgent->CreateConfigurations(fpConfigurationHandler, rawReader, runloader);
1386     }
1387   }
1388
1389   return iResult;
1390 }
1391
1392 int AliHLTSystem::BuildTaskListsFromReconstructionChains(AliRawReader* rawReader, AliRunLoader* runloader)
1393 {
1394   // see header file for class documentation
1395   if (CheckStatus(kRunning)) {
1396     HLTError("HLT system in running state, can not configure");
1397     return -EBUSY;
1398   }
1399   if (!CheckStatus(kConfigurationLoaded)) {
1400     HLTWarning("configurations not yet loaded");
1401     return 0;
1402   }
1403
1404   int iResult=0;
1405   int bHaveOutput=0;
1406
1407   // query chains
1408   TString chains;
1409   if (fChains.Length()>0) {
1410     chains=fChains;
1411     HLTImportant("custom reconstruction chain: %s", chains.Data());
1412   } else {
1413     for (AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
1414          pAgent && iResult>=0;
1415          pAgent=AliHLTModuleAgent::GetNextAgent()) {
1416       const char* agentchains=pAgent->GetReconstructionChains(rawReader, runloader);
1417       if (agentchains) {
1418         if (!chains.IsNull()) chains+=" ";
1419         chains+=agentchains;
1420         HLTInfo("reconstruction chains for agent %s (%p): %s", pAgent->GetName(), pAgent, agentchains);
1421       }
1422     }
1423   }
1424
1425   // build task list for chains
1426   TObjArray* pTokens=chains.Tokenize(" ");
1427   if (pTokens) {
1428     int iEntries=pTokens->GetEntriesFast();
1429     for (int i=0; i<iEntries && iResult>=0; i++) {
1430       const char* pCID=((TObjString*)pTokens->At(i))->GetString().Data();
1431       AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(pCID);
1432       if (pConf) {
1433         iResult=BuildTaskList(pConf);
1434         if (true) { // condition was deprecated but kept for sake of svn diff
1435           // bHaveOutput variable has to be set for both running modes
1436           // AliHLTSimulation and AliHLTReconstruction
1437           assert(fpComponentHandler!=NULL);
1438           TString cid=pConf->GetComponentID();
1439           if (runloader!=NULL && cid.CompareTo("HLTOUT")==0) {
1440             // remove from the input of a global HLTOUT configuration
1441             chains.ReplaceAll(pCID, "");
1442           } else if (bHaveOutput==0) {
1443             // check whether this configuration produces data output
1444             if ((bHaveOutput=fpComponentHandler->HasOutputData(cid.Data()))<0) {
1445               bHaveOutput=0;
1446               chains.ReplaceAll(pCID, "");
1447             }
1448           }
1449         }
1450       } else {
1451         HLTWarning("can not find configuration %s", pCID);
1452       }
1453     }
1454     delete pTokens;
1455   }
1456
1457   // build HLTOUT for simulation
1458   if (iResult>=0 && runloader) {
1459     if (bHaveOutput) {
1460       // there are components in the chain which produce data which need to be
1461       // piped to an HLTOUT
1462
1463       // add the SchemaEvolutionComponent which analyzes the ROOT objects in
1464       // the output stream
1465       if (fpComponentHandler->FindComponentIndex("ROOTSchemaEvolutionComponent")>=0 ||
1466           fpComponentHandler->LoadLibrary("libAliHLTUtil.so")>=0) {
1467         AliHLTConfiguration schemaevo("_schemaevolution_", "ROOTSchemaEvolutionComponent", 
1468                                       chains.Data(), "-file=HLT.StreamerInfo.root");
1469         iResult=BuildTaskList("_schemaevolution_");
1470       } else {
1471         HLTWarning("can not load libAliHLTUtil.so and ROOTSchemaEvolutionComponent");
1472       }
1473
1474       // add the HLTOUT component
1475       if (fpComponentHandler->FindComponentIndex("HLTOUT")>=0 ||
1476           fpComponentHandler->LoadLibrary("libHLTsim.so")>=0) {
1477         // for the default HLTOUTComponent type 'global' the data generation is steered
1478         // by global flags from AliSimulation. This allows for emulation of the old
1479         // AliHLTSimulation behavior where only one chain is run on either digits or
1480         // simulated raw data and the HLT digits and raw files have been generated
1481         // depending on the configuration
1482         const char* HLTOUTComponentId="HLTOUT";
1483         if (!fUseHLTOUTComponentTypeGlobal) {
1484           // choose the type of output depending  on the availability of
1485           // the raw reader
1486           if (rawReader) HLTOUTComponentId="HLTOUTraw";
1487           else HLTOUTComponentId="HLTOUTdigits";
1488         }
1489         AliHLTConfiguration globalout("_globalout_", HLTOUTComponentId, chains.Data(), NULL);
1490         iResult=BuildTaskList("_globalout_");
1491       } else {
1492         HLTError("can not load libHLTsim.so and HLTOUT component");
1493         iResult=-EFAULT;
1494       }
1495     }
1496   }
1497
1498   // build HLTOUT task for reconstruction
1499   // Matthias 08.07.2008 the rawReader is never set when running embedded into
1500   // AliReconstruction. The system is configured during AliHLTReconstructor::Init
1501   // where the RawReader is not available. It is available in the first invocation
1502   // of Reconstruct.
1503   // 
1504   // That means that policy is slightly changed:
1505   // - if the run loader is available -> AliSimulation
1506   // - no run loader available -> AliReconstruction
1507   if (iResult>=0 && !runloader) {
1508     if (bHaveOutput) {
1509       // there are components in the chain which produce data which need to be
1510       // piped to an HLTOUT sub-collection
1511       if (!fpHLTOUTTask) {
1512         iResult=AddHLTOUTTask(chains.Data());
1513       }
1514     }
1515   }
1516
1517   if (iResult>=0) SetStatusFlags(kTaskListCreated);
1518
1519   return iResult;
1520 }
1521
1522 int AliHLTSystem::AddHLTOUTTask(const char* hltoutchains)
1523 {
1524   // see header file for class documentation
1525   int iResult=0;
1526   if (!hltoutchains || hltoutchains[0]==0) return 0;
1527
1528   // check chains for output
1529   TString chains=hltoutchains;
1530   TObjArray* pTokens=chains.Tokenize(" ");
1531   if (pTokens) {
1532     int iEntries=pTokens->GetEntriesFast();
1533     for (int i=0; i<iEntries && iResult>=0; i++) {
1534       const char* token=((TObjString*)pTokens->At(i))->GetString().Data();
1535       AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(token);
1536       if (pConf) {
1537         TString cid=pConf->GetComponentID();
1538         if (fpComponentHandler->HasOutputData(cid.Data())) {
1539           continue;
1540         }
1541       } else {
1542         HLTWarning("can not find configuration %s", token);
1543       }
1544       // remove from the list of hltout chains
1545       chains.ReplaceAll(token, "");
1546     }
1547     delete pTokens;
1548   }
1549
1550   // do not create the HLTOUT task if none of the chains have output
1551   if (chains.IsNull()) return 0;
1552
1553   // indicate the task to be available
1554   iResult=1;
1555
1556   if (fpHLTOUTTask) {
1557     if (strcmp(chains.Data(), fpHLTOUTTask->GetSourceChains())==0) {
1558       HLTWarning("HLTOUT task already added for chains \"%s\" %p", chains.Data(), fpHLTOUTTask);
1559     } else {
1560       HLTError("HLTOUT task already added for chains \"%s\" %p, ignoring new chains  \"%s\"",
1561                fpHLTOUTTask->GetSourceChains(), fpHLTOUTTask, chains.Data());
1562     }
1563     return iResult;
1564   }
1565
1566   fpHLTOUTTask=new AliHLTOUTTask(chains);
1567   if (fpHLTOUTTask) {
1568     if (fpHLTOUTTask->GetConf() && 
1569         (fpHLTOUTTask->GetConf()->SourcesResolved()>0 ||
1570          fpHLTOUTTask->GetConf()->ExtractSources()>0)) {
1571       iResult=InsertTask(fpHLTOUTTask);
1572     } else {
1573       HLTError("HLTOUT task (%s) sources not resolved", fpHLTOUTTask->GetName());
1574       iResult=-ENOENT;
1575     }
1576
1577     if (iResult<0) {
1578       delete fpHLTOUTTask;
1579     }
1580
1581   } else {
1582     iResult=-ENOMEM;
1583   }
1584   return iResult;
1585 }
1586
1587 int AliHLTSystem::CheckStatus(int flag)
1588 {
1589   // see header file for class documentation
1590   if (flag==kUninitialized && flag==fState) return 1;
1591   if ((fState&flag)==flag) return 1;
1592   return 0;
1593 }
1594
1595 int AliHLTSystem::GetStatusFlags()
1596 {
1597   // see header file for class documentation
1598   return fState;
1599 }
1600
1601 int AliHLTSystem::SetStatusFlags(int flags)
1602 {
1603   // see header file for class documentation
1604   fState|=flags;
1605   return fState;
1606 }
1607
1608 int AliHLTSystem::ClearStatusFlags(int flags)
1609 {
1610   // see header file for class documentation
1611   fState&=~flags;
1612   return fState;
1613 }
1614
1615 AliHLTfctVoid AliHLTSystem::FindDynamicSymbol(const char* library, const char* symbol)
1616 {
1617   // see header file for class documentation
1618   if (fpComponentHandler==NULL) return NULL;
1619   return fpComponentHandler->FindSymbol(library, symbol);
1620 }
1621
1622 void AliHLTSystem::SetFrameworkLog(AliHLTComponentLogSeverity level) 
1623 {
1624   // see header file for class documentation
1625   SetLocalLoggingLevel(level);
1626   if (fpComponentHandler) fpComponentHandler->SetLocalLoggingLevel(level);
1627   if (fpConfigurationHandler) fpConfigurationHandler->SetLocalLoggingLevel(level);
1628 }
1629
1630 int AliHLTSystem::LoggingVarargs(AliHLTComponentLogSeverity severity, 
1631                                  const char* originClass, const char* originFunc,
1632                                  const char* file, int line, ... ) const
1633 {
1634   // see header file for function documentation
1635   int iResult=0;
1636
1637   va_list args;
1638   va_start(args, line);
1639
1640   if (!fName.IsNull())
1641     AliHLTLogging::SetLogString(this, " (%p)", "%s_pfmt_: ", fName.Data());
1642   iResult=SendMessage(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args, !fName.IsNull() /*append if non empty*/));
1643   va_end(args);
1644
1645   return iResult;
1646 }
1647
1648 int AliHLTSystem::InitHLTOUT(AliHLTOUT* instance)
1649 {
1650   // Init the HLTOUT instance for the current event.
1651   // The instance can be used by other classes to get hold on the data
1652   // from HLTOUT.
1653   if (!instance) return -EINVAL;
1654   if (fpHLTOUT && fpHLTOUT!=instance) return -EBUSY;
1655   fpHLTOUT=instance;
1656   return 0;
1657 }
1658
1659 int AliHLTSystem::InvalidateHLTOUT(AliHLTOUT** target)
1660 {
1661   // Clear the HLTOUT instance.
1662   int iResult=0;
1663   if (fHLTOUTUse>0) {
1664     HLTWarning("HLTOUT instance still in use, potential problem due to invalid pointer ahead");
1665     fHLTOUTUse=0;
1666     iResult=-EBUSY;
1667   }
1668   if (target) *target=fpHLTOUT;
1669   fpHLTOUT=NULL;
1670   return iResult;
1671 }
1672
1673 AliHLTOUT* AliHLTSystem::RequestHLTOUT()
1674 {
1675   // Get the HLTOUT instance.
1676   // User method for processing classes. To be released after use.
1677   if (!fpHLTOUT) return NULL;
1678   fHLTOUTUse++;
1679   return fpHLTOUT;
1680 }
1681
1682 int AliHLTSystem::ReleaseHLTOUT(const AliHLTOUT* instance)
1683 {
1684   // Release the HLTOUT instance after use.
1685   if (!instance) return -EINVAL;
1686   if (instance!=fpHLTOUT) return -ENOENT;
1687   fHLTOUTUse--;
1688   return 0;
1689 }