The Gaudi Framework  v33r2 (a6f0ec87)
Algorithm.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #pragma once
12 // ============================================================================
13 // Include files
14 // ============================================================================
15 #include "GaudiKernel/IAlgorithm.h"
17 #include "GaudiKernel/IProperty.h"
18 #include "GaudiKernel/IService.h"
19 #include "GaudiKernel/IStateful.h"
22 
23 #include <string>
24 #include <vector>
25 
26 // Extra include files (forward declarations should be sufficient)
29 #include "GaudiKernel/DataObjID.h" // must be include before Property.h, which is included in PropertyHolder.h
39 #include "GaudiKernel/INTupleSvc.h"
41 #include "GaudiKernel/IToolSvc.h"
42 #include "GaudiKernel/Property.h"
44 #include "GaudiKernel/System.h"
45 #include "GaudiKernel/ToolHandle.h"
46 #include <Gaudi/PluginService.h>
47 
48 // For concurrency
49 #include "GaudiKernel/DataHandle.h"
53 
54 class IAlgTool;
55 class ToolHandleInfo;
56 class AlgorithmManager;
57 
58 #ifndef PACKAGE_VERSION
59 # define PACKAGE_VERSION "unknown"
60 #endif
61 namespace Gaudi {
62  namespace Details {
64  }
65 
90  : public DataHandleHolderBase<CounterHolder<
91  PropertyHolder<CommonMessaging<implements<IAlgorithm, IDataHandleHolder, IProperty, IStateful>>>>> {
92  public:
93 #ifndef __REFLEX__
94  typedef Gaudi::PluginService::Factory<IAlgorithm*( const std::string&, ISvcLocator* )> Factory;
95 #endif
97 
103  : m_name( std::move( name ) )
104  , m_version( std::move( version ) ) // incremented by AlgResourcePool
105  , m_pSvcLocator( svcloc ) {}
106 
112  StatusCode sysStart() override;
113 
119  StatusCode sysInitialize() override;
120 
126  StatusCode sysReinitialize() override;
127 
132  StatusCode sysRestart() override;
133 
141  StatusCode sysExecute( const EventContext& ctx ) override;
142 
146  StatusCode sysStop() override;
147 
152  StatusCode sysFinalize() override;
153 
161  const std::string& name() const override;
162  const Gaudi::StringKey& nameKey() const override;
163 
166  const std::string& type() const override { return m_type; }
167  void setType( std::string type ) override { m_type = std::move( type ); } // BH, TODO: move to proper place
168 
169  const std::string& version() const override;
170 
171  unsigned int index() const override;
172 
174  StatusCode configure() override { return StatusCode::SUCCESS; }
176  StatusCode terminate() override { return StatusCode::SUCCESS; }
177 
181  StatusCode start() override { return StatusCode::SUCCESS; }
183  StatusCode stop() override { return StatusCode::SUCCESS; }
185  StatusCode finalize() override { return StatusCode::SUCCESS; }
187  StatusCode reinitialize() override;
189  StatusCode restart() override;
191  Gaudi::StateMachine::State FSMState() const override { return m_state; }
193  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
194 
196  bool isEnabled() const override;
197 
199  bool isSequence() const override { return false; }
200 
202  unsigned int errorCount() const;
203 
205  template <class T>
206  StatusCode service( std::string_view name, T*& psvc, bool createIf = true ) const {
207  return service_i( name, createIf, T::interfaceID(), (void**)&psvc );
208  }
209 
211  template <class T>
212  StatusCode service( std::string_view svcType, std::string_view svcName, T*& psvc ) const {
213  return service_i( svcType, svcName, T::interfaceID(), reinterpret_cast<void**>( &psvc ) );
214  }
215 
217  SmartIF<IService> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const;
218 
219  template <class T>
220  SmartIF<T> service( std::string_view name, bool createIf = true, bool quiet = false ) const {
221  return service( name, createIf, quiet ).as<T>();
222  }
223 
227  SmartIF<IAuditorSvc>& auditorSvc() const;
228 
232  SmartIF<IChronoStatSvc>& chronoSvc() const;
233 
237  SmartIF<IDataProviderSvc>& detSvc() const;
238 
242  SmartIF<IConversionSvc>& detCnvSvc() const;
243 
247  SmartIF<IDataProviderSvc>& eventSvc() const;
249  SmartIF<IDataProviderSvc>& evtSvc() const { return eventSvc(); }
250 
254  SmartIF<IConversionSvc>& eventCnvSvc() const;
255 
259  SmartIF<IHistogramSvc>& histoSvc() const;
260 
264  SmartIF<INTupleSvc>& ntupleSvc() const;
265 
269  SmartIF<IRndmGenSvc>& randSvc() const;
270 
272  SmartIF<IToolSvc>& toolSvc() const;
273 
275  SmartIF<IExceptionSvc>& exceptionSvc() const;
276 
278  SmartIF<IAlgContextSvc>& contextSvc() const;
279 
280  SmartIF<ITimelineSvc>& timelineSvc() const;
281 
287  SmartIF<ISvcLocator>& serviceLocator() const override;
289  SmartIF<ISvcLocator>& svcLoc() const { return serviceLocator(); }
290 
292 
293  SmartIF<IAlgExecStateSvc>& algExecStateSvc() const;
294 
296  bool registerContext() const { return m_registerContext; }
297 
305  StatusCode setProperties();
306 
307  // ==========================================================================
308  using PropertyHolderImpl::declareProperty;
309 
310  // declare Tools to the Algorithms
311  template <class T>
313  const std::string& doc = "none" ) {
314  this->declareTool( hndl, hndl.typeAndName() ).ignore();
315  return PropertyHolderImpl::declareProperty( name, hndl, doc );
316  }
317 
318  // ==========================================================================
319  // declare ToolHandleArrays to the Algorithms
320 
321  template <class T>
323  const std::string& doc = "none" ) {
324  addToolsArray( hndlArr );
325  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
326  }
327 
328  // ==========================================================================
339  // If not already located try to locate it without forcing a creation
340  if ( !m_pMonitorSvc ) {
341  m_pMonitorSvc = service( m_monitorSvcName, false, true ); // do not create and be quiet
342  }
343  return m_pMonitorSvc;
344  }
345 
351  template <class T>
352  void declareInfo( const std::string& name, const T& var, const std::string& desc ) const {
353  IMonitorSvc* mS = monitorSvc().get();
354  if ( mS ) mS->declareInfo( name, var, desc, this );
355  }
356 
364  void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
365  const std::string& desc ) const {
366  IMonitorSvc* mS = monitorSvc().get();
367  if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
368  }
369 
370  public:
371  void acceptDHVisitor( IDataHandleVisitor* ) const override;
372 
373  void registerTool( IAlgTool* tool ) const;
374  void deregisterTool( IAlgTool* tool ) const;
375 
376  template <class T>
377  StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
378  return this->declareTool( handle, handle.typeAndName(), createIf );
379  }
380 
381  template <class T>
382  StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true ) {
383 
384  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
385  if ( UNLIKELY( !sc ) ) {
386  throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
387  " tool " + toolTypeAndName,
388  name(), sc};
389  }
390 
391  m_toolHandles.push_back( &handle );
392 
393  return sc;
394  }
395 
396  template <class T>
398  m_toolHandleArrays.push_back( &hndlArr );
399  }
400 
401  const std::vector<IAlgTool*>& tools() const;
402 
403  // Return the blocking flag
404  bool isBlocking() const { return m_blocking; }
405  // Set the blocking flag
406  void setBlocking( bool value ) { m_blocking = value; }
407 
408  protected:
409  std::vector<IAlgTool*>& tools();
410 
411  // // adds declared in- and outputs of subAlgorithms to own DOHs
412  // void addSubAlgorithmDataObjectHandles();
413 
414  private:
415  // place IAlgTools defined via ToolHandles in m_tools
416  void initToolHandles() const;
417 
418  public:
419  // /// Specifies the clonability of the algorithm
420  // bool isClonable() const override { return false; }
421 
423  unsigned int cardinality() const override { return m_cardinality; }
424 
425  const std::vector<std::string>& neededResources() const override { return m_neededResources; }
426 
427  protected:
429  bool isInitialized() const override { return Gaudi::StateMachine::INITIALIZED == m_state; }
430 
432  bool isFinalized() const override { return Gaudi::StateMachine::CONFIGURED == m_state; }
433 
435  void setIndex( const unsigned int& idx ) override;
436 
437  public:
439  AlgExecState& execState( const EventContext& ctx ) const override;
440 
442  std::ostream& toControlFlowExpression( std::ostream& os ) const override;
443 
444  private:
445  unsigned int maxErrors() const { return m_errorMax; }
446 
447  protected:
448  bool isReEntrant() const override { return true; }
449 
450  private:
454  unsigned int m_index = 0;
455 
456  // tools used by algorithm
460 
461  private:
462  template <typename IFace>
463  SmartIF<IFace>& get_svc_( SmartIF<IFace>& p, const char* service_name ) const;
464 
480 
483 
485 
486  protected:
489 
490  private:
491  // Properties
492  Gaudi::Property<int> m_outputLevel{
493  this, "OutputLevel", MSG::NIL,
494  [this]( Gaudi::Details::PropertyBase& ) { this->updateMsgStreamOutputLevel( this->m_outputLevel ); },
495  "output level"};
496  Gaudi::Property<bool> m_isEnabled{this, "Enable", true, "should the algorithm be executed or not"};
497 
498  Gaudi::Property<unsigned int> m_errorMax{this, "ErrorMax", 1, "[[deprecated]] max number of errors"};
499 
500  Gaudi::Property<bool> m_auditInit{this, "AuditAlgorithms", Details::getDefaultAuditorValue( m_pSvcLocator ),
501  "[[deprecated]] unused"};
502  Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", m_auditInit.value(),
503  "trigger auditor on initialize()"};
504  Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", m_auditInit.value(),
505  "trigger auditor on reinitialize()"};
506  Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", m_auditInit.value(), "trigger auditor on restart()"};
507  Gaudi::Property<bool> m_auditorExecute{this, "AuditExecute", m_auditInit.value(), "trigger auditor on execute()"};
508  Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", m_auditInit.value(),
509  "trigger auditor on finalize()"};
510  Gaudi::Property<bool> m_auditorStart{this, "AuditStart", m_auditInit.value(), "trigger auditor on start()"};
511  Gaudi::Property<bool> m_auditorStop{this, "AuditStop", m_auditInit.value(), "trigger auditor on stop()"};
512 
513  Gaudi::Property<bool> m_doTimeline{this, "Timeline", true, "send events to TimelineSvc"};
514 
515  Gaudi::Property<std::string> m_monitorSvcName{this, "MonitorService", "MonitorSvc",
516  "name to use for Monitor Service"};
517 
518  Gaudi::Property<bool> m_registerContext{this, "RegisterForContextService", false,
519  "flag to enforce the registration for Algorithm Context Service"};
520 
521  Gaudi::Property<int> m_cardinality{this, "Cardinality", 0, "how many clones to create - 0 means algo is reentrant"};
523  this, "NeededResources", {}, "named resources needed during event looping"};
524 
526  this, "Blocking", false,
527  "if algorithm invokes CPU-blocking system calls (offloads computations to accelerators or "
528  "quantum processors, performs disk or network I/O, is bound by resource synchronization, etc)"};
529 
530  // The default should be changed to "false" for v29
531  Gaudi::Property<bool> m_filterCircDeps{this, "FilterCircularDependencies", true,
532  "filter out circular data dependencies"};
533 
534  mutable bool m_toolHandlesInit = false;
535 
539 
541  StatusCode service_i( std::string_view svcName, bool createIf, const InterfaceID& iid, void** ppSvc ) const;
542  StatusCode service_i( std::string_view svcType, std::string_view svcName, const InterfaceID& iid,
543  void** ppSvc ) const;
544 
546  Algorithm( const Algorithm& ) = delete;
547 
549  Algorithm& operator=( const Algorithm& ) = delete;
550  };
551 } // namespace Gaudi
std::string typeAndName() const override
Definition: ToolHandle.h:270
StatusCode declareTool(ToolHandle< T > &handle, std::string toolTypeAndName, bool createIf=true)
Definition: Algorithm.h:382
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Algorithm.h:476
The AlgorithmManager class is in charge of the creation of concrete instances of Algorithms.
void setBlocking(bool value)
Definition: Algorithm.h:406
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: Algorithm.h:397
#define UNLIKELY(x)
Definition: Kernel.h:106
constexpr auto size(const T &, Args &&...) noexcept
SmartIF< IAlgExecStateSvc > m_aess
Alg execution state mgr.
Definition: Algorithm.h:482
SmartIF< T > service(std::string_view name, bool createIf=true, bool quiet=false) const
Definition: Algorithm.h:220
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:35
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
std::unique_ptr< IDataHandleVisitor > m_updateDataHandles
Hook for for derived classes to provide a custom visitor for data handles.
Definition: Algorithm.h:488
Implementation of property with value of concrete type.
Definition: Property.h:370
SmartIF< IExceptionSvc > m_EXS
Exception Handler Service.
Definition: Algorithm.h:475
StatusCode start() override
the default (empty) implementation of IStateful::start() method
Definition: Algorithm.h:181
SmartIF< IAlgContextSvc > m_contextSvc
Algorithm Context Service.
Definition: Algorithm.h:479
StatusCode service(std::string_view svcType, std::string_view svcName, T *&psvc) const
Access a service by name and type, creating it if it doesn't already exist.
Definition: Algorithm.h:212
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition: Algorithm.h:102
SmartIF< IChronoStatSvc > m_CSS
Chrono & Stat Service.
Definition: Algorithm.h:473
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:364
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
bool isReEntrant() const override
Definition: Algorithm.h:448
SmartIF< IDataProviderSvc > & evtSvc() const
shortcut for method eventSvc
Definition: Algorithm.h:249
STL namespace.
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: ToolHandle.h:345
SmartIF< IDataProviderSvc > m_DDS
Detector data service.
Definition: Algorithm.h:469
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:22
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition: Algorithm.h:352
SmartIF< IToolSvc > m_ptoolSvc
ToolSvc Service.
Definition: Algorithm.h:477
This class represents an entry point to all the event specific data.
Definition: EventContext.h:34
The helper class to represent the efficient "key" for access.
Definition: StringKey.h:44
bool isBlocking() const
Definition: Algorithm.h:404
STL class.
SmartIF< IMonitorSvc > & monitorSvc() const
Access the monitor service.
Definition: Algorithm.h:338
Interface ID class.
Definition: IInterface.h:39
#define PACKAGE_VERSION
Definition: Algorithm.h:59
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:179
std::string m_version
Algorithm's version.
Definition: Algorithm.h:453
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: Algorithm.h:377
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: Algorithm.h:312
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition: IMonitorSvc.h:33
bool getDefaultAuditorValue(ISvcLocator *loc)
Definition: Algorithm.cpp:47
SmartIF< IConversionSvc > m_ECS
Event conversion service.
Definition: Algorithm.h:468
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
StatusCode service(std::string_view name, T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Algorithm.h:206
bool isPublic() const noexcept
Definition: ToolHandle.h:50
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:554
const std::string & type() const override
The type of the algorithm object.
Definition: Algorithm.h:166
SmartIF< IHistogramSvc > m_HDS
Histogram data service.
Definition: Algorithm.h:471
SmartIF< IMessageSvc > m_MS
Message service.
Definition: Algorithm.h:465
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition: Algorithm.h:185
SmartIF< ISvcLocator > m_pSvcLocator
Pointer to service locator service.
Definition: Algorithm.h:484
StatusCode terminate() override
Dummy implementation of IStateful::terminate() method.
Definition: Algorithm.h:176
Gaudi::PluginService::Factory< IAlgorithm *(const std::string &, ISvcLocator *)> Factory
Definition: Algorithm.h:94
std::vector< IAlgTool * > m_tools
Definition: Algorithm.h:457
SmartIF< IConversionSvc > m_DCS
Detector conversion service.
Definition: Algorithm.h:470
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:42
SmartIF< IDataProviderSvc > m_EDS
Event data service.
Definition: Algorithm.h:466
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: Algorithm.h:322
bool registerContext() const
register for Algorithm Context Service?
Definition: Algorithm.h:296
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:38
T move(T... args)
const std::vector< std::string > & neededResources() const override
Definition: Algorithm.h:425
SmartIF< INTupleSvc > m_NTS
N tuple service.
Definition: Algorithm.h:472
Handle to be used in lieu of naked pointers to tools.
Definition: ToolHandle.h:135
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition: ToolHandle.h:210
SmartIF< IRndmGenSvc > m_RGS
Random Number Generator Service.
Definition: Algorithm.h:474
bool isInitialized() const override
Has the Algorithm already been initialized?
Definition: Algorithm.h:429
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:478
std::string m_type
Algorithm's type.
Definition: Algorithm.h:452
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:33
unsigned int maxErrors() const
Definition: Algorithm.h:445
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:89
SmartIF< ISvcLocator > & svcLoc() const
shortcut for method serviceLocator
Definition: Algorithm.h:289
friend AlgorithmManager
Definition: Algorithm.h:96
SmartIF< ITimelineSvc > m_timelineSvc
Timeline Service.
Definition: Algorithm.h:481
bool isSequence() const override
Are we a Sequence?
Definition: Algorithm.h:199
Gaudi::StateMachine::State FSMState() const override
returns the current state of the algorithm
Definition: Algorithm.h:191
Gaudi::StringKey m_name
Algorithm's name for identification.
Definition: Algorithm.h:451
bool isFinalized() const override
Has the Algorithm already been finalized?
Definition: Algorithm.h:432
SmartIF< IHiveWhiteBoard > m_WB
Event data service (whiteboard)
Definition: Algorithm.h:467
bool m_isFinalized
Algorithm has been finalized flag.
Definition: Algorithm.h:538
#define GAUDI_API
Definition: Kernel.h:81
unsigned int cardinality() const override
Return the cardinality.
Definition: Algorithm.h:423
StatusCode configure() override
Dummy implementation of IStateful::configure() method.
Definition: Algorithm.h:174
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: Algorithm.h:459
STL class.
std::vector< BaseToolHandle * > m_toolHandles
Definition: Algorithm.h:458
StatusCode stop() override
the default (empty) implementation of IStateful::stop() method
Definition: Algorithm.h:183
Gaudi::StateMachine::State targetFSMState() const override
returns the state the algorithm will be in after the ongoing transition
Definition: Algorithm.h:193
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:41
void setType(std::string type) override
Definition: Algorithm.h:167