The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
AlgTool.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11#include <Gaudi/Algorithm.h>
12#include <Gaudi/Auditor.h>
13#include <GaudiKernel/AlgTool.h>
16#include <GaudiKernel/Guards.h>
20#include <GaudiKernel/Service.h>
22#include <GaudiKernel/System.h>
24
25namespace {
26 template <typename FUN>
27 StatusCode attempt( AlgTool& tool, const char* label, FUN&& fun ) {
28 try {
29 return fun();
30 } catch ( const GaudiException& Exception ) {
31 MsgStream log( tool.msgSvc(), tool.name() + "." + label );
32 log << MSG::FATAL << " Exception with tag=" << Exception.tag() << " is caught" << endmsg;
33 log << MSG::ERROR << Exception << endmsg;
34 } catch ( const std::exception& Exception ) {
35 MsgStream log( tool.msgSvc(), tool.name() + "." + label );
36 log << MSG::FATAL << " Standard std::exception is caught" << endmsg;
37 log << MSG::ERROR << Exception.what() << endmsg;
38 } catch ( ... ) {
39 MsgStream log( tool.msgSvc(), tool.name() + "." + label );
40 log << MSG::FATAL << "UNKNOWN Exception is caught" << endmsg;
41 }
43 }
44} // namespace
45
46void const* AlgTool::i_cast( const InterfaceID& riid ) const {
47 if ( auto output = base_class::i_cast( riid ) ) { return output; }
48 if ( auto i =
49 std::find_if( std::begin( m_interfaceList ), std::end( m_interfaceList ),
50 [&]( const std::pair<InterfaceID, void*>& item ) { return item.first.versionMatch( riid ); } );
51 i != std::end( m_interfaceList ) ) {
52 return i->second;
53 }
54 return nullptr;
55}
56
57const std::string& AlgTool::name() const { return m_name; }
58
59const std::string& AlgTool::type() const { return m_type; }
60
61const IInterface* AlgTool::parent() const { return m_parent; }
62
64
66 if ( !m_evtSvc ) {
67 m_evtSvc = service( "EventDataSvc", true );
68 if ( !m_evtSvc ) { throw GaudiException( "Service [EventDataSvc] not found", name(), StatusCode::FAILURE ); }
69 }
70 return m_evtSvc.get();
71}
73 if ( !m_ptoolSvc ) {
74 m_ptoolSvc = service( "ToolSvc", true );
75 if ( !m_ptoolSvc ) { throw GaudiException( "Service [ToolSvc] not found", name(), StatusCode::FAILURE ); }
76 }
77 return m_ptoolSvc.get();
78}
79
80AlgTool::AlgTool( std::string type, std::string name, const IInterface* parent )
81 : m_type( std::move( type ) ), m_name( std::move( name ) ), m_parent( parent ) {
82 addRef(); // Initial count set to 1
83
84 IInterface* _p = const_cast<IInterface*>( parent );
85
86 if ( Gaudi::Algorithm* _alg = dynamic_cast<Gaudi::Algorithm*>( _p ) ) {
87 m_svcLocator = _alg->serviceLocator();
88 } else if ( Service* _svc = dynamic_cast<Service*>( _p ) ) {
89 m_svcLocator = _svc->serviceLocator();
90 } else if ( AlgTool* _too = dynamic_cast<AlgTool*>( _p ) ) {
91 m_svcLocator = _too->serviceLocator();
92 } else if ( Gaudi::Auditor* _aud = dynamic_cast<Gaudi::Auditor*>( _p ) ) {
93 m_svcLocator = _aud->serviceLocator();
94 } else {
95 throw GaudiException( "Failure to create tool '" + m_type + "/" + m_name + "': illegal parent type '" +
96 System::typeinfoName( typeid( *_p ) ) + "'",
97 "AlgTool", StatusCode::FAILURE );
98 }
99
100 // inherit output level from parent
101 // get the "OutputLevel" property from parent
102 if ( SmartIF<IProperty> pprop( _p ); pprop && pprop->hasProperty( "OutputLevel" ) ) {
103 m_outputLevel.assign( pprop->getProperty( "OutputLevel" ) );
104 }
105
106 // Auditor monitoring properties
107 // Initialize the default value from ApplicationMgr AuditAlgorithms
108 Gaudi::Property<bool> audit( "AuditTools", false );
109 // note that here we need that the service locator is already defined
110 if ( auto appMgr = serviceLocator()->service<IProperty>( "ApplicationMgr" ) ) {
111 appMgr->getProperty( &audit ).ignore();
112 }
113
114 m_auditorInitialize = audit;
115 m_auditorStart = audit;
116 m_auditorStop = audit;
117 m_auditorFinalize = audit;
118 m_auditorReinitialize = audit;
119 m_auditorRestart = audit;
120}
121
123 return attempt( *this, "sysInitialize", [&]() {
126 // check if we want to audit the initialize
128 StatusCode sc = initialize();
129 if ( !sc ) return sc;
130
133
134 // check for explicit circular data dependencies in declared handles
135 DataObjIDColl out;
136 for ( auto& h : outputHandles() ) {
137 if ( !h->objKey().empty() ) out.emplace( h->fullKey() );
138 }
139 for ( auto& h : inputHandles() ) {
140 if ( !h->objKey().empty() && out.find( h->fullKey() ) != out.end() ) {
141 error() << "Explicit circular data dependency found for id " << h->fullKey() << endmsg;
143 }
144 }
145
146 if ( !sc ) return sc;
147
148 // visit all sub-tools, build full set
150 acceptDHVisitor( &avis );
151
152 // initialize handles
153 initDataHandleHolder(); // this should 'freeze' the handle configuration.
154
155 return sc;
156 } );
157}
159 // For the time being there is nothing to be done here.
160 // Setting the properties is done by the ToolSvc calling setProperties()
161 // explicitly.
162 return StatusCode::SUCCESS;
163}
164
166 return attempt( *this, "sysStart", [&]() {
169 // check if we want to audit the initialize
171 StatusCode sc = start();
172 if ( sc.isSuccess() ) m_state = m_targetState;
173 return sc;
174 } );
175}
176
178 // For the time being there is nothing to be done here.
179 return StatusCode::SUCCESS;
180}
181
183 return attempt( *this, "sysStop", [&]() {
186 // check if we want to audit the initialize
188 StatusCode sc = stop();
189 if ( sc.isSuccess() ) m_state = m_targetState;
190 return sc;
191 } );
192}
193
195 // For the time being there is nothing to be done here.
196 return StatusCode::SUCCESS;
197}
198
200 return attempt( *this, "sysFinalize", [&]() {
203 // check if we want to audit the initialize
205 StatusCode sc = finalize();
206 if ( sc.isSuccess() ) m_state = m_targetState;
207 return sc;
208 } );
209}
211 // For the time being there is nothing to be done here.
212 return StatusCode::SUCCESS;
213}
214
216
217 // Check that the current status is the correct one.
219 error() << "sysReinitialize(): cannot reinitialize tool not initialized" << endmsg;
220 return StatusCode::FAILURE;
221 }
222
223 return attempt( *this, "SysReinitialize()", [&]() {
225 // check if we want to audit the initialize
227 return reinitialize();
228 } );
229}
230
232 /* @TODO
233 * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
234 * is causing too many problems
235 *
236 // Default implementation is finalize+initialize
237 StatusCode sc = finalize();
238 if (sc.isFailure()) {
239 error() << "reinitialize(): cannot be finalized" << endmsg;
240 return sc;
241 }
242 sc = initialize();
243 if (sc.isFailure()) {
244 error() << "reinitialize(): cannot be initialized" << endmsg;
245 return sc;
246 }
247 */
248 return StatusCode::SUCCESS;
249}
250
252
253 // Check that the current status is the correct one.
255 error() << "sysRestart(): cannot reinitialize tool not started" << endmsg;
256 return StatusCode::FAILURE;
257 }
258
259 return attempt( *this, "sysRestart", [&]() {
262 // check if we want to audit the initialize
264 return restart();
265 } );
266}
267
269 // Default implementation is stop+start
270 StatusCode sc = stop();
271 if ( sc.isFailure() ) {
272 error() << "restart(): cannot be stopped" << endmsg;
273 return sc;
274 }
275 sc = start();
276 if ( sc.isFailure() ) {
277 error() << "restart(): cannot be started" << endmsg;
278 return sc;
279 }
280 return StatusCode::SUCCESS;
281}
282
284 if ( m_pMonitorSvc ) { m_pMonitorSvc->undeclareAll( this ); }
285}
286
288 auto init_one = [&]( BaseToolHandle* th ) {
289 if ( !th->isEnabled() ) {
290 if ( msgLevel( MSG::DEBUG ) && !th->typeAndName().empty() )
291 debug() << "ToolHandle " << th->typeAndName() << " not used" << endmsg;
292 return;
293 }
294 if ( !th->get() ) {
295 auto sc = th->retrieve();
296 if ( sc.isFailure() ) {
297 throw GaudiException( "Failed to retrieve tool " + th->typeAndName(), this->name(), StatusCode::FAILURE );
298 }
299 }
300 auto* tool = th->get();
301 if ( msgLevel( MSG::DEBUG ) )
302 debug() << "Adding " << ( th->isPublic() ? "public" : "private" ) << " ToolHandle tool " << tool->name() << " ("
303 << tool->type() << ")" << endmsg;
304 m_tools.push_back( tool );
305 };
306
307 for ( auto thArr : m_toolHandleArrays ) {
308 if ( msgLevel( MSG::DEBUG ) )
309 debug() << "Registering all Tools in ToolHandleArray " << thArr->propertyName() << endmsg;
310 // Iterate over its tools:
311 for ( auto toolHandle : thArr->getBaseArray() ) {
312 // Try to cast it into a BaseToolHandle pointer:
313 BaseToolHandle* bth = dynamic_cast<BaseToolHandle*>( toolHandle );
314 if ( bth ) {
315 init_one( bth );
316 } else {
317 error() << "Error retrieving Tool " << toolHandle->typeAndName() << " in ToolHandleArray "
318 << thArr->propertyName() << ". Not registered" << endmsg;
319 }
320 }
321 }
322
323 for ( BaseToolHandle* th : m_toolHandles ) init_one( th );
324
325 m_toolHandlesInit = true;
326}
327
328const std::vector<IAlgTool*>& AlgTool::tools() const {
330
331 return m_tools;
332}
333
334std::vector<IAlgTool*>& AlgTool::tools() {
336
337 return m_tools;
338}
339
340SmartIF<IService> AlgTool::service( std::string_view name, const bool createIf, const bool quiet ) const {
341 const ServiceLocatorHelper helper( *serviceLocator(), *this );
342 return helper.service( name, quiet, createIf );
343}
344
346 if ( !m_pAuditorSvc ) {
347 m_pAuditorSvc = service( "AuditorSvc", true );
348 if ( !m_pAuditorSvc ) { throw GaudiException( "Service [AuditorSvc] not found", name(), StatusCode::FAILURE ); }
349 }
350 return m_pAuditorSvc.get();
351}
352
354 vis->visit( this );
355
356 for ( auto tool : tools() ) vis->visit( dynamic_cast<AlgTool*>( tool ) );
357}
std::unordered_set< DataObjID, DataObjID_Hasher > DataObjIDColl
Definition DataObjID.h:121
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
Base class from which all the concrete tool classes should be derived.
Definition AlgTool.h:55
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition AlgTool.h:295
std::unique_ptr< IDataHandleVisitor > m_updateDataHandles
Hook for for derived classes to provide a custom visitor for data handles.
Definition AlgTool.h:208
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition AlgTool.h:267
StatusCode sysFinalize() override
Finalize AlgTool.
Definition AlgTool.cpp:199
StatusCode sysReinitialize() override
Initialize AlgTool.
Definition AlgTool.cpp:215
Gaudi::Property< int > m_outputLevel
Definition AlgTool.h:277
const std::string m_name
AlgTool full name.
Definition AlgTool.h:264
Gaudi::StateMachine::State m_state
flag indicating whether ToolHandle tools have been added to m_tools
Definition AlgTool.h:298
Gaudi::Property< bool > m_auditorInitialize
Definition AlgTool.h:285
InterfaceList m_interfaceList
Interface list.
Definition AlgTool.h:273
const IInterface * parent() const override
Retrieve parent of the sub-algtool.
Definition AlgTool.cpp:61
StatusCode initialize() override
Definition AlgTool.cpp:158
AlgTool(std::string type, std::string name, const IInterface *parent)
Standard Constructor.
Definition AlgTool.cpp:80
const std::string & type() const override
Retrieve type (concrete class) of the sub-algtool.
Definition AlgTool.cpp:59
~AlgTool() override
Definition AlgTool.cpp:283
const IInterface * m_parent
AlgTool parent.
Definition AlgTool.h:265
SmartIF< IService > service(std::string_view name, const bool createIf=true, const bool quiet=false) const
Return a pointer to the service identified by name (or "type/name")
Definition AlgTool.cpp:340
const std::vector< IAlgTool * > & tools() const
Definition AlgTool.cpp:328
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition AlgTool.h:271
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition AlgTool.cpp:57
std::vector< BaseToolHandle * > m_toolHandles
Definition AlgTool.h:294
Gaudi::Property< bool > m_auditorStart
Definition AlgTool.h:286
IDataProviderSvc * evtSvc() const
accessor to event service service
Definition AlgTool.cpp:65
std::vector< IAlgTool * > m_tools
Definition AlgTool.h:293
IToolSvc * toolSvc() const
The standard ToolSvc service, Return a pointer to the service if present.
Definition AlgTool.cpp:72
bool m_toolHandlesInit
Definition AlgTool.h:296
Gaudi::StateMachine::State FSMState() const override
Definition AlgTool.h:83
StatusCode reinitialize() override
Definition AlgTool.cpp:231
Gaudi::Property< bool > m_auditorStop
Definition AlgTool.h:287
Gaudi::Property< bool > m_auditorReinitialize
Definition AlgTool.h:289
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition AlgTool.h:268
std::string m_type
AlgTool type (concrete class name)
Definition AlgTool.h:263
StatusCode sysRestart() override
Start AlgTool.
Definition AlgTool.cpp:251
StatusCode restart() override
Definition AlgTool.cpp:268
StatusCode start() override
Definition AlgTool.cpp:177
void initToolHandles() const
Definition AlgTool.cpp:287
void acceptDHVisitor(IDataHandleVisitor *) const override
Definition AlgTool.cpp:353
StatusCode sysStart() override
Start AlgTool.
Definition AlgTool.cpp:165
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition AlgTool.h:270
Gaudi::Property< bool > m_auditorFinalize
Definition AlgTool.h:288
StatusCode sysInitialize() override
Initialize AlgTool.
Definition AlgTool.cpp:122
IAuditorSvc * auditorSvc() const
Access the auditor service.
Definition AlgTool.cpp:345
StatusCode stop() override
Definition AlgTool.cpp:194
StatusCode sysStop() override
Stop AlgTool.
Definition AlgTool.cpp:182
Gaudi::Property< bool > m_auditorRestart
Definition AlgTool.h:290
Gaudi::StateMachine::State m_targetState
state of the Tool
Definition AlgTool.h:299
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition AlgTool.cpp:63
StatusCode finalize() override
Definition AlgTool.cpp:210
SmartIF< IToolSvc > m_ptoolSvc
Tool service.
Definition AlgTool.h:269
void const * i_cast(const InterfaceID &riid) const override
Query for a given interface.
Definition AlgTool.cpp:46
Non-templated base class for actual ToolHandle<T>.
Definition ToolHandle.h:78
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Base class from which all concrete algorithm classes should be derived.
Definition Algorithm.h:87
Base class from which all concrete auditor classes should be derived.
Definition Auditor.h:33
It is a simple guard, which "locks" the scope for the Auditor Service is am exception-safe way.
Definition Guards.h:202
static const std::string Stop
Definition IAuditor.h:52
static const std::string Initialize
Definition IAuditor.h:47
static const std::string Finalize
Definition IAuditor.h:53
static const std::string Start
Definition IAuditor.h:49
static const std::string ReInitialize
Definition IAuditor.h:48
static const std::string ReStart
Definition IAuditor.h:50
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27
Define general base for Gaudi exception.
The interface implemented by the IAuditorSvc base class.
Definition IAuditorSvc.h:24
virtual void visit(const IDataHandleHolder *)=0
Data provider interface definition.
Definition of the basic interface.
Definition IInterface.h:225
The interface implemented by the IToolSvc base class.
Definition IToolSvc.h:28
Interface ID class.
Definition IInterface.h:38
Definition of the MsgStream class used to transmit messages.
Definition MsgStream.h:29
Base class for all services.
Definition Service.h:39
an helper to share the implementation of service() among the various kernel base classes
SmartIF< IService > service(std::string_view name, const bool quiet=false, const bool createIf=true) const
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isFailure() const
Definition StatusCode.h:129
bool isSuccess() const
Definition StatusCode.h:314
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
void const * i_cast(const InterfaceID &tid) const override
Implementation of IInterface::i_cast.
Definition extends.h:32
State GAUDI_API ChangeState(const Transition transition, const State state)
Function to get the new state according to the required transition, checking if the transition is all...
@ FATAL
Definition IMessageSvc.h:22
@ DEBUG
Definition IMessageSvc.h:22
@ ERROR
Definition IMessageSvc.h:22
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition System.cpp:260
STL namespace.