Algorithm.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_ALGORITHM_H
2 #define GAUDIKERNEL_ALGORITHM_H
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 #include "GaudiKernel/IProperty.h"
7 #include "GaudiKernel/IAlgorithm.h"
8 #include "GaudiKernel/IService.h"
9 #include "GaudiKernel/PropertyMgr.h"
10 #include "GaudiKernel/ISvcLocator.h"
11 #include "GaudiKernel/IMessageSvc.h"
12 #include "GaudiKernel/IStateful.h"
13 
14 #include <string>
15 #include <vector>
16 
17 // Extra include files (forward declarations should be sufficient)
18 #include "GaudiKernel/IDataProviderSvc.h"
19 #include "GaudiKernel/IHistogramSvc.h"
20 #include "GaudiKernel/IConversionSvc.h"
21 #include "GaudiKernel/INTupleSvc.h"
22 #include "GaudiKernel/IChronoStatSvc.h"
23 #include "GaudiKernel/IRndmGenSvc.h"
24 #include "GaudiKernel/IAuditorSvc.h"
25 #include "GaudiKernel/IToolSvc.h"
26 #include "GaudiKernel/IMonitorSvc.h"
27 #include "GaudiKernel/IExceptionSvc.h"
28 #include "GaudiKernel/IAlgContextSvc.h"
29 #include "GaudiKernel/Property.h"
30 #include <Gaudi/PluginService.h>
31 
32 #ifndef PACKAGE_VERSION
33 #define PACKAGE_VERSION "unknown"
34 #endif
35 
61 class GAUDI_API Algorithm: public implements3<IAlgorithm, IProperty, IStateful> {
62 public:
63 #ifndef __REFLEX__
65  const std::string&,
67 #endif
68 
73  Algorithm( const std::string& name, ISvcLocator *svcloc,
74  const std::string& version=PACKAGE_VERSION );
76  ~Algorithm() override = default;
77 
83  StatusCode sysStart() override;
84 
90  StatusCode sysInitialize() override;
91 
92 
98  StatusCode sysReinitialize() override;
99 
104  StatusCode sysRestart() override;
105 
113  StatusCode sysExecute() override;
114 
118  StatusCode sysStop() override;
119 
124  StatusCode sysFinalize() override;
125 
131  StatusCode sysBeginRun( ) override;
132 
138  StatusCode sysEndRun( ) override;
139 
147  const std::string& name() const override;
148 
149  const std::string& version() const override;
150 
152  StatusCode configure() override { return StatusCode::SUCCESS ; }
154  StatusCode terminate() override { return StatusCode::SUCCESS ; }
155 
157  StatusCode initialize() override { return StatusCode::SUCCESS ; }
159  StatusCode start() override { return StatusCode::SUCCESS ; }
161  StatusCode stop() override { return StatusCode::SUCCESS ; }
163  StatusCode finalize() override { return StatusCode::SUCCESS ; }
165  StatusCode reinitialize() override;
167  StatusCode restart() override;
169  Gaudi::StateMachine::State FSMState() const override { return m_state; }
171  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
172 
174  bool isExecuted( ) const override;
175 
177  void setExecuted( bool state ) override;
178 
182  void resetExecuted( ) override;
183 
187  StatusCode beginRun() override;
188 
190  StatusCode endRun() override;
191 
192 
194  bool isEnabled( ) const override;
195 
197  bool filterPassed( ) const override;
198 
200  void setFilterPassed( bool state ) override;
201 
203  inline int errorCount() const { return m_errorCount; }
204 
206  template <class T>
207  StatusCode service
208  ( const std::string& name, T*& psvc, bool createIf = true ) const {
209  return service_i(name, createIf, T::interfaceID(), (void**)&psvc);
210  }
211 
213  template <class T>
214  StatusCode service( const std::string& svcType, const std::string& svcName,
215  T*& psvc) const {
216  return service_i(svcType, svcName, T::interfaceID(), (void**)&psvc);
217  }
218 
220  SmartIF<IService> service(const std::string& name, const bool createIf = true, const bool quiet = false) const;
221 
222  template <class T>
223  SmartIF<T> service( const std::string& name, bool createIf = true, bool quiet = false ) const {
224  return service(name,createIf,quiet).as<T>();
225  }
226 
228  void setOutputLevel( int level );
229 
233  SmartIF<IAuditorSvc>& auditorSvc() const;
234 
238  SmartIF<IChronoStatSvc>& chronoSvc() const;
240  SmartIF<IChronoStatSvc>& chronoStatService() const;
241 
245  SmartIF<IDataProviderSvc>& detSvc() const;
246 
248  SmartIF<IDataProviderSvc>& detDataService() const;
249 
253  SmartIF<IConversionSvc>& detCnvSvc() const;
254 
256  SmartIF<IConversionSvc>& detDataCnvService() const;
257 
261  SmartIF<IDataProviderSvc>& eventSvc() const;
263  SmartIF<IDataProviderSvc>& evtSvc () const { return eventSvc() ; }
265  SmartIF<IDataProviderSvc>& eventDataService() const;
266 
270  SmartIF<IConversionSvc>& eventCnvSvc() const;
272  SmartIF<IConversionSvc>& eventDataCnvService() const;
273 
277  SmartIF<IHistogramSvc>& histoSvc() const;
279  SmartIF<IHistogramSvc>& histogramDataService() const;
280 
285  SmartIF<IMessageSvc>& msgSvc() const;
286 
288  SmartIF<IMessageSvc>& messageService() const;
289 
293  SmartIF<INTupleSvc>& ntupleSvc() const;
294 
296  SmartIF<INTupleSvc>& ntupleService() const;
297 
301  // SmartIF<IAIDATupleSvc>& atupleSvc() const;
302 
303 
307  SmartIF<IRndmGenSvc>& randSvc() const;
308 
310  SmartIF<IToolSvc>& toolSvc() const;
311 
313  SmartIF<IExceptionSvc>& exceptionSvc() const;
314 
316  SmartIF<IAlgContextSvc>& contextSvc() const ;
317 
323  SmartIF<ISvcLocator>& serviceLocator() const;
325  SmartIF<ISvcLocator>& svcLoc () const { return serviceLocator() ; }
326 
328  bool registerContext() const { return m_registerContext ; }
329 
342  StatusCode createSubAlgorithm( const std::string& type,
343  const std::string& name, Algorithm*& pSubAlg );
344 
346  const std::vector<Algorithm*>* subAlgorithms() const;
347 
349  std::vector<Algorithm*>* subAlgorithms() ;
350 
352  StatusCode setProperty( const Property& p ) override;
354  StatusCode setProperty( const std::string& s ) override;
356  StatusCode setProperty( const std::string& n, const std::string& v) override;
358  StatusCode getProperty(Property* p) const override;
360  const Property& getProperty( const std::string& name) const override;
362  StatusCode getProperty( const std::string& n, std::string& v ) const override;
364  const std::vector<Property*>& getProperties( ) const override;
366  bool hasProperty(const std::string& name) const override;
367 
375  StatusCode setProperties();
376  // ==========================================================================
406  template <class T>
407  Property* declareProperty
408  ( const std::string& name ,
409  T& property ,
410  const std::string& doc = "none" ) const
411  {
412  return m_propertyMgr->declareProperty(name, property, doc);
413  }
414  // ==========================================================================
416  Property* declareRemoteProperty
417  ( const std::string& name ,
418  IProperty* rsvc ,
419  const std::string& rname = "" ) const
420  {
421  return m_propertyMgr -> declareRemoteProperty ( name , rsvc , rname );
422  }
423  // ==========================================================================
434  {
435  // If not already located try to locate it without forcing a creation
436  if ( !m_pMonitorSvc ){
437  m_pMonitorSvc = service(m_monitorSvcName, false, true); // do not create and be quiet
438  }
439  return m_pMonitorSvc;
440  }
441 
447  template <class T>
448  void declareInfo( const std::string& name,
449  const T& var,
450  const std::string& desc ) const
451  {
452  IMonitorSvc* mS = monitorSvc().get();
453  if ( mS ) mS->declareInfo(name, var, desc, this);
454  }
455 
463  void declareInfo( const std::string& name,
464  const std::string& format,
465  const void* var,
466  int size,
467  const std::string& desc ) const
468  {
469  IMonitorSvc* mS = monitorSvc().get();
470  if ( mS ) mS->declareInfo(name, format, var, size, desc, this);
471  }
472 
473  // ==========================================================================
474 public:
475  // ==========================================================================
517  template <class TYPE>
519  ( const std::string& name ,
520  const TYPE& value )
521  { return Gaudi::Utils::setProperty ( m_propertyMgr.get() , name , value ) ; }
522  // ==========================================================================
523 protected:
524 
526  bool isInitialized( ) const override { return Gaudi::StateMachine::INITIALIZED == m_state; }
527 
529  bool isFinalized( ) const override{ return Gaudi::StateMachine::CONFIGURED == m_state; }
530 
532  int outputLevel() const { return (int)m_outputLevel ; }
533 
535  IntegerProperty & outputLevelProperty() { return m_outputLevel; }
536 
538  void initOutputLevel(Property& prop);
539 
540 
541 private:
542 
543  std::string m_name;
544  std::string m_version;
545  std::vector<Algorithm *> m_subAlgms;
546 
554  // mutable SmartIF<IAIDATupleSvc> m_ATS; ///< AIDA tuple service
562  bool m_registerContext = false ;
563  std::string m_monitorSvcName;
579  bool m_filterPassed = true;
580  bool m_isEnabled = true;
581  bool m_isExecuted = false;
585 
587  StatusCode service_i(const std::string& svcName,
588  bool createIf,
589  const InterfaceID& iid,
590  void** ppSvc) const;
591  StatusCode service_i(const std::string& svcType,
592  const std::string& svcName,
593  const InterfaceID& iid,
594  void** ppSvc) const;
595 
597  Algorithm(const Algorithm& a);
598 
600  Algorithm& operator=(const Algorithm& rhs);
601 };
602 
603 #ifndef GAUDI_NEW_PLUGIN_SERVICE
604 template <class T>
605 class AlgFactory {
606 public:
607 #ifndef __REFLEX__
608  template <typename S, typename... Args>
609  static typename S::ReturnType create(Args&&... args) {
610  return new T(std::forward<Args>(args)...);
611  }
612 #endif
613 };
614 
615 // Macros to declare component factories
616 #define DECLARE_ALGORITHM_FACTORY(x) \
617  DECLARE_FACTORY_WITH_CREATOR(x, AlgFactory< x >, Algorithm::Factory)
618 #define DECLARE_NAMED_ALGORITHM_FACTORY(x, n) \
619  DECLARE_FACTORY_WITH_CREATOR_AND_ID(x, AlgFactory< x >, \
620  #n, Algorithm::Factory)
621 #define DECLARE_NAMESPACE_ALGORITHM_FACTORY(n, x) \
622  DECLARE_ALGORITHM_FACTORY(n::x)
623 
624 #else
625 
626 // Macros to declare component factories
627 #define DECLARE_ALGORITHM_FACTORY(x) DECLARE_COMPONENT(x)
628 #define DECLARE_NAMED_ALGORITHM_FACTORY(x, n) DECLARE_COMPONENT_WITH_ID(x, #n)
629 #define DECLARE_NAMESPACE_ALGORITHM_FACTORY(n, x) DECLARE_COMPONENT(n::x)
630 
631 #endif
632 
633 #endif //GAUDIKERNEL_ALGORITHM_H
StatusCode service(const std::string &svcType, const std::string &svcName, T *&psvc) const
Access a service by name and type, creating it if it doesn't already exist.
Definition: Algorithm.h:214
SmartIF< IChronoStatSvc > m_CSS
Chrono & Stat Service.
Definition: Algorithm.h:555
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
Base class used to implement the interfaces.
Definition: implements.h:8
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
SmartIF< ISvcLocator > & svcLoc() const
shortcut for method serviceLocator
Definition: Algorithm.h:325
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: Algorithm.h:463
SmartIF< IHistogramSvc > m_HDS
Histogram data service.
Definition: Algorithm.h:552
IntegerProperty & outputLevelProperty()
Accessor for the Message level property.
Definition: Algorithm.h:535
SmartIF< IConversionSvc > m_DCS
Detector conversion service.
Definition: Algorithm.h:551
#define GAUDI_API
Definition: Kernel.h:107
BooleanProperty m_auditInit
global flag for auditors
Definition: Algorithm.h:569
StatusCode terminate() override
Dummy implementation of IStateful::terminate() method.
Definition: Algorithm.h:154
SmartIF< IToolSvc > m_ptoolSvc
ToolSvc Service.
Definition: Algorithm.h:559
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition: Algorithm.h:163
SmartIF< ISvcLocator > m_pSvcLocator
Pointer to service locator service.
Definition: Algorithm.h:564
Gaudi::PluginService::Factory< IAlgorithm *, const std::string &, ISvcLocator * > Factory
Definition: Algorithm.h:66
int errorCount() const
Get the number of failures of the algorithm.
Definition: Algorithm.h:203
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:157
bool m_auditorBeginRun
flag for auditors in "beginRun()"
Definition: Algorithm.h:575
SmartIF< IExceptionSvc > m_EXS
Exception Handler Service.
Definition: Algorithm.h:557
SmartIF< IRndmGenSvc > m_RGS
Random Number Generator Service.
Definition: Algorithm.h:556
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:12
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Algorithm.h:558
bool isFinalized() const override
Has the Algorithm already been finalized?
Definition: Algorithm.h:529
const char *PyHelper() getProperty(IInterface *p, char *name)
Definition: Bootstrap.cpp:259
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: Algorithm.h:560
std::string m_version
Algorithm's version.
Definition: Algorithm.h:544
int m_errorMax
Algorithm Max number of errors.
Definition: Algorithm.h:567
#define PACKAGE_VERSION
Definition: Algorithm.h:33
int m_errorCount
Algorithm error counter.
Definition: Algorithm.h:568
implements & operator=(const implements &)
Assignment operator (do not touch the reference count).
Definition: implements.h:37
Interface ID class.
Definition: IInterface.h:30
SmartIF< IDataProviderSvc > m_DDS
Detector data service.
Definition: Algorithm.h:550
std::vector< Algorithm * > m_subAlgms
Sub algorithms.
Definition: Algorithm.h:545
SmartIF< IDataProviderSvc > m_EDS
Event data service.
Definition: Algorithm.h:548
bool m_auditorExecute
flag for auditors in "execute()"
Definition: Algorithm.h:573
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition: IMonitorSvc.h:21
SmartIF< T > service(const std::string &name, bool createIf=true, bool quiet=false) const
Definition: Algorithm.h:223
bool m_auditorReinitialize
flag for auditors in "Reinitialize()"
Definition: Algorithm.h:571
SmartIF< PropertyMgr > m_propertyMgr
For management of properties.
Definition: Algorithm.h:565
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
SmartIF< IConversionSvc > m_ECS
Event conversion service.
Definition: Algorithm.h:549
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:255
bool m_isFinalized
Algorithm has been finalized flag.
Definition: Algorithm.h:584
SmartIF< INTupleSvc > m_NTS
N tuple service.
Definition: Algorithm.h:553
SmartIF< IMonitorSvc > & monitorSvc() const
Access the monitor service.
Definition: Algorithm.h:433
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:19
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: Algorithm.h:448
bool m_auditorRestart
flag for auditors in "Restart()"
Definition: Algorithm.h:572
bool m_auditorStart
flag for auditors in "initialize()"
Definition: Algorithm.h:577
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:61
SmartIF< IDataProviderSvc > & evtSvc() const
shortcut for method eventSvc
Definition: Algorithm.h:263
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
virtual void declareInfo(const std::string &name, const bool &var, const std::string &desc, const IInterface *owner)=0
Declare monitoring information.
list args
Definition: gaudirun.py:291
Gaudi::StateMachine::State FSMState() const override
returns the current state of the algorithm
Definition: Algorithm.h:169
bool isInitialized() const override
Has the Algorithm already been initialized?
Definition: Algorithm.h:526
Class wrapping the signature for a factory with any number of arguments.
Definition: PluginService.h:47
bool registerContext() const
register for Algorithm Context Service?
Definition: Algorithm.h:328
std::string m_name
Algorithm's name for identification.
Definition: Algorithm.h:543
static S::ReturnType create(Args &&...args)
Definition: Algorithm.h:609
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:157
bool m_auditorInitialize
flag for auditors in "initialize()"
Definition: Algorithm.h:570
IntegerProperty m_outputLevel
Algorithm output level.
Definition: Algorithm.h:566
string s
Definition: gaudirun.py:246
StatusCode start() override
the default (empty) implementation of IStateful::start() method
Definition: Algorithm.h:159
SmartIF< IAlgContextSvc > m_contextSvc
Algorithm Context Service.
Definition: Algorithm.h:561
int outputLevel() const
retrieve the Algorithm output level
Definition: Algorithm.h:532
StatusCode stop() override
the default (empty) implementation of IStateful::stop() method
Definition: Algorithm.h:161
SmartIF< IMessageSvc > m_MS
Message service.
Definition: Algorithm.h:547
bool m_auditorStop
flag for auditors in "Reinitialize()"
Definition: Algorithm.h:578
Gaudi::StateMachine::State targetFSMState() const override
returns the state the algorithm will be in after the ongoing transition
Definition: Algorithm.h:171
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:21
bool m_auditorEndRun
flag for auditors in "endRun()"
Definition: Algorithm.h:576
std::string m_monitorSvcName
Name to use for Monitor Service.
Definition: Algorithm.h:563
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:120
StatusCode configure() override
Dummy implementation of IStateful::configure() method.
Definition: Algorithm.h:152
bool m_auditorFinalize
flag for auditors in "finalize()"
Definition: Algorithm.h:574
string type
Definition: gaudirun.py:151