Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AlgorithmManager.cpp
Go to the documentation of this file.
1 // Include files
2 #include "AlgorithmManager.h"
7 #include "GaudiKernel/System.h"
9 #include <Gaudi/Algorithm.h>
10 #include <iostream>
11 #ifndef _WIN32
12 # include <errno.h>
13 #endif
14 
16 static SmartIF<IAlgorithm> no_algorithm;
17 
18 // constructor
19 AlgorithmManager::AlgorithmManager( IInterface* application ) : base_class( application, IAlgorithm::interfaceID() ) {
20  addRef(); // Initial count set to 1
21 }
22 
23 // addAlgorithm
25  m_algs.push_back( alg );
26  return StatusCode::SUCCESS;
27 }
28 
29 // removeAlgorithm
31  auto it = std::find( m_algs.begin(), m_algs.end(), alg );
32  if ( it != m_algs.end() ) {
33  m_algs.erase( it );
34  return StatusCode::SUCCESS;
35  }
36  return StatusCode::FAILURE;
37 }
38 
39 // createService
41  IAlgorithm*& algorithm, bool managed, bool checkIfExists ) {
42  // Check is the algorithm is already existing
43  if ( checkIfExists ) {
44  if ( existsAlgorithm( algname ) ) {
45  // return an error because an algorithm with that name already exists
46  return StatusCode::FAILURE;
47  }
48  }
49  std::string actualalgtype( algtype );
50  // a '\' in front of the type name prevents alias replacement
51  if ( ( actualalgtype.size() > 8 ) && ( actualalgtype.compare( 0, 8, "unalias:" ) == 0 ) ) {
52  actualalgtype = actualalgtype.substr( 8 );
53  } else {
54  auto typeAlias = m_algTypeAliases.find( algtype );
55  if ( typeAlias != m_algTypeAliases.end() ) { actualalgtype = typeAlias->second; }
56  }
57  algorithm = Gaudi::Algorithm::Factory::create( actualalgtype, algname, serviceLocator().get() ).release();
58  if ( !algorithm ) {
59  this->error() << "Algorithm of type " << actualalgtype << " is unknown (No factory available)." << endmsg;
60 #ifndef _WIN32
61  errno =
62  0xAFFEDEAD; // code used by Gaudi for library load errors: forces getLastErrorString do use dlerror (on Linux)
63 #endif
65  if ( !err.empty() ) this->error() << err << endmsg;
66  this->error() << "More information may be available by setting the global jobOpt \"PluginDebugLevel\" to 1"
67  << endmsg;
68  return StatusCode::FAILURE;
69  }
70  // Check the compatibility of the version of the interface obtained
71  if ( !isValidInterface( algorithm ) ) {
72  fatal() << "Incompatible interface IAlgorithm version for " << actualalgtype << endmsg;
73  return StatusCode::FAILURE;
74  }
75  m_algs.emplace_back( algorithm, managed );
76  // let the algorithm know its type
77  algorithm->setType( algtype );
78  // this is needed to keep the reference count correct, since isValidInterface(algorithm)
79  // implies an increment of the counter by 1
80  algorithm->release();
81  StatusCode rc;
82  if ( managed ) {
83  // Bring the created algorithm to the target state of the ApplicationMgr
85  rc = algorithm->sysInitialize();
86  if ( rc.isSuccess() && targetFSMState() >= Gaudi::StateMachine::RUNNING ) { rc = algorithm->sysStart(); }
87  }
88  if ( !rc.isSuccess() ) { this->error() << "Failed to initialize algorithm: [" << algname << "]" << endmsg; }
89  }
90  return rc;
91 }
92 
94  auto it = std::find( m_algs.begin(), m_algs.end(), typeName.name() );
95  if ( it != m_algs.end() ) { // found
96  return it->algorithm;
97  }
98  if ( createIf ) {
99  IAlgorithm* alg;
100  if ( createAlgorithm( typeName.type(), typeName.name(), alg, true ).isSuccess() ) {
101  return algorithm( typeName, false );
102  }
103  }
104  return no_algorithm;
105 }
106 
107 // existsAlgorithm
109  return m_algs.end() != std::find( m_algs.begin(), m_algs.end(), name );
110 }
111 
112 // Return the list of Algorithms
115  m_listOfPtrs.reserve( m_algs.size() );
117  []( const AlgorithmItem& alg ) { return alg.algorithm.get(); } );
118  return m_listOfPtrs;
119 }
120 
122  StatusCode rc;
123  for ( auto& it : m_algs ) {
124  if ( !it.managed || it.algorithm->FSMState() >= Gaudi::StateMachine::INITIALIZED ) continue;
125  rc = it.algorithm->sysInitialize();
126  if ( rc.isFailure() ) return rc;
127  }
128  return rc;
129 }
130 
132  StatusCode rc;
133  for ( auto& it : m_algs ) {
134  if ( !it.managed || it.algorithm->FSMState() >= Gaudi::StateMachine::RUNNING ) continue;
135  rc = it.algorithm->sysStart();
136  if ( rc.isFailure() ) return rc;
137  }
138  return rc;
139 }
140 
142  StatusCode rc;
143  for ( auto& it : m_algs ) {
144  if ( !it.managed ) continue;
145  rc = it.algorithm->sysStop();
146  if ( rc.isFailure() ) return rc;
147  }
148  return rc;
149 }
150 
152  StatusCode rc;
153  auto it = m_algs.begin();
154  while ( it != m_algs.end() ) { // finalize and remove from the list the managed algorithms
155  if ( it->managed ) {
156  rc = it->algorithm->sysFinalize();
157  if ( rc.isFailure() ) return rc;
158  it = m_algs.erase( it );
159  } else {
160  ++it;
161  }
162  }
163  return rc;
164 }
165 
167  StatusCode rc;
168  for ( auto& it : m_algs ) {
169  if ( !it.managed ) continue;
170  rc = it.algorithm->sysReinitialize();
171  if ( rc.isFailure() ) {
172  this->error() << "Unable to re-initialize algorithm: " << it.algorithm->name() << endmsg;
173  return rc;
174  }
175  }
176  return rc;
177 }
178 
181  m_aess = serviceLocator()->service( "AlgExecStateSvc" );
182  if ( !m_aess.isValid() ) {
183  fatal() << "Error retrieving AlgExecStateSvc" << endmsg;
184  return StatusCode::FAILURE;
185  }
186 
187  StatusCode rc;
188 
189  for ( auto& it : m_algs ) {
190  if ( !it.managed ) continue;
191  rc = it.algorithm->sysRestart();
192  m_aess->resetErrorCount( it.algorithm );
193  if ( rc.isFailure() ) {
194  this->error() << "Unable to re-initialize algorithm: " << it.algorithm->name() << endmsg;
195  return rc;
196  }
197  }
198  return rc;
199 }
200 
202  resetMessaging();
203  for ( auto& algItem : m_algs ) {
204  const auto alg = dynamic_cast<Gaudi::Algorithm*>( algItem.algorithm.get() );
205  if ( alg ) alg->resetMessaging();
206  }
207 }
void outputLevelUpdate() override
Function to call to update the outputLevel of the components (after a change in MessageSvc).
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).
T empty(T...args)
StatusCode addAlgorithm(IAlgorithm *alg) override
implementation of IAlgManager::addAlgorithm
StatusCode start() override
Start (from INITIALIZED to RUNNING).
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
const std::vector< IAlgorithm * > & getAlgorithms() const override
implementation of IAlgManager::getAlgorithms
virtual StatusCode sysStart()=0
Startup method invoked by the framework.
AlgTypeAliasesMap m_algTypeAliases
std::vector< AlgorithmItem > m_algs
algorithms maintained by AlgorithmManager
MSG::Level resetMessaging()
Reinitialize internal states.
bool isSuccess() const
Definition: StatusCode.h:267
Gaudi::StateMachine::State targetFSMState() const override
When we are in the middle of a transition, get the state where the transition is leading us...
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
std::vector< IAlgorithm * > m_listOfPtrs
List of pointers to the know services used to implement getAlgorithms()
T end(T...args)
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
bool isValidInterface(I *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:341
bool isFailure() const
Definition: StatusCode.h:130
SmartIF< ISvcLocator > & serviceLocator() const override
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
virtual StatusCode sysInitialize()=0
Initialization method invoked by the framework.
STL class.
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Helper class to parse a string of format "type/name".
bool existsAlgorithm(const std::string &name) const override
implementation of IAlgManager::existsAlgorithm
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
Definition of the basic interface.
Definition: IInterface.h:244
StatusCode removeAlgorithm(IAlgorithm *alg) override
implementation of IAlgManager::removeAlgorithm
T clear(T...args)
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:28
virtual void setType(const std::string &)=0
T find(T...args)
T size(T...args)
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:62
T begin(T...args)
T back_inserter(T...args)
const std::string & type() const
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
constexpr static const auto FAILURE
Definition: StatusCode.h:86
T substr(T...args)
AlgorithmManager(IInterface *iface)
default creator
SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
T transform(T...args)
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition: System.cpp:273
const std::string & name() const
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:21
virtual void resetErrorCount(const IAlgorithm *iAlg)=0
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
T compare(T...args)
StatusCode createAlgorithm(const std::string &algtype, const std::string &algname, IAlgorithm *&algorithm, bool managed=false, bool checkIfExists=true) override
implementation of IAlgManager::createAlgorithm
SmartIF< IAlgorithm > algorithm
T reserve(T...args)
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).