The Gaudi Framework  v32r2 (46d42edc)
Algorithm.h
Go to the documentation of this file.
1 #pragma once
2 // ============================================================================
3 // Include files
4 // ============================================================================
8 #include "GaudiKernel/IService.h"
12 
13 #include <string>
14 #include <vector>
15 
16 // Extra include files (forward declarations should be sufficient)
19 #include "GaudiKernel/DataObjID.h" // must be include before Property.h, which is included in PropertyHolder.h
29 #include "GaudiKernel/INTupleSvc.h"
31 #include "GaudiKernel/IToolSvc.h"
32 #include "GaudiKernel/Property.h"
34 #include "GaudiKernel/System.h"
35 #include "GaudiKernel/ToolHandle.h"
36 #include <Gaudi/PluginService.h>
37 
38 // For concurrency
39 #include "GaudiKernel/DataHandle.h"
43 
44 class IAlgTool;
45 class ToolHandleInfo;
46 class AlgorithmManager;
47 
48 #ifndef PACKAGE_VERSION
49 # define PACKAGE_VERSION "unknown"
50 #endif
51 namespace Gaudi {
52  namespace Details {
54  }
55 
80  : public DataHandleHolderBase<CounterHolder<
81  PropertyHolder<CommonMessaging<implements<IAlgorithm, IDataHandleHolder, IProperty, IStateful>>>>> {
82  public:
83 #ifndef __REFLEX__
84  typedef Gaudi::PluginService::Factory<IAlgorithm*( const std::string&, ISvcLocator* )> Factory;
85 #endif
87 
92  Algorithm( const std::string& name, ISvcLocator* svcloc, const std::string& version = PACKAGE_VERSION );
93 
99  StatusCode sysStart() override;
100 
106  StatusCode sysInitialize() override;
107 
113  StatusCode sysReinitialize() override;
114 
119  StatusCode sysRestart() override;
120 
128  StatusCode sysExecute( const EventContext& ctx ) override;
129 
133  StatusCode sysStop() override;
134 
139  StatusCode sysFinalize() override;
140 
146  StatusCode sysBeginRun() override;
147 
153  StatusCode sysEndRun() override;
154 
162  const std::string& name() const override;
163  const Gaudi::StringKey& nameKey() const override;
164 
167  const std::string& type() const override { return m_type; }
168  void setType( const std::string& type ) override { m_type = type; } // BH, TODO: move to proper place
169 
170  const std::string& version() const override;
171 
172  unsigned int index() const override;
173 
175  StatusCode configure() override { return StatusCode::SUCCESS; }
177  StatusCode terminate() override { return StatusCode::SUCCESS; }
178 
182  StatusCode start() override { return StatusCode::SUCCESS; }
184  StatusCode stop() override { return StatusCode::SUCCESS; }
186  StatusCode finalize() override { return StatusCode::SUCCESS; }
188  StatusCode reinitialize() override;
190  StatusCode restart() override;
192  Gaudi::StateMachine::State FSMState() const override { return m_state; }
194  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
195 
199  [[deprecated( "try using Algorithm::Start instead" )]] StatusCode beginRun() override;
200 
202  [[deprecated( "try using Algorithm::Stop instead" )]] StatusCode endRun() override;
203 
205  bool isEnabled() const override;
206 
208  bool isSequence() const override { return false; }
209 
211  unsigned int errorCount() const;
212 
214  template <class T>
215  StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
216  return service_i( name, createIf, T::interfaceID(), (void**)&psvc );
217  }
218 
220  template <class T>
221  StatusCode service( const std::string& svcType, const std::string& svcName, T*& psvc ) const {
222  return service_i( svcType, svcName, T::interfaceID(), reinterpret_cast<void**>( &psvc ) );
223  }
224 
226  SmartIF<IService> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const;
227 
228  template <class T>
229  SmartIF<T> service( const std::string& name, bool createIf = true, bool quiet = false ) const {
230  return service( name, createIf, quiet ).as<T>();
231  }
232 
236  SmartIF<IAuditorSvc>& auditorSvc() const;
237 
241  SmartIF<IChronoStatSvc>& chronoSvc() const;
242  [[deprecated( "use chronoSvc() instead" )]] SmartIF<IChronoStatSvc>& chronoStatService() const {
243  return chronoSvc();
244  }
245 
249  SmartIF<IDataProviderSvc>& detSvc() const;
250  [[deprecated( "use detSvc() instead" )]] SmartIF<IDataProviderSvc>& detDataService() const { return detSvc(); }
251 
255  SmartIF<IConversionSvc>& detCnvSvc() const;
256  [[deprecated( "use detCnvSvc() instead" )]] SmartIF<IConversionSvc>& detDataCnvService() const {
257  return detCnvSvc();
258  }
259 
263  SmartIF<IDataProviderSvc>& eventSvc() const;
265  SmartIF<IDataProviderSvc>& evtSvc() const { return eventSvc(); }
266  [[deprecated( "use eventSvc() instead" )]] SmartIF<IDataProviderSvc>& eventDataService() const {
267  return eventSvc();
268  }
269 
273  SmartIF<IConversionSvc>& eventCnvSvc() const;
274  [[deprecated( "use eventCnvSvc() instead" )]] SmartIF<IConversionSvc>& eventDataCnvService() const {
275  return eventCnvSvc();
276  }
277 
281  SmartIF<IHistogramSvc>& histoSvc() const;
282  [[deprecated( "use histoSvc() instead" )]] SmartIF<IHistogramSvc>& histogramDataService() const {
283  return histoSvc();
284  }
285 
289  SmartIF<INTupleSvc>& ntupleSvc() const;
290  [[deprecated( "use ntupleSvc() instead" )]] SmartIF<INTupleSvc>& ntupleService() const { return ntupleSvc(); }
291 
295  SmartIF<IRndmGenSvc>& randSvc() const;
296 
298  SmartIF<IToolSvc>& toolSvc() const;
299 
301  SmartIF<IExceptionSvc>& exceptionSvc() const;
302 
304  SmartIF<IAlgContextSvc>& contextSvc() const;
305 
306  SmartIF<ITimelineSvc>& timelineSvc() const;
307 
313  SmartIF<ISvcLocator>& serviceLocator() const override;
315  SmartIF<ISvcLocator>& svcLoc() const { return serviceLocator(); }
316 
318 
319  SmartIF<IAlgExecStateSvc>& algExecStateSvc() const;
320 
322  bool registerContext() const { return m_registerContext; }
323 
331  StatusCode setProperties();
332 
333  // ==========================================================================
334  using PropertyHolderImpl::declareProperty;
335 
336  // declare Tools to the Algorithms
337  template <class T>
339  const std::string& doc = "none" ) {
340  this->declareTool( hndl, hndl.typeAndName() ).ignore();
341  return PropertyHolderImpl::declareProperty( name, hndl, doc );
342  }
343 
344  // ==========================================================================
345  // declare ToolHandleArrays to the Algorithms
346 
347  template <class T>
349  const std::string& doc = "none" ) {
350  addToolsArray( hndlArr );
351  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
352  }
353 
354  // ==========================================================================
365  // If not already located try to locate it without forcing a creation
366  if ( !m_pMonitorSvc ) {
367  m_pMonitorSvc = service( m_monitorSvcName, false, true ); // do not create and be quiet
368  }
369  return m_pMonitorSvc;
370  }
371 
377  template <class T>
378  void declareInfo( const std::string& name, const T& var, const std::string& desc ) const {
379  IMonitorSvc* mS = monitorSvc().get();
380  if ( mS ) mS->declareInfo( name, var, desc, this );
381  }
382 
390  void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
391  const std::string& desc ) const {
392  IMonitorSvc* mS = monitorSvc().get();
393  if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
394  }
395 
396  public:
397  void acceptDHVisitor( IDataHandleVisitor* ) const override;
398 
399  public:
400  void registerTool( IAlgTool* tool ) const;
401  void deregisterTool( IAlgTool* tool ) const;
402 
403  template <class T>
404  StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
405  return this->declareTool( handle, handle.typeAndName(), createIf );
406  }
407 
408  template <class T>
409  StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true ) {
410 
411  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
412  if ( UNLIKELY( !sc ) ) {
413  throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
414  " tool " + toolTypeAndName,
415  name(), sc};
416  }
417 
418  m_toolHandles.push_back( &handle );
419 
420  return sc;
421  }
422 
423  template <class T>
425  m_toolHandleArrays.push_back( &hndlArr );
426  }
427 
428  const std::vector<IAlgTool*>& tools() const;
429 
430  // Return the I/O-boundness flag
431  bool isIOBound() const { return m_isIOBound; }
432  // Set the I/O-boundness flag
433  void setIOBound( bool value ) { m_isIOBound = value; }
434 
435  protected:
436  std::vector<IAlgTool*>& tools();
437 
438  // // adds declared in- and outputs of subAlgorithms to own DOHs
439  // void addSubAlgorithmDataObjectHandles();
440 
441  private:
442  // place IAlgTools defined via ToolHandles in m_tools
443  void initToolHandles() const;
444 
445  public:
446  // /// Specifies the clonability of the algorithm
447  // bool isClonable() const override { return false; }
448 
450  unsigned int cardinality() const override { return m_cardinality; }
451 
452  const std::vector<std::string>& neededResources() const override { return m_neededResources; }
453 
454  protected:
456  bool isInitialized() const override { return Gaudi::StateMachine::INITIALIZED == m_state; }
457 
459  bool isFinalized() const override { return Gaudi::StateMachine::CONFIGURED == m_state; }
460 
462  void setIndex( const unsigned int& idx ) override;
463 
464  public:
466  AlgExecState& execState( const EventContext& ctx ) const override;
467 
469  std::ostream& toControlFlowExpression( std::ostream& os ) const override;
470 
471  private:
472  unsigned int maxErrors() const { return m_errorMax; }
473 
474  protected:
475  bool isReEntrant() const override { return true; }
476 
477  private:
481  unsigned int m_index = 0;
482 
483  // tools used by algorithm
487 
488  private:
489  template <typename IFace>
490  SmartIF<IFace>& get_svc_( SmartIF<IFace>& p, const char* service_name ) const;
491 
507 
510 
512 
513  protected:
516 
517  private:
518  // Properties
519  Gaudi::Property<int> m_outputLevel{
520  this, "OutputLevel", MSG::NIL,
521  [this]( Gaudi::Details::PropertyBase& ) { this->updateMsgStreamOutputLevel( this->m_outputLevel ); },
522  "output level"};
523  Gaudi::Property<bool> m_isEnabled{this, "Enable", true, "should the algorithm be executed or not"};
524 
525  Gaudi::Property<unsigned int> m_errorMax{this, "ErrorMax", 1, "[[deprecated]] max number of errors"};
526 
527  Gaudi::Property<bool> m_auditInit{this, "AuditAlgorithms", Details::getDefaultAuditorValue( m_pSvcLocator ),
528  "[[deprecated]] unused"};
529  Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", m_auditInit.value(),
530  "trigger auditor on initialize()"};
531  Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", m_auditInit.value(),
532  "trigger auditor on reinitialize()"};
533  Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", m_auditInit.value(), "trigger auditor on restart()"};
534  Gaudi::Property<bool> m_auditorExecute{this, "AuditExecute", m_auditInit.value(), "trigger auditor on execute()"};
535  Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", m_auditInit.value(),
536  "trigger auditor on finalize()"};
537  Gaudi::Property<bool> m_auditorBeginRun{this, "AuditBeginRun", m_auditInit.value(),
538  "trigger auditor on beginRun()"};
539  Gaudi::Property<bool> m_auditorEndRun{this, "AuditEndRun", m_auditInit.value(), "trigger auditor on endRun()"};
540  Gaudi::Property<bool> m_auditorStart{this, "AuditStart", m_auditInit.value(), "trigger auditor on start()"};
541  Gaudi::Property<bool> m_auditorStop{this, "AuditStop", m_auditInit.value(), "trigger auditor on stop()"};
542 
543  Gaudi::Property<bool> m_doTimeline{this, "Timeline", true, "send events to TimelineSvc"};
544 
545  Gaudi::Property<std::string> m_monitorSvcName{this, "MonitorService", "MonitorSvc",
546  "name to use for Monitor Service"};
547 
548  Gaudi::Property<bool> m_registerContext{this, "RegisterForContextService", false,
549  "flag to enforce the registration for Algorithm Context Service"};
550 
551  Gaudi::Property<int> m_cardinality{this, "Cardinality", 0, "how many clones to create - 0 means algo is reentrant"};
553  this, "NeededResources", {}, "named resources needed during event looping"};
554 
555  Gaudi::Property<bool> m_isIOBound{this, "IsIOBound", false,
556  "if the algorithm is I/O-bound (in the broad sense of Von Neumann bottleneck)"};
557 
558  // The default should be changed to "false" for v29
559  Gaudi::Property<bool> m_filterCircDeps{this, "FilterCircularDependencies", true,
560  "filter out circular data dependencies"};
561 
562  mutable bool m_toolHandlesInit = false;
563 
567 
571 
573  StatusCode service_i( const std::string& svcName, bool createIf, const InterfaceID& iid, void** ppSvc ) const;
574  StatusCode service_i( const std::string& svcType, const std::string& svcName, const InterfaceID& iid,
575  void** ppSvc ) const;
576 
578  Algorithm( const Algorithm& a ) = delete;
579 
581  Algorithm& operator=( const Algorithm& rhs ) = delete;
582  };
583 } // namespace Gaudi
std::string typeAndName() const override
Definition: ToolHandle.h:260
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Definition: Algorithm.h:409
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Algorithm.h:503
The AlgorithmManager class is in charge of the creation of concrete instances of Algorithms.
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: Algorithm.h:424
#define UNLIKELY(x)
Definition: Kernel.h:96
constexpr auto size(const T &, Args &&...) noexcept
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:221
SmartIF< IAlgExecStateSvc > m_aess
Alg execution state mgr.
Definition: Algorithm.h:509
bool isIOBound() const
Definition: Algorithm.h:431
Define general base for Gaudi exception.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:109
std::unique_ptr< IDataHandleVisitor > m_updateDataHandles
Hook for for derived classes to provide a custom visitor for data handles.
Definition: Algorithm.h:515
Implementation of property with value of concrete type.
Definition: Property.h:352
SmartIF< IExceptionSvc > m_EXS
Exception Handler Service.
Definition: Algorithm.h:502
StatusCode start() override
the default (empty) implementation of IStateful::start() method
Definition: Algorithm.h:182
SmartIF< IAlgContextSvc > m_contextSvc
Algorithm Context Service.
Definition: Algorithm.h:506
SmartIF< IConversionSvc > & eventDataCnvService() const
Definition: Algorithm.h:274
SmartIF< IChronoStatSvc > m_CSS
Chrono & Stat Service.
Definition: Algorithm.h:500
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:390
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
bool isReEntrant() const override
Definition: Algorithm.h:475
SmartIF< IDataProviderSvc > & evtSvc() const
shortcut for method eventSvc
Definition: Algorithm.h:265
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: ToolHandle.h:350
SmartIF< IDataProviderSvc > m_DDS
Detector data service.
Definition: Algorithm.h:496
SmartIF< IConversionSvc > & detDataCnvService() const
Definition: Algorithm.h:256
SmartIF< IHistogramSvc > & histogramDataService() const
Definition: Algorithm.h:282
SmartIF< IDataProviderSvc > & detDataService() const
Definition: Algorithm.h:250
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:12
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: Algorithm.h:378
SmartIF< IToolSvc > m_ptoolSvc
ToolSvc Service.
Definition: Algorithm.h:504
This class represents an entry point to all the event specific data.
Definition: EventContext.h:24
The helper class to represent the efficient "key" for access.
Definition: StringKey.h:34
SmartIF< IDataProviderSvc > & eventDataService() const
Definition: Algorithm.h:266
STL class.
SmartIF< IMonitorSvc > & monitorSvc() const
Access the monitor service.
Definition: Algorithm.h:364
void setType(const std::string &type) override
Definition: Algorithm.h:168
Interface ID class.
Definition: IInterface.h:29
#define PACKAGE_VERSION
Definition: Algorithm.h:49
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:180
std::string m_version
Algorithm's version.
Definition: Algorithm.h:480
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: Algorithm.h:404
SmartIF< T > service(const std::string &name, bool createIf=true, bool quiet=false) const
Definition: Algorithm.h:229
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: Algorithm.h:338
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition: IMonitorSvc.h:23
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Algorithm.h:215
bool getDefaultAuditorValue(ISvcLocator *loc)
Definition: Algorithm.cpp:36
SmartIF< IConversionSvc > m_ECS
Event conversion service.
Definition: Algorithm.h:495
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
bool isPublic() const noexcept
Definition: ToolHandle.h:40
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:526
const std::string & type() const override
The type of the algorithm object.
Definition: Algorithm.h:167
void setIOBound(bool value)
Definition: Algorithm.h:433
SmartIF< IHistogramSvc > m_HDS
Histogram data service.
Definition: Algorithm.h:498
SmartIF< IMessageSvc > m_MS
Message service.
Definition: Algorithm.h:492
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition: Algorithm.h:186
SmartIF< ISvcLocator > m_pSvcLocator
Pointer to service locator service.
Definition: Algorithm.h:511
StatusCode terminate() override
Dummy implementation of IStateful::terminate() method.
Definition: Algorithm.h:177
Gaudi::PluginService::Factory< IAlgorithm *(const std::string &, ISvcLocator *)> Factory
Definition: Algorithm.h:84
std::vector< IAlgTool * > m_tools
Definition: Algorithm.h:484
SmartIF< IConversionSvc > m_DCS
Detector conversion service.
Definition: Algorithm.h:497
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
SmartIF< IDataProviderSvc > m_EDS
Event data service.
Definition: Algorithm.h:493
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: Algorithm.h:348
bool registerContext() const
register for Algorithm Context Service?
Definition: Algorithm.h:322
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:28
const std::vector< std::string > & neededResources() const override
Definition: Algorithm.h:452
bool m_beginRunCalled
temporary flags to check if derived class's begin/endRun methods called
Definition: Algorithm.h:569
SmartIF< INTupleSvc > m_NTS
N tuple service.
Definition: Algorithm.h:499
Handle to be used in lieu of naked pointers to tools.
Definition: ToolHandle.h:125
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:200
SmartIF< IRndmGenSvc > m_RGS
Random Number Generator Service.
Definition: Algorithm.h:501
bool isInitialized() const override
Has the Algorithm already been initialized?
Definition: Algorithm.h:456
virtual void declareInfo(const std::string &name, const bool &var, const std::string &desc, const IInterface *owner)=0
Declare monitoring information.
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition: Algorithm.h:505
std::string m_type
Algorithm's type.
Definition: Algorithm.h:479
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
unsigned int maxErrors() const
Definition: Algorithm.h:472
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
SmartIF< ISvcLocator > & svcLoc() const
shortcut for method serviceLocator
Definition: Algorithm.h:315
friend AlgorithmManager
Definition: Algorithm.h:86
SmartIF< ITimelineSvc > m_timelineSvc
Timeline Service.
Definition: Algorithm.h:508
bool isSequence() const override
Are we a Sequence?
Definition: Algorithm.h:208
Gaudi::StateMachine::State FSMState() const override
returns the current state of the algorithm
Definition: Algorithm.h:192
Gaudi::StringKey m_name
Algorithm's name for identification.
Definition: Algorithm.h:478
bool isFinalized() const override
Has the Algorithm already been finalized?
Definition: Algorithm.h:459
SmartIF< IHiveWhiteBoard > m_WB
Event data service (whiteboard)
Definition: Algorithm.h:494
bool m_isFinalized
Algorithm has been finalized flag.
Definition: Algorithm.h:566
#define GAUDI_API
Definition: Kernel.h:71
unsigned int cardinality() const override
Return the cardinality.
Definition: Algorithm.h:450
StatusCode configure() override
Dummy implementation of IStateful::configure() method.
Definition: Algorithm.h:175
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: Algorithm.h:486
STL class.
std::vector< BaseToolHandle * > m_toolHandles
Definition: Algorithm.h:485
StatusCode stop() override
the default (empty) implementation of IStateful::stop() method
Definition: Algorithm.h:184
Gaudi::StateMachine::State targetFSMState() const override
returns the state the algorithm will be in after the ongoing transition
Definition: Algorithm.h:194
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
SmartIF< IChronoStatSvc > & chronoStatService() const
Definition: Algorithm.h:242
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:31
SmartIF< INTupleSvc > & ntupleService() const
Definition: Algorithm.h:290