Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AlgTool.h
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 #pragma once
12 
13 #include <Gaudi/PluginService.h>
15 #include <GaudiKernel/DataObjID.h>
16 #include <GaudiKernel/IAlgTool.h>
21 #include <GaudiKernel/IProperty.h>
22 #include <GaudiKernel/IService.h>
23 #include <GaudiKernel/IStateful.h>
25 #include <GaudiKernel/IToolSvc.h>
27 #include <GaudiKernel/ToolHandle.h>
28 
29 #include <GaudiKernel/DataHandle.h>
32 #include <list>
33 #include <vector>
34 
35 template <class T>
37 class ToolHandleInfo;
38 class ToolSvc;
39 class ToolVisitor;
40 
54  : public DataHandleHolderBase<
55  PropertyHolder<CommonMessaging<implements<IAlgTool, IDataHandleHolder, IProperty, IStateful>>>> {
56  friend ToolSvc;
57  friend class ToolVisitor;
58 
59 public:
60  using Factory = Gaudi::PluginService::Factory<IAlgTool*( const std::string&, const std::string&, const IInterface* )>;
61 
63  StatusCode queryInterface( const InterfaceID& riid, void** ppvUnknown ) override;
64  void const* i_cast( const InterfaceID& riid ) const override;
65 
67  const std::string& name() const override;
68 
70  const std::string& type() const override;
71 
73  const IInterface* parent() const override;
74 
75  // State machine implementation
76  StatusCode configure() override { return StatusCode::SUCCESS; }
77  StatusCode initialize() override;
78  StatusCode start() override;
79  StatusCode stop() override;
80  StatusCode finalize() override;
81  StatusCode terminate() override { return StatusCode::SUCCESS; }
82  StatusCode reinitialize() override;
83  StatusCode restart() override;
84  Gaudi::StateMachine::State FSMState() const override { return m_state; }
85  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
86 
88  StatusCode sysInitialize() override;
89 
91  StatusCode sysStart() override;
92 
94  StatusCode sysStop() override;
95 
97  StatusCode sysFinalize() override;
98 
100  StatusCode sysReinitialize() override;
101 
103  StatusCode sysRestart() override;
104 
105 public:
111  AlgTool( std::string type, std::string name, const IInterface* parent );
112 
114  SmartIF<ISvcLocator>& serviceLocator() const override;
115 
117  ISvcLocator* svcLoc() const { return serviceLocator(); }
118 
122  IDataProviderSvc* evtSvc() const;
123 
125  IToolSvc* toolSvc() const;
126 
128  SmartIF<IService> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const;
129 
130  template <typename T>
131  SmartIF<T> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const {
132  return SmartIF<T>( service( name, createIf, quiet ) );
133  }
134 
135 protected:
136  template <typename I>
137  void declareInterface( I* i ) {
138  m_interfaceList.emplace_back( I::interfaceID(), i );
139  }
140 
141 public:
142  using PropertyHolderImpl::declareProperty;
143 
144  template <class T>
146  const std::string& doc = "none" ) {
147  this->declareTool( hndl, hndl.typeAndName() ).ignore();
148  return PropertyHolderImpl::declareProperty( name, hndl, doc );
149  }
150 
151  template <class T>
152  StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
153  return this->declareTool( handle, handle.typeAndName(), createIf );
154  }
155 
156  template <class T>
157  StatusCode declareTool( ToolHandle<T>& handle, const std::string& toolTypeAndName, bool createIf = true ) {
158 
159  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
160  if ( !sc ) {
161  throw GaudiException{ std::string{ "Cannot create handle for " } + ( handle.isPublic() ? "public" : "private" ) +
162  " tool " + toolTypeAndName,
163  name(), sc };
164  }
165 
166  m_toolHandles.push_back( &handle );
167 
168  return sc;
169  }
170 
171  // declare ToolHandleArrays to the AlgTool
172  template <class T>
174  const std::string& doc = "none" ) {
175  addToolsArray( hndlArr );
176  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
177  }
178 
179  template <class T>
181  m_toolHandleArrays.push_back( &hndlArr );
182  }
183 
184 public:
185  void acceptDHVisitor( IDataHandleVisitor* ) const override;
186 
187 public:
188  void registerTool( IAlgTool* tool ) const {
189  if ( msgLevel( MSG::DEBUG ) ) debug() << "Registering tool " << tool->name() << endmsg;
190  m_tools.push_back( tool );
191  }
192 
193  void deregisterTool( IAlgTool* tool ) const {
194  auto it = std::find( m_tools.begin(), m_tools.end(), tool );
195  if ( it != m_tools.end() ) {
196  if ( msgLevel( MSG::DEBUG ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
197  m_tools.erase( it );
198  } else {
199  if ( msgLevel( MSG::DEBUG ) ) debug() << "Could not de-register tool " << tool->name() << endmsg;
200  }
201  }
202 
203  const std::vector<IAlgTool*>& tools() const;
204 
205 protected:
206  std::vector<IAlgTool*>& tools();
207 
209  std::unique_ptr<IDataHandleVisitor> m_updateDataHandles;
210 
211 private:
212  // place IAlgTools defined via ToolHandles in m_tools
213  void initToolHandles() const;
214 
215 public:
217  IAuditorSvc* auditorSvc() const;
218 
228  inline IMonitorSvc* monitorSvc() const {
229  // If not already located try to locate it without forcing a creation
230  if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
231  return m_pMonitorSvc.get();
232  }
233 
239  template <class T>
240  void declareInfo( const std::string& name, const T& var, const std::string& desc ) const {
241  IMonitorSvc* mS = monitorSvc();
242  if ( mS ) mS->declareInfo( name, var, desc, this );
243  }
244 
252  void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
253  const std::string& desc ) const {
254  IMonitorSvc* mS = monitorSvc();
255  if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
256  }
257 
258  // Standard destructor.
259  ~AlgTool() override;
260 
261 private:
262  typedef std::list<std::pair<InterfaceID, void*>> InterfaceList;
263 
264  std::string m_type;
265  const std::string m_name;
266  const IInterface* m_parent = nullptr;
267 
273 
275 
276  // Properties
277  // initialize output level from MessageSvc and initialize messaging (before enabling update handler)
278  Gaudi::Property<int> m_outputLevel{
279  this, "OutputLevel", setUpMessaging(),
280  [this]( Gaudi::Details::PropertyBase& ) { this->updateMsgStreamOutputLevel( this->m_outputLevel ); },
281  "output level" };
282 
283  Gaudi::Property<std::string> m_monitorSvcName{ this, "MonitorService", "MonitorSvc",
284  "name to use for Monitor Service" };
285 
286  Gaudi::Property<bool> m_auditorInitialize{ this, "AuditInitialize", false, "trigger auditor on initialize()" };
287  Gaudi::Property<bool> m_auditorStart{ this, "AuditStart", false, "trigger auditor on start()" };
288  Gaudi::Property<bool> m_auditorStop{ this, "AuditStop", false, "trigger auditor on stop()" };
289  Gaudi::Property<bool> m_auditorFinalize{ this, "AuditFinalize", false, "trigger auditor on finalize()" };
290  Gaudi::Property<bool> m_auditorReinitialize{ this, "AuditReinitialize", false, "trigger auditor on reinitialize()" };
291  Gaudi::Property<bool> m_auditorRestart{ this, "AuditRestart", false, "trigger auditor on restart()" };
292 
293  // tools used by tool
294  mutable std::vector<IAlgTool*> m_tools;
295  mutable std::vector<BaseToolHandle*> m_toolHandles;
296  mutable std::vector<GaudiHandleArrayBase*> m_toolHandleArrays;
297  mutable bool m_toolHandlesInit = false;
298 
301 };
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:22
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: PropertyBase.h:34
AlgTool::service
SmartIF< T > service(std::string_view name, const bool createIf=true, const bool quiet=false) const
Definition: AlgTool.h:131
AlgTool::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: AlgTool.h:173
DataHandleHolderBase
Definition: DataHandleHolderBase.h:33
AlgTool::svcLoc
ISvcLocator * svcLoc() const
shortcut for the method service locator
Definition: AlgTool.h:117
IService.h
IAlgTool
Definition: IAlgTool.h:29
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
AlgTool::m_pMonitorSvc
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: AlgTool.h:271
AlgTool::m_toolHandles
std::vector< BaseToolHandle * > m_toolHandles
Definition: AlgTool.h:295
AlgTool::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
Definition: AlgTool.h:85
AlgTool::addToolsArray
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: AlgTool.h:180
AlgTool::m_updateDataHandles
std::unique_ptr< IDataHandleVisitor > m_updateDataHandles
Hook for for derived classes to provide a custom visitor for data handles.
Definition: AlgTool.h:209
AlgTool::InterfaceList
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition: AlgTool.h:262
AlgTool::declareInterface
void declareInterface(I *i)
Definition: AlgTool.h:137
AlgTool::declareTool
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: AlgTool.h:152
AlgTool::registerTool
void registerTool(IAlgTool *tool) const
Definition: AlgTool.h:188
AlgTool::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition: AlgTool.h:268
ISvcLocator
Definition: ISvcLocator.h:42
GaudiException
Definition: GaudiException.h:29
extends< BASE, IDataHandleHolder >::i_cast
void const * i_cast(const InterfaceID &tid) const override
Implementation of IInterface::i_cast.
Definition: extends.h:30
AlgTool::m_evtSvc
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition: AlgTool.h:269
AlgTool::declareInfo
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:252
DataObjID.h
AlgTool::m_tools
std::vector< IAlgTool * > m_tools
Definition: AlgTool.h:294
ToolSvc
Definition: ToolSvc.cpp:45
IMonitorSvc
Definition: IMonitorSvc.h:32
IOTest.start
start
Definition: IOTest.py:110
IMessageSvc.h
IAuditorSvc.h
IDataProviderSvc.h
AlgTool::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: AlgTool.h:84
DataObjectHandle
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:36
AlgTool::ToolSvc
friend ToolSvc
Definition: AlgTool.h:56
IToolSvc.h
AlgTool::declareInfo
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: AlgTool.h:240
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:21
PluginService.h
ToolHandle
Definition: ToolHandle.h:130
bug_34121.tool
tool
Definition: bug_34121.py:18
Gaudi::StateMachine::CONFIGURED
@ CONFIGURED
Definition: StateMachine.h:23
AlgTool::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: AlgTool.h:145
StatusCode
Definition: StatusCode.h:64
AlgTool::monitorSvc
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition: AlgTool.h:228
IMonitorSvc::declareInfo
virtual void declareInfo(const std::string &name, const bool &var, const std::string &desc, const IInterface *owner)=0
Declare monitoring information.
AlgTool::terminate
StatusCode terminate() override
Definition: AlgTool.h:81
ToolHandleInfo
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:40
IAlgTool.h
ToolHandleArray
Definition: ToolHandle.h:397
AlgTool::m_type
std::string m_type
AlgTool type (concrete class name)
Definition: AlgTool.h:264
AlgTool::Factory
Gaudi::PluginService::Factory< IAlgTool *(const std::string &, const std::string &, const IInterface *)> Factory
Definition: AlgTool.h:60
IStateful.h
AlgTool::m_ptoolSvc
SmartIF< IToolSvc > m_ptoolSvc
Tool service.
Definition: AlgTool.h:270
AlgTool::m_toolHandleArrays
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: AlgTool.h:296
SmartIF< ISvcLocator >
CommonMessaging.h
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:93
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:198
IDataHandleVisitor
Definition: IDataHandleHolder.h:45
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:49
IMonitorSvc.h
AlgTool::m_interfaceList
InterfaceList m_interfaceList
Interface list.
Definition: AlgTool.h:274
gaudirun.type
type
Definition: gaudirun.py:160
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:99
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
IAuditorSvc
The interface implemented by the IAuditorSvc base class.
Definition: IAuditorSvc.h:24
AlgTool
Definition: AlgTool.h:55
IDataHandleHolder.h
DataHandleHolderBase.h
ToolHandle.h
AlgTool::deregisterTool
void deregisterTool(IAlgTool *tool) const
Definition: AlgTool.h:193
extends< BASE, IDataHandleHolder >::queryInterface
StatusCode queryInterface(const InterfaceID &ti, void **pp) override
Implementation of IInterface::queryInterface.
Definition: extends.h:37
AlgTool::declareTool
StatusCode declareTool(ToolHandle< T > &handle, const std::string &toolTypeAndName, bool createIf=true)
Definition: AlgTool.h:157
IInterface
Definition: IInterface.h:225
DataHandle.h
AlgTool::m_name
const std::string m_name
AlgTool full name.
Definition: AlgTool.h:265
AlgTool::m_pAuditorSvc
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: AlgTool.h:272
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:211
IProperty.h
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:271
IDataProviderSvc
Definition: IDataProviderSvc.h:48
InterfaceID
Definition: IInterface.h:38
ISvcLocator.h
IToolSvc
Definition: IToolSvc.h:28
PropertyHolder.h
AlgTool::configure
StatusCode configure() override
Definition: AlgTool.h:76
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
Gaudi::Property< int >