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 <TObjArray.h>
44 #include <TObjString.h>
45 #include <TStopwatch.h>
46 //#include <TSystem.h>
48 //#include <TInterpreter.h>
50 /** HLT default component libraries */
51 const char* AliHLTSystem::fgkHLTDefaultLibs[]= {
55 // "libAliHLTSample.so",
59 "libAliHLTTrigger.so",
63 /** ROOT macro for the implementation of ROOT specific class methods */
64 ClassImp(AliHLTSystem)
66 AliHLTSystem::AliHLTSystem(AliHLTComponentLogSeverity loglevel)
68 fpComponentHandler(AliHLTComponentHandler::CreateHandler()),
69 fpConfigurationHandler(AliHLTConfigurationHandler::CreateHandler()),
73 fStopwatches(new TObjArray),
76 fpChainHandlers(NULL),
78 fpProprietaryHandlers(NULL),
81 // see header file for class documentation
83 // refer to README to build package
85 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
87 if (fgNofInstances++>0) {
88 // July 2008: multiple instances are now allowed
89 // AliHLTSystem is used in multiple instances for the kChain HLTOUT handler
90 //HLTWarning("multiple instances of AliHLTSystem, you should not use more than one at a time");
93 SetGlobalLoggingLevel(loglevel);
94 SetFrameworkLog(loglevel);
95 if (fpComponentHandler) {
96 AliHLTAnalysisEnvironment env;
97 memset(&env, 0, sizeof(AliHLTAnalysisEnvironment));
98 env.fStructSize=sizeof(AliHLTAnalysisEnvironment);
99 env.fAllocMemoryFunc=AliHLTSystem::AllocMemory;
100 env.fLoggingFunc=NULL;
101 fpComponentHandler->SetEnvironment(&env);
102 InitAliLogFunc(fpComponentHandler);
103 if (fgNofInstances==1) {
104 fpComponentHandler->AnnounceVersion();
107 HLTFatal("can not create Component Handler");
109 if (fpConfigurationHandler) {
110 AliHLTConfiguration::GlobalInit(fpConfigurationHandler);
112 HLTFatal("can not create Configuration Handler");
116 AliHLTSystem::~AliHLTSystem()
118 // see header file for class documentation
121 AliHLTConfiguration::GlobalDeinit(fpConfigurationHandler);
122 if (fpConfigurationHandler) {
123 fpConfigurationHandler->Destroy();
125 fpConfigurationHandler=NULL;
127 if (fpComponentHandler) {
128 fpComponentHandler->Destroy();
130 fpComponentHandler=NULL;
133 int AliHLTSystem::fgNofInstances=0;
135 int AliHLTSystem::AddConfiguration(AliHLTConfiguration* pConf)
137 // see header file for class documentation
138 HLTLogKeyword("configuration handling");
141 HLTError("function not yet implemented");
149 int AliHLTSystem::InsertConfiguration(AliHLTConfiguration* pConf, AliHLTConfiguration* pPrec)
151 // see header file for class documentation
152 HLTLogKeyword("configuration handling");
157 HLTError("function not yet implemented");
166 int AliHLTSystem::DeleteConfiguration(AliHLTConfiguration* pConf)
168 // see header file for class documentation
169 HLTLogKeyword("configuration handling");
172 HLTError("function not yet implemented");
180 int AliHLTSystem::BuildTaskList(const char* id)
182 // see header file for class documentation
185 if (fpConfigurationHandler) {
186 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(id);
188 iResult=BuildTaskList(pConf);
190 HLTError("unknown configuration \"%s\"", id);
202 int AliHLTSystem::BuildTaskList(AliHLTConfiguration* pConf)
204 // see header file for class documentation
207 AliHLTTask* pTask=NULL;
208 if ((pTask=FindTask(pConf->GetName()))!=NULL) {
209 if (pTask->GetConf()!=pConf) {
210 HLTError("configuration mismatch, there is already a task with configuration name \"%s\", but it is different. Most likely configuration %p is not registered properly", pConf->GetName(), pConf);
213 // task for this configuration exists, terminate
215 } else if (pConf->SourcesResolved(1)!=1) {
216 HLTError("configuration \"%s\" has unresolved sources, aborting ...", pConf->GetName());
219 pTask=new AliHLTTask(pConf);
223 pTask->SetLocalLoggingLevel(GetLocalLoggingLevel());
226 static int iterationLevel=0;
227 if (pTask && iResult>=0) {
228 // check for circular dependencies
229 if ((iResult=pConf->FollowDependency(pConf->GetName()))>0) {
230 HLTError("detected circular dependency for configuration \"%s\"", pTask->GetName());
231 pTask->PrintDependencyTree(pTask->GetName(), 1/*use the configuration list*/);
232 HLTError("aborted ...");
236 // check whether all dependencies are already in the task list
237 // create the missing ones
238 // this step is an iterative process which calls this function again for the missing
239 // configurations, in order to avoid the currently processed task to be created
240 // again it is added to the list temporarily and removed afterwards
241 // This is of high importance to preserve the order of the tasks. Furthermore, the
242 // InsertTask method has to be used in order to set all the cross links right
243 fTaskList.Add(pTask);
244 AliHLTConfiguration* pDep=pConf->GetFirstSource();
245 while (pDep!=NULL && iResult>=0) {
246 HLTDebug("iteration %d: checking dependency %s (%p)", iterationLevel, pDep->GetName(), pDep);
247 if (FindTask(pDep->GetName())==NULL) {
248 HLTDebug("iteration %d: building task list for configuration %s (%p)", iterationLevel, pDep->GetName(), pDep);
250 iResult=BuildTaskList(pDep);
253 pDep=pConf->GetNextSource();
255 // remove the temporarily added task
256 fTaskList.Remove(pTask);
258 // insert the task and set the cross-links
260 HLTDebug("iteration %d: inserting task %s (%p)", iterationLevel, pTask->GetName(), pTask);
261 iResult=InsertTask(pTask);
274 int AliHLTSystem::CleanTaskList()
276 // see header file for class documentation
279 while ((lnk=fTaskList.LastLink())!=NULL) {
280 delete (lnk->GetObject());
281 fTaskList.Remove(lnk);
286 int AliHLTSystem::InsertTask(AliHLTTask* pTask)
288 // see header file for class documentation
290 TObjLink *lnk = NULL;
291 if ((iResult=pTask->CheckDependencies())>0)
292 lnk=fTaskList.FirstLink();
293 while (lnk && iResult>0) {
294 AliHLTTask* pCurr = (AliHLTTask*)lnk->GetObject();
295 //HLTDebug("checking \"%s\"", pCurr->GetName());
296 iResult=pTask->Depends(pCurr);
298 iResult=pTask->SetDependency(pCurr);
299 pCurr->SetTarget(pTask);
300 HLTDebug("set dependency \"%s\" for configuration \"%s\"", pCurr->GetName(), pTask->GetName());
302 if (pCurr->Depends(pTask)) {
303 // circular dependency
304 HLTError("circular dependency: can not resolve dependencies for configuration \"%s\"", pTask->GetName());
306 } else if ((iResult=pTask->CheckDependencies())>0) {
312 fTaskList.AddAfter(lnk, pTask);
314 fTaskList.AddFirst(pTask);
316 HLTDebug("task \"%s\" (%p) inserted (size %d)", pTask->GetName(), pTask, sizeof(AliHLTTask));
317 } else if (iResult>0) {
318 HLTError("can not resolve dependencies for configuration \"%s\" (%d unresolved)", pTask->GetName(), iResult);
324 AliHLTTask* AliHLTSystem::FindTask(const char* id)
326 // see header file for class documentation
327 AliHLTTask* pTask=NULL;
329 pTask=dynamic_cast<AliHLTTask*>(fTaskList.FindObject(id));
334 void AliHLTSystem::PrintTaskList()
336 // see header file for class documentation
337 HLTLogKeyword("task list");
338 TObjLink *lnk = NULL;
339 HLTMessage("Task List");
340 lnk=fTaskList.FirstLink();
342 TObject* obj=lnk->GetObject();
344 HLTMessage(" %s - status:", obj->GetName());
345 AliHLTTask* pTask=(AliHLTTask*)obj;
346 pTask->PrintStatus();
353 int AliHLTSystem::Run(Int_t iNofEvents, int bStop)
355 // see header file for class documentation
358 SetStatusFlags(kRunning);
359 if (fEventCount>=0 || (iResult=InitTasks())>=0) {
360 if (fEventCount>=0 || (iResult=StartTasks())>=0) {
361 if (fEventCount==0) {
362 InitBenchmarking(fStopwatches);
364 ResumeBenchmarking(fStopwatches);
366 for (int i=fEventCount; i<fEventCount+iNofEvents && iResult>=0; i++) {
368 if (iNofEvents>1 && i==fEventCount) {
369 HLTWarning("can not add more than one event to the HLTOUT, skipping all but last block");
371 // reset and prepare for new data
372 fpHLTOUTTask->Reset();
374 if ((iResult=ProcessTasks(i))>=0) {
378 // TODO: define different running modes to either ignore errors in
379 // event processing or not
384 fEventCount+=iNofEvents;
385 if (bStop) StopTasks();
386 else PauseBenchmarking(fStopwatches);
388 if (bStop) DeinitTasks();
392 } else if (iResult==-126 /*ENOKEY*/) {
393 iResult=0; // do not propagate the error
395 ClearStatusFlags(kRunning);
399 int AliHLTSystem::InitTasks()
401 // see header file for class documentation
403 TObjLink *lnk=fTaskList.FirstLink();
406 HLTWarning("Task list is empty, skipping HLT");
407 return -126 /*ENOKEY*/;
409 while (lnk && iResult>=0) {
410 TObject* obj=lnk->GetObject();
412 AliHLTTask* pTask=(AliHLTTask*)obj;
413 iResult=pTask->Init(NULL, fpComponentHandler);
414 // ProcInfo_t ProcInfo;
415 // gSystem->GetProcInfo(&ProcInfo);
416 // HLTInfo("task %s initialized (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
422 HLTError("can not initialize task list, error %d", iResult);
428 int AliHLTSystem::InitBenchmarking(TObjArray* pStopwatches)
430 // see header file for class documentation
432 if (pStopwatches==NULL) return 0;
434 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
435 TStopwatch* pStopwatch= new TStopwatch;
438 pStopwatches->AddAt(pStopwatch, i);
445 TObjLink *lnk=fTaskList.FirstLink();
446 while (lnk && iResult>=0) {
447 TObject* obj=lnk->GetObject();
449 AliHLTTask* pTask=(AliHLTTask*)obj;
450 AliHLTComponent* pComp=NULL;
451 if (iResult>=0 && (pComp=pTask->GetComponent())!=NULL) {
452 switch (pComp->GetComponentType()) {
453 case AliHLTComponent::kProcessor:
454 pComp->SetStopwatches(pStopwatches);
456 case AliHLTComponent::kSource:
458 // this switch determines whether the time consumption of the
459 // AliHLTComponent base methods should be counted to the input
460 // stopwatch or base stopwatch.
461 //int inputBase=(int)AliHLTComponent::kSWBase;
462 int inputBase=(int)AliHLTComponent::kSWInput;
463 pComp->SetStopwatch(pStopwatches->At(inputBase), AliHLTComponent::kSWBase);
464 pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWInput), AliHLTComponent::kSWDA);
467 case AliHLTComponent::kSink:
469 // this switch determines whether the time consumption of the
470 // AliHLTComponent base methods should be counted to the output
471 // stopwatch or base stopwatch.
472 //int outputBase=(int)AliHLTComponent::kSWBase;
473 int outputBase=(int)AliHLTComponent::kSWOutput;
474 pComp->SetStopwatch(pStopwatches->At(outputBase), AliHLTComponent::kSWBase);
475 pComp->SetStopwatch(pStopwatches->At((int)AliHLTComponent::kSWOutput), AliHLTComponent::kSWDA);
479 HLTWarning("unknown component type %d", (int)pComp->GetComponentType());
489 int AliHLTSystem::PauseBenchmarking(TObjArray* pStopwatches) const
491 // see header file for class documentation
492 if (pStopwatches==NULL) return 0;
494 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
495 if (!pStopwatches->At(i)) continue;
496 TStopwatch* pSw=dynamic_cast<TStopwatch*>(pStopwatches->At(i));
497 if (pSw) pSw->Stop();
502 int AliHLTSystem::ResumeBenchmarking(TObjArray* pStopwatches) const
504 // see header file for class documentation
505 if (pStopwatches==NULL) return 0;
507 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
508 if (!pStopwatches->At(i)) continue;
509 TStopwatch* pSw=dynamic_cast<TStopwatch*>(pStopwatches->At(i));
510 if (pSw) pSw->Continue();
515 int AliHLTSystem::PrintBenchmarking(TObjArray* pStopwatches, int bClean) const
517 // see header file for class documentation
519 if (pStopwatches==NULL) return 0;
521 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
522 if (!dynamic_cast<TStopwatch*>(pStopwatches->At(i))) {
528 if (iInitialized!=0) {
529 HLTImportant("HLT statistics:\n"
530 " base: R:%.3fs C:%.3fs\n"
531 " input: R:%.3fs C:%.3fs\n"
532 " output: R:%.3fs C:%.3fs\n"
533 " event processing : R:%.3fs C:%.3fs"
534 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->RealTime()
535 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWBase))->CpuTime()
536 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->RealTime()
537 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWInput))->CpuTime()
538 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->RealTime()
539 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWOutput))->CpuTime()
540 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->RealTime()
541 , dynamic_cast<TStopwatch*>(pStopwatches->At(AliHLTComponent::kSWDA))->CpuTime()
546 for (int i=0; i<(int)AliHLTComponent::kSWTypeCount; i++) {
547 TObject* pObj=pStopwatches->RemoveAt(i);
548 if (pObj) delete pObj;
554 int AliHLTSystem::StartTasks()
556 // see header file for class documentation
558 TObjLink *lnk=fTaskList.FirstLink();
559 while (lnk && iResult>=0) {
560 TObject* obj=lnk->GetObject();
562 AliHLTTask* pTask=(AliHLTTask*)obj;
563 iResult=pTask->StartRun();
564 // ProcInfo_t ProcInfo;
565 // gSystem->GetProcInfo(&ProcInfo);
566 // HLTInfo("task %s started (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
572 HLTError("can not start task list, error %d", iResult);
574 SetStatusFlags(kStarted);
577 if ((iResult=SendControlEvent(kAliHLTDataTypeSOR))<0) {
578 HLTError("can not send SOR event");
584 int AliHLTSystem::ProcessTasks(Int_t eventNo)
586 // see header file for class documentation
588 HLTDebug("processing event no %d", eventNo);
589 TObjLink *lnk=fTaskList.FirstLink();
590 while (lnk && iResult>=0) {
591 TObject* obj=lnk->GetObject();
593 AliHLTTask* pTask=(AliHLTTask*)obj;
594 iResult=pTask->ProcessTask(eventNo);
595 // ProcInfo_t ProcInfo;
596 // gSystem->GetProcInfo(&ProcInfo);
597 // HLTInfo("task %s processed (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
604 HLTImportant("Event %d successfully finished (%d)", eventNo, iResult);
607 HLTError("Processing of event %d failed (%d)", eventNo, iResult);
613 int AliHLTSystem::StopTasks()
615 // see header file for class documentation
617 if ((iResult=SendControlEvent(kAliHLTDataTypeEOR))<0) {
618 HLTError("can not send EOR event");
620 TObjLink *lnk=fTaskList.FirstLink();
622 TObject* obj=lnk->GetObject();
624 AliHLTTask* pTask=(AliHLTTask*)obj;
625 int locResult=pTask->EndRun();
626 if (iResult>=0 && locResult<0) iResult=locResult;
627 // ProcInfo_t ProcInfo;
628 // gSystem->GetProcInfo(&ProcInfo);
629 // HLTInfo("task %s stopped (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
634 PrintBenchmarking(fStopwatches, 1 /*clean*/);
635 ClearStatusFlags(kStarted);
639 int AliHLTSystem::SendControlEvent(AliHLTComponentDataType dt)
641 // see header file for class documentation
643 // disabled for the moment
647 AliHLTRunDesc runDesc;
648 memset(&runDesc, 0, sizeof(AliHLTRunDesc));
649 runDesc.fStructSize=sizeof(AliHLTRunDesc);
650 HLTDebug("sending event %s, run descriptor %p", AliHLTComponent::DataType2Text(dt).c_str(), &runDesc);
651 TObjLink *lnk=fTaskList.FirstLink();
652 while (lnk && iResult>=0) {
653 TObject* obj=lnk->GetObject();
655 AliHLTTask* pTask=(AliHLTTask*)obj;
656 iResult=pTask->ProcessTask(-1);
661 HLTDebug("event %s done (%d)", AliHLTComponent::DataType2Text(dt).c_str(), iResult);
665 int AliHLTSystem::DeinitTasks()
667 // see header file for class documentation
669 TObjLink *lnk=fTaskList.FirstLink();
670 while (lnk && iResult>=0) {
671 TObject* obj=lnk->GetObject();
673 AliHLTTask* pTask=(AliHLTTask*)obj;
674 iResult=pTask->Deinit();
675 // ProcInfo_t ProcInfo;
676 // gSystem->GetProcInfo(&ProcInfo);
677 // HLTInfo("task %s cleaned (%d), current memory usage %d %d", pTask->GetName(), iResult, ProcInfo.fMemResident, ProcInfo.fMemVirtual);
688 void* AliHLTSystem::AllocMemory( void* /*param*/, unsigned long size )
690 // see header file for class documentation
693 p=(void*)new char[size];
697 log.LoggingVarargs(kHLTLogError, "AliHLTSystem" , "AllocMemory" , __FILE__ , __LINE__ , "exeption during memory allocation" );
702 int AliHLTSystem::Reconstruct(int nofEvents, AliRunLoader* runLoader,
703 AliRawReader* rawReader)
705 // see header file for class documentation
707 if (runLoader || rawReader || nofEvents==0) {
708 if (nofEvents>0) {HLTInfo("Run Loader %p, Raw Reader %p , %d event(s)", runLoader, rawReader, nofEvents);}
709 if (CheckStatus(kReady)) {
711 // special case to close the reconstruction
712 if (!CheckStatus(kError)) {
717 if ((iResult=AliHLTOfflineInterface::SetParamsToComponents(runLoader, rawReader))>=0) {
718 // the system always remains started after event processing, a specific
719 // call with nofEvents==0 is needed to execute the stop sequence
720 if ((iResult=Run(nofEvents, 0))<0) SetStatusFlags(kError);
724 HLTError("wrong state %#x, required flags %#x", GetStatusFlags(), kReady);
727 HLTError("missing RunLoader (%p)/RawReader (%p) instance", runLoader, rawReader);
733 int AliHLTSystem::FillESD(int eventNo, AliRunLoader* runLoader, AliESDEvent* esd)
735 // see header file for class documentation
737 if (runLoader || esd) {
738 HLTInfo("Event %d: Run Loader %p, ESD %p", eventNo, runLoader, esd);
739 iResult=AliHLTOfflineInterface::FillComponentESDs(eventNo, runLoader, esd);
741 HLTError("missing run loader/ESD instance(s)");
747 int AliHLTSystem::ProcessHLTOUT(AliHLTOUT* pHLTOUT, AliESDEvent* esd)
749 // see header file for class documentation
751 if (!pHLTOUT) return -EINVAL;
752 HLTDebug("processing %d HLT data blocks", pHLTOUT->GetNofDataBlocks());
754 // add the current HLTOUT task to the collection
756 AliHLTOUT* pTask=dynamic_cast<AliHLTOUT*>(fpHLTOUTTask);
757 if (pTask && (iResult=pTask->Init())>=0) {
758 if (pTask->GetNofDataBlocks()>0) {
759 pHLTOUT->AddSubCollection(pTask);
762 HLTWarning("can not initialize HLTOUT sub collection %s for reconstruction chain (%d), data blocks are lost", pTask?fpHLTOUTTask->GetName():"nil", iResult);
769 // process all kChain handlers first
771 if ((iResult=ProcessHLTOUTkChain(pHLTOUT))<0) {
772 HLTWarning("Processing of kChain-type data blocks failed with error code %d", iResult);
777 fpEsdHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
778 if (!fpProprietaryHandlers)
779 fpProprietaryHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
781 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pEsdHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpEsdHandlers);
782 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pProprietaryHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpProprietaryHandlers);
783 if (!pEsdHandlers || !pProprietaryHandlers) return -ENOMEM;
785 // invalidate all blocks
786 AliHLTOUT::InvalidateBlocks(*pEsdHandlers);
787 AliHLTOUT::InvalidateBlocks(*pProprietaryHandlers);
789 // first come first serve: the ESD of the first handler is also filled into
790 // the main ESD. Has to be changed later.
791 // currently, merging to the provided ESDs crashes at the level of the
792 // TTree::Fill in AliReconstruction, furthermore, the wrong ESD is passed
794 AliESDEvent* pMasterESD=NULL;
797 AliHLTComponentDataTypeList esdBlocks;
799 for (iResult=pHLTOUT->SelectFirstDataBlock();
801 iResult=pHLTOUT->SelectNextDataBlock()) {
802 AliHLTComponentDataType dt=kAliHLTVoidDataType;
803 AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
804 pHLTOUT->GetDataBlockDescription(dt, spec);
805 AliHLTOUTHandler* pHandler=pHLTOUT->GetHandler();
806 AliHLTModuleAgent::AliHLTOUTHandlerType handlerType=pHLTOUT->GetDataBlockHandlerType();
808 // default handling for ESD data blocks does not require an explicite handler
809 if (!pHandler && (dt==kAliHLTDataTypeESDObject || dt==kAliHLTDataTypeESDTree)) {
810 handlerType=AliHLTModuleAgent::kEsd;
812 const char* pMsg="invalid";
813 switch (handlerType) {
814 case AliHLTModuleAgent::kEsd:
817 // schedule for later processing
818 pHLTOUT->InsertHandler(*pEsdHandlers, pHLTOUT->GetDataBlockHandlerDesc());
820 AliHLTComponentDataTypeList::iterator element=esdBlocks.begin();
821 for (; element!=esdBlocks.end(); element++) {
823 HLTWarning("multiple ESDs of identical data type %s, please add appropriate handler to merge ESDs", AliHLTComponent::DataType2Text(dt).c_str());
827 if (element==esdBlocks.end()) esdBlocks.push_back(dt);
830 const AliHLTUInt8_t* pBuffer=NULL;
831 AliHLTUInt32_t size=0;
832 if (pHLTOUT->GetDataBuffer(pBuffer, size)>=0) {
833 pHLTOUT->WriteESD(pBuffer, size, dt);
835 pHLTOUT->WriteESD(pBuffer, size, dt, pMasterESD);
838 pHLTOUT->ReleaseDataBuffer(pBuffer);
840 pHLTOUT->MarkDataBlockProcessed();
844 case AliHLTModuleAgent::kRawReader:
845 // handled in the AliRawReaderHLT
847 case AliHLTModuleAgent::kRawStream:
848 HLTWarning("HLTOUT handler type 'kRawStream' not yet implemented: agent %s, data type %s, specification %#x",
849 pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
850 AliHLTComponent::DataType2Text(dt).c_str(), spec);
852 case AliHLTModuleAgent::kChain:
853 HLTWarning("HLTOUT handler type 'kChain' has already been processed: agent %s, data type %s, specification %#x\n"
854 "New block of this type added by the chain? Skipping data block ...",
855 pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
856 AliHLTComponent::DataType2Text(dt).c_str(), spec);
858 case AliHLTModuleAgent::kProprietary:
859 HLTDebug("processing proprietary data: agent %s, data type %s, specification %#x",
860 pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
861 AliHLTComponent::DataType2Text(dt).c_str(), spec);
863 AliHLTOUT::AliHLTOUTLockGuard g(pHLTOUT);
864 int res=pHandler->ProcessData(pHLTOUT);
866 HLTWarning("processing proprietary data failed (%d): agent %s, data type %s, specification %#x",
867 res, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
868 AliHLTComponent::DataType2Text(dt).c_str(), spec);
872 case AliHLTModuleAgent::kUnknownOutput:
874 // fall trough intended
876 HLTWarning("%s handler type: agent %s, data type %s, specification %#x, ... skipping data block",
877 pMsg, pHLTOUT->GetAgent()?pHLTOUT->GetAgent()->GetModuleId():"<invalid>",
878 AliHLTComponent::DataType2Text(dt).c_str(), spec);
881 // TODO: the return value of SelectFirst/NextDataBlock must be
882 // changed in order to avoid this check
883 if (iResult==-ENOENT) iResult=0;
885 AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator handler;
887 // process and write all esd data blocks
888 for (handler=pEsdHandlers->begin(); handler!=pEsdHandlers->end() && iResult>=0; handler++) {
889 AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));
890 AliHLTOUTHandler* pHandler=*handler;
891 const AliHLTUInt8_t* pBuffer=NULL;
892 AliHLTUInt32_t size=0;
893 pHandler->ProcessData(pHLTOUT);
894 if ((size=pHandler->GetProcessedData(pBuffer))>0) {
895 AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*handler;
896 AliHLTComponentDataType dt=desc;
897 pHLTOUT->WriteESD(pBuffer, size, dt);
899 pHLTOUT->WriteESD(pBuffer, size, dt, pMasterESD);
902 pHandler->ReleaseProcessedData(pBuffer, size);
904 pHLTOUT->MarkDataBlocksProcessed(&(*handler));
907 // process all kProprietary data blocks
908 for (handler=pProprietaryHandlers->begin(); handler!=pProprietaryHandlers->end() && iResult>=0; handler++) {
909 AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*handler));
910 AliHLTOUTHandler* pHandler=*handler;
911 const AliHLTUInt8_t* pBuffer=NULL;
912 AliHLTUInt32_t size=0;
913 pHandler->ProcessData(pHLTOUT);
914 if ((size=pHandler->GetProcessedData(pBuffer))>0) {
915 HLTWarning("data produced by kProprietary handler ignored");
916 pHandler->ReleaseProcessedData(pBuffer, size);
918 pHLTOUT->MarkDataBlocksProcessed(&(*handler));
921 // remove all empty handlers form the list (handlers which did not get a block this time)
922 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pEsdHandlers);
923 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pProprietaryHandlers);
928 int AliHLTSystem::ProcessHLTOUTkChain(AliHLTOUT* pHLTOUT)
930 // see header file for class documentation
932 if (!pHLTOUT) return -EINVAL;
934 if (!fpChainHandlers)
935 fpChainHandlers=new AliHLTOUT::AliHLTOUTHandlerListEntryVector;
937 AliHLTOUT::AliHLTOUTHandlerListEntryVector* pChainHandlers=reinterpret_cast<AliHLTOUT::AliHLTOUTHandlerListEntryVector*>(fpChainHandlers);
938 if (!pChainHandlers) return -ENOMEM;
940 // invalidate all blocks
941 AliHLTOUT::InvalidateBlocks(*pChainHandlers);
944 pHLTOUT->FillHandlerList(*pChainHandlers, AliHLTModuleAgent::kChain);
946 // process all defined chain handlers
947 AliHLTOUT::AliHLTOUTHandlerListEntryVector::iterator chainHandler;
948 for (chainHandler=pChainHandlers->begin(); chainHandler!=pChainHandlers->end() && iResult>=0; chainHandler++) {
949 if (chainHandler->IsEmpty()) continue;
950 AliHLTOUT::AliHLTOUTSelectionGuard g(pHLTOUT, &(*chainHandler));
951 AliHLTOUTHandler* pHandler=*chainHandler;
952 const AliHLTUInt8_t* pBuffer=NULL;
953 AliHLTUInt32_t size=0;
954 pHandler->ProcessData(pHLTOUT);
955 if ((size=pHandler->GetProcessedData(pBuffer))>0) {
956 AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*chainHandler;
957 AliHLTComponentDataType dt=desc;
959 pHandler->ReleaseProcessedData(pBuffer, size);
961 pHLTOUT->MarkDataBlocksProcessed(&(*chainHandler));
964 // remove all empty handlers form the list (handlers which did not get a block this time)
965 AliHLTOUT::RemoveEmptyDuplicateHandlers(*pChainHandlers);
970 int AliHLTSystem::LoadComponentLibraries(const char* libraries)
972 // see header file for class documentation
975 if (fpComponentHandler) {
976 TString libs(libraries);
977 TObjArray* pTokens=libs.Tokenize(" ");
979 int iEntries=pTokens->GetEntries();
980 for (int i=0; i<iEntries && iResult>=0; i++) {
981 iResult=fpComponentHandler->LoadLibrary((((TObjString*)pTokens->At(i))->GetString()).Data());
986 SetStatusFlags(kLibrariesLoaded);
988 // lets see if we need this, probably not
989 //fpComponentHandler->UnloadLibraries();
990 ClearStatusFlags(kLibrariesLoaded);
994 HLTFatal("no component handler available");
1002 int AliHLTSystem::Configure(AliRunLoader* runloader)
1004 // see header file for class documentation
1005 return Configure(NULL, runloader);
1008 int AliHLTSystem::Configure(AliRawReader* rawReader, AliRunLoader* runloader)
1010 // see header file for class documentation
1012 if (CheckStatus(kRunning)) {
1013 HLTError("HLT system in running state, can not configure");
1016 ClearStatusFlags(kTaskListCreated);
1017 if (CheckFilter(kHLTLogDebug))
1018 AliHLTModuleAgent::PrintStatus();
1019 if (CheckStatus(kConfigurationLoaded)==0) {
1020 iResult=LoadConfigurations(rawReader, runloader);
1022 if (fChains.Length()==0) {
1023 HLTError("custom configuration(s) specified, but no configuration to run in local reconstruction, use \'chains=<chain,...>\' option");
1028 SetStatusFlags(kConfigurationLoaded);
1029 if (CheckFilter(kHLTLogDebug))
1030 fpConfigurationHandler->PrintConfigurations();
1031 iResult=BuildTaskListsFromReconstructionChains(rawReader, runloader);
1033 SetStatusFlags(kTaskListCreated);
1036 if (iResult<0) SetStatusFlags(kError);
1041 int AliHLTSystem::ScanOptions(const char* options)
1043 // see header file for class documentation
1046 //AliHLTComponentHandler::TLibraryMode libMode=AliHLTComponentHandler::kDynamic;
1048 TString alloptions(options);
1049 TObjArray* pTokens=alloptions.Tokenize(" ");
1051 int iEntries=pTokens->GetEntries();
1052 for (int i=0; i<iEntries; i++) {
1053 TString token=(((TObjString*)pTokens->At(i))->GetString());
1054 if (token.Contains("loglevel=")) {
1055 TString param=token.ReplaceAll("loglevel=", "");
1056 if (param.IsDigit()) {
1057 SetGlobalLoggingLevel((AliHLTComponentLogSeverity)param.Atoi());
1058 } else if (param.BeginsWith("0x") &&
1059 param.Replace(0,2,"",0).IsHex()) {
1061 sscanf(param.Data(),"%x", &severity);
1062 SetGlobalLoggingLevel((AliHLTComponentLogSeverity)severity);
1064 HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
1066 } else if (token.Contains("frameworklog=")) {
1067 TString param=token.ReplaceAll("frameworklog=", "");
1068 if (param.IsDigit()) {
1069 SetFrameworkLog((AliHLTComponentLogSeverity)param.Atoi());
1070 } else if (param.BeginsWith("0x") &&
1071 param.Replace(0,2,"",0).IsHex()) {
1073 sscanf(param.Data(),"%x", &severity);
1074 SetFrameworkLog((AliHLTComponentLogSeverity)severity);
1076 HLTWarning("wrong parameter for option \'loglevel=\', (hex) number expected");
1078 } else if (token.Contains("alilog=off")) {
1080 } else if (token.Contains("config=")) {
1081 TString param=token.ReplaceAll("config=", "");
1083 gROOT->Macro(param.Data(), &error);
1085 SetStatusFlags(kConfigurationLoaded);
1087 HLTError("can not execute macro \'%s\'", param.Data());
1090 } else if (token.Contains("chains=")) {
1091 TString param=token.ReplaceAll("chains=", "");
1092 fChains=param.ReplaceAll(",", " ");
1093 if (fChains.IsNull()) fChains=" "; // disable all chains
1094 } else if (token.Contains("libmode=")) {
1095 TString param=token.ReplaceAll("libmode=", "");
1096 param.ReplaceAll(",", " ");
1097 if (fpComponentHandler) {
1098 if (param.CompareTo("static")==0) {
1099 fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kStatic);
1100 } else if (param.CompareTo("dynamic")==0) {
1101 fpComponentHandler->SetLibraryMode(AliHLTComponentHandler::kDynamic);
1103 HLTWarning("wrong argument for option \'libmode=\', use \'static\' or \'dynamic\'");
1106 } else if (token.BeginsWith("lib") && token.EndsWith(".so")) {
1110 HLTWarning("unknown option \'%s\'", token.Data());
1117 if (libs.IsNull()) {
1118 const char** deflib=fgkHLTDefaultLibs;
1124 if ((!CheckStatus(AliHLTSystem::kLibrariesLoaded)) &&
1125 (LoadComponentLibraries(libs.Data())<0)) {
1126 HLTError("error while loading HLT libraries");
1134 int AliHLTSystem::Reset(int bForce)
1136 // see header file for class documentation
1138 if (!bForce && CheckStatus(kRunning)) {
1139 HLTError("HLT system in running state, can not configure");
1143 ClearStatusFlags(~kUninitialized);
1147 int AliHLTSystem::LoadConfigurations(AliRawReader* rawReader, AliRunLoader* runloader)
1149 // see header file for class documentation
1150 if (CheckStatus(kRunning)) {
1151 HLTError("HLT system in running state, can not configure");
1155 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
1157 while (pAgent && iResult>=0) {
1158 const char* deplibs=pAgent->GetRequiredComponentLibraries();
1160 HLTDebug("required libraries \'%s\' for agent %s (%p)", deplibs, pAgent->GetName(), pAgent);
1164 HLTDebug("load configurations for agent %s (%p)", pAgent->GetName(), pAgent);
1165 pAgent->CreateConfigurations(fpConfigurationHandler, rawReader, runloader);
1166 pAgent=AliHLTModuleAgent::GetNextAgent();
1170 iResult=LoadComponentLibraries(extralibs.Data());
1176 int AliHLTSystem::BuildTaskListsFromReconstructionChains(AliRawReader* rawReader, AliRunLoader* runloader)
1178 // see header file for class documentation
1179 if (CheckStatus(kRunning)) {
1180 HLTError("HLT system in running state, can not configure");
1183 if (!CheckStatus(kConfigurationLoaded)) {
1184 HLTWarning("configurations not yet loaded");
1193 if (fChains.Length()>0) {
1195 HLTImportant("custom reconstruction chain: %s", chains.Data());
1197 AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent();
1198 while ((pAgent || fChains.Length()>0) && iResult>=0) {
1199 const char* agentchains=pAgent->GetReconstructionChains(rawReader, runloader);
1201 if (!chains.IsNull()) chains+=" ";
1202 chains+=agentchains;
1203 HLTInfo("reconstruction chains for agent %s (%p): %s", pAgent->GetName(), pAgent, agentchains);
1205 pAgent=AliHLTModuleAgent::GetNextAgent();
1209 // build task list for chains
1210 TObjArray* pTokens=chains.Tokenize(" ");
1212 int iEntries=pTokens->GetEntries();
1213 for (int i=0; i<iEntries && iResult>=0; i++) {
1214 const char* pCID=((TObjString*)pTokens->At(i))->GetString().Data();
1215 AliHLTConfiguration* pConf=fpConfigurationHandler->FindConfiguration(pCID);
1217 iResult=BuildTaskList(pConf);
1218 if (true) { // condition was deprecated but kept for sake of svn diff
1219 // bHaveOutput variable has to be set for both running modes
1220 // AliHLTSimulation and AliHLTReconstruction
1221 assert(fpComponentHandler!=NULL);
1222 TString cid=pConf->GetComponentID();
1223 if (runloader!=NULL && cid.CompareTo("HLTOUT")==0) {
1224 // remove from the input of a global HLTOUT configuration
1225 chains.ReplaceAll(pCID, "");
1226 } else if (bHaveOutput==0) {
1227 // check whether this configuration produces data output
1228 if ((bHaveOutput=fpComponentHandler->HasOutputData(cid.Data()))<0) {
1230 chains.ReplaceAll(pCID, "");
1235 HLTWarning("can not find configuration %s", pCID);
1241 // build HLTOUT for simulation
1242 if (iResult>=0 && runloader) {
1244 // there are components in the chain which produce data which need to be
1245 // piped to an HLTOUT
1246 if (fpComponentHandler->FindComponentIndex("HLTOUT")>=0 ||
1247 fpComponentHandler->LoadLibrary("libHLTsim.so")>=0) {
1248 AliHLTConfiguration globalout("_globalout_", "HLTOUT", chains.Data(), NULL);
1249 iResult=BuildTaskList("_globalout_");
1251 HLTError("can not load libHLTsim.so and HLTOUT component");
1257 // build HLTOUT task for reconstruction
1258 // Matthias 08.07.2008 the rawReader is never set when running embedded into
1259 // AliReconstruction. The system is configured during AliHLTReconstructor::Init
1260 // where the RawReader is not available. It is available in the first invocation
1263 // That means that policy is slightly changed:
1264 // - if the run loader is available -> AliSimulation
1265 // - no run loader available -> AliReconstruction
1266 if (iResult>=0 && !runloader) {
1268 // there are components in the chain which produce data which need to be
1269 // piped to an HLTOUT sub-collection
1270 if (!fpHLTOUTTask) {
1271 fpHLTOUTTask=new AliHLTOUTTask(chains.Data());
1273 if (fpHLTOUTTask->GetConf() && fpHLTOUTTask->GetConf()->SourcesResolved()>=0) {
1274 iResult=InsertTask(fpHLTOUTTask);
1276 HLTError("HLTOUT task (%s) sources not resolved", fpHLTOUTTask->GetName());
1284 if (iResult>=0) SetStatusFlags(kTaskListCreated);
1289 int AliHLTSystem::CheckStatus(int flag)
1291 // see header file for class documentation
1292 if (flag==kUninitialized && flag==fState) return 1;
1293 if ((fState&flag)==flag) return 1;
1297 int AliHLTSystem::GetStatusFlags()
1299 // see header file for class documentation
1303 int AliHLTSystem::SetStatusFlags(int flags)
1305 // see header file for class documentation
1310 int AliHLTSystem::ClearStatusFlags(int flags)
1312 // see header file for class documentation
1317 void* AliHLTSystem::FindDynamicSymbol(const char* library, const char* symbol)
1319 // see header file for class documentation
1320 if (fpComponentHandler==NULL) return NULL;
1321 return fpComponentHandler->FindSymbol(library, symbol);
1324 void AliHLTSystem::SetFrameworkLog(AliHLTComponentLogSeverity level)
1326 // see header file for class documentation
1327 SetLocalLoggingLevel(level);
1328 if (fpComponentHandler) fpComponentHandler->SetLocalLoggingLevel(level);
1329 if (fpConfigurationHandler) fpConfigurationHandler->SetLocalLoggingLevel(level);