| The Gaudi Framework v32r1 (f65d50dc)

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/DataObjID.h"
7 #include "GaudiKernel/IAlgTool.h"
8 #include "GaudiKernel/IAuditorSvc.h"
9 #include "GaudiKernel/IDataProviderSvc.h"
10 #include "GaudiKernel/IMessageSvc.h"
11 #include "GaudiKernel/IMonitorSvc.h"
12 #include "GaudiKernel/IProperty.h"
13 #include "GaudiKernel/IService.h"
14 #include "GaudiKernel/IStateful.h"
15 #include "GaudiKernel/ISvcLocator.h"
16 #include "GaudiKernel/IToolSvc.h"
17 #include "GaudiKernel/PropertyHolder.h"
18 #include "GaudiKernel/ToolHandle.h"
19 #include <Gaudi/PluginService.h>
20
21 #include "GaudiKernel/DataHandle.h"
22 #include "GaudiKernel/DataHandleHolderBase.h"
23 #include "GaudiKernel/IDataHandleHolder.h"
24
25 template <class T>
26 class DataObjectHandle;
27
28 class ToolHandleInfo;
29
30 #include <list>
31 #include <vector>
32
33 // Forward declarations
34
47 class GAUDI_API AlgTool
48 : public DataHandleHolderBase<
49 PropertyHolder<CommonMessaging<implements<IAlgTool, IDataHandleHolder, IProperty, IStateful>>>> {
50 public:
51 using Factory = Gaudi::PluginService::Factory<IAlgTool*( const std::string&, const std::string&, const IInterface* )>;
52
54 StatusCode queryInterface( const InterfaceID& riid, void** ppvUnknown ) override;
55
57 const std::string& name() const override;
58
60 const std::string& type() const override;
61
63 const IInterface* parent() const override;
64
65 // State machine implementation
66 StatusCode configure() override { return StatusCode::SUCCESS; }
67 StatusCode initialize() override;
68 StatusCode start() override;
69 StatusCode stop() override;
70 StatusCode finalize() override;
71 StatusCode terminate() override { return StatusCode::SUCCESS; }
72 StatusCode reinitialize() override;
73 StatusCode restart() override;
74 Gaudi::StateMachine::State FSMState() const override { return m_state; }
75 Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
76
78 StatusCode sysInitialize() override;
79
81 StatusCode sysStart() override;
82
84 StatusCode sysStop() override;
85
87 StatusCode sysFinalize() override;
88
90 StatusCode sysReinitialize() override;
91
93 StatusCode sysRestart() override;
94
95 public:
101 AlgTool( const std::string& type, const std::string& name, const IInterface* parent );
102
104 SmartIF<ISvcLocator>& serviceLocator() const override;
105
107 ISvcLocator* svcLoc() const { return serviceLocator(); }
108
112 IDataProviderSvc* evtSvc() const;
113
115 IToolSvc* toolSvc() const;
116
122 StatusCode setProperties();
123
127 template <class T>
128 StatusCode service( const std::string& name, T*& svc, bool createIf = true ) const {
129 return service_i( name, createIf, T::interfaceID(), (void**)&svc );
130 }
131
134 template <class T>
135 StatusCode service( const std::string& type, const std::string& name, T*& svc ) const {
136 return service_i( type, name, T::interfaceID(), reinterpret_cast<void**>( &svc ) );
137 }
138
140 SmartIF<IService> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const;
141
142 template <typename T>
143 SmartIF<T> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const {
144 return SmartIF<T>( service( name, createIf, quiet ) );
145 }
146
147 protected:
148 template <typename I>
149 void declareInterface( I* i ) {
150 m_interfaceList.emplace_back( I::interfaceID(), i );
151 }
152
153 public:
154 using PropertyHolderImpl::declareProperty;
155
156 template <class T>
157 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandle<T>& hndl,
158 const std::string& doc = "none" ) {
159 this->declareTool( hndl, hndl.typeAndName() ).ignore();
160 return PropertyHolderImpl::declareProperty( name, hndl, doc );
161 }
162
163 template <class T>
164 StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
165 return this->declareTool( handle, handle.typeAndName(), createIf );
166 }
167
168 template <class T>
169 StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true ) {
170
171 StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
172 if ( UNLIKELY( !sc ) ) {
173 throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
174 " tool " + toolTypeAndName,
175 name(), sc};
176 }
177
178 m_toolHandles.push_back( &handle );
179
180 return sc;
181 }
182
183 // ==========================================================================
184 // declare ToolHandleArrays to the AlgTool
185 template <class T>
186 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, ToolHandleArray<T>& hndlArr,
187 const std::string& doc = "none" ) {
188 addToolsArray( hndlArr );
189 return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
190 }
191
192 template <class T>
193 void addToolsArray( ToolHandleArray<T>& hndlArr ) {
194 m_toolHandleArrays.push_back( &hndlArr );
195 }
196
197 public:
198 void acceptDHVisitor( IDataHandleVisitor* ) const override;
199
200 public:
201 void registerTool( IAlgTool* tool ) const {
202 if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Registering tool " << tool->name() << endmsg;
203 m_tools.push_back( tool );
204 }
205
206 void deregisterTool( IAlgTool* tool ) const {
207 auto it = std::find( m_tools.begin(), m_tools.end(), tool );
208 if ( it != m_tools.end() ) {
209 if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "De-Registering tool " << tool->name() << endmsg;
210 m_tools.erase( it );
211 } else {
212 if ( UNLIKELY( msgLevel( MSG::DEBUG ) ) ) debug() << "Could not de-register tool " << tool->name() << endmsg;
213 }
214 }
215
216 const std::vector<IAlgTool*>& tools() const;
217
218 protected:
219 std::vector<IAlgTool*>& tools();
220
222 std::unique_ptr<IDataHandleVisitor> m_updateDataHandles;
223
224 private:
225 // place IAlgTools defined via ToolHandles in m_tools
226 void initToolHandles() const;
227
228 public:
229 // ==========================================================================
231 IAuditorSvc* auditorSvc() const;
232
242 inline IMonitorSvc* monitorSvc() const {
243 // If not already located try to locate it without forcing a creation
244 if ( !m_pMonitorSvc ) m_pMonitorSvc = service( m_monitorSvcName, false, true );
245 return m_pMonitorSvc.get();
246 }
247
253 template <class T>
254 void declareInfo( const std::string& name, const T& var, const std::string& desc ) const {
255 IMonitorSvc* mS = monitorSvc();
256 if ( mS ) mS->declareInfo( name, var, desc, this );
257 }
258
266 void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
267 const std::string& desc ) const {
268 IMonitorSvc* mS = monitorSvc();
269 if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
270 }
271
272 // Standard destructor.
273 ~AlgTool() override;
274
275 private:
276 typedef std::list<std::pair<InterfaceID, void*>> InterfaceList;
277
278 std::string m_type;
279 const std::string m_name;
280 const IInterface* m_parent = nullptr;
281
282 mutable SmartIF<ISvcLocator> m_svcLocator;
283 mutable SmartIF<IDataProviderSvc> m_evtSvc;
284 mutable SmartIF<IToolSvc> m_ptoolSvc;
285 mutable SmartIF<IMonitorSvc> m_pMonitorSvc;
286 mutable SmartIF<IAuditorSvc> m_pAuditorSvc;
287
288 InterfaceList m_interfaceList;
289
290 // Properties
291 // initialize output level from MessageSvc and initialize messaging (before enabling update handler)
292 Gaudi::Property<int> m_outputLevel{
293 this, "OutputLevel", setUpMessaging(),
294 [this]( Gaudi::Details::PropertyBase& ) { this->updateMsgStreamOutputLevel( this->m_outputLevel ); },
295 "output level"};
296
297 Gaudi::Property<std::string> m_monitorSvcName{this, "MonitorService", "MonitorSvc",
298 "name to use for Monitor Service"};
299
300 Gaudi::Property<bool> m_auditInit{this, "AuditTools", false, "[[deprecated]] unused"};
301 Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", false, "trigger auditor on initialize()"};
302 Gaudi::Property<bool> m_auditorStart{this, "AuditStart", false, "trigger auditor on start()"};
303 Gaudi::Property<bool> m_auditorStop{this, "AuditStop", false, "trigger auditor on stop()"};
304 Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", false, "trigger auditor on finalize()"};
305 Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", false, "trigger auditor on reinitialize()"};
306 Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", false, "trigger auditor on restart()"};
307
308 // tools used by tool
309 mutable std::vector<IAlgTool*> m_tools;
310 mutable std::vector<BaseToolHandle*> m_toolHandles;
311 mutable std::vector<GaudiHandleArrayBase*> m_toolHandleArrays;
312 mutable bool m_toolHandlesInit = false;
313
315 StatusCode service_i( const std::string& algName, bool createIf, const InterfaceID& iid, void** ppSvc ) const;
316 StatusCode service_i( const std::string& svcType, const std::string& svcName, const InterfaceID& iid,
317 void** ppS ) const;
318
319 Gaudi::StateMachine::State m_state = Gaudi::StateMachine::CONFIGURED;
320 Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::CONFIGURED;
321 };
322
323 #endif // GAUDIKERNEL_ALGTOOL_H
ToolHandle::typeAndName
std::string typeAndName() const override
Definition: ToolHandle.h:260
AlgTool::registerTool
void registerTool(IAlgTool *tool) const
Definition: AlgTool.h:201
IToolSvc
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:96
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:14
PropertyHolder.h
IDataHandleHolder.h
AlgTool::m_pMonitorSvc
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: AlgTool.h:285
SmartIF< ISvcLocator >
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
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:109
AlgTool::svcLoc
ISvcLocator * svcLoc() const
shortcut for the method service locator
Definition: AlgTool.h:107
AlgTool::m_type
std::string m_type
AlgTool type (concrete class name)
Definition: AlgTool.h:278
Gaudi::StateMachine::CONFIGURED
Definition: StateMachine.h:14
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:352
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:266
AlgTool::addToolsArray
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: AlgTool.h:193
IAuditorSvc.h
AlgTool::InterfaceList
std::list< std::pair< InterfaceID, void * > > InterfaceList
Definition: AlgTool.h:276
AlgTool::declareTool
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: AlgTool.h:164
MSG::DEBUG
Definition: IMessageSvc.h:15
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
ToolHandleArray
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: ToolHandle.h:350
AlgTool::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: AlgTool.h:157
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:12
IDataProviderSvc
Data provider interface definition.
Definition: IDataProviderSvc.h:43
AlgTool::m_tools
std::vector< IAlgTool * > m_tools
Definition: AlgTool.h:309
IToolSvc.h
IOTest.start
def start
Definition: IOTest.py:97
AlgTool::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: AlgTool.h:74
AlgTool::service
SmartIF< T > service(const std::string &name, const bool createIf=true, const bool quiet=false) const
Definition: AlgTool.h:143
INamedInterface::name
virtual const std::string & name() const =0
Retrieve the name of the instance.
std::string
STL class.
AlgTool::m_name
const std::string m_name
AlgTool full name.
Definition: AlgTool.h:279
DataObjectHandle
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:26
AlgTool::monitorSvc
IMonitorSvc * monitorSvc() const
Access the monitor service.
Definition: AlgTool.h:242
InterfaceID
Interface ID class.
Definition: IInterface.h:29
extends< BASE, IDataHandleHolder >::queryInterface
StatusCode queryInterface(const InterfaceID &ti, void **pp) override
Implementation of IInterface::queryInterface.
Definition: extends.h:28
IDataProviderSvc.h
IMonitorSvc
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition: IMonitorSvc.h:23
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
ToolHandleInfo::isPublic
bool isPublic() const noexcept
Definition: ToolHandle.h:40
IInterface
Definition of the basic interface.
Definition: IInterface.h:244
AlgTool::targetFSMState
Gaudi::StateMachine::State targetFSMState() const override
Definition: AlgTool.h:75
gaudirun.type
type
Definition: gaudirun.py:142
DataHandleHolderBase
Definition: DataHandleHolderBase.h:24
CommonMessaging.h
AlgTool::configure
StatusCode configure() override
Definition: AlgTool.h:66
IAlgTool.h
AlgTool::Factory
Gaudi::PluginService::Factory< IAlgTool *(const std::string &, const std::string &, const IInterface *)> Factory
Definition: AlgTool.h:51
AlgTool::service
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
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:284
IStateful.h
std::list
STL class.
IService.h
AlgTool::declareTool
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Definition: AlgTool.h:169
DataHandleHolderBase.h
DataObjID.h
AlgTool::m_pAuditorSvc
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: AlgTool.h:286
IMonitorSvc.h
PluginService.h
IDataHandleVisitor
Definition: IDataHandleHolder.h:35
ToolHandle
Handle to be used in lieu of naked pointers to tools.
Definition: ToolHandle.h:125
ToolHandle::initialize
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:200
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:310
AlgTool::declareInterface
void declareInterface(I *i)
Definition: AlgTool.h:149
AlgTool
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:47
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:282
AlgTool::m_evtSvc
SmartIF< IDataProviderSvc > m_evtSvc
Event data service.
Definition: AlgTool.h:283
AlgTool::m_interfaceList
InterfaceList m_interfaceList
Interface list.
Definition: AlgTool.h:288
AlgTool::terminate
StatusCode terminate() override
Definition: AlgTool.h:71
IAuditorSvc
The interface implemented by the IAuditorSvc base class.
Definition: IAuditorSvc.h:15
DataHandle.h
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:71
AlgTool::deregisterTool
void deregisterTool(IAlgTool *tool) const
Definition: AlgTool.h:206
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't already exist.
Definition: AlgTool.h:135
ISvcLocator.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
ToolHandleInfo
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:31
AlgTool::declareInfo
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: AlgTool.h:254
AlgTool::m_toolHandleArrays
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: AlgTool.h:311
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:222
AlgTool::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: AlgTool.h:186