The Gaudi Framework  v33r0 (d5ea422b)
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 
102  Algorithm( const std::string& name, ISvcLocator* svcloc, const std::string& version = PACKAGE_VERSION );
103 
109  StatusCode sysStart() override;
110 
116  StatusCode sysInitialize() override;
117 
123  StatusCode sysReinitialize() override;
124 
129  StatusCode sysRestart() override;
130 
138  StatusCode sysExecute( const EventContext& ctx ) override;
139 
143  StatusCode sysStop() override;
144 
149  StatusCode sysFinalize() override;
150 
158  const std::string& name() const override;
159  const Gaudi::StringKey& nameKey() const override;
160 
163  const std::string& type() const override { return m_type; }
164  void setType( const std::string& type ) override { m_type = type; } // BH, TODO: move to proper place
165 
166  const std::string& version() const override;
167 
168  unsigned int index() const override;
169 
171  StatusCode configure() override { return StatusCode::SUCCESS; }
173  StatusCode terminate() override { return StatusCode::SUCCESS; }
174 
178  StatusCode start() override { return StatusCode::SUCCESS; }
180  StatusCode stop() override { return StatusCode::SUCCESS; }
182  StatusCode finalize() override { return StatusCode::SUCCESS; }
184  StatusCode reinitialize() override;
186  StatusCode restart() override;
188  Gaudi::StateMachine::State FSMState() const override { return m_state; }
190  Gaudi::StateMachine::State targetFSMState() const override { return m_targetState; }
191 
193  bool isEnabled() const override;
194 
196  bool isSequence() const override { return false; }
197 
199  unsigned int errorCount() const;
200 
202  template <class T>
203  StatusCode service( const std::string& name, T*& psvc, bool createIf = true ) const {
204  return service_i( name, createIf, T::interfaceID(), (void**)&psvc );
205  }
206 
208  template <class T>
209  StatusCode service( const std::string& svcType, const std::string& svcName, T*& psvc ) const {
210  return service_i( svcType, svcName, T::interfaceID(), reinterpret_cast<void**>( &psvc ) );
211  }
212 
214  SmartIF<IService> service( const std::string& name, const bool createIf = true, const bool quiet = false ) const;
215 
216  template <class T>
217  SmartIF<T> service( const std::string& name, bool createIf = true, bool quiet = false ) const {
218  return service( name, createIf, quiet ).as<T>();
219  }
220 
224  SmartIF<IAuditorSvc>& auditorSvc() const;
225 
229  SmartIF<IChronoStatSvc>& chronoSvc() const;
230  [[deprecated( "use chronoSvc() instead" )]] SmartIF<IChronoStatSvc>& chronoStatService() const {
231  return chronoSvc();
232  }
233 
237  SmartIF<IDataProviderSvc>& detSvc() const;
238  [[deprecated( "use detSvc() instead" )]] SmartIF<IDataProviderSvc>& detDataService() const { return detSvc(); }
239 
243  SmartIF<IConversionSvc>& detCnvSvc() const;
244  [[deprecated( "use detCnvSvc() instead" )]] SmartIF<IConversionSvc>& detDataCnvService() const {
245  return detCnvSvc();
246  }
247 
251  SmartIF<IDataProviderSvc>& eventSvc() const;
253  SmartIF<IDataProviderSvc>& evtSvc() const { return eventSvc(); }
254  [[deprecated( "use eventSvc() instead" )]] SmartIF<IDataProviderSvc>& eventDataService() const {
255  return eventSvc();
256  }
257 
261  SmartIF<IConversionSvc>& eventCnvSvc() const;
262  [[deprecated( "use eventCnvSvc() instead" )]] SmartIF<IConversionSvc>& eventDataCnvService() const {
263  return eventCnvSvc();
264  }
265 
269  SmartIF<IHistogramSvc>& histoSvc() const;
270  [[deprecated( "use histoSvc() instead" )]] SmartIF<IHistogramSvc>& histogramDataService() const {
271  return histoSvc();
272  }
273 
277  SmartIF<INTupleSvc>& ntupleSvc() const;
278  [[deprecated( "use ntupleSvc() instead" )]] SmartIF<INTupleSvc>& ntupleService() const { return ntupleSvc(); }
279 
283  SmartIF<IRndmGenSvc>& randSvc() const;
284 
286  SmartIF<IToolSvc>& toolSvc() const;
287 
289  SmartIF<IExceptionSvc>& exceptionSvc() const;
290 
292  SmartIF<IAlgContextSvc>& contextSvc() const;
293 
294  SmartIF<ITimelineSvc>& timelineSvc() const;
295 
301  SmartIF<ISvcLocator>& serviceLocator() const override;
303  SmartIF<ISvcLocator>& svcLoc() const { return serviceLocator(); }
304 
306 
307  SmartIF<IAlgExecStateSvc>& algExecStateSvc() const;
308 
310  bool registerContext() const { return m_registerContext; }
311 
319  StatusCode setProperties();
320 
321  // ==========================================================================
322  using PropertyHolderImpl::declareProperty;
323 
324  // declare Tools to the Algorithms
325  template <class T>
327  const std::string& doc = "none" ) {
328  this->declareTool( hndl, hndl.typeAndName() ).ignore();
329  return PropertyHolderImpl::declareProperty( name, hndl, doc );
330  }
331 
332  // ==========================================================================
333  // declare ToolHandleArrays to the Algorithms
334 
335  template <class T>
337  const std::string& doc = "none" ) {
338  addToolsArray( hndlArr );
339  return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
340  }
341 
342  // ==========================================================================
353  // If not already located try to locate it without forcing a creation
354  if ( !m_pMonitorSvc ) {
355  m_pMonitorSvc = service( m_monitorSvcName, false, true ); // do not create and be quiet
356  }
357  return m_pMonitorSvc;
358  }
359 
365  template <class T>
366  void declareInfo( const std::string& name, const T& var, const std::string& desc ) const {
367  IMonitorSvc* mS = monitorSvc().get();
368  if ( mS ) mS->declareInfo( name, var, desc, this );
369  }
370 
378  void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
379  const std::string& desc ) const {
380  IMonitorSvc* mS = monitorSvc().get();
381  if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
382  }
383 
384  public:
385  void acceptDHVisitor( IDataHandleVisitor* ) const override;
386 
387  public:
388  void registerTool( IAlgTool* tool ) const;
389  void deregisterTool( IAlgTool* tool ) const;
390 
391  template <class T>
392  StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
393  return this->declareTool( handle, handle.typeAndName(), createIf );
394  }
395 
396  template <class T>
397  StatusCode declareTool( ToolHandle<T>& handle, std::string toolTypeAndName, bool createIf = true ) {
398 
399  StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
400  if ( UNLIKELY( !sc ) ) {
401  throw GaudiException{std::string{"Cannot create handle for "} + ( handle.isPublic() ? "public" : "private" ) +
402  " tool " + toolTypeAndName,
403  name(), sc};
404  }
405 
406  m_toolHandles.push_back( &handle );
407 
408  return sc;
409  }
410 
411  template <class T>
413  m_toolHandleArrays.push_back( &hndlArr );
414  }
415 
416  const std::vector<IAlgTool*>& tools() const;
417 
418  // Return the I/O-boundness flag
419  bool isIOBound() const { return m_isIOBound; }
420  // Set the I/O-boundness flag
421  void setIOBound( bool value ) { m_isIOBound = value; }
422 
423  protected:
424  std::vector<IAlgTool*>& tools();
425 
426  // // adds declared in- and outputs of subAlgorithms to own DOHs
427  // void addSubAlgorithmDataObjectHandles();
428 
429  private:
430  // place IAlgTools defined via ToolHandles in m_tools
431  void initToolHandles() const;
432 
433  public:
434  // /// Specifies the clonability of the algorithm
435  // bool isClonable() const override { return false; }
436 
438  unsigned int cardinality() const override { return m_cardinality; }
439 
440  const std::vector<std::string>& neededResources() const override { return m_neededResources; }
441 
442  protected:
444  bool isInitialized() const override { return Gaudi::StateMachine::INITIALIZED == m_state; }
445 
447  bool isFinalized() const override { return Gaudi::StateMachine::CONFIGURED == m_state; }
448 
450  void setIndex( const unsigned int& idx ) override;
451 
452  public:
454  AlgExecState& execState( const EventContext& ctx ) const override;
455 
457  std::ostream& toControlFlowExpression( std::ostream& os ) const override;
458 
459  private:
460  unsigned int maxErrors() const { return m_errorMax; }
461 
462  protected:
463  bool isReEntrant() const override { return true; }
464 
465  private:
469  unsigned int m_index = 0;
470 
471  // tools used by algorithm
475 
476  private:
477  template <typename IFace>
478  SmartIF<IFace>& get_svc_( SmartIF<IFace>& p, const char* service_name ) const;
479 
495 
498 
500 
501  protected:
504 
505  private:
506  // Properties
507  Gaudi::Property<int> m_outputLevel{
508  this, "OutputLevel", MSG::NIL,
509  [this]( Gaudi::Details::PropertyBase& ) { this->updateMsgStreamOutputLevel( this->m_outputLevel ); },
510  "output level"};
511  Gaudi::Property<bool> m_isEnabled{this, "Enable", true, "should the algorithm be executed or not"};
512 
513  Gaudi::Property<unsigned int> m_errorMax{this, "ErrorMax", 1, "[[deprecated]] max number of errors"};
514 
515  Gaudi::Property<bool> m_auditInit{this, "AuditAlgorithms", Details::getDefaultAuditorValue( m_pSvcLocator ),
516  "[[deprecated]] unused"};
517  Gaudi::Property<bool> m_auditorInitialize{this, "AuditInitialize", m_auditInit.value(),
518  "trigger auditor on initialize()"};
519  Gaudi::Property<bool> m_auditorReinitialize{this, "AuditReinitialize", m_auditInit.value(),
520  "trigger auditor on reinitialize()"};
521  Gaudi::Property<bool> m_auditorRestart{this, "AuditRestart", m_auditInit.value(), "trigger auditor on restart()"};
522  Gaudi::Property<bool> m_auditorExecute{this, "AuditExecute", m_auditInit.value(), "trigger auditor on execute()"};
523  Gaudi::Property<bool> m_auditorFinalize{this, "AuditFinalize", m_auditInit.value(),
524  "trigger auditor on finalize()"};
525  Gaudi::Property<bool> m_auditorStart{this, "AuditStart", m_auditInit.value(), "trigger auditor on start()"};
526  Gaudi::Property<bool> m_auditorStop{this, "AuditStop", m_auditInit.value(), "trigger auditor on stop()"};
527 
528  Gaudi::Property<bool> m_doTimeline{this, "Timeline", true, "send events to TimelineSvc"};
529 
530  Gaudi::Property<std::string> m_monitorSvcName{this, "MonitorService", "MonitorSvc",
531  "name to use for Monitor Service"};
532 
533  Gaudi::Property<bool> m_registerContext{this, "RegisterForContextService", false,
534  "flag to enforce the registration for Algorithm Context Service"};
535 
536  Gaudi::Property<int> m_cardinality{this, "Cardinality", 0, "how many clones to create - 0 means algo is reentrant"};
538  this, "NeededResources", {}, "named resources needed during event looping"};
539 
540  Gaudi::Property<bool> m_isIOBound{this, "IsIOBound", false,
541  "if the algorithm is I/O-bound (in the broad sense of Von Neumann bottleneck)"};
542 
543  // The default should be changed to "false" for v29
544  Gaudi::Property<bool> m_filterCircDeps{this, "FilterCircularDependencies", true,
545  "filter out circular data dependencies"};
546 
547  mutable bool m_toolHandlesInit = false;
548 
552 
554  StatusCode service_i( const std::string& svcName, bool createIf, const InterfaceID& iid, void** ppSvc ) const;
555  StatusCode service_i( const std::string& svcType, const std::string& svcName, const InterfaceID& iid,
556  void** ppSvc ) const;
557 
559  Algorithm( const Algorithm& a ) = delete;
560 
562  Algorithm& operator=( const Algorithm& rhs ) = delete;
563  };
564 } // 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:397
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition: Algorithm.h:491
The AlgorithmManager class is in charge of the creation of concrete instances of Algorithms.
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition: Algorithm.h:412
#define UNLIKELY(x)
Definition: Kernel.h:106
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:209
SmartIF< IAlgExecStateSvc > m_aess
Alg execution state mgr.
Definition: Algorithm.h:497
bool isIOBound() const
Definition: Algorithm.h:419
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:503
Implementation of property with value of concrete type.
Definition: Property.h:370
SmartIF< IExceptionSvc > m_EXS
Exception Handler Service.
Definition: Algorithm.h:490
StatusCode start() override
the default (empty) implementation of IStateful::start() method
Definition: Algorithm.h:178
SmartIF< IAlgContextSvc > m_contextSvc
Algorithm Context Service.
Definition: Algorithm.h:494
SmartIF< IConversionSvc > & eventDataCnvService() const
Definition: Algorithm.h:262
SmartIF< IChronoStatSvc > m_CSS
Chrono & Stat Service.
Definition: Algorithm.h:488
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:378
constexpr static const auto SUCCESS
Definition: StatusCode.h:96
bool isReEntrant() const override
Definition: Algorithm.h:463
SmartIF< IDataProviderSvc > & evtSvc() const
shortcut for method eventSvc
Definition: Algorithm.h:253
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:484
SmartIF< IConversionSvc > & detDataCnvService() const
Definition: Algorithm.h:244
SmartIF< IHistogramSvc > & histogramDataService() const
Definition: Algorithm.h:270
SmartIF< IDataProviderSvc > & detDataService() const
Definition: Algorithm.h:238
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:366
SmartIF< IToolSvc > m_ptoolSvc
ToolSvc Service.
Definition: Algorithm.h:492
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
SmartIF< IDataProviderSvc > & eventDataService() const
Definition: Algorithm.h:254
STL class.
SmartIF< IMonitorSvc > & monitorSvc() const
Access the monitor service.
Definition: Algorithm.h:352
void setType(const std::string &type) override
Definition: Algorithm.h:164
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:176
std::string m_version
Algorithm's version.
Definition: Algorithm.h:468
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition: Algorithm.h:392
SmartIF< T > service(const std::string &name, bool createIf=true, bool quiet=false) const
Definition: Algorithm.h:217
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition: Algorithm.h:326
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition: IMonitorSvc.h:33
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:203
bool getDefaultAuditorValue(ISvcLocator *loc)
Definition: Algorithm.cpp:47
SmartIF< IConversionSvc > m_ECS
Event conversion service.
Definition: Algorithm.h:483
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
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:163
void setIOBound(bool value)
Definition: Algorithm.h:421
SmartIF< IHistogramSvc > m_HDS
Histogram data service.
Definition: Algorithm.h:486
SmartIF< IMessageSvc > m_MS
Message service.
Definition: Algorithm.h:480
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition: Algorithm.h:182
SmartIF< ISvcLocator > m_pSvcLocator
Pointer to service locator service.
Definition: Algorithm.h:499
StatusCode terminate() override
Dummy implementation of IStateful::terminate() method.
Definition: Algorithm.h:173
Gaudi::PluginService::Factory< IAlgorithm *(const std::string &, ISvcLocator *)> Factory
Definition: Algorithm.h:94
std::vector< IAlgTool * > m_tools
Definition: Algorithm.h:472
SmartIF< IConversionSvc > m_DCS
Detector conversion service.
Definition: Algorithm.h:485
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:42
SmartIF< IDataProviderSvc > m_EDS
Event data service.
Definition: Algorithm.h:481
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition: Algorithm.h:336
bool registerContext() const
register for Algorithm Context Service?
Definition: Algorithm.h:310
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:38
const std::vector< std::string > & neededResources() const override
Definition: Algorithm.h:440
SmartIF< INTupleSvc > m_NTS
N tuple service.
Definition: Algorithm.h:487
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:489
bool isInitialized() const override
Has the Algorithm already been initialized?
Definition: Algorithm.h:444
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:493
std::string m_type
Algorithm's type.
Definition: Algorithm.h:467
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:33
unsigned int maxErrors() const
Definition: Algorithm.h:460
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:303
friend AlgorithmManager
Definition: Algorithm.h:96
SmartIF< ITimelineSvc > m_timelineSvc
Timeline Service.
Definition: Algorithm.h:496
bool isSequence() const override
Are we a Sequence?
Definition: Algorithm.h:196
Gaudi::StateMachine::State FSMState() const override
returns the current state of the algorithm
Definition: Algorithm.h:188
Gaudi::StringKey m_name
Algorithm's name for identification.
Definition: Algorithm.h:466
bool isFinalized() const override
Has the Algorithm already been finalized?
Definition: Algorithm.h:447
SmartIF< IHiveWhiteBoard > m_WB
Event data service (whiteboard)
Definition: Algorithm.h:482
bool m_isFinalized
Algorithm has been finalized flag.
Definition: Algorithm.h:551
#define GAUDI_API
Definition: Kernel.h:81
unsigned int cardinality() const override
Return the cardinality.
Definition: Algorithm.h:438
StatusCode configure() override
Dummy implementation of IStateful::configure() method.
Definition: Algorithm.h:171
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition: Algorithm.h:474
STL class.
std::vector< BaseToolHandle * > m_toolHandles
Definition: Algorithm.h:473
StatusCode stop() override
the default (empty) implementation of IStateful::stop() method
Definition: Algorithm.h:180
Gaudi::StateMachine::State targetFSMState() const override
returns the state the algorithm will be in after the ongoing transition
Definition: Algorithm.h:190
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
SmartIF< IChronoStatSvc > & chronoStatService() const
Definition: Algorithm.h:230
General info and helper functions for toolhandles and arrays.
Definition: ToolHandle.h:41
SmartIF< INTupleSvc > & ntupleService() const
Definition: Algorithm.h:278