| The Gaudi Framework v29r0 (ff2e7097)

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

- File List
- File Members

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/CommonMessaging.h"
6 #include "GaudiKernel/IAlgTool.h"
7 #include "GaudiKernel/IAuditorSvc.h"
8 #include "GaudiKernel/IDataProviderSvc.h"
9 #include "GaudiKernel/IMessageSvc.h"
10 #include "GaudiKernel/IMonitorSvc.h"
11 #include "GaudiKernel/IProperty.h"
12 #include "GaudiKernel/IService.h"
13 #include "GaudiKernel/IStateful.h"
14 #include "GaudiKernel/ISvcLocator.h"
15 #include "GaudiKernel/IToolSvc.h"
16 #include "GaudiKernel/PropertyHolder.h"
17 #include "GaudiKernel/ToolHandle.h"
18 #include <Gaudi/PluginService.h>
19
20 #include "GaudiKernel/DataObjIDProperty.h"
21
22 #include "GaudiKernel/DataHandle.h"
23 #include "GaudiKernel/DataHandleHolderBase.h"
24 #include "GaudiKernel/IDataHandleHolder.h"
25
26 template <class T>
27 class DataObjectHandle;
28
29 class ToolHandleInfo;
30
31 #include <list>
32 #include <vector>
33
34 // Forward declarations
35
48 class GAUDI_API AlgTool
49 : public DataHandleHolderBase<
50 PropertyHolder<CommonMessaging<implements<IAlgTool, IDataHandleHolder, IProperty, IStateful>>>>
51 {
52 public:
53 #ifndef __REFLEX__
54 typedef Gaudi::PluginService::Factory<IAlgTool*, const std::string&, const std::string&, const IInterface*> Factory;
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
99 public:
105 AlgTool( const std::string& type, const std::string& name, const IInterface* parent );
106
108 SmartIF<ISvcLocator>& serviceLocator() const override;
109
111 ISvcLocator* svcLoc() const { return serviceLocator(); }
112
116 IDataProviderSvc* evtSvc() const;
117
119 IToolSvc* toolSvc() const;
120
126 StatusCode setProperties();
127
131 template <class T>
132 StatusCode service( const std::string& name, T*& svc, bool createIf = true ) const
133 {
134 return service_i( name, createIf, T::interfaceID(), (void**)&svc );
135 }
136
139 template <class T>
140 StatusCode service( const std::string& type, const std::string& name, T*& svc ) const
141 {
142 return service_i( type, name, T::interfaceID(), (void**)&svc );
143 }
144
146 SmartIF<IService> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const;
147
148 template <typename T>
149 SmartIF<T> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const
150 {
151 return SmartIF<T>( service( name, createIf, quiet ) );
152 }
153
154 protected:
155 template <typename I>
156 void declareInterface( I* i )
157 {
158 m_interfaceList.emplace_back( I::interfaceID(), i );
159 }
160
161 public:
162 using PropertyHolderImpl::declareProperty;
163
164 template <class T>
165 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandle<T>& hndl,
166 const std::string& doc = "none" )
167 {
168 this->declareTool( hndl, hndl.typeAndName() ).ignore();
169 return PropertyHolderImpl::declareProperty( name, hndl, doc );
170 }
171
172 template <class T>
173 StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true )
174 {
175
176 StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
177 if ( UNLIKELY( !sc ) ) {
178 throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
179 " tool " + toolTypeAndName,
180 name(), sc};
181 }
182
183 m_toolHandles.push_back( &handle );
184
185 return sc;
186 }
187
188 // ==========================================================================
189 // declare ToolHandleArrays to the AlgTool
190 template <class T>
191 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandleArray<T>& hndlArr,
192 const std::string& doc = "none" )
193 {
194 addToolsArray( hndlArr );
195 return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
196 }
197
198 template <class T>
199 void addToolsArray( ToolHandleArray<T>& hndlArr )
200 {
201 m_toolHandleArrays.push_back( &hndlArr );
202 }
203
204 public:
205 virtual void acceptDHVisitor( IDataHandleVisitor* ) const override;
206
207 void commitHandles() override;
208
209 public:
210 void registerTool( IAlgTool* tool ) const
211 {
212 if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Registering tool " << tool->name() << endmsg;
213 m_tools.push_back( tool );
214 }
215
216 void deregisterTool( IAlgTool* tool ) const
217 {
218 auto it = std::find( m_tools.begin(), m_tools.end(), tool );
219 if ( it != m_tools.end() ) {
220 if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
221 m_tools.erase( it );
222 } else {
223 if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Could not de-register tool " << tool->name() << endmsg;
224 }
225 }
226
227 const std::vector<IAlgTool*>& tools() const;
228
229 protected:
230 std::vector<IAlgTool*>& tools();
231
233 std::unique_ptr<IDataHandleVisitor> m_updateDataHandles;
234
235 private:
236 // place IAlgTools defined via ToolHandles in m_tools
237 void initToolHandles() const;
238
239 public:
240 // ==========================================================================
242 IAuditorSvc* auditorSvc() const;
243
253 inline IMonitorSvc* monitorSvc() const
254 {
255 // If not already located try to locate it without forcing a creation
256 if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
257 return m_pMonitorSvc.get();
258 }
259
265 template <class T>
266 void declareInfo( const std::string& name, const T& var, const std::string& desc ) const
267 {
268 IMonitorSvc* mS = monitorSvc();
269 if ( mS ) mS->declareInfo( name, var, desc, this );
270 }
271
279 void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
280 const std::string& desc ) const
281 {
282 IMonitorSvc* mS = monitorSvc();
283 if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
284 }
285
286 // Standard destructor.
287 ~AlgTool() override;
288
289 private:
290 typedef std::list<std::pair<InterfaceID, void*>> InterfaceList;
291
292 std::string m_type;
293 const std::string m_name;
294 const IInterface* m_parent = nullptr;
295
296 mutable SmartIF<ISvcLocator> m_svcLocator;
297 mutable SmartIF<IDataProviderSvc> m_evtSvc;
298 mutable SmartIF<IToolSvc> m_ptoolSvc;
299 mutable SmartIF<IMonitorSvc> m_pMonitorSvc;
300 mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
301
302 InterfaceList m_interfaceList;
303
304 // Properties
305 Gaudi::Property<int> m_outputLevel{this, "OutputLevel", MSG::NIL, "output level"};
306
307 Gaudi::Property<std::string> m_monitorSvcName{this, "MonitorService", "MonitorSvc",
308 "name to use for Monitor Service"};
309
310 Gaudi::Property<bool> m_auditInit{this, "AuditTools", false, "[[deprecated]] unused"};
311 Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
312 Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
313 Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
314 Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
315 Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
316 Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
317
318 std::string m_threadID;
319
320 // tools used by tool
321 mutable std::vector<IAlgTool*> m_tools;
322 mutable std::vector<BaseToolHandle*> m_toolHandles;
323 mutable std::vector<GaudiHandleArrayBase*> m_toolHandleArrays;
324 mutable bool m_toolHandlesInit = false;
325
327 StatusCode service_i( const std::string& algName, bool createIf, const InterfaceID& iid, void** ppSvc ) const;
328 StatusCode service_i( const std::string& svcType, const std::string& svcName, const InterfaceID& iid,
329 void** ppS ) const;
330
331 Gaudi::StateMachine::State m_state = Gaudi::StateMachine::CONFIGURED;
332 Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::CONFIGURED;
333 };
334
335 #ifndef GAUDI_NEW_PLUGIN_SERVICE
336 template <class T>
337 struct ToolFactory {
338 template <typename S, typename... Args>
339 static typename S::ReturnType create( Args&&... args )
340 {
341 return new T( std::forward<Args>( args )... );
342 }
343 };
344
345 // Macros to declare component factories
346 #define DECLARE_TOOL_FACTORY( x ) DECLARE_FACTORY_WITH_CREATOR( x, ToolFactory<x>, AlgTool::Factory )
347 #define DECLARE_NAMESPACE_TOOL_FACTORY( n, x ) DECLARE_TOOL_FACTORY( n::x )
348
349 #else
350
351 // Macros to declare component factories
352 #define DECLARE_TOOL_FACTORY( x ) DECLARE_COMPONENT( x )
353 #define DECLARE_NAMESPACE_TOOL_FACTORY( n, x ) DECLARE_COMPONENT( n::x )
354
355 #endif
356
357 #endif // GAUDIKERNEL_ALGTOOL_H
IToolSvc
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
AlgTool::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
Definition: AlgTool.h:79
ToolFactory
Definition: AlgTool.h:337
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:128
PropertyHolder.h
IDataHandleHolder.h
StatusCode::SUCCESS
Definition: StatusCode.h:31
AlgTool::m_pMonitorSvc
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: AlgTool.h:299
SmartIF< ISvcLocator >
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:22
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
AlgTool::m_type
std::string m_type
AlgTool type (concrete class name)
Definition: AlgTool.h:292
Gaudi::StateMachine::CONFIGURED
Definition: StateMachine.h:16
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:319
gaudirun.args
args
Definition: gaudirun.py:244
AlgTool::addToolsArray
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: AlgTool.h:199
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:37
AlgTool::deregisterTool
void deregisterTool(IAlgTool *tool) const
Definition: AlgTool.h:216
IAuditorSvc.h
AlgTool::InterfaceList
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition: AlgTool.h:290
MSG::DEBUG
Definition: IMessageSvc.h:16
ToolHandleArray
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyHolder.h:34
AlgTool::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: AlgTool.h:165
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:14
AlgTool::registerTool
void registerTool(IAlgTool *tool) const
Definition: AlgTool.h:210
IDataProviderSvc
Data provider interface definition.
Definition: IDataProviderSvc.h:45
AlgTool::m_tools
std::vector< IAlgTool * > m_tools
Definition: AlgTool.h:321
IToolSvc.h
std::string
STL class.
AlgTool::m_name
const std::string m_name
AlgTool full name.
Definition: AlgTool.h:293
DataObjectHandle
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:27
AlgTool::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: AlgTool.h:78
InterfaceID
Interface ID class.
Definition: IInterface.h:29
AlgTool::monitorSvc
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition: AlgTool.h:253
extends< BASE, IDataHandleHolder >::queryInterface
StatusCode queryInterface(const InterfaceID &ti, void **pp) override
Implementation of IInterface::queryInterface.
Definition: extends.h:30
IDataProviderSvc.h
ToolFactory::create
static S::ReturnType create(Args &&...args)
Definition: AlgTool.h:339
AlgTool::svcLoc
ISvcLocator * svcLoc() const
shortcut for the method service locator
Definition: AlgTool.h:111
IMonitorSvc
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition: IMonitorSvc.h:24
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:279
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:223
IInterface
Definition of the basic interface.
Definition: IInterface.h:277
gaudirun.type
type
Definition: gaudirun.py:133
DataHandleHolderBase
Definition: DataHandleHolderBase.h:25
CommonMessaging.h
AlgTool::configure
StatusCode configure() override
Definition: AlgTool.h:70
AlgTool::m_threadID
std::string m_threadID
Thread Id for Alg Tool.
Definition: AlgTool.h:318
IAlgTool.h
AlgTool::service
StatusCode service(const std::string &name, T *&svc, bool createIf=true) const
Access a service by name, creating it if it doesn&#39;t already exist.
Definition: AlgTool.h:132
MSG::NIL
Definition: IMessageSvc.h:16
IOTest.start
start
Definition: IOTest.py:99
IMessageSvc.h
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
AlgTool::m_ptoolSvc
SmartIF< IToolSvc > m_ptoolSvc
Tool service.
Definition: AlgTool.h:298
IStateful.h
std::list
STL class.
DataObjIDProperty.h
IService.h
AlgTool::declareTool
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Definition: AlgTool.h:173
DataHandleHolderBase.h
AlgTool::m_pAuditorSvc
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: AlgTool.h:300
AlgTool::declareInfo
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: AlgTool.h:266
IMonitorSvc.h
PluginService.h
IDataHandleVisitor
Definition: IDataHandleHolder.h:38
ToolHandle
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyHolder.h:30
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:185
std::find
T find(T...args)
IProperty.h
std::vector< IAlgTool * >
IMonitorSvc::declareInfo
virtual void declareInfo(const std::string &name, const bool &var, const std::string &desc, const IInterface *owner)=0
Declare monitoring information.
std::unique_ptr< IDataHandleVisitor >
AlgTool::m_toolHandles
std::vector< BaseToolHandle * > m_toolHandles
Definition: AlgTool.h:322
Gaudi::PluginService::Factory
Class wrapping the signature for a factory with any number of arguments.
Definition: PluginService.h:45
AlgTool::service
StatusCode service(const std::string &type, const std::string &name, T *&svc) const
Access a service by name, type creating it if it doesn&#39;t already exist.
Definition: AlgTool.h:140
AlgTool::declareInterface
void declareInterface(I *i)
Definition: AlgTool.h:156
AlgTool::service
SmartIF< T > service(const std::string &name, const bool createIf=true, const bool quiet=false) const
Definition: AlgTool.h:149
AlgTool
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:48
IAlgTool
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
ToolHandle.h
AlgTool::m_svcLocator
SmartIF< ISvcLocator > m_svcLocator
Pointer to Service Locator service.
Definition: AlgTool.h:296
AlgTool::m_evtSvc
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition: AlgTool.h:297
AlgTool::m_interfaceList
InterfaceList m_interfaceList
Interface list.
Definition: AlgTool.h:302
AlgTool::terminate
StatusCode terminate() override
Definition: AlgTool.h:75
IAuditorSvc
The interface implemented by the IAuditorSvc base class.
Definition: IAuditorSvc.h:15
DataHandle.h
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:110
ISvcLocator.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
INamedInterface::name
virtual const std::string & name() const =0
Retrieve the name of the instance.
ToolHandleInfo
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:25
AlgTool::Factory
Gaudi::PluginService::Factory< IAlgTool *, const std::string &, const std::string &, const IInterface * > Factory
Definition: AlgTool.h:54
AlgTool::m_toolHandleArrays
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: AlgTool.h:323
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:233
AlgTool::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: AlgTool.h:191