All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AlgTool.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_ALGTOOL_H
2 #define GAUDIKERNEL_ALGTOOL_H
3 // ============================================================================
4 // Include files
5 #include "GaudiKernel/IAlgTool.h"
7 #include "GaudiKernel/IService.h"
11 #include "GaudiKernel/IToolSvc.h"
15 #include "GaudiKernel/IStateful.h"
16 #include <Gaudi/PluginService.h>
17 #include "GaudiKernel/ToolHandle.h"
19 
21 #include "GaudiKernel/DataHandle.h"
22 
23 template<class T>
25 
26 class ToolHandleInfo;
27 
28 #include <vector>
29 #include <list>
30 
31 // Forward declarations
32 
45 class GAUDI_API AlgTool: public CommonMessaging<implements<IAlgTool,
46  IDataHandleHolder,
47  IProperty,
48  IStateful>> {
49 public:
50 #ifndef __REFLEX__
52  const std::string&,
53  const std::string&,
55 #endif
56 
58  StatusCode queryInterface(const InterfaceID& riid, void** ppvUnknown) override;
59 
61  const std::string& name() const override;
62 
64  const std::string& type() const override;
65 
67  const IInterface* parent() const override;
68 
69  // State machine implementation
70  StatusCode configure() override { return StatusCode::SUCCESS; }
71  StatusCode initialize() override;
72  StatusCode start() override;
73  StatusCode stop() override;
74  StatusCode finalize() override;
75  StatusCode terminate() override { return StatusCode::SUCCESS; }
76  StatusCode reinitialize() override;
77  StatusCode restart() override;
78  Gaudi::StateMachine::State FSMState() const override { return m_state; }
79  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
80 
82  StatusCode sysInitialize() override;
83 
85  StatusCode sysStart() override;
86 
88  StatusCode sysStop() override;
89 
91  StatusCode sysFinalize() override;
92 
94  StatusCode sysReinitialize() override;
95 
97  StatusCode sysRestart() override;
98 
100  StatusCode setProperty( const Property& p ) override;
101  StatusCode setProperty( const std::string& s ) override;
102  StatusCode setProperty( const std::string& n, const std::string& v) override;
103  StatusCode getProperty(Property* p) const override;
104  const Property& getProperty( const std::string& name) const override;
105  StatusCode getProperty( const std::string& n, std::string& v ) const override;
106  const std::vector<Property*>& getProperties( ) const override;
107  bool hasProperty(const std::string& name) const override;
108 
109  inline PropertyMgr * getPropertyMgr() const { return m_propertyMgr; }
110 
111 public:
112 
153  template <class TYPE>
155  ( const std::string& name ,
156  const TYPE& value )
157  { return Gaudi::Utils::setProperty ( m_propertyMgr.get() , name , value ) ; }
158 
159 
165  AlgTool( const std::string& type,
166  const std::string& name,
167  const IInterface* parent);
168 
170  SmartIF<ISvcLocator>& serviceLocator() const override;
171 
173  ISvcLocator* svcLoc() const { return serviceLocator() ; }
174 
178  IDataProviderSvc* evtSvc () const ;
179 
181  IToolSvc* toolSvc() const;
182 
188  StatusCode setProperties();
189 
193  template <class T>
194  StatusCode service
195  ( const std::string& name, T*& svc, bool createIf = true ) const {
196  return service_i(name, createIf, T::interfaceID(), (void**)&svc);
197  }
198 
201  template <class T>
202  StatusCode service( const std::string& type, const std::string& name, T*& svc) const {
203  return service_i(type, name, T::interfaceID(), (void**)&svc);
204  }
205 
207  SmartIF<IService> service(const std::string& name, const bool createIf = true, const bool quiet = false) const;
208 
209  template <typename T>
210  SmartIF<T> service(const std::string& name, const bool createIf = true, const bool quiet = false) const
211  { return SmartIF<T>( service(name,createIf,quiet) ); }
212 
213 protected:
214  template <typename I>
215  void declareInterface( I* i ) { m_interfaceList.emplace_back( I::interfaceID(), i ); }
216 public:
217  // ==========================================================================
247  template <class T>
248  Property* declareProperty
249  ( const std::string& name ,
250  T& property ,
251  const std::string& doc = "none" ) const
252  {
253  return m_propertyMgr -> declareProperty ( name , property , doc ) ;
254  }
256  Property* declareRemoteProperty
257  ( const std::string& name ,
258  IProperty* rsvc ,
259  const std::string& rname = "" ) const
260  {
261  return m_propertyMgr-> declareRemoteProperty ( name , rsvc , rname ) ;
262  }
263 
264  template<class T>
265 
266  Property* declareProperty(const std::string& name,
267  ToolHandle<T>& hndl,
268  const std::string& doc = "none" ) const {
269 
270  AlgTool* a = const_cast<AlgTool*>(this);
271  a->declareTool(hndl).ignore();
272 
273  return m_propertyMgr->declareProperty(name, hndl, doc);
274 
275  }
276 
277  template<class T>
279  std::string toolTypeAndName = "",
280  bool createIf = true) {
281  if (handle.isPublic()) {
282  return declarePublicTool(handle, toolTypeAndName, createIf);
283  } else {
284  return declarePrivateTool(handle, toolTypeAndName, createIf);
285  }
286  }
287 
288  // ==========================================================================
289  // declare ToolHandleArrays to the AlgTool
290  template <class T>
291  Property* declareProperty(const std::string& name,
292  ToolHandleArray<T>& hndlArr,
293  const std::string& doc = "none" ) const {
294  m_toolHandleArrays.push_back( &hndlArr );
295  return m_propertyMgr->declareProperty(name, hndlArr, doc);
296  }
297 
298  protected:
299  virtual void declareInput(Gaudi::DataHandle* im) override {
300  m_inputHandles.push_back(im);
301  }
302 
303  virtual void declareOutput(Gaudi::DataHandle* im) override {
304  m_outputHandles.push_back(im);
305  }
306  public:
307 
309  return m_inputHandles; }
311  return m_outputHandles; }
312 
313  virtual const DataObjIDColl& extraInputDeps() const override {
314  return m_extInputDataObjs;
315  }
316  virtual const DataObjIDColl& extraOutputDeps() const override {
317  return m_extOutputDataObjs;
318  }
319 
320 
321  virtual void acceptDHVisitor(IDataHandleVisitor*) const override;
322 
323  DataObjIDColl inputDataObjs() const { return m_inputDataObjs; }
324  DataObjIDColl outputDataObjs() const { return m_outputDataObjs; }
325 
326  void commitHandles() override;
327 
328 
329  private:
331  DataObjIDColl m_inputDataObjs, m_outputDataObjs;
332 
333  DataObjIDColl m_extInputDataObjs, m_extOutputDataObjs;
334 
335  public:
336 
337 
338 
339  void registerTool(IAlgTool * tool) const {
341  debug() << "Registering tool " << tool->name() << endmsg;
342  m_tools.push_back(tool);
343  }
344 
345  void deregisterTool(IAlgTool * tool) const {
346  auto it = std::find(m_tools.begin(), m_tools.end(), tool);
347  if (it != m_tools.end()) {
349  debug() << "De-Registering tool " << tool->name() << endmsg;
350  m_tools.erase(it);
351  } else {
353  debug() << "Could not de-register tool " << tool->name() << endmsg;
354  }
355  }
356 
364  template<class T>
366  std::string toolTypeAndName = "",
367  bool createIf = true) {
368 
369  if (toolTypeAndName == "")
370  toolTypeAndName = handle.typeAndName();
371 
372  StatusCode sc = handle.initialize(toolTypeAndName, 0, createIf);
373  if (UNLIKELY(!sc)) {
374  throw GaudiException{"Cannot create handle for public tool " + toolTypeAndName,
375  name(), sc};
376  }
377 
378  m_toolHandles.push_back(&handle);
379 
380  return sc;
381  }
382 
390  template<class T>
392  std::string toolTypeAndName = "",
393  bool createIf = true) {
394 
395  if (toolTypeAndName == "")
396  toolTypeAndName = handle.typeAndName();
397 
398  StatusCode sc = handle.initialize(toolTypeAndName, this, createIf);
399  if (UNLIKELY(!sc)) {
400  throw GaudiException{"Cannot create handle for private tool " + toolTypeAndName,
401  name(), sc};
402  }
403 
404  m_toolHandles.push_back(&handle);
405 
406  return sc;
407  }
408 
409  const std::vector<IAlgTool *> & tools() const;
410 
411 protected:
412 
413  std::vector<IAlgTool *> & tools();
414 
417 
418 private:
419  //place IAlgTools defined via ToolHandles in m_tools
420  void initToolHandles() const;
421 
422 public:
423 
424  // ==========================================================================
426  IAuditorSvc* auditorSvc() const;
427 
437  inline IMonitorSvc* monitorSvc() const
438  {
439  // If not already located try to locate it without forcing a creation
440  if ( !m_pMonitorSvc ) m_pMonitorSvc = service(m_monitorSvcName, false, true);
441  return m_pMonitorSvc.get();
442  }
443 
449  template <class T>
450  void declareInfo( const std::string& name,
451  const T& var,
452  const std::string& desc ) const
453  {
454  IMonitorSvc* mS = monitorSvc();
455  if ( mS ) mS->declareInfo(name, var, desc, this);
456  }
457 
465  void declareInfo( const std::string& name,
466  const std::string& format,
467  const void* var,
468  int size,
469  const std::string& desc ) const
470  {
471  IMonitorSvc* mS = monitorSvc();
472  if ( mS ) mS->declareInfo(name, format, var, size, desc, this);
473  }
474 
475  // Standard destructor.
476  ~AlgTool() override;
477 
478 private:
480  IntegerProperty m_outputLevel = MSG::NIL;
481  std::string m_type;
482  const std::string m_name;
483  const IInterface* m_parent = nullptr;
488  std::string m_monitorSvcName;
489  protected:
491  private:
492  InterfaceList m_interfaceList;
493  std::string m_threadID;
494 
495  //tools used by tool
499  mutable bool m_toolHandlesInit = false;
500 
502  StatusCode service_i(const std::string& algName,
503  bool createIf,
504  const InterfaceID& iid,
505  void** ppSvc) const;
506  StatusCode service_i(const std::string& svcType,
507  const std::string& svcName,
508  const InterfaceID& iid,
509  void** ppS) const;
510 
512 
513  BooleanProperty m_auditInit = false;
514  bool m_auditorInitialize = false;
515  bool m_auditorStart = false;
516  bool m_auditorStop = false;
517  bool m_auditorFinalize = false;
518  bool m_auditorReinitialize = false;
519  bool m_auditorRestart = false;
520 
523 };
524 
525 
526 #ifndef GAUDI_NEW_PLUGIN_SERVICE
527 template <class T>
528 struct ToolFactory {
529  template <typename S, typename... Args>
530  static typename S::ReturnType create(Args&&... args) {
531  return new T(std::forward<Args>(args)...);
532  }
533 };
534 
535 // Macros to declare component factories
536 #define DECLARE_TOOL_FACTORY(x) \
537  DECLARE_FACTORY_WITH_CREATOR(x, ToolFactory< x >, AlgTool::Factory)
538 #define DECLARE_NAMESPACE_TOOL_FACTORY(n, x) \
539  DECLARE_TOOL_FACTORY(n::x)
540 
541 #else
542 
543 // Macros to declare component factories
544 #define DECLARE_TOOL_FACTORY(x) DECLARE_COMPONENT(x)
545 #define DECLARE_NAMESPACE_TOOL_FACTORY(n,x) DECLARE_COMPONENT(n::x)
546 
547 #endif
548 
549 
550 #endif // GAUDIKERNEL_ALGTOOL_H
551 
std::string typeAndName() const override
Definition: ToolHandle.h:199
StatusCode setProperty(IProperty *component, const std::string &name, const TYPE &value, const std::string &doc)
simple function to set the property of the given object from the value
Definition: Property.h:1187
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:18
#define UNLIKELY(x)
Definition: Kernel.h:126
std::vector< IAlgTool * > m_tools
Definition: AlgTool.h:496
std::vector< BaseToolHandle * > m_toolHandles
Definition: AlgTool.h:497
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: AlgTool.h:487
Define general base for Gaudi exception.
def initialize()
Definition: AnalysisTest.py:12
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
std::string m_type
AlgTool type (concrete class name)
Definition: AlgTool.h:481
DataObjIDColl inputDataObjs() const
Definition: AlgTool.h:323
StatusCode declarePrivateTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used private tool.
Definition: AlgTool.h:391
void deregisterTool(IAlgTool *tool) const
Definition: AlgTool.h:345
GAUDI_API bool hasProperty(const IProperty *p, const std::string &name)
simple function which check the existence of the property with the given name.
Definition: Property.cpp:159
Property * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none") const
Definition: AlgTool.h:266
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyMgr.h:24
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:12
void registerTool(IAlgTool *tool) const
Definition: AlgTool.h:339
Data provider interface definition.
const char *PyHelper() getProperty(IInterface *p, char *name)
Definition: Bootstrap.cpp:258
Property manager helper class.
Definition: PropertyMgr.h:37
DataObjIDColl outputDataObjs() const
Definition: AlgTool.h:324
Gaudi::StateMachine::State FSMState() const override
Definition: AlgTool.h:78
virtual const std::string & name() const =0
Retrieve the name of the instance.
STL class.
std::string m_monitorSvcName
Name to use for Monitor Service.
Definition: AlgTool.h:488
const std::string m_name
AlgTool full name.
Definition: AlgTool.h:482
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:24
Interface ID class.
Definition: IInterface.h:30
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition: AlgTool.h:437
SmartIF< PropertyMgr > m_propertyMgr
Property Manager.
Definition: AlgTool.h:490
static S::ReturnType create(Args &&...args)
Definition: AlgTool.h:530
ISvcLocator * svcLoc() const
shortcut for the method service locator
Definition: AlgTool.h:173
string type
Definition: gaudirun.py:151
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition: IMonitorSvc.h:21
void declareInfo(const std::string &name, const std::string &format, const void *var, int size, const std::string &desc) const
Declare monitoring information (special case)
Definition: AlgTool.h:465
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Definition of the basic interface.
Definition: IInterface.h:234
Gaudi::StateMachine::State targetFSMState() const override
Definition: AlgTool.h:79
StatusCode configure() override
Definition: AlgTool.h:70
virtual const DataObjIDColl & extraOutputDeps() const override
Definition: AlgTool.h:316
std::string m_threadID
Thread Id for Alg Tool.
Definition: AlgTool.h:493
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:254
SmartIF< IToolSvc > m_ptoolSvc
Tool service.
Definition: AlgTool.h:486
STL class.
virtual void declareInput(Gaudi::DataHandle *im) override
Definition: AlgTool.h:299
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: AlgTool.h:511
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: AlgTool.h:450
bool isPublic() const
Definition: ToolHandle.h:34
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyMgr.h:22
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:157
T find(T...args)
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Definition: AlgTool.h:278
virtual void declareInfo(const std::string &name, const bool &var, const std::string &desc, const IInterface *owner)=0
Declare monitoring information.
virtual const DataObjIDColl & extraInputDeps() const override
Definition: AlgTool.h:313
list args
Definition: gaudirun.py:290
DataObjIDColl m_outputDataObjs
Definition: AlgTool.h:331
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Class wrapping the signature for a factory with any number of arguments.
Definition: PluginService.h:47
std::vector< Gaudi::DataHandle * > m_outputHandles
Definition: AlgTool.h:330
PropertyMgr * getPropertyMgr() const
Definition: AlgTool.h:109
StatusCode service(const std::string &type, const std::string &name, T *&svc) const
Access a service by name, type creating it if it doesn't already exist.
Definition: AlgTool.h:202
void declareInterface(I *i)
Definition: AlgTool.h:215
StatusCode declarePublicTool(ToolHandle< T > &handle, std::string toolTypeAndName="", bool createIf=true)
Declare used public tool.
Definition: AlgTool.h:365
SmartIF< T > service(const std::string &name, const bool createIf=true, const bool quiet=false) const
Definition: AlgTool.h:210
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:45
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
Property * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none") const
Definition: AlgTool.h:291
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition: AlgTool.h:484
string s
Definition: gaudirun.py:245
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition: AlgTool.h:485
InterfaceList m_interfaceList
Interface list.
Definition: AlgTool.h:492
void ignore() const
Definition: StatusCode.h:108
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:21
StatusCode terminate() override
Definition: AlgTool.h:75
The interface implemented by the IAuditorSvc base class.
Definition: IAuditorSvc.h:15
virtual std::vector< Gaudi::DataHandle * > inputHandles() const override
Definition: AlgTool.h:308
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
virtual std::vector< Gaudi::DataHandle * > outputHandles() const override
Definition: AlgTool.h:310
#define GAUDI_API
Definition: Kernel.h:107
list i
Definition: ana.py:128
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition: AlgTool.h:479
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
virtual void declareOutput(Gaudi::DataHandle *im) override
Definition: AlgTool.h:303
tuple start
Definition: IOTest.py:88
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:25
DataObjIDColl m_extOutputDataObjs
Definition: AlgTool.h:333
Gaudi::PluginService::Factory< IAlgTool *, const std::string &, const std::string &, const IInterface * > Factory
Definition: AlgTool.h:54
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: AlgTool.h:498
std::unique_ptr< IDataHandleVisitor > m_updateDataHandles
Hook for for derived classes to provide a custom visitor for data handles.
Definition: AlgTool.h:416