3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project *
5 //* ALICE Experiment at CERN, All rights reserved. *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 //* for The ALICE HLT Project. *
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 //**************************************************************************
19 /** @file AliHLTSystem.cxx
20 @author Matthias Richter
22 @brief Implementation of HLT module management.
30 #include "AliHLTStdIncludes.h"
31 #include "AliHLTSystem.h"
32 #include "AliHLTComponentHandler.h"
33 #include "AliHLTComponent.h"
34 #include "AliHLTConfiguration.h"
35 #include "AliHLTConfigurationHandler.h"
36 #include "AliHLTTask.h"
37 #include "AliHLTModuleAgent.h"
38 #include "AliHLTOfflineInterface.h"
39 #include "AliHLTDataSource.h"
40 #include "AliHLTOUT.h"
41 #include "AliHLTOUTHandler.h"
42 #include "AliHLTOUTTask.h"
43 #include "AliHLTControlTask.h"
44 #include <TObjArray.h>
45 #include <TObjString.h>
46 #include <TStopwatch.h>
47 //#include <TSystem.h>
49 //#include <TInterpreter.h>
51 /** HLT default component libraries */
52 const char* AliHLTSystem::fgkHLTDefaultLibs[]= {
56 // "libAliHLTSample.so",
61 "libAliHLTTrigger.so",
65 /** ROOT macro for the implementation of ROOT specific class methods */
66 ClassImp(AliHLTSystem)
68 AliHLTSystem::AliHLTSystem(AliHLTComponentLogSeverity loglevel, const char* name)
70 fpComponentHandler(AliHLTComponentHandler::CreateHandler()),
71 fpConfigurationHandler(AliHLTConfigurationHandler::CreateHandler()),
75 fStopwatches(new TObjArray),
78 fpChainHandlers(NULL),
80 fpProprietaryHandlers(NULL),
85 // see header file for class documentation
87 // refer to README to build package
89 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
91 if (fgNofInstances++>0) {
92 // July 2008: multiple instances are now allowed
93 // AliHLTSystem is used in multiple instances for the kChain HLTOUT handler
94 //HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
97 SetGlobalLoggingLevel(loglevel);
98 SetFrameworkLog(loglevel);
99 if (fpComponentHandler) {
100 AliHLTAnalysisEnvironment env;
101 memset(&env, 0, sizeof(AliHLTAnalysisEnvironment));
102 env.fStructSize=sizeof(AliHLTAnalysisEnvironment);
103 env.fAllocMemoryFunc=AliHLTSystem::AllocMemory;
104 env.fLoggingFunc=NULL;
105 fpComponentHandler->SetEnvironment(&env);
106 InitAliLogFunc(fpComponentHandler);
107 if (fgNofInstances==1) {
108 fpComponentHandler->AnnounceVersion();
111 HLTFatal("can not create Component Handler");
113 if (fpConfigurationHandler) {
114 AliHLTConfiguration::GlobalInit(fpConfigurationHandler);
116 HLTFatal("can not create Configuration Handler");
120 AliHLTSystem::~AliHLTSystem()
122 // see header file for class documentation
126 AliHLTConfiguration::GlobalDeinit(fpConfigurationHandler);
127 if (fpConfigurationHandler) {
128 fpConfigurationHandler->Destroy();
130 fpConfigurationHandler=NULL;
132 if (fpComponentHandler) {
133 fpComponentHandler->Destroy();
135 fpComponentHandler=NULL;
137 // note: fpHLTOUTTask and fpControlTask are deleted by
141 int AliHLTSystem::fgNofInstances=0;
143 int AliHLTSystem::AddConfiguration(AliHLTConfiguration* pConf)
145 // see header file for class documentation
146 HLTLogKeyword("configuration handling");
149 HLTError("function not yet implemented");
157 int AliHLTSystem::InsertConfiguration(AliHLTConfiguration* pConf, AliHLTConfiguration* pPrec)
159 // see header file for class documentation
160 HLTLogKeyword("configuration handling");
165 HLTError("function not yet implemented");
174 int AliHLTSystem::DeleteConfiguration(AliHLTConfiguration* pConf)
176 // see header file for class documentation
177 HLTLogKeyword("configuration handling");
180 HLTError("function not yet implemented");
188 int AliHLTSystem::BuildTaskList(const char* id)
190 // see header file for class documentation
193 if (fpConfigurationHandler) {
194 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(id);
196 iResult=BuildTaskList(pConf);
198 HLTError("unknown configuration \"%s\"", id);
210 int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
212 // see header file for class documentation
215 AliHLTTask* pTask=NULL;
216 if ((pTask=FindTask(pConf->GetName()))!=NULL) {
217 if (pTask->GetConf()!=pConf) {
218 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);
221 // task for this configuration exists, terminate
223 } else if (pConf->SourcesResolved(1)!=1) {
224 HLTError("configuration \"%s\" has unresolved sources, aborting ...", pConf->GetName());
227 pTask=new AliHLTTask(pConf);
231 pTask->SetLocalLoggingLevel(GetLocalLoggingLevel());
234 static int iterationLevel=0;
235 if (pTask && iResult>=0) {
236 // check for circular dependencies
237 if ((iResult=pConf->FollowDependency(pConf->GetName()))>0) {
238 HLTError("detected circular dependency for configuration \"%s\"", pTask->GetName());
239 pTask->PrintDependencyTree(pTask->GetName(), 1/*use the configuration list*/);
240 HLTError("aborted ...");
244 // check whether all dependencies are already in the task list
245 // create the missing ones
246 // this step is an iterative process which calls this function again for the missing
247 // configurations, in order to avoid the currently processed task to be created
248 // again it is added to the list temporarily and removed afterwards
249 // This is of high importance to preserve the order of the tasks. Furthermore, the
250 // InsertTask method has to be used in order to set all the cross links right
251 fTaskList.Add(pTask);
252 AliHLTConfiguration* pDep=pConf->GetFirstSource();
253 while (pDep!=NULL && iResult>=0) {
254 HLTDebug("iteration %d: checking dependency %s (%p)", iterationLevel, pDep->GetName(), pDep);
255 if (FindTask(pDep->GetName())==NULL) {
256 HLTDebug("iteration %d: building task list for configuration %s (%p)", iterationLevel, pDep->GetName(), pDep);
258 iResult=BuildTaskList(pDep);
261 pDep=pConf->GetNextSource();
263 // remove the temporarily added task
264 fTaskList.Remove(pTask);
266 // insert the task and set the cross-links
268 HLTDebug("iteration %d: inserting task %s (%p)", iterationLevel, pTask->GetName(), pTask);
269 iResult=InsertTask(pTask);
282 int AliHLTSystem::CleanTaskList()
284 // see header file for class documentation
289 while ((lnk=fTaskList.LastLink())!=NULL) {
290 delete (lnk->GetObject());
291 fTaskList.Remove(lnk);
297 int AliHLTSystem::InsertTask(AliHLTTask* pTask)
299 // see header file for class documentation
301 if (fpControlTask==NULL) {
302 fpControlTask=new AliHLTControlTask;
303 if (!fpControlTask) return -ENOMEM;
304 fTaskList.AddFirst(fpControlTask);
306 TObjLink *controlLnk=NULL;
307 TObjLink *lnk = fTaskList.FirstLink();
308 assert(!lnk || lnk->GetObject()==fpControlTask || fpControlTask==NULL);
309 if (lnk && lnk->GetObject()==fpControlTask) {
310 if (pTask->GetConf() && pTask->GetConf()->GetFirstSource()==NULL) {
311 pTask->SetDependency(fpControlTask);
312 fpControlTask->SetTarget(pTask);
317 if ((iResult=pTask->CheckDependencies())<=0)
319 while (lnk && iResult>0) {
320 AliHLTTask* pCurr = (AliHLTTask*)lnk->GetObject();
321 //HLTDebug("checking \"%s\"", pCurr->GetName());
322 iResult=pTask->Depends(pCurr);
324 iResult=pTask->SetDependency(pCurr);
325 pCurr->SetTarget(pTask);
326 HLTDebug("set dependency \"%s\" for configuration \"%s\"", pCurr->GetName(), pTask->GetName());
328 if (pCurr->Depends(pTask)) {
329 // circular dependency
330 HLTError("circular dependency: can not resolve dependencies for configuration \"%s\"", pTask->GetName());
332 } else if ((iResult=pTask->CheckDependencies())>0) {
338 fTaskList.AddAfter(lnk, pTask);
339 } else if (controlLnk) {
340 fTaskList.AddAfter(controlLnk, pTask);
342 fTaskList.AddFirst(pTask);
344 HLTDebug("task \"%s\" (%p) inserted (size %d)", pTask->GetName(), pTask, sizeof(AliHLTTask));
345 } else if (iResult>0) {
346 HLTError("can not resolve dependencies for configuration \"%s\" (%d unresolved)", pTask->GetName(), iResult);
352 AliHLTTask* AliHLTSystem::FindTask(const char* id)
354 // see header file for class documentation
355 AliHLTTask* pTask=NULL;
357 pTask=dynamic_cast<AliHLTTask*>(fTaskList.FindObject(id));
362 void AliHLTSystem::PrintTaskList()
364 // see header file for class documentation
365 HLTLogKeyword("task list");
366 TObjLink *lnk = NULL;
367 HLTMessage("Task List");
368 lnk=fTaskList.FirstLink();
370 TObject* obj=lnk->GetObject();
372 HLTMessage(" %s - status:", obj->GetName());
373 AliHLTTask* pTask=(AliHLTTask*)obj;
374 pTask->PrintStatus();
381 int AliHLTSystem::Run(Int_t iNofEvents, int bStop)
383 // see header file for class documentation
386 SetStatusFlags(kRunning);
387 if (fEventCount>=0 || (iResult=InitTasks())>=0) {
388 if (fEventCount>=0 || (iResult=StartTasks())>=0) {
389 if (fEventCount==0) {
390 InitBenchmarking(fStopwatches);
392 // Matthias Oct 11 2008 this is a bug
393 // By resuming the stopwatches at this point, all continued counting, but the
394 // starting and stopping is controlled by the AliHLTStopwatchGuard
395 //ResumeBenchmarking(fStopwatches);
397 for (int i=fEventCount; i<fEventCount+iNofEvents && iResult>=0; i++) {
399 if (iNofEvents>1 && i==fEventCount) {
400 HLTWarning("can not add more than one event to the HLTOUT, skipping all but last block");
402 // reset and prepare for new data
403 fpHLTOUTTask->Reset();
405 if ((iResult=ProcessTasks(i))>=0) {
409 // TODO: define different running modes to either ignore errors in
410 // event processing or not
415 fEventCount+=iNofEvents;
416 if (bStop) StopTasks();
417 else PauseBenchmarking(fStopwatches);
419 if (bStop) DeinitTasks();
423 } else if (iResult==-126 /*ENOKEY*/) {
424 iResult=0; // do not propagate the error
426 ClearStatusFlags(kRunning);
430 int AliHLTSystem::InitTasks()
432 // see header file for class documentation
434 TObjLink *lnk=fTaskList.FirstLink();
437 HLTWarning("%s%sTask list is empty, skipping HLT", fName.Data(), fName.IsNull()?"":": ");
438 return -126 /*ENOKEY*/;
440 while (lnk && iResult>=0) {
441 TObject* obj=lnk->GetObject();
443 AliHLTTask* pTask=(AliHLTTask*)obj;
444 iResult=pTask->Init(NULL, fpComponentHandler);
445 // ProcInfo_t ProcInfo;
446 // gSystem->GetProcInfo(&ProcInfo);
447 // HLTInfo("task %s initialized (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
453 HLTError("%s%scan not initialize task list, error %d", fName.Data(), fName.IsNull()?"":": ", iResult);
459 int AliHLTSystem::InitBenchmarking(TObjArray* pStopwatches)
461 // see header file for class documentation
463 if (pStopwatches==NULL) return 0;
465 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
466 TStopwatch* pStopwatch= new TStopwatch;
469 pStopwatches->AddAt(pStopwatch, i);
476 TObjLink *lnk=fTaskList.FirstLink();
477 while (lnk && iResult>=0) {
478 TObject* obj=lnk->GetObject();
480 AliHLTTask* pTask=(AliHLTTask*)obj;
481 AliHLTComponent* pComp=NULL;
482 if (iResult>=0 && (pComp=pTask->GetComponent())!=NULL) {
483 switch (pComp->GetComponentType()) {
484 case AliHLTComponent::kProcessor:
485 pComp->SetStopwatches(pStopwatches);
487 case AliHLTComponent::kSource:
489 // this switch determines whether the time consumption of the
490 // AliHLTComponent base methods should be counted to the input
491 // stopwatch or base stopwatch.
492 //int inputBase=(int)AliHLTComponent::kSWBase;
493 int inputBase=(int)AliHLTComponent::kSWInput;
494 pComp->SetStopwatch(pStopwatches->At(inputBase), AliHLTComponent::kSWBase);
495 pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWInput), AliHLTComponent::kSWDA);
498 case AliHLTComponent::kSink:
500 // this switch determines whether the time consumption of the
501 // AliHLTComponent base methods should be counted to the output
502 // stopwatch or base stopwatch.
503 //int outputBase=(int)AliHLTComponent::kSWBase;
504 int outputBase=(int)AliHLTComponent::kSWOutput;
505 pComp->SetStopwatch(pStopwatches->At(outputBase), AliHLTComponent::kSWBase);
506 pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWOutput), AliHLTComponent::kSWDA);
510 HLTWarning("unknown component type %d", (int)pComp->GetComponentType());
520 int AliHLTSystem::PauseBenchmarking(TObjArray* pStopwatches) const
522 // see header file for class documentation
523 if (pStopwatches==NULL) return 0;
525 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
526 if (!pStopwatches->At(i)) continue;
527 TStopwatch* pSw=dynamic_cast<TStopwatch*>(pStopwatches->At(i));
528 if (pSw) pSw->Stop();
533 int AliHLTSystem::ResumeBenchmarking(TObjArray* pStopwatches) const
535 // see header file for class documentation
536 if (pStopwatches==NULL) return 0;
538 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
539 if (!pStopwatches->At(i)) continue;
540 TStopwatch* pSw=dynamic_cast<TStopwatch*>(pStopwatches->At(i));
541 if (pSw) pSw->Continue();
546 int AliHLTSystem::PrintBenchmarking(TObjArray* pStopwatches, int bClean) const
548 // see header file for class documentation
550 if (pStopwatches==NULL) return 0;
552 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
553 if (!dynamic_cast<TStopwatch*>(pStopwatches->At(i))) {
559 if (iInitialized!=0) {
560 HLTImportant("HLT statistics:\n"
561 " base: R:%.3fs C:%.3fs\n"
562 " input: R:%.3fs C:%.3fs\n"
563 " output: R:%.3fs C:%.3fs\n"
564 " event processing : R:%.3fs C:%.3fs"
565 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->RealTime()
566 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->CpuTime()
567 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->RealTime()
568 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->CpuTime()
569 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->RealTime()
570 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->CpuTime()
571 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->RealTime()
572 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->CpuTime()
577 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
578 TObject* pObj=pStopwatches->RemoveAt(i);
579 if (pObj) delete pObj;
585 int AliHLTSystem::StartTasks()
587 // see header file for class documentation
589 TObjLink *lnk=fTaskList.FirstLink();
590 while (lnk && iResult>=0) {
591 TObject* obj=lnk->GetObject();
593 AliHLTTask* pTask=(AliHLTTask*)obj;
594 iResult=pTask->StartRun();
595 // ProcInfo_t ProcInfo;
596 // gSystem->GetProcInfo(&ProcInfo);
597 // HLTInfo("task %s started (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
603 HLTError("%s%scan not start task list, error %d", fName.Data(), fName.IsNull()?"":": ", iResult);
605 SetStatusFlags(kStarted);
608 if ((iResult=SendControlEvent(kAliHLTDataTypeSOR))<0) {
609 HLTError("%s%scan not send SOR event", fName.Data(), fName.IsNull()?"":": ");
615 int AliHLTSystem::ProcessTasks(Int_t eventNo)
617 // see header file for class documentation
619 HLTDebug("processing event no %d", eventNo);
620 TObjLink *lnk=fTaskList.FirstLink();
621 while (lnk && iResult>=0) {
622 TObject* obj=lnk->GetObject();
624 AliHLTTask* pTask=(AliHLTTask*)obj;
625 iResult=pTask->ProcessTask(eventNo);
626 // ProcInfo_t ProcInfo;
627 // gSystem->GetProcInfo(&ProcInfo);
628 // HLTInfo("task %s processed (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
635 HLTImportant("%s%sEvent %d successfully finished (%d)", fName.Data(), fName.IsNull()?"":": ", eventNo, iResult);
638 HLTError("%s%sProcessing of event %d failed (%d)", fName.Data(), fName.IsNull()?"":": ", eventNo, iResult);
644 int AliHLTSystem::StopTasks()
646 // see header file for class documentation
648 if ((iResult=SendControlEvent(kAliHLTDataTypeEOR))<0) {
649 HLTError("%s%scan not send EOR event", fName.Data(), fName.IsNull()?"":": ");
652 // cleanup blocks from the last event. This is a bit awkward. All output
653 // blocks from the chains need to be stored in the HLTOUT task. Though,
654 // we do not know, whether HLTOUT is going to be processed or not.
656 fpHLTOUTTask->Reset();
658 TObjLink *lnk=fTaskList.FirstLink();
660 TObject* obj=lnk->GetObject();
662 AliHLTTask* pTask=(AliHLTTask*)obj;
663 int locResult=pTask->EndRun();
664 if (iResult>=0 && locResult<0) iResult=locResult;
665 // ProcInfo_t ProcInfo;
666 // gSystem->GetProcInfo(&ProcInfo);
667 // HLTInfo("task %s stopped (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
672 PrintBenchmarking(fStopwatches, 1 /*clean*/);
673 ClearStatusFlags(kStarted);
677 int AliHLTSystem::SendControlEvent(AliHLTComponentDataType dt)
679 // see header file for class documentation
682 AliHLTRunDesc runDesc;
683 memset(&runDesc, 0, sizeof(AliHLTRunDesc));
684 runDesc.fStructSize=sizeof(AliHLTRunDesc);
685 AliHLTControlTask::AliHLTControlEventGuard g(fpControlTask, dt, kAliHLTVoidDataSpec, (AliHLTUInt8_t*)&runDesc, sizeof(AliHLTRunDesc));
686 HLTDebug("sending event %s, run descriptor %p", AliHLTComponent::DataType2Text(dt).c_str(), &runDesc);
687 TObjLink *lnk=fTaskList.FirstLink();
688 while (lnk && iResult>=0) {
689 TObject* obj=lnk->GetObject();
691 AliHLTTask* pTask=(AliHLTTask*)obj;
692 AliHLTUInt32_t eventType=gkAliEventTypeUnknown;
693 if (dt==kAliHLTDataTypeSOR) eventType=gkAliEventTypeStartOfRun;
694 else if (dt==kAliHLTDataTypeEOR) eventType=gkAliEventTypeEndOfRun;
695 else HLTWarning("unknown control event %s", AliHLTComponent::DataType2Text(dt).c_str());
696 iResult=pTask->ProcessTask(-1, eventType);
702 // control events are not supposed to go into the HLTOUT
704 fpHLTOUTTask->Reset();
706 HLTDebug("event %s done (%d)", AliHLTComponent::DataType2Text(dt).c_str(), iResult);
710 int AliHLTSystem::DeinitTasks()
712 // see header file for class documentation
714 TObjLink *lnk=fTaskList.FirstLink();
715 while (lnk && iResult>=0) {
716 TObject* obj=lnk->GetObject();
718 AliHLTTask* pTask=(AliHLTTask*)obj;
719 iResult=pTask->Deinit();
720 // ProcInfo_t ProcInfo;
721 // gSystem->GetProcInfo(&ProcInfo);
722 // HLTInfo("task %s cleaned (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
733 int AliHLTSystem::CleanHLTOUT()
735 // see header file for class documentation
736 if (fpChainHandlers) {
737 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpChainHandlers);
738 fpChainHandlers=NULL;
740 AliHLTOUT::InvalidateBlocks(*pHandlers);
741 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
743 assert(pHandlers->size()==0);
748 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpEsdHandlers);
751 AliHLTOUT::InvalidateBlocks(*pHandlers);
752 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
754 assert(pHandlers->size()==0);
758 if (fpProprietaryHandlers) {
759 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpProprietaryHandlers);
760 fpProprietaryHandlers=NULL;
762 AliHLTOUT::InvalidateBlocks(*pHandlers);
763 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pHandlers);
765 assert(pHandlers->size()==0);
771 void* AliHLTSystem::AllocMemory( void* /*param*/, unsigned long size )
773 // see header file for class documentation
776 p=(void*)new char[size];
780 log.LoggingVarargs(kHLTLogError, "AliHLTSystem" , "AllocMemory" , __FILE__ , __LINE__ , "exeption during memory allocation" );
785 int AliHLTSystem::Reconstruct(int nofEvents, AliRunLoader* runLoader,
786 AliRawReader* rawReader)
788 // see header file for class documentation
790 if (runLoader || rawReader || nofEvents==0) {
791 if (nofEvents>0) {HLTInfo("Run Loader %p, Raw Reader %p , %d event(s)", runLoader, rawReader, nofEvents);}
792 if (CheckStatus(kReady)) {
794 // special case to close the reconstruction
795 if (!CheckStatus(kError)) {
801 if ((iResult=AliHLTOfflineInterface::SetParamsToComponents(runLoader, rawReader))>=0) {
802 // the system always remains started after event processing, a specific
803 // call with nofEvents==0 is needed to execute the stop sequence
804 if ((iResult=Run(nofEvents, 0))<0) SetStatusFlags(kError);
808 HLTError("wrong state %#x, required flags %#x", GetStatusFlags(), kReady);
811 HLTError("missing RunLoader (%p)/RawReader (%p) instance", runLoader, rawReader);
817 int AliHLTSystem::FillESD(int eventNo, AliRunLoader* runLoader, AliESDEvent* esd)
819 // see header file for class documentation
821 if (runLoader || esd) {
822 HLTInfo("Event %d: Run Loader %p, ESD %p", eventNo, runLoader, esd);
823 iResult=AliHLTOfflineInterface::FillComponentESDs(eventNo, runLoader, esd);
825 HLTError("missing run loader/ESD instance(s)");
831 int AliHLTSystem::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd)
833 // see header file for class documentation
835 if (!pHLTOUT) return -EINVAL;
836 HLTDebug("processing %d HLT data blocks", pHLTOUT->GetNofDataBlocks());
838 // add the current HLTOUT task to the collection
840 AliHLTOUT* pTask=dynamic_cast<AliHLTOUT*>(fpHLTOUTTask);
841 if (pTask && (iResult=pTask->Init())>=0) {
842 if (pTask->GetNofDataBlocks()>0) {
843 pHLTOUT->AddSubCollection(pTask);
846 HLTWarning("can not initialize HLTOUT sub collection %s for reconstruction chain (%d), data blocks are lost", pTask?fpHLTOUTTask->GetName():"nil", iResult);
853 // process all kChain handlers first
855 if ((iResult=ProcessHLTOUTkChain(pHLTOUT))<0) {
856 HLTWarning("Processing of kChain-type data blocks failed with error code %d", iResult);
861 fpEsdHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
862 if (!fpProprietaryHandlers)
863 fpProprietaryHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
865 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pEsdHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpEsdHandlers);
866 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pProprietaryHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpProprietaryHandlers);
867 if (!pEsdHandlers || !pProprietaryHandlers) return -ENOMEM;
869 // invalidate all blocks
870 AliHLTOUT::InvalidateBlocks(*pEsdHandlers);
871 AliHLTOUT::InvalidateBlocks(*pProprietaryHandlers);
873 // first come first serve: the ESD of the first handler is also filled into
874 // the main ESD. Has to be changed later.
875 // currently, merging to the provided ESDs crashes at the level of the
876 // TTree::Fill in AliReconstruction, furthermore, the wrong ESD is passed
878 AliESDEvent* pMasterESD=NULL;
881 AliHLTComponentDataTypeList esdBlocks;
883 for (iResult=pHLTOUT->SelectFirstDataBlock();
885 iResult=pHLTOUT->SelectNextDataBlock()) {
886 AliHLTComponentDataType dt=kAliHLTVoidDataType;
887 AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
888 pHLTOUT->GetDataBlockDescription(dt, spec);
889 AliHLTOUTHandler* pHandler=pHLTOUT->GetHandler();
890 AliHLTModuleAgent::AliHLTOUTHandlerType handlerType=pHLTOUT->GetDataBlockHandlerType();
892 // default handling for ESD data blocks does not require an explicite handler
893 if (!pHandler && (dt==kAliHLTDataTypeESDObject || dt==kAliHLTDataTypeESDTree)) {
894 handlerType=AliHLTModuleAgent::kEsd;
896 const char* pMsg="invalid";
897 switch (handlerType) {
898 case AliHLTModuleAgent::kEsd:
901 // schedule for later processing
902 pHLTOUT->InsertHandler(*pEsdHandlers, pHLTOUT->GetDataBlockHandlerDesc());
904 AliHLTComponentDataTypeList::iterator element=esdBlocks.begin();
905 for (; element!=esdBlocks.end(); element++) {
907 HLTWarning("multiple ESDs of identical data type %s, please add appropriate handler to merge ESDs", AliHLTComponent::DataType2Text(dt).c_str());
911 if (element==esdBlocks.end()) esdBlocks.push_back(dt);
914 const AliHLTUInt8_t* pBuffer=NULL;
915 AliHLTUInt32_t size=0;
916 if (pHLTOUT->GetDataBuffer(pBuffer, size)>=0) {
917 pHLTOUT->WriteESD(pBuffer, size, dt);
919 pHLTOUT->WriteESD(pBuffer, size, dt, pMasterESD);
922 pHLTOUT->ReleaseDataBuffer(pBuffer);
924 pHLTOUT->MarkDataBlockProcessed();
928 case AliHLTModuleAgent::kRawReader:
929 // handled in the AliRawReaderHLT
931 case AliHLTModuleAgent::kRawStream:
932 HLTWarning("HLTOUT handler type 'kRawStream' not yet implemented: agent %s, data type %s, specification %#x",
933 pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
934 AliHLTComponent::DataType2Text(dt).c_str(), spec);
936 case AliHLTModuleAgent::kChain:
937 HLTWarning("HLTOUT handler type 'kChain' has already been processed: agent %s, data type %s, specification %#x\n"
938 "New block of this type added by the chain? Skipping data block ...",
939 pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
940 AliHLTComponent::DataType2Text(dt).c_str(), spec);
942 case AliHLTModuleAgent::kProprietary:
943 HLTDebug("processing proprietary data: agent %s, data type %s, specification %#x",
944 pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
945 AliHLTComponent::DataType2Text(dt).c_str(), spec);
947 AliHLTOUT::AliHLTOUTLockGuard g(pHLTOUT);
948 int res=pHandler->ProcessData(pHLTOUT);
950 HLTWarning("processing proprietary data failed (%d): agent %s, data type %s, specification %#x",
951 res, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
952 AliHLTComponent::DataType2Text(dt).c_str(), spec);
956 case AliHLTModuleAgent::kUnknownOutput:
958 // fall trough intended
960 HLTWarning("%s handler type: agent %s, data type %s, specification %#x, ... skipping data block",
961 pMsg, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
962 AliHLTComponent::DataType2Text(dt).c_str(), spec);
965 // TODO: the return value of SelectFirst/NextDataBlock must be
966 // changed in order to avoid this check
967 if (iResult==-ENOENT) iResult=0;
969 AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator handler;
971 // process and write all esd data blocks
972 for (handler=pEsdHandlers->begin(); handler!=pEsdHandlers->end() && iResult>=0; handler++) {
973 AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));
974 AliHLTOUTHandler* pHandler=*handler;
975 const AliHLTUInt8_t* pBuffer=NULL;
976 AliHLTUInt32_t size=0;
977 pHandler->ProcessData(pHLTOUT);
978 if ((size=pHandler->GetProcessedData(pBuffer))>0) {
979 AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*handler;
980 AliHLTComponentDataType dt=desc;
981 pHLTOUT->WriteESD(pBuffer, size, dt);
983 pHLTOUT->WriteESD(pBuffer, size, dt, pMasterESD);
986 pHandler->ReleaseProcessedData(pBuffer, size);
988 pHLTOUT->MarkDataBlocksProcessed(&(*handler));
991 // process all kProprietary data blocks
992 for (handler=pProprietaryHandlers->begin(); handler!=pProprietaryHandlers->end() && iResult>=0; handler++) {
993 AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));
994 AliHLTOUTHandler* pHandler=*handler;
995 const AliHLTUInt8_t* pBuffer=NULL;
996 AliHLTUInt32_t size=0;
997 pHandler->ProcessData(pHLTOUT);
998 if ((size=pHandler->GetProcessedData(pBuffer))>0) {
999 HLTWarning("data produced by kProprietary handler ignored");
1000 pHandler->ReleaseProcessedData(pBuffer, size);
1002 pHLTOUT->MarkDataBlocksProcessed(&(*handler));
1005 // remove all empty handlers form the list (handlers which did not get a block this time)
1006 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pEsdHandlers);
1007 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pProprietaryHandlers);
1012 int AliHLTSystem::ProcessHLTOUTkChain(AliHLTOUT* pHLTOUT)
1014 // see header file for class documentation
1016 if (!pHLTOUT) return -EINVAL;
1018 if (!fpChainHandlers)
1019 fpChainHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
1021 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pChainHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpChainHandlers);
1022 if (!pChainHandlers) return -ENOMEM;
1024 // invalidate all blocks
1025 AliHLTOUT::InvalidateBlocks(*pChainHandlers);
1028 pHLTOUT->FillHandlerList(*pChainHandlers, AliHLTModuleAgent::kChain);
1030 // process all defined chain handlers
1031 AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator chainHandler;
1032 for (chainHandler=pChainHandlers->begin(); chainHandler!=pChainHandlers->end() && iResult>=0; chainHandler++) {
1033 if (chainHandler->IsEmpty()) continue;
1034 AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*chainHandler));
1035 AliHLTOUTHandler* pHandler=*chainHandler;
1036 const AliHLTUInt8_t* pBuffer=NULL;
1037 AliHLTUInt32_t size=0;
1038 pHandler->ProcessData(pHLTOUT);
1039 if ((size=pHandler->GetProcessedData(pBuffer))>0) {
1040 AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*chainHandler;
1041 AliHLTComponentDataType dt=desc;
1043 pHandler->ReleaseProcessedData(pBuffer, size);
1045 pHLTOUT->MarkDataBlocksProcessed(&(*chainHandler));
1048 // remove all empty handlers form the list (handlers which did not get a block this time)
1049 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pChainHandlers);
1054 int AliHLTSystem::LoadComponentLibraries(const char* libraries)
1056 // see header file for class documentation
1059 if (fpComponentHandler) {
1060 TString libs(libraries);
1061 TObjArray* pTokens=libs.Tokenize(" ");
1063 int iEntries=pTokens->GetEntries();
1064 for (int i=0; i<iEntries && iResult>=0; i++) {
1065 iResult=fpComponentHandler->LoadLibrary((((TObjString*)pTokens->At(i))->GetString()).Data());
1070 SetStatusFlags(kLibrariesLoaded);
1072 // lets see if we need this, probably not
1073 //fpComponentHandler->UnloadLibraries();
1074 ClearStatusFlags(kLibrariesLoaded);
1078 HLTFatal("no component handler available");
1086 int AliHLTSystem::Configure(AliRunLoader* runloader)
1088 // see header file for class documentation
1089 return Configure(NULL, runloader);
1092 int AliHLTSystem::Configure(AliRawReader* rawReader, AliRunLoader* runloader)
1094 // see header file for class documentation
1096 if (CheckStatus(kRunning)) {
1097 HLTError("HLT system in running state, can not configure");
1100 ClearStatusFlags(kTaskListCreated);
1101 if (CheckFilter(kHLTLogDebug))
1102 AliHLTModuleAgent::PrintStatus();
1103 if (CheckStatus(kConfigurationLoaded)==0) {
1104 iResult=LoadConfigurations(rawReader, runloader);
1106 if (fChains.Length()==0) {
1107 HLTError("custom configuration(s) specified, but no configuration to run in local reconstruction, use \'chains=<chain,...>\' option");
1112 SetStatusFlags(kConfigurationLoaded);
1113 if (CheckFilter(kHLTLogDebug))
1114 fpConfigurationHandler->PrintConfigurations();
1115 iResult=BuildTaskListsFromReconstructionChains(rawReader, runloader);
1117 SetStatusFlags(kTaskListCreated);
1120 if (iResult<0) SetStatusFlags(kError);
1125 int AliHLTSystem::ScanOptions(const char* options)
1127 // see header file for class documentation
1130 //AliHLTComponentHandler::TLibraryMode libMode=AliHLTComponentHandler::kDynamic;
1132 TString alloptions(options);
1133 TObjArray* pTokens=alloptions.Tokenize(" ");
1135 int iEntries=pTokens->GetEntries();
1136 for (int i=0; i<iEntries; i++) {
1137 TString token=(((TObjString*)pTokens->At(i))->GetString());
1138 if (token.Contains("loglevel=")) {
1139 TString param=token.ReplaceAll("loglevel=", "");
1140 if (param.IsDigit()) {
1141 SetGlobalLoggingLevel((AliHLTComponentLogSeverity)param.Atoi());
1142 } else if (param.BeginsWith("0x") &&
1143 param.Replace(0,2,"",0).IsHex()) {
1145 sscanf(param.Data(),"%x", &severity);
1146 SetGlobalLoggingLevel((AliHLTComponentLogSeverity)severity);
1148 HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
1150 } else if (token.Contains("frameworklog=")) {
1151 TString param=token.ReplaceAll("frameworklog=", "");
1152 if (param.IsDigit()) {
1153 SetFrameworkLog((AliHLTComponentLogSeverity)param.Atoi());
1154 } else if (param.BeginsWith("0x") &&
1155 param.Replace(0,2,"",0).IsHex()) {
1157 sscanf(param.Data(),"%x", &severity);
1158 SetFrameworkLog((AliHLTComponentLogSeverity)severity);
1160 HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
1162 } else if (token.Contains("alilog=off")) {
1164 } else if (token.Contains("config=")) {
1165 TString param=token.ReplaceAll("config=", "");
1167 gROOT->Macro(param.Data(), &error);
1169 SetStatusFlags(kConfigurationLoaded);
1171 HLTError("can not execute macro \'%s\'", param.Data());
1174 } else if (token.Contains("chains=")) {
1175 TString param=token.ReplaceAll("chains=", "");
1176 fChains=param.ReplaceAll(",", " ");
1177 if (fChains.IsNull()) fChains=" "; // disable all chains
1178 } else if (token.Contains("libmode=")) {
1179 TString param=token.ReplaceAll("libmode=", "");
1180 param.ReplaceAll(",", " ");
1181 if (fpComponentHandler) {
1182 if (param.CompareTo("static")==0) {
1183 fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kStatic);
1184 } else if (param.CompareTo("dynamic")==0) {
1185 fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kDynamic);
1187 HLTWarning("wrong argument for option \'libmode=\', use \'static\' or \'dynamic\'");
1190 } else if (token.BeginsWith("lib") && token.EndsWith(".so")) {
1194 HLTWarning("unknown option \'%s\'", token.Data());
1201 if (libs.IsNull()) {
1202 const char** deflib=fgkHLTDefaultLibs;
1208 if ((!CheckStatus(AliHLTSystem::kLibrariesLoaded)) &&
1209 (LoadComponentLibraries(libs.Data())<0)) {
1210 HLTError("error while loading HLT libraries");
1218 int AliHLTSystem::Reset(int bForce)
1220 // see header file for class documentation
1222 if (!bForce && CheckStatus(kRunning)) {
1223 HLTError("HLT system in running state, can not configure");
1227 ClearStatusFlags(~kUninitialized);
1231 int AliHLTSystem::LoadConfigurations(AliRawReader* rawReader, AliRunLoader* runloader)
1233 // see header file for class documentation
1234 if (CheckStatus(kRunning)) {
1235 HLTError("HLT system in running state, can not configure");
1239 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
1241 while (pAgent && iResult>=0) {
1242 const char* deplibs=pAgent->GetRequiredComponentLibraries();
1244 HLTDebug("required libraries \'%s\' for agent %s (%p)", deplibs, pAgent->GetName(), pAgent);
1248 HLTDebug("load configurations for agent %s (%p)", pAgent->GetName(), pAgent);
1249 pAgent->CreateConfigurations(fpConfigurationHandler, rawReader, runloader);
1250 pAgent=AliHLTModuleAgent::GetNextAgent();
1254 iResult=LoadComponentLibraries(extralibs.Data());
1260 int AliHLTSystem::BuildTaskListsFromReconstructionChains(AliRawReader* rawReader, AliRunLoader* runloader)
1262 // see header file for class documentation
1263 if (CheckStatus(kRunning)) {
1264 HLTError("HLT system in running state, can not configure");
1267 if (!CheckStatus(kConfigurationLoaded)) {
1268 HLTWarning("configurations not yet loaded");
1277 if (fChains.Length()>0) {
1279 HLTImportant("custom reconstruction chain: %s", chains.Data());
1281 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
1282 while ((pAgent || fChains.Length()>0) && iResult>=0) {
1283 const char* agentchains=pAgent->GetReconstructionChains(rawReader, runloader);
1285 if (!chains.IsNull()) chains+=" ";
1286 chains+=agentchains;
1287 HLTInfo("reconstruction chains for agent %s (%p): %s", pAgent->GetName(), pAgent, agentchains);
1289 pAgent=AliHLTModuleAgent::GetNextAgent();
1293 // build task list for chains
1294 TObjArray* pTokens=chains.Tokenize(" ");
1296 int iEntries=pTokens->GetEntries();
1297 for (int i=0; i<iEntries && iResult>=0; i++) {
1298 const char* pCID=((TObjString*)pTokens->At(i))->GetString().Data();
1299 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(pCID);
1301 iResult=BuildTaskList(pConf);
1302 if (true) { // condition was deprecated but kept for sake of svn diff
1303 // bHaveOutput variable has to be set for both running modes
1304 // AliHLTSimulation and AliHLTReconstruction
1305 assert(fpComponentHandler!=NULL);
1306 TString cid=pConf->GetComponentID();
1307 if (runloader!=NULL && cid.CompareTo("HLTOUT")==0) {
1308 // remove from the input of a global HLTOUT configuration
1309 chains.ReplaceAll(pCID, "");
1310 } else if (bHaveOutput==0) {
1311 // check whether this configuration produces data output
1312 if ((bHaveOutput=fpComponentHandler->HasOutputData(cid.Data()))<0) {
1314 chains.ReplaceAll(pCID, "");
1319 HLTWarning("can not find configuration %s", pCID);
1325 // build HLTOUT for simulation
1326 if (iResult>=0 && runloader) {
1328 // there are components in the chain which produce data which need to be
1329 // piped to an HLTOUT
1330 if (fpComponentHandler->FindComponentIndex("HLTOUT")>=0 ||
1331 fpComponentHandler->LoadLibrary("libHLTsim.so")>=0) {
1332 AliHLTConfiguration globalout("_globalout_", "HLTOUT", chains.Data(), NULL);
1333 iResult=BuildTaskList("_globalout_");
1335 HLTError("can not load libHLTsim.so and HLTOUT component");
1341 // build HLTOUT task for reconstruction
1342 // Matthias 08.07.2008 the rawReader is never set when running embedded into
1343 // AliReconstruction. The system is configured during AliHLTReconstructor::Init
1344 // where the RawReader is not available. It is available in the first invocation
1347 // That means that policy is slightly changed:
1348 // - if the run loader is available -> AliSimulation
1349 // - no run loader available -> AliReconstruction
1350 if (iResult>=0 && !runloader) {
1352 // there are components in the chain which produce data which need to be
1353 // piped to an HLTOUT sub-collection
1354 if (!fpHLTOUTTask) {
1355 iResult=AddHLTOUTTask(chains.Data());
1360 if (iResult>=0) SetStatusFlags(kTaskListCreated);
1365 int AliHLTSystem::AddHLTOUTTask(const char* hltoutchains)
1367 // see header file for class documentation
1369 if (!hltoutchains || hltoutchains[0]==0) return 0;
1371 // check chains for output
1372 TString chains=hltoutchains;
1373 TObjArray* pTokens=chains.Tokenize(" ");
1375 int iEntries=pTokens->GetEntries();
1376 for (int i=0; i<iEntries && iResult>=0; i++) {
1377 const char* token=((TObjString*)pTokens->At(i))->GetString().Data();
1378 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(token);
1380 TString cid=pConf->GetComponentID();
1381 if (fpComponentHandler->HasOutputData(cid.Data())) {
1385 HLTWarning("can not find configuration %s", token);
1387 // remove from the list of hltout chains
1388 chains.ReplaceAll(token, "");
1393 // do not create the HLTOUT task if none of the chains have output
1394 if (chains.IsNull()) return 0;
1396 // indicate the task to be available
1400 if (strcmp(chains.Data(), fpHLTOUTTask->GetSourceChains())==0) {
1401 HLTWarning("HLTOUT task already added for chains \"%s\" %p", chains.Data(), fpHLTOUTTask);
1403 HLTError("HLTOUT task already added for chains \"%s\" %p, ignoring new chains \"%s\"",
1404 fpHLTOUTTask->GetSourceChains(), fpHLTOUTTask, chains.Data());
1409 fpHLTOUTTask=new AliHLTOUTTask(chains);
1411 if (fpHLTOUTTask->GetConf() && fpHLTOUTTask->GetConf()->SourcesResolved()>=0) {
1412 iResult=InsertTask(fpHLTOUTTask);
1414 HLTError("HLTOUT task (%s) sources not resolved", fpHLTOUTTask->GetName());
1419 delete fpHLTOUTTask;
1428 int AliHLTSystem::CheckStatus(int flag)
1430 // see header file for class documentation
1431 if (flag==kUninitialized && flag==fState) return 1;
1432 if ((fState&flag)==flag) return 1;
1436 int AliHLTSystem::GetStatusFlags()
1438 // see header file for class documentation
1442 int AliHLTSystem::SetStatusFlags(int flags)
1444 // see header file for class documentation
1449 int AliHLTSystem::ClearStatusFlags(int flags)
1451 // see header file for class documentation
1456 void* AliHLTSystem::FindDynamicSymbol(const char* library, const char* symbol)
1458 // see header file for class documentation
1459 if (fpComponentHandler==NULL) return NULL;
1460 return fpComponentHandler->FindSymbol(library, symbol);
1463 void AliHLTSystem::SetFrameworkLog(AliHLTComponentLogSeverity level)
1465 // see header file for class documentation
1466 SetLocalLoggingLevel(level);
1467 if (fpComponentHandler) fpComponentHandler->SetLocalLoggingLevel(level);
1468 if (fpConfigurationHandler) fpConfigurationHandler->SetLocalLoggingLevel(level);