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"
6 #include "GaudiKernel/System.h"
10 #include <iostream>
11 #ifndef _WIN32
12 #include <errno.h>
13 #endif
14 
16 static SmartIF<IAlgorithm> no_algorithm;
17 
18 // constructor
20  base_class(application, IAlgorithm::interfaceID())
21 {
22  addRef(); // Initial count set to 1
23 }
24 
25 // addAlgorithm
27  m_algs.push_back(alg);
28  return StatusCode::SUCCESS;
29 }
30 
31 // removeAlgorithm
33  auto it = std::find(m_algs.begin(), m_algs.end(), alg);
34  if (it != m_algs.end()) {
35  m_algs.erase(it);
36  return StatusCode::SUCCESS;
37  }
38  return StatusCode::FAILURE;
39 }
40 
41 // createService
43  const std::string& algname,
45  bool managed,
46  bool checkIfExists)
47 {
48  // Check is the algorithm is already existing
49  if (checkIfExists) {
50  if( existsAlgorithm( algname ) ) {
51  // return an error because an algorithm with that name already exists
52  return StatusCode::FAILURE;
53  }
54  }
55  std::string actualalgtype(algtype);
56  // a '\' in front of the type name prevents alias replacement
57  if ((actualalgtype.size() > 8) && (actualalgtype.compare(0, 8,"unalias:") == 0)) {
58  actualalgtype = actualalgtype.substr(8);
59  } else {
60  auto typeAlias = m_algTypeAliases.find(algtype);
61  if (typeAlias != m_algTypeAliases.end()) {
62  actualalgtype = typeAlias->second;
63  }
64  }
65  algorithm = Algorithm::Factory::create(actualalgtype, algname, serviceLocator().get());
66  if ( !algorithm ) {
67  this->error() << "Algorithm of type " << actualalgtype
68  << " is unknown (No factory available)." << endmsg;
69 #ifndef _WIN32
70  errno = 0xAFFEDEAD; // code used by Gaudi for library load errors: forces getLastErrorString do use dlerror (on Linux)
71 #endif
73  if (! err.empty()) this->error() << err << endmsg;
74  this->error() << "More information may be available by setting the global jobOpt \"PluginDebugLevel\" to 1" << endmsg;
75  return StatusCode::FAILURE;
76  }
77  // Check the compatibility of the version of the interface obtained
78  if( !isValidInterface(algorithm) ) {
79  fatal() << "Incompatible interface IAlgorithm version for " << actualalgtype << endmsg;
80  return StatusCode::FAILURE;
81  }
82  m_algs.emplace_back(algorithm, managed);
83  // let the algorithm know its type
84  algorithm->setType(algtype);
85  // this is needed to keep the reference count correct, since isValidInterface(algorithm)
86  // implies an increment of the counter by 1
87  algorithm->release();
88  StatusCode rc;
89  if ( managed ) {
90  // Bring the created algorithm to the same state of the ApplicationMgr
92  rc = algorithm->sysInitialize();
94  rc = algorithm->sysStart();
95  }
96  }
97  if ( !rc.isSuccess() ) {
98  this->error() << "Failed to initialize algorithm: [" << algname << "]" << endmsg;
99  }
100  }
101  return rc;
102 }
103 
105  auto it = std::find(m_algs.begin(), m_algs.end(), typeName.name());
106  if (it != m_algs.end()) { // found
107  return it->algorithm;
108  }
109  if (createIf) {
110  IAlgorithm* alg;
111  if (createAlgorithm(typeName.type(), typeName.name(), alg, true).isSuccess()) {
112  return algorithm(typeName, false);
113  }
114  }
115  return no_algorithm;
116 }
117 
118 // existsAlgorithm
120  return m_algs.end() != std::find(m_algs.begin(), m_algs.end(), name);
121 }
122 
123  // Return the list of Algorithms
125 {
127  m_listOfPtrs.reserve(m_algs.size());
130  [](const AlgorithmItem& alg) {
131  return const_cast<IAlgorithm*>(alg.algorithm.get());
132  } );
133  return m_listOfPtrs;
134 }
135 
137  StatusCode rc;
138  for (auto& it : m_algs ) {
139  if (!it.managed) continue;
140  rc = it.algorithm->sysInitialize();
141  if ( rc.isFailure() ) return rc;
142  }
143  return rc;
144 }
145 
147  StatusCode rc;
148  for (auto& it : m_algs ) {
149  if (!it.managed) continue;
150  rc = it.algorithm->sysStart();
151  if ( rc.isFailure() ) return rc;
152  }
153  return rc;
154 }
155 
157  StatusCode rc;
158  for (auto& it : m_algs) {
159  if (!it.managed) continue;
160  rc = it.algorithm->sysStop();
161  if ( rc.isFailure() ) return rc;
162  }
163  return rc;
164 }
165 
167  StatusCode rc;
168  auto it = m_algs.begin();
169  while (it != m_algs.end()){ // finalize and remove from the list the managed algorithms
170  if (it->managed) {
171  rc = it->algorithm->sysFinalize();
172  if( rc.isFailure() ) return rc;
173  it = m_algs.erase(it);
174  } else {
175  ++it;
176  }
177  }
178  return rc;
179 }
180 
182  StatusCode rc;
183  for (auto& it : m_algs ) {
184  if (!it.managed) continue;
185  rc = it.algorithm->sysReinitialize();
186  if( rc.isFailure() ){
187  this->error() << "Unable to re-initialize algorithm: " << it.algorithm->name() << endmsg;
188  return rc;
189  }
190  }
191  return rc;
192 }
193 
196  m_aess = serviceLocator()->service("AlgExecStateSvc");
197  if( !m_aess.isValid() ) {
198  fatal() << "Error retrieving AlgExecStateSvc" << endmsg;
199  return StatusCode::FAILURE;
200  }
201 
202  StatusCode rc;
203 
204  for (auto& it : m_algs ) {
205  if (!it.managed) continue;
206  rc = it.algorithm->sysRestart();
207  m_aess->resetErrorCount(it.algorithm);
208  if( rc.isFailure() ){
209  this->error() << "Unable to re-initialize algorithm: " << it.algorithm->name() << endmsg;
210  return rc;
211  }
212  }
213  return rc;
214 }
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
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
std::vector< IAlgorithm * > m_listOfPtrs
List of pointers to the know services used to implement getAlgorithms()
Gaudi::StateMachine::State FSMState() const override
Get the current state.
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:323
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:84
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:78
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:26
Definition of the basic interface.
Definition: IInterface.h:234
rc
Definition: IOTest.py:92
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:27
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
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:256
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:244
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).