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