| The Gaudi Framework v28r2p1 (f1a77ff4)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

AlgorithmManager.cpp
Go to the documentation of this file.
1 // Include files
2 #include "AlgorithmManager.h"
3 #include "GaudiKernel/IAlgorithm.h"
4 #include "GaudiKernel/Algorithm.h"
5 #include "GaudiKernel/ISvcLocator.h"
6 #include "GaudiKernel/System.h"
7 #include "GaudiKernel/MsgStream.h"
8 #include "GaudiKernel/TypeNameString.h"
9 #include "GaudiKernel/IAlgExecStateSvc.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):
20 base_class(application, IAlgorithm::interfaceID())
21 {
22 addRef(); // Initial count set to 1
23 }
24
25 // addAlgorithm
26 StatusCode AlgorithmManager::addAlgorithm(IAlgorithm* alg) {
27 m_algs.push_back(alg);
28 return StatusCode::SUCCESS;
29 }
30
31 // removeAlgorithm
32 StatusCode AlgorithmManager::removeAlgorithm(IAlgorithm* alg) {
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
42 StatusCode AlgorithmManager::createAlgorithm( const std::string& algtype,
43 const std::string& algname,
44 IAlgorithm*& algorithm,
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
72 std::string err = System::getLastErrorString();
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
91 if (FSMState() >= Gaudi::StateMachine::INITIALIZED) {
92 rc = algorithm->sysInitialize();
93 if (rc.isSuccess() && FSMState() >= Gaudi::StateMachine::RUNNING) {
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
104 SmartIF<IAlgorithm>& AlgorithmManager::algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf) {
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
119 bool AlgorithmManager::existsAlgorithm(const std::string& name) const {
120 return m_algs.end() != std::find(m_algs.begin(), m_algs.end(), name);
121 }
122
123 // Return the list of Algorithms
124 const std::vector<IAlgorithm*>& AlgorithmManager::getAlgorithms() const
125 {
126 m_listOfPtrs.clear();
127 m_listOfPtrs.reserve(m_algs.size());
128 std::transform( std::begin(m_algs), std::end(m_algs),
129 std::back_inserter(m_listOfPtrs),
130 [](const AlgorithmItem& alg) {
131 return const_cast<IAlgorithm*>(alg.algorithm.get());
132 } );
133 return m_listOfPtrs;
134 }
135
136 StatusCode AlgorithmManager::initialize() {
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
146 StatusCode AlgorithmManager::start() {
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
156 StatusCode AlgorithmManager::stop() {
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
166 StatusCode AlgorithmManager::finalize() {
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
181 StatusCode AlgorithmManager::reinitialize() {
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
194 StatusCode AlgorithmManager::restart() {
195 SmartIF<IAlgExecStateSvc> m_aess;
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 }
System.h
AlgorithmManager::initialize
StatusCode initialize() override
Initialization (from CONFIGURED to INITIALIZED).
Definition: AlgorithmManager.cpp:136
Gaudi::StateMachine::INITIALIZED
Definition: StateMachine.h:15
StatusCode::SUCCESS
Definition: StatusCode.h:30
Algorithm.h
SmartIF< IAlgorithm >
std::string::empty
T empty(T...args)
AlgorithmManager::addAlgorithm
StatusCode addAlgorithm(IAlgorithm *alg) override
implementation of IAlgManager::addAlgorithm
Definition: AlgorithmManager.cpp:26
AlgorithmManager::start
StatusCode start() override
Start (from INITIALIZED to RUNNING).
Definition: AlgorithmManager.cpp:146
AlgorithmManager::name
const std::string & name() const override
Return the name of the manager (implementation of INamedInterface)
Definition: AlgorithmManager.h:84
AlgorithmManager::getAlgorithms
const std::vector< IAlgorithm * > & getAlgorithms() const override
implementation of IAlgManager::getAlgorithms
Definition: AlgorithmManager.cpp:124
IAlgExecStateSvc.h
StatusCode::FAILURE
Definition: StatusCode.h:29
IAlgorithm::sysStart
virtual StatusCode sysStart()=0
Startup method invoked by the framework.
AlgorithmManager::m_algTypeAliases
AlgTypeAliasesMap m_algTypeAliases
Definition: AlgorithmManager.h:100
MsgStream.h
AlgorithmManager::m_algs
std::vector< AlgorithmItem > m_algs
algorithms maintained by AlgorithmManager
Definition: AlgorithmManager.h:95
IAlgorithm.h
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
AlgorithmManager::m_listOfPtrs
std::vector< IAlgorithm * > m_listOfPtrs
List of pointers to the know services used to implement getAlgorithms()
Definition: AlgorithmManager.h:98
AlgorithmManager::AlgorithmItem
Definition: AlgorithmManager.h:35
ComponentManager::FSMState
Gaudi::StateMachine::State FSMState() const override
Get the current state.
Definition: ComponentManager.h:59
std::map::end
T end(T...args)
AlgorithmManager::reinitialize
StatusCode reinitialize() override
Initialization (from INITIALIZED or RUNNING to INITIALIZED, via CONFIGURED).
Definition: AlgorithmManager.cpp:181
isValidInterface
bool isValidInterface(I *i)
Templated function that throws an exception if the version if the interface implemented by the object...
Definition: IInterface.h:323
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:84
ComponentManager::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Definition: ComponentManager.h:28
AlgorithmManager::stop
StatusCode stop() override
Stop (from RUNNING to INITIALIZED).
Definition: AlgorithmManager.cpp:156
CommonMessagingBase::err
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:114
AlgorithmManager::finalize
StatusCode finalize() override
Finalize (from INITIALIZED to CONFIGURED).
Definition: AlgorithmManager.cpp:166
IAlgorithm::sysInitialize
virtual StatusCode sysInitialize()=0
Initialization method invoked by the framework.
std::string
STL class.
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:78
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:117
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:12
AlgorithmManager.h
AlgorithmManager::existsAlgorithm
bool existsAlgorithm(const std::string &name) const override
implementation of IAlgManager::existsAlgorithm
Definition: AlgorithmManager.cpp:119
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
IInterface
Definition of the basic interface.
Definition: IInterface.h:234
IOTest.rc
rc
Definition: IOTest.py:92
AlgorithmManager::removeAlgorithm
StatusCode removeAlgorithm(IAlgorithm *alg) override
implementation of IAlgManager::removeAlgorithm
Definition: AlgorithmManager.cpp:32
std::vector::clear
T clear(T...args)
IAlgorithm
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:27
IAlgorithm::setType
virtual void setType(const std::string &)=0
std::find
T find(T...args)
std::string::size
T size(T...args)
Gaudi::StateMachine::RUNNING
Definition: StateMachine.h:16
std::vector< IAlgorithm * >
SmartIF::isValid
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:62
std::begin
T begin(T...args)
std::back_inserter
T back_inserter(T...args)
Gaudi::Utils::TypeNameString::type
const std::string & type() const
Definition: TypeNameString.h:39
TypeNameString.h
CommonMessaging
Definition: CommonMessaging.h:55
std::string::substr
T substr(T...args)
AlgorithmManager::AlgorithmManager
AlgorithmManager(IInterface *iface)
default creator
Definition: AlgorithmManager.cpp:19
AlgorithmManager::algorithm
SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true) override
Definition: AlgorithmManager.cpp:104
std::transform
T transform(T...args)
System::getLastErrorString
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition: System.cpp:256
Gaudi::Utils::TypeNameString::name
const std::string & name() const
Definition: TypeNameString.h:40
CommonMessagingBase::fatal
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
Definition: CommonMessaging.h:111
GaudiDict::typeName
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:21
ISvcLocator.h
IAlgExecStateSvc::resetErrorCount
virtual void resetErrorCount(const IAlgorithm *iAlg)=0
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
std::string::compare
T compare(T...args)
AlgorithmManager::createAlgorithm
StatusCode createAlgorithm(const std::string &algtype, const std::string &algname, IAlgorithm *&algorithm, bool managed=false, bool checkIfExists=true) override
implementation of IAlgManager::createAlgorithm
Definition: AlgorithmManager.cpp:42
AlgorithmManager::AlgorithmItem::algorithm
SmartIF< IAlgorithm > algorithm
Definition: AlgorithmManager.h:38
std::vector::reserve
T reserve(T...args)
AlgorithmManager::restart
StatusCode restart() override
Initialization (from RUNNING to RUNNING, via INITIALIZED).
Definition: AlgorithmManager.cpp:194