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