The Gaudi Framework  v32r2 (46d42edc)
AlgTool.cpp
Go to the documentation of this file.
1 // Include files
2 #include "GaudiKernel/AlgTool.h"
7 
8 #include "GaudiKernel/Auditor.h"
11 #include "GaudiKernel/Guards.h"
12 #include "GaudiKernel/Service.h"
14 #include "GaudiKernel/System.h"
15 #include "GaudiKernel/ToolHandle.h"
16 #include <Gaudi/Algorithm.h>
17 
18 //------------------------------------------------------------------------------
19 namespace {
20  template <typename FUN>
21  StatusCode attempt( AlgTool& tool, const char* label, FUN&& fun ) {
22  try {
23  return fun();
24  } catch ( const GaudiException& Exception ) {
25  MsgStream log( tool.msgSvc(), tool.name() + "." + label );
26  log << MSG::FATAL << " Exception with tag=" << Exception.tag() << " is caught" << endmsg;
27  log << MSG::ERROR << Exception << endmsg;
28  } catch ( const std::exception& Exception ) {
29  MsgStream log( tool.msgSvc(), tool.name() + "." + label );
30  log << MSG::FATAL << " Standard std::exception is caught" << endmsg;
31  log << MSG::ERROR << Exception.what() << endmsg;
32  } catch ( ... ) {
33  MsgStream log( tool.msgSvc(), tool.name() + "." + label );
34  log << MSG::FATAL << "UNKNOWN Exception is caught" << endmsg;
35  }
36  return StatusCode::FAILURE;
37  }
38 } // namespace
39 
40 //------------------------------------------------------------------------------
41 StatusCode AlgTool::queryInterface( const InterfaceID& riid, void** ppvi )
42 //------------------------------------------------------------------------------
43 {
44  if ( !ppvi ) { return StatusCode::FAILURE; } // RETURN
45  StatusCode sc = base_class::queryInterface( riid, ppvi );
46  if ( sc.isSuccess() ) return sc;
48  [&]( const std::pair<InterfaceID, void*>& item ) { return item.first.versionMatch( riid ); } );
49  if ( i == std::end( m_interfaceList ) ) {
50  *ppvi = nullptr;
51  return Status::NO_INTERFACE; // RETURN
52  }
53  *ppvi = i->second;
54  addRef();
55  return StatusCode::SUCCESS; // RETURN
56 }
57 //------------------------------------------------------------------------------
58 const std::string& AlgTool::name() const
59 //------------------------------------------------------------------------------
60 {
61  return m_name;
62 }
63 
64 //------------------------------------------------------------------------------
65 const std::string& AlgTool::type() const
66 //------------------------------------------------------------------------------
67 {
68  return m_type;
69 }
70 
71 //------------------------------------------------------------------------------
73 //------------------------------------------------------------------------------
74 {
75  return m_parent;
76 }
77 
78 //------------------------------------------------------------------------------
80 //------------------------------------------------------------------------------
81 {
82  return m_svcLocator;
83 }
84 
85 // ============================================================================
86 // accessor to event service service
87 // ============================================================================
89  if ( !m_evtSvc ) {
90  m_evtSvc = service( "EventDataSvc", true );
91  if ( !m_evtSvc ) { throw GaudiException( "Service [EventDataSvc] not found", name(), StatusCode::FAILURE ); }
92  }
93  return m_evtSvc.get();
94 }
95 //------------------------------------------------------------------------------
97 //------------------------------------------------------------------------------
98 {
99  if ( !m_ptoolSvc ) {
100  m_ptoolSvc = service( "ToolSvc", true );
101  if ( !m_ptoolSvc ) { throw GaudiException( "Service [ToolSvc] not found", name(), StatusCode::FAILURE ); }
102  }
103  return m_ptoolSvc.get();
104 }
105 
106 //------------------------------------------------------------------------------
108 //------------------------------------------------------------------------------
109 {
110  if ( !m_svcLocator ) return StatusCode::FAILURE;
111  auto jos = m_svcLocator->service<IJobOptionsSvc>( "JobOptionsSvc" );
112  if ( !jos ) return StatusCode::FAILURE;
113 
114  return jos->setMyProperties( name(), this );
115 }
116 
117 //------------------------------------------------------------------------------
118 AlgTool::AlgTool( const std::string& type, const std::string& name, const IInterface* parent )
119  //------------------------------------------------------------------------------
120  : m_type( type ), m_name( name ), m_parent( parent ) {
121  addRef(); // Initial count set to 1
122 
123  IInterface* _p = const_cast<IInterface*>( parent );
124 
125  if ( Gaudi::Algorithm* _alg = dynamic_cast<Gaudi::Algorithm*>( _p ) ) {
126  m_svcLocator = _alg->serviceLocator();
127  } else if ( Service* _svc = dynamic_cast<Service*>( _p ) ) {
128  m_svcLocator = _svc->serviceLocator();
129  } else if ( AlgTool* _too = dynamic_cast<AlgTool*>( _p ) ) {
130  m_svcLocator = _too->serviceLocator();
131  } else if ( Auditor* _aud = dynamic_cast<Auditor*>( _p ) ) {
132  m_svcLocator = _aud->serviceLocator();
133  } else {
134  throw GaudiException( "Failure to create tool '" + type + "/" + name + "': illegal parent type '" +
135  System::typeinfoName( typeid( *_p ) ) + "'",
136  "AlgTool", StatusCode::FAILURE );
137  }
138 
139  // inherit output level from parent
140  { // get the "OutputLevel" property from parent
141  SmartIF<IProperty> pprop( _p );
142  if ( pprop && pprop->hasProperty( "OutputLevel" ) ) { m_outputLevel.assign( pprop->getProperty( "OutputLevel" ) ); }
143  }
144 
145  {
146  // Auditor monitoring properties
147  // Initialize the default value from ApplicationMgr AuditAlgorithms
148  Gaudi::Property<bool> audit( false );
149  // note that here we need that the service locator is already defined
150  auto appMgr = serviceLocator()->service<IProperty>( "ApplicationMgr" );
151  if ( appMgr && appMgr->hasProperty( "AuditTools" ) ) { audit.assign( appMgr->getProperty( "AuditTools" ) ); }
152  m_auditInit = audit;
153  m_auditorInitialize = audit;
154  m_auditorStart = audit;
155  m_auditorStop = audit;
156  m_auditorFinalize = audit;
157  m_auditorReinitialize = audit;
158  m_auditorRestart = audit;
159  }
160 }
161 
162 //-----------------------------------------------------------------------------
164  //-----------------------------------------------------------------------------
165  return attempt( *this, "sysInitialize", [&]() {
167  Gaudi::Guards::AuditorGuard guard( this,
168  // check if we want to audit the initialize
170  StatusCode sc = initialize();
171  if ( !sc ) return sc;
172 
175 
176  // check for explicit circular data dependencies in declared handles
177  DataObjIDColl out;
178  for ( auto& h : outputHandles() ) {
179  if ( !h->objKey().empty() ) out.emplace( h->fullKey() );
180  }
181  for ( auto& h : inputHandles() ) {
182  if ( !h->objKey().empty() && out.find( h->fullKey() ) != out.end() ) {
183  error() << "Explicit circular data dependency found for id " << h->fullKey() << endmsg;
184  sc = StatusCode::FAILURE;
185  }
186  }
187 
188  if ( !sc ) return sc;
189 
190  // visit all sub-tools, build full set
192  acceptDHVisitor( &avis );
193 
194  // initialize handles
195  initDataHandleHolder(); // this should 'freeze' the handle configuration.
196 
197  return sc;
198  } );
199 }
200 //------------------------------------------------------------------------------
202 //------------------------------------------------------------------------------
203 {
204  // For the time being there is nothing to be done here.
205  // Setting the properties is done by the ToolSvc calling setProperties()
206  // explicitly.
207  return StatusCode::SUCCESS;
208 }
209 
210 //-----------------------------------------------------------------------------
212  //-----------------------------------------------------------------------------
213  return attempt( *this, "sysStart", [&]() {
215  Gaudi::Guards::AuditorGuard guard( this,
216  // check if we want to audit the initialize
217  m_auditorStart ? auditorSvc() : nullptr, IAuditor::Start );
218  StatusCode sc = start();
219  if ( sc.isSuccess() ) m_state = m_targetState;
220  return sc;
221  } );
222 }
223 
224 //------------------------------------------------------------------------------
226 //------------------------------------------------------------------------------
227 {
228  // For the time being there is nothing to be done here.
229  return StatusCode::SUCCESS;
230 }
231 
232 //-----------------------------------------------------------------------------
234  //-----------------------------------------------------------------------------
235  return attempt( *this, "sysStop", [&]() {
237  Gaudi::Guards::AuditorGuard guard( this,
238  // check if we want to audit the initialize
239  m_auditorStop ? auditorSvc() : nullptr, IAuditor::Stop );
240  StatusCode sc = stop();
241  if ( sc.isSuccess() ) m_state = m_targetState;
242  return sc;
243  } );
244 }
245 
246 //------------------------------------------------------------------------------
248 //------------------------------------------------------------------------------
249 {
250  // For the time being there is nothing to be done here.
251  return StatusCode::SUCCESS;
252 }
253 
254 //-----------------------------------------------------------------------------
256  //-----------------------------------------------------------------------------
257  return attempt( *this, "sysFinalize", [&]() {
259  Gaudi::Guards::AuditorGuard guard( this,
260  // check if we want to audit the initialize
262  StatusCode sc = finalize();
263  if ( sc.isSuccess() ) m_state = m_targetState;
264  return sc;
265  } );
266 }
267 //------------------------------------------------------------------------------
269 //------------------------------------------------------------------------------
270 {
271  // For the time being there is nothing to be done here.
272  return StatusCode::SUCCESS;
273 }
274 
275 //-----------------------------------------------------------------------------
277  //-----------------------------------------------------------------------------
278 
279  // Check that the current status is the correct one.
281  error() << "sysReinitialize(): cannot reinitialize tool not initialized" << endmsg;
282  return StatusCode::FAILURE;
283  }
284 
285  return attempt( *this, "SysReinitialize()", [&]() {
286  Gaudi::Guards::AuditorGuard guard( this,
287  // check if we want to audit the initialize
289  return reinitialize();
290  } );
291 }
292 
293 //------------------------------------------------------------------------------
295 //------------------------------------------------------------------------------
296 {
297  /* @TODO
298  * MCl 2008-10-23: the implementation of reinitialize as finalize+initialize
299  * is causing too many problems
300  *
301  // Default implementation is finalize+initialize
302  StatusCode sc = finalize();
303  if (sc.isFailure()) {
304  error() << "reinitialize(): cannot be finalized" << endmsg;
305  return sc;
306  }
307  sc = initialize();
308  if (sc.isFailure()) {
309  error() << "reinitialize(): cannot be initialized" << endmsg;
310  return sc;
311  }
312  */
313  return StatusCode::SUCCESS;
314 }
315 
316 //-----------------------------------------------------------------------------
318  //-----------------------------------------------------------------------------
319 
320  // Check that the current status is the correct one.
322  error() << "sysRestart(): cannot reinitialize tool not started" << endmsg;
323  return StatusCode::FAILURE;
324  }
325 
326  return attempt( *this, "sysRestart", [&]() {
328  Gaudi::Guards::AuditorGuard guard( this,
329  // check if we want to audit the initialize
331  return restart();
332  } );
333 }
334 
335 //------------------------------------------------------------------------------
337 //------------------------------------------------------------------------------
338 {
339  // Default implementation is stop+start
340  StatusCode sc = stop();
341  if ( sc.isFailure() ) {
342  error() << "restart(): cannot be stopped" << endmsg;
343  return sc;
344  }
345  sc = start();
346  if ( sc.isFailure() ) {
347  error() << "restart(): cannot be started" << endmsg;
348  return sc;
349  }
350  return StatusCode::SUCCESS;
351 }
352 
353 //------------------------------------------------------------------------------
355 //------------------------------------------------------------------------------
356 {
357  if ( m_pMonitorSvc ) { m_pMonitorSvc->undeclareAll( this ); }
358 }
359 
361  auto init_one = [&]( BaseToolHandle* th ) {
362  if ( !th->isEnabled() ) {
363  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) && !th->typeAndName().empty() )
364  debug() << "ToolHandle " << th->typeAndName() << " not used" << endmsg;
365  return;
366  }
367  if ( !th->get() ) {
368  auto sc = th->retrieve();
369  if ( UNLIKELY( sc.isFailure() ) ) {
370  throw GaudiException( "Failed to retrieve tool " + th->typeAndName(), this->name(), StatusCode::FAILURE );
371  }
372  }
373  auto* tool = th->get();
374  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) )
375  debug() << "Adding " << ( th->isPublic() ? "public" : "private" ) << " ToolHandle tool " << tool->name() << " ("
376  << tool->type() << ")" << endmsg;
377  m_tools.push_back( tool );
378  };
379 
380  for ( auto thArr : m_toolHandleArrays ) {
381  if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) )
382  debug() << "Registering all Tools in ToolHandleArray " << thArr->propertyName() << endmsg;
383  // Iterate over its tools:
384  for ( auto toolHandle : thArr->getBaseArray() ) {
385  // Try to cast it into a BaseToolHandle pointer:
386  BaseToolHandle* bth = dynamic_cast<BaseToolHandle*>( toolHandle );
387  if ( bth ) {
388  init_one( bth );
389  } else {
390  error() << "Error retrieving Tool " << toolHandle->typeAndName() << " in ToolHandleArray "
391  << thArr->propertyName() << ". Not registered" << endmsg;
392  }
393  }
394  }
395 
396  for ( BaseToolHandle* th : m_toolHandles ) init_one( th );
397 
398  m_toolHandlesInit = true;
399 }
400 
403 
404  return m_tools;
405 }
406 
409 
410  return m_tools;
411 }
412 
413 //------------------------------------------------------------------------------
415 StatusCode AlgTool::service_i( const std::string& svcName, bool createIf, const InterfaceID& iid, void** ppSvc ) const {
416  const ServiceLocatorHelper helper( *serviceLocator(), *this );
417  return helper.getService( svcName, createIf, iid, ppSvc );
418 }
419 
420 //------------------------------------------------------------------------------
421 StatusCode AlgTool::service_i( const std::string& svcType, const std::string& svcName, const InterfaceID& iid,
422  void** ppSvc ) const {
423  const ServiceLocatorHelper helper( *serviceLocator(), *this );
424  return helper.createService( svcType, svcName, iid, ppSvc );
425 }
426 
427 SmartIF<IService> AlgTool::service( const std::string& name, const bool createIf, const bool quiet ) const {
428  const ServiceLocatorHelper helper( *serviceLocator(), *this );
429  return helper.service( name, quiet, createIf );
430 }
431 
432 //-----------------------------------------------------------------------------
434  //---------------------------------------------------------------------------
435  if ( !m_pAuditorSvc ) {
436  m_pAuditorSvc = service( "AuditorSvc", true );
437  if ( !m_pAuditorSvc ) { throw GaudiException( "Service [AuditorSvc] not found", name(), StatusCode::FAILURE ); }
438  }
439  return m_pAuditorSvc.get();
440 }
441 
442 //-----------------------------------------------------------------------------
444  //-----------------------------------------------------------------------------
445  vis->visit( this );
446 
447  for ( auto tool : tools() ) vis->visit( dynamic_cast<AlgTool*>( tool ) );
448 }
void initToolHandles() const
Definition: AlgTool.cpp:360
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
#define UNLIKELY(x)
Definition: Kernel.h:96
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
an helper to share the implementation of service() among the various kernel base classes
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: AlgTool.h:285
Define general base for Gaudi exception.
Gaudi::Property< bool > m_auditorStart
Definition: AlgTool.h:302
const SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
StatusCode createService(const std::string &name, const InterfaceID &iid, void **ppSvc) const
bool m_toolHandlesInit
Definition: AlgTool.h:312
std::string m_type
AlgTool type (concrete class name)
Definition: AlgTool.h:278
Implementation of property with value of concrete type.
Definition: Property.h:352
StatusCode initialize() override
Definition: AlgTool.cpp:201
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:298
StatusCode getService(const std::string &name, bool createIf, const InterfaceID &iid, void **ppSvc) const
virtual const std::string & tag() const
name tag for the exception, or exception type
const IInterface * m_parent
AlgTool parent.
Definition: AlgTool.h:280
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
Non-templated base class for actual ToolHandle<T>.
Definition: ToolHandle.h:69
void initDataHandleHolder()
initializes all handles - called by the sysInitialize method of any descendant of this
StatusCode sysReinitialize() override
Initialize AlgTool.
Definition: AlgTool.cpp:276
T end(T... args)
Gaudi::Property< bool > m_auditorReinitialize
Definition: AlgTool.h:305
Data provider interface definition.
~AlgTool() override
Definition: AlgTool.cpp:354
std::vector< IAlgTool * > m_tools
Definition: AlgTool.h:309
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
StatusCode queryInterface(const InterfaceID &riid, void **ppvUnknown) override
Query for a given interface.
Definition: AlgTool.cpp:41
StatusCode setProperties()
Method for setting declared properties to the values specified in the jobOptions via the job option s...
Definition: AlgTool.cpp:107
StatusCode sysStart() override
Start AlgTool.
Definition: AlgTool.cpp:211
Gaudi::StateMachine::State FSMState() const override
Definition: AlgTool.h:74
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
StatusCode sysStop() override
Stop AlgTool.
Definition: AlgTool.cpp:233
StatusCode service_i(const std::string &algName, bool createIf, const InterfaceID &iid, void **ppSvc) const
flag indicating whether ToolHandle tools have been added to m_tools
Definition: AlgTool.cpp:415
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: AlgTool.cpp:79
StatusCode finalize() override
Definition: AlgTool.cpp:268
const std::string m_name
AlgTool full name.
Definition: AlgTool.h:279
virtual void visit(const IDataHandleHolder *)=0
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
T push_back(T... args)
Interface ID class.
Definition: IInterface.h:29
AlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Standard Constructor.
Definition: AlgTool.cpp:118
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Main interface for the JobOptions service.
T what(T... args)
IAuditorSvc * auditorSvc() const
Access the auditor service.
Definition: AlgTool.cpp:433
Gaudi::Property< int > m_outputLevel
Definition: AlgTool.h:292
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
Definition of the basic interface.
Definition: IInterface.h:244
virtual bool hasProperty(const std::string &name) const =0
Return true if we have a property with the given name.
Gaudi::Property< bool > m_auditorStop
Definition: AlgTool.h:303
virtual StatusCode getProperty(Gaudi::Details::PropertyBase *p) const =0
Get the property by property.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
StatusCode service(const std::string &name, T *&svc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: AlgTool.h:128
StatusCode restart() override
Definition: AlgTool.cpp:336
const std::vector< IAlgTool * > & tools() const
Definition: AlgTool.cpp:401
SmartIF< IToolSvc > m_ptoolSvc
Tool service.
Definition: AlgTool.h:284
SmartIF< IService > service(const std::string &name, const bool quiet=false, const bool createIf=true) const
bool isSuccess() const
Definition: StatusCode.h:267
STL class.
void acceptDHVisitor(IDataHandleVisitor *) const override
Definition: AlgTool.cpp:443
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: AlgTool.h:286
Gaudi::Property< bool > m_auditorRestart
Definition: AlgTool.h:306
T get(T... args)
T find_if(T... args)
Gaudi::StateMachine::State m_state
state of the Tool
Definition: AlgTool.h:319
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...
Definition: StateMachine.cpp:9
std::vector< BaseToolHandle * > m_toolHandles
Definition: AlgTool.h:310
T begin(T... args)
Gaudi::Property< bool > m_auditInit
Definition: AlgTool.h:300
IToolSvc * toolSvc() const
The standard ToolSvc service, Return a pointer to the service if present.
Definition: AlgTool.cpp:96
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:47
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
T emplace(T... args)
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition: AlgTool.h:282
appMgr
Definition: IOTest.py:93
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition: AlgTool.h:283
constexpr static const auto FAILURE
Definition: StatusCode.h:86
const std::string & type() const override
Retrieve type (concrete class) of the sub-algtool.
Definition: AlgTool.cpp:65
bool assign(const Details::PropertyBase &source) override
get the value from another property
Definition: Property.h:659
Gaudi::Property< bool > m_auditorInitialize
Definition: AlgTool.h:301
StatusCode reinitialize() override
Definition: AlgTool.cpp:294
InterfaceList m_interfaceList
Interface list.
Definition: AlgTool.h:288
bool isFailure() const
Definition: StatusCode.h:130
StatusCode stop() override
Definition: AlgTool.cpp:247
StatusCode sysInitialize() override
Initialize AlgTool.
Definition: AlgTool.cpp:163
Gaudi::StateMachine::State m_targetState
state of the Tool
Definition: AlgTool.h:320
IDataProviderSvc * evtSvc() const
accessor to event service service
Definition: AlgTool.cpp:88
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:20
It is a simple guard, which "locks" the scope for the Auditor Service is am exception-safe way.
Definition: Guards.h:203
The interface implemented by the IAuditorSvc base class.
Definition: IAuditorSvc.h:15
Base class for all services.
Definition: Service.h:36
virtual void undeclareAll(const IInterface *owner)=0
Undeclare monitoring information.
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition: AlgTool.cpp:58
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
StatusCode sysFinalize() override
Finalize AlgTool.
Definition: AlgTool.cpp:255
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: AlgTool.h:311
std::unique_ptr< IDataHandleVisitor > m_updateDataHandles
Hook for for derived classes to provide a custom visitor for data handles.
Definition: AlgTool.h:222
Gaudi::Property< bool > m_auditorFinalize
Definition: AlgTool.h:304
Base class from which all concrete auditor classes should be derived.
Definition: Auditor.h:34
StatusCode sysRestart() override
Start AlgTool.
Definition: AlgTool.cpp:317
StatusCode start() override
Definition: AlgTool.cpp:225
const IInterface * parent() const override
Retrieve parent of the sub-algtool.
Definition: AlgTool.cpp:72