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  virtual ~Algorithm();
77 
83  virtual StatusCode sysStart();
84 
90  virtual StatusCode sysInitialize();
91 
92 
98  virtual StatusCode sysReinitialize();
99 
104  virtual StatusCode sysRestart();
105 
113  virtual StatusCode sysExecute();
114 
118  virtual StatusCode sysStop();
119 
124  virtual StatusCode sysFinalize();
125 
131  virtual StatusCode sysBeginRun( );
132 
138  virtual StatusCode sysEndRun( );
139 
147  virtual const std::string& name() const;
148 
149  virtual const std::string& version() const;
150 
152  virtual StatusCode configure () { return StatusCode::SUCCESS ; }
154  virtual StatusCode terminate () { return StatusCode::SUCCESS ; }
155 
157  virtual StatusCode initialize () { return StatusCode::SUCCESS ; }
159  virtual StatusCode start () { return StatusCode::SUCCESS ; }
161  virtual StatusCode stop () { return StatusCode::SUCCESS ; }
163  virtual StatusCode finalize () { return StatusCode::SUCCESS ; }
164 
166  virtual StatusCode reinitialize ();
168  virtual StatusCode restart ();
169 
171  virtual bool isExecuted( ) const;
172 
174  virtual void setExecuted( bool state );
175 
179  virtual void resetExecuted( );
180 
184  virtual StatusCode beginRun();
185 
187  virtual StatusCode endRun();
188 
190  virtual Gaudi::StateMachine::State FSMState() const { return m_state; }
191 
193  virtual Gaudi::StateMachine::State targetFSMState() const { return m_targetState; }
194 
196  virtual bool isEnabled( ) const;
197 
199  virtual bool filterPassed( ) const;
200 
202  virtual void setFilterPassed( bool state );
203 
205  inline int errorCount() const { return m_errorCount; }
206 
208  template <class T>
209  StatusCode service
210  ( const std::string& name, T*& psvc, bool createIf = true ) const {
211  return service_i(name, createIf, T::interfaceID(), (void**)&psvc);
212  }
213 
215  template <class T>
216  StatusCode service( const std::string& svcType, const std::string& svcName,
217  T*& psvc) const {
218  return service_i(svcType, svcName, T::interfaceID(), (void**)&psvc);
219  }
220 
222  SmartIF<IService> service(const std::string& name, const bool createIf = true, const bool quiet = false) const;
223 
225  void setOutputLevel( int level );
226 
230  SmartIF<IAuditorSvc>& auditorSvc() const;
231 
235  SmartIF<IChronoStatSvc>& chronoSvc() const;
237  SmartIF<IChronoStatSvc>& chronoStatService() const;
238 
242  SmartIF<IDataProviderSvc>& detSvc() const;
243 
245  SmartIF<IDataProviderSvc>& detDataService() const;
246 
250  SmartIF<IConversionSvc>& detCnvSvc() const;
251 
253  SmartIF<IConversionSvc>& detDataCnvService() const;
254 
258  SmartIF<IDataProviderSvc>& eventSvc() const;
260  SmartIF<IDataProviderSvc>& evtSvc () const { return eventSvc() ; }
262  SmartIF<IDataProviderSvc>& eventDataService() const;
263 
267  SmartIF<IConversionSvc>& eventCnvSvc() const;
269  SmartIF<IConversionSvc>& eventDataCnvService() const;
270 
274  SmartIF<IHistogramSvc>& histoSvc() const;
276  SmartIF<IHistogramSvc>& histogramDataService() const;
277 
282  SmartIF<IMessageSvc>& msgSvc() const;
283 
285  SmartIF<IMessageSvc>& messageService() const;
286 
290  SmartIF<INTupleSvc>& ntupleSvc() const;
291 
293  SmartIF<INTupleSvc>& ntupleService() const;
294 
298  // SmartIF<IAIDATupleSvc>& atupleSvc() const;
299 
300 
304  SmartIF<IRndmGenSvc>& randSvc() const;
305 
307  SmartIF<IToolSvc>& toolSvc() const;
308 
310  SmartIF<IExceptionSvc>& exceptionSvc() const;
311 
313  SmartIF<IAlgContextSvc>& contextSvc() const ;
314 
320  SmartIF<ISvcLocator>& serviceLocator() const;
322  SmartIF<ISvcLocator>& svcLoc () const { return serviceLocator() ; }
323 
325  bool registerContext() const { return m_registerContext ; }
326 
339  StatusCode createSubAlgorithm( const std::string& type,
340  const std::string& name, Algorithm*& pSubAlg );
341 
343  std::vector<Algorithm*>* subAlgorithms() const;
344 
346  virtual StatusCode setProperty( const Property& p );
348  virtual StatusCode setProperty( const std::string& s );
350  virtual StatusCode setProperty( const std::string& n, const std::string& v);
352  virtual StatusCode getProperty(Property* p) const;
354  virtual const Property& getProperty( const std::string& name) const;
356  virtual StatusCode getProperty( const std::string& n, std::string& v ) const;
358  virtual const std::vector<Property*>& getProperties( ) const;
360  virtual bool hasProperty(const std::string& name) const;
361 
369  StatusCode setProperties();
370  // ==========================================================================
400  template <class T>
401  Property* declareProperty
402  ( const std::string& name ,
403  T& property ,
404  const std::string& doc = "none" ) const
405  {
406  return m_propertyMgr->declareProperty(name, property, doc);
407  }
408  // ==========================================================================
410  Property* declareRemoteProperty
411  ( const std::string& name ,
412  IProperty* rsvc ,
413  const std::string& rname = "" ) const
414  {
415  return m_propertyMgr -> declareRemoteProperty ( name , rsvc , rname );
416  }
417  // ==========================================================================
428  {
429  // If not already located try to locate it without forcing a creation
430  if ( !m_pMonitorSvc.isValid() ){
431  m_pMonitorSvc = service(m_monitorSvcName, false, true); // do not create and be quiet
432  }
433  return m_pMonitorSvc;
434  }
435 
441  template <class T>
442  void declareInfo( const std::string& name,
443  const T& var,
444  const std::string& desc ) const
445  {
446  IMonitorSvc* mS = monitorSvc().get();
447  if ( mS ) mS->declareInfo(name, var, desc, this);
448  }
449 
457  void declareInfo( const std::string& name,
458  const std::string& format,
459  const void* var,
460  int size,
461  const std::string& desc ) const
462  {
463  IMonitorSvc* mS = monitorSvc().get();
464  if ( mS ) mS->declareInfo(name, format, var, size, desc, this);
465  }
466 
467  // ==========================================================================
468 public:
469  // ==========================================================================
511  template <class TYPE>
513  ( const std::string& name ,
514  const TYPE& value )
515  { return Gaudi::Utils::setProperty ( m_propertyMgr , name , value ) ; }
516  // ==========================================================================
517 protected:
518 
520  bool isInitialized( ) const { return Gaudi::StateMachine::INITIALIZED == m_state; }
521 
523  bool isFinalized( ) const { return Gaudi::StateMachine::CONFIGURED == m_state; }
524 
526  int outputLevel() const { return (int)m_outputLevel ; }
527 
529  IntegerProperty & outputLevelProperty() { return m_outputLevel; }
530 
532  void initOutputLevel(Property& prop);
533 
534 
535 private:
536 
537  std::string m_name;
538  std::string m_version;
539  std::vector<Algorithm *>* m_subAlgms;
540 
548  // mutable SmartIF<IAIDATupleSvc> m_ATS; ///< AIDA tuple service
557  std::string m_monitorSvcName;
574  bool m_isEnabled;
579 
581  StatusCode service_i(const std::string& svcName,
582  bool createIf,
583  const InterfaceID& iid,
584  void** ppSvc) const;
585  StatusCode service_i(const std::string& svcType,
586  const std::string& svcName,
587  const InterfaceID& iid,
588  void** ppSvc) const;
589 
591  Algorithm(const Algorithm& a);
592 
594  Algorithm& operator=(const Algorithm& rhs);
595 };
596 
597 #ifndef GAUDI_NEW_PLUGIN_SERVICE
598 template <class T>
599 class AlgFactory {
600 public:
601 #ifndef __REFLEX__
602  template <typename S, typename... Args>
603  static typename S::ReturnType create(Args... args) {
604  return new T(args...);
605  }
606 #endif
607 };
608 
609 // Macros to declare component factories
610 #define DECLARE_ALGORITHM_FACTORY(x) \
611  DECLARE_FACTORY_WITH_CREATOR(x, AlgFactory< x >, Algorithm::Factory)
612 #define DECLARE_NAMED_ALGORITHM_FACTORY(x, n) \
613  DECLARE_FACTORY_WITH_CREATOR_AND_ID(x, AlgFactory< x >, \
614  #n, Algorithm::Factory)
615 #define DECLARE_NAMESPACE_ALGORITHM_FACTORY(n, x) \
616  DECLARE_ALGORITHM_FACTORY(n::x)
617 
618 #else
619 
620 // Macros to declare component factories
621 #define DECLARE_ALGORITHM_FACTORY(x) DECLARE_COMPONENT(x)
622 #define DECLARE_NAMED_ALGORITHM_FACTORY(x, n) DECLARE_COMPONENT_WITH_ID(x, #n)
623 #define DECLARE_NAMESPACE_ALGORITHM_FACTORY(n, x) DECLARE_COMPONENT(n::x)
624 
625 #endif
626 
627 #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:216
SmartIF< IChronoStatSvc > m_CSS
Chrono & Stat Service.
Definition: Algorithm.h:549
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:1212
PropertyMgr * m_propertyMgr
For management of properties.
Definition: Algorithm.h:559
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
virtual StatusCode start()
the default (empty) implementation of IStateful::start() method
Definition: Algorithm.h:159
SmartIF< ISvcLocator > & svcLoc() const
shortcut for method serviceLocator
Definition: Algorithm.h:322
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:457
SmartIF< IHistogramSvc > m_HDS
Histogram data service.
Definition: Algorithm.h:546
IntegerProperty & outputLevelProperty()
Accessor for the Message level property.
Definition: Algorithm.h:529
SmartIF< IConversionSvc > m_DCS
Detector conversion service.
Definition: Algorithm.h:545
bool m_registerContext
flag to register for Algorithm Context Service
Definition: Algorithm.h:556
Base class used to implement the interfaces.
Definition: implements.h:167
#define GAUDI_API
Definition: Kernel.h:108
BooleanProperty m_auditInit
global flag for auditors
Definition: Algorithm.h:563
static S::ReturnType create(Args...args)
Definition: Algorithm.h:603
SmartIF< IToolSvc > m_ptoolSvc
ToolSvc Service.
Definition: Algorithm.h:553
SmartIF< ISvcLocator > m_pSvcLocator
Pointer to service locator service.
Definition: Algorithm.h:558
virtual StatusCode terminate()
Dummy implementation of IStateful::terminate() method.
Definition: Algorithm.h:154
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:205
bool m_isEnabled
Algorithm is enabled flag.
Definition: Algorithm.h:574
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:232
bool m_auditorBeginRun
flag for auditors in "beginRun()"
Definition: Algorithm.h:569
SmartIF< IExceptionSvc > m_EXS
Exception Handler Service.
Definition: Algorithm.h:551
SmartIF< IRndmGenSvc > m_RGS
Random Number Generator Service.
Definition: Algorithm.h:550
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm, Service, AlgTool).
Definition: StateMachine.h:12
virtual StatusCode configure()
Dummy implementation of IStateful::configure() method.
Definition: Algorithm.h:152
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Algorithm.h:552
const char *PyHelper() getProperty(IInterface *p, char *name)
Definition: Bootstrap.cpp:297
Property manager helper class.
Definition: PropertyMgr.h:34
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: Algorithm.h:554
std::string m_version
Algorithm's version.
Definition: Algorithm.h:538
int m_errorMax
Algorithm Max number of errors.
Definition: Algorithm.h:561
virtual StatusCode stop()
the default (empty) implementation of IStateful::stop() method
Definition: Algorithm.h:161
#define PACKAGE_VERSION
Definition: Algorithm.h:33
int m_errorCount
Algorithm error counter.
Definition: Algorithm.h:562
Gaudi::InterfaceId< IInterface, 0, 0 > iid
Interface ID.
Definition: IInterface.h:164
Interface ID class.
Definition: IInterface.h:55
bool m_filterPassed
Filter passed flag.
Definition: Algorithm.h:573
SmartIF< IDataProviderSvc > m_DDS
Detector data service.
Definition: Algorithm.h:544
SmartIF< IDataProviderSvc > m_EDS
Event data service.
Definition: Algorithm.h:542
bool m_auditorExecute
flag for auditors in "execute()"
Definition: Algorithm.h:567
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition: IMonitorSvc.h:21
bool m_auditorReinitialize
flag for auditors in "Reinitialize()"
Definition: Algorithm.h:565
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
virtual Gaudi::StateMachine::State FSMState() const
returns the current state of the algorithm
Definition: Algorithm.h:190
SmartIF< IConversionSvc > m_ECS
Event conversion service.
Definition: Algorithm.h:543
bool isInitialized() const
Has the Algorithm already been initialized?
Definition: Algorithm.h:520
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:290
bool m_isFinalized
Algorithm has been finalized flag.
Definition: Algorithm.h:578
SmartIF< INTupleSvc > m_NTS
N tuple service.
Definition: Algorithm.h:547
SmartIF< IMonitorSvc > & monitorSvc() const
Access the monitor service.
Definition: Algorithm.h:427
Gaudi::StateMachine::State m_targetState
Algorithm has been initialized flag.
Definition: Algorithm.h:577
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:20
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: Algorithm.h:442
std::vector< Algorithm * > * m_subAlgms
Sub algorithms.
Definition: Algorithm.h:539
bool m_auditorRestart
flag for auditors in "Restart()"
Definition: Algorithm.h:566
bool m_auditorStart
flag for auditors in "initialize()"
Definition: Algorithm.h:571
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:260
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:43
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:289
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:325
std::string m_name
Algorithm's name for identification.
Definition: Algorithm.h:537
bool m_auditorInitialize
flag for auditors in "initialize()"
Definition: Algorithm.h:564
IntegerProperty m_outputLevel
Algorithm output level.
Definition: Algorithm.h:560
virtual Gaudi::StateMachine::State targetFSMState() const
returns the state the algorithm will be in after the ongoing transition
Definition: Algorithm.h:193
bool isFinalized() const
Has the Algorithm already been finalized?
Definition: Algorithm.h:523
string s
Definition: gaudirun.py:244
virtual StatusCode initialize()
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:157
virtual StatusCode finalize()
the default (empty) implementation of IStateful::finalize() method
Definition: Algorithm.h:163
SmartIF< IAlgContextSvc > m_contextSvc
Algorithm Context Service.
Definition: Algorithm.h:555
Gaudi::StateMachine::State m_state
Algorithm has been initialized flag.
Definition: Algorithm.h:576
int outputLevel() const
retrieve the Algorithm output level
Definition: Algorithm.h:526
SmartIF< IMessageSvc > m_MS
Message service.
Definition: Algorithm.h:541
bool m_auditorStop
flag for auditors in "Reinitialize()"
Definition: Algorithm.h:572
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:22
bool m_auditorEndRun
flag for auditors in "endRun()"
Definition: Algorithm.h:570
bool m_isExecuted
Algorithm is executed flag.
Definition: Algorithm.h:575
std::string m_monitorSvcName
Name to use for Monitor Service.
Definition: Algorithm.h:557
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:133
bool m_auditorFinalize
flag for auditors in "finalize()"
Definition: Algorithm.h:568
string type
Definition: gaudirun.py:151