The Gaudi Framework  v41r0 (bdbe7744)
Loading...
Searching...
No Matches
Algorithm.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 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
20#include <string>
21#include <vector>
22
23// Extra include files (forward declarations should be sufficient)
24#include <Gaudi/PluginService.h>
25#include <Gaudi/Property.h>
27#include <GaudiKernel/DataObjID.h> // must be include before Property.h, which is included in PropertyHolder.h
41#include <GaudiKernel/System.h>
43
44// For concurrency
49
50class IAlgTool;
51class ToolHandleInfo;
53
54#ifndef PACKAGE_VERSION
55# define PACKAGE_VERSION "unknown"
56#endif
57namespace Gaudi {
58 namespace Details {
60 }
61
86 : public DataHandleHolderBase<
87 PropertyHolder<CommonMessaging<implements<IAlgorithm, IDataHandleHolder, IProperty, IStateful>>>> {
88 public:
89#ifndef __REFLEX__
90 typedef Gaudi::PluginService::Factory<IAlgorithm*( const std::string&, ISvcLocator* )> Factory;
91#endif
93
98 Algorithm( std::string name, ISvcLocator* svcloc, std::string version = PACKAGE_VERSION )
99 : m_name( std::move( name ) )
100 , m_version( std::move( version ) ) // incremented by AlgResourcePool
101 , m_pSvcLocator( svcloc ) {}
102
108 StatusCode sysStart() override;
109
115 StatusCode sysInitialize() override;
116
122 StatusCode sysReinitialize() override;
123
128 StatusCode sysRestart() override;
129
137 StatusCode sysExecute( const EventContext& ctx ) override;
138
142 StatusCode sysStop() override;
143
148 StatusCode sysFinalize() override;
149
157 const std::string& name() const override;
158 const Gaudi::StringKey& nameKey() const override;
159
162 const std::string& type() const override { return m_type; }
163 void setType( std::string type ) override { m_type = std::move( type ); } // BH, TODO: move to proper place
164
165 const std::string& version() const override;
166
167 unsigned int index() const override;
168
173
177 StatusCode start() override { return StatusCode::SUCCESS; }
179 StatusCode stop() override { return StatusCode::SUCCESS; }
183 StatusCode reinitialize() override;
185 StatusCode restart() override;
187 Gaudi::StateMachine::State FSMState() const override { return m_state; }
190
192 bool isEnabled() const override;
193
195 bool isSequence() const override { return false; }
196
198 unsigned int errorCount() const;
199
201 SmartIF<IService> service( std::string_view name, const bool createIf = true, const bool quiet = false ) const;
202
203 template <class T>
204 SmartIF<T> service( std::string_view name, bool createIf = true, bool quiet = false ) const {
205 return service( name, createIf, quiet ).as<T>();
206 }
207
211 SmartIF<IAuditorSvc>& auditorSvc() const;
212
216 SmartIF<IChronoStatSvc>& chronoSvc() const;
217
221 SmartIF<IDataProviderSvc>& detSvc() const;
222
226 SmartIF<IConversionSvc>& detCnvSvc() const;
227
231 SmartIF<IDataProviderSvc>& eventSvc() const;
234
238 SmartIF<IConversionSvc>& eventCnvSvc() const;
239
243 SmartIF<IHistogramSvc>& histoSvc() const;
244
248 SmartIF<INTupleSvc>& ntupleSvc() const;
249
253 SmartIF<IRndmGenSvc>& randSvc() const;
254
256 SmartIF<IToolSvc>& toolSvc() const;
257
259 SmartIF<IExceptionSvc>& exceptionSvc() const;
260
262 SmartIF<IAlgContextSvc>& contextSvc() const;
263
264 SmartIF<ITimelineSvc>& timelineSvc() const;
265
271 SmartIF<ISvcLocator>& serviceLocator() const override;
274
275 SmartIF<IHiveWhiteBoard>& whiteboard() const;
276
277 SmartIF<IAlgExecStateSvc>& algExecStateSvc() const;
278
280 bool registerContext() const { return m_registerContext; }
281
282 using PropertyHolderImpl::declareProperty;
283
284 // declare Tools to the Algorithms
285 template <class T>
287 const std::string& doc = "none" ) {
288 this->declareTool( hndl, hndl.typeAndName() ).ignore();
289 return PropertyHolderImpl::declareProperty( name, hndl, doc );
290 }
291
292 // declare ToolHandleArrays to the Algorithms
293
294 template <class T>
296 const std::string& doc = "none" ) {
297 addToolsArray( hndlArr );
298 return PropertyHolderImpl::declareProperty( name, hndlArr, doc );
299 }
300
311 // If not already located try to locate it without forcing a creation
312 if ( !m_pMonitorSvc ) {
313 m_pMonitorSvc = service( m_monitorSvcName, false, true ); // do not create and be quiet
314 }
315 return m_pMonitorSvc;
316 }
317
323 template <class T>
324 void declareInfo( const std::string& name, const T& var, const std::string& desc ) const {
325 IMonitorSvc* mS = monitorSvc().get();
326 if ( mS ) mS->declareInfo( name, var, desc, this );
327 }
328
336 void declareInfo( const std::string& name, const std::string& format, const void* var, int size,
337 const std::string& desc ) const {
338 IMonitorSvc* mS = monitorSvc().get();
339 if ( mS ) mS->declareInfo( name, format, var, size, desc, this );
340 }
341
342 public:
343 void acceptDHVisitor( IDataHandleVisitor* ) const override;
344
345 void registerTool( IAlgTool* tool ) const;
346 void deregisterTool( IAlgTool* tool ) const;
347
348 template <class T>
349 StatusCode declareTool( ToolHandle<T>& handle, bool createIf = true ) {
350 return this->declareTool( handle, handle.typeAndName(), createIf );
351 }
352
353 template <class T>
354 StatusCode declareTool( ToolHandle<T>& handle, const std::string& toolTypeAndName, bool createIf = true ) {
355
356 StatusCode sc = handle.initialize( toolTypeAndName, handle.isPublic() ? nullptr : this, createIf );
357 if ( !sc ) {
358 throw GaudiException{ std::string{ "Cannot create handle for " } +
359 ( handle.isPublic() ? "public" : "private" ) + " tool " + toolTypeAndName,
360 name(), sc };
361 }
362
363 m_toolHandles.push_back( &handle );
364
365 return sc;
366 }
367
368 template <class T>
370 m_toolHandleArrays.push_back( &hndlArr );
371 }
372
373 const std::vector<IAlgTool*>& tools() const;
374
375 // Return the asynchronous flag
376 bool isAsynchronous() const { return m_asynchronous; }
377 // Set the asynchronous flag
378 void setAsynchronous( bool value ) { m_asynchronous = value; }
379
380 protected:
381 // Disable synchronous timeline recording and return if it was enabled before
382 bool setAsynchronousTimeline() { return std::exchange( m_doTimeline, false ); }
383
384 std::vector<IAlgTool*>& tools();
385
386 // // adds declared in- and outputs of subAlgorithms to own DOHs
387 // void addSubAlgorithmDataObjectHandles();
388
389 private:
390 // place IAlgTools defined via ToolHandles in m_tools
391 void initToolHandles() const;
392
393 public:
394 // /// Specifies the clonability of the algorithm
395 // bool isClonable() const override { return false; }
396
398 unsigned int cardinality() const override { return m_cardinality; }
399
400 const AlgResources_t& neededResources() const override { return m_neededResources; }
401
402 protected:
404 bool isInitialized() const override { return Gaudi::StateMachine::INITIALIZED == m_state; }
405
407 bool isFinalized() const override { return Gaudi::StateMachine::CONFIGURED == m_state; }
408
410 void setIndex( const unsigned int& idx ) override;
411
412 public:
415 AlgExecStateRef execState( const EventContext& ctx ) const override;
416
418 std::ostream& toControlFlowExpression( std::ostream& os ) const override;
419
420 protected:
421 bool isReEntrant() const override { return true; }
422
423 private:
425 std::string m_type;
426 std::string m_version;
427 unsigned int m_index = 0;
428
431
432 // tools used by algorithm
433 mutable std::vector<IAlgTool*> m_tools;
434 mutable std::vector<BaseToolHandle*> m_toolHandles;
435 mutable std::vector<GaudiHandleArrayBase*> m_toolHandleArrays;
436
437 private:
438 template <typename IFace>
439 SmartIF<IFace>& get_svc_( SmartIF<IFace>& p, const char* service_name ) const;
440
456
459
461
462 protected:
464 std::unique_ptr<IDataHandleVisitor> m_updateDataHandles;
465
466 private:
467 // Properties
469 this, "OutputLevel", MSG::NIL,
470 [this]( Gaudi::Details::PropertyBase& ) { this->updateMsgStreamOutputLevel( this->m_outputLevel ); },
471 "output level" };
472 Gaudi::Property<bool> m_isEnabled{ this, "Enable", true, "should the algorithm be executed or not" };
473
475 this, "AuditInitialize", Details::getDefaultAuditorValue( m_pSvcLocator ), "trigger auditor on initialize()" };
477 "trigger auditor on reinitialize()" };
479 "trigger auditor on restart()" };
481 "trigger auditor on execute()" };
483 "trigger auditor on finalize()" };
485 "trigger auditor on start()" };
486 Gaudi::Property<bool> m_auditorStop{ this, "AuditStop", m_auditorInitialize.value(), "trigger auditor on stop()" };
487
488 Gaudi::Property<bool> m_doTimeline{ this, "Timeline", true, "send events to TimelineSvc" };
489
490 Gaudi::Property<std::string> m_monitorSvcName{ this, "MonitorService", "MonitorSvc",
491 "name to use for Monitor Service" };
492
493 Gaudi::Property<bool> m_registerContext{ this, "RegisterForContextService", false,
494 "flag to enforce the registration for Algorithm Context Service" };
495
496 Gaudi::Property<int> m_cardinality{ this, "Cardinality", 0,
497 "how many clones to create - 0 means algo is reentrant" };
499 this, "NeededResources", {}, "named resources needed during event looping" };
500
501 Gaudi::Property<bool> m_asynchronous{ this, "Asynchronous", false,
502 "whether algorithm is asynchronous and uses Boost Fiber to suspend "
503 "while offloaded code is running." };
504 // The default should be changed to "false" for v29
505 Gaudi::Property<bool> m_filterCircDeps{ this, "FilterCircularDependencies", true,
506 "filter out circular data dependencies" };
507
508 mutable bool m_toolHandlesInit = false;
509
512
514 Algorithm( const Algorithm& ) = delete;
515
517 Algorithm& operator=( const Algorithm& ) = delete;
518 };
519} // namespace Gaudi
#define PACKAGE_VERSION
Definition Algorithm.h:55
#define GAUDI_API
Definition Kernel.h:49
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition MsgStream.cpp:93
wrapper on an Algorithm state.
The AlgorithmManager class is in charge of the creation of concrete instances of Algorithms.
This class represents an entry point to all the event specific data.
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
Definition Algorithm.h:295
Gaudi::StringKey m_name
Algorithm's name for identification.
Definition Algorithm.h:424
SmartIF< ISvcLocator > & svcLoc() const
shortcut for method serviceLocator
Definition Algorithm.h:273
unsigned int m_index
Algorithm's index.
Definition Algorithm.h:427
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:336
bool setAsynchronousTimeline()
Definition Algorithm.h:382
SmartIF< IDataProviderSvc > m_EDS
Event data service.
Definition Algorithm.h:442
Gaudi::Property< bool > m_auditorStart
Definition Algorithm.h:484
SmartIF< IAlgContextSvc > m_contextSvc
Algorithm Context Service.
Definition Algorithm.h:455
SmartIF< IChronoStatSvc > m_CSS
Chrono & Stat Service.
Definition Algorithm.h:449
SmartIF< IRndmGenSvc > m_RGS
Random Number Generator Service.
Definition Algorithm.h:450
std::unique_ptr< IDataHandleVisitor > m_updateDataHandles
Hook for for derived classes to provide a custom visitor for data handles.
Definition Algorithm.h:464
AlgExecStateRef::AlgKey m_execSvcKey
identifier of this algorithm in the AlgExecStateSvc
Definition Algorithm.h:430
Gaudi::Property< int > m_cardinality
Definition Algorithm.h:496
SmartIF< IDataProviderSvc > & eventSvc() const
The standard event data service.
Gaudi::Property< bool > m_doTimeline
Definition Algorithm.h:488
Gaudi::StateMachine::State FSMState() const override
returns the current state of the algorithm
Definition Algorithm.h:187
SmartIF< IFace > & get_svc_(SmartIF< IFace > &p, const char *service_name) const
Algorithm(const Algorithm &)=delete
delete copy constructor: NO COPY ALLOWED
Gaudi::Property< int > m_outputLevel
Definition Algorithm.h:468
Gaudi::Property< AlgResources_t > m_neededResources
Definition Algorithm.h:498
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition Algorithm.h:98
StatusCode declareTool(ToolHandle< T > &handle, bool createIf=true)
Definition Algorithm.h:349
StatusCode configure() override
Dummy implementation of IStateful::configure() method.
Definition Algorithm.h:170
SmartIF< T > service(std::string_view name, bool createIf=true, bool quiet=false) const
Definition Algorithm.h:204
Gaudi::Property< std::string > m_monitorSvcName
Definition Algorithm.h:490
Gaudi::StateMachine::State m_targetState
Algorithm has been initialized flag.
Definition Algorithm.h:511
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
Definition Algorithm.h:286
SmartIF< IConversionSvc > m_ECS
Event conversion service.
Definition Algorithm.h:444
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition Algorithm.h:175
void setAsynchronous(bool value)
Definition Algorithm.h:378
SmartIF< IExceptionSvc > m_EXS
Exception Handler Service.
Definition Algorithm.h:451
std::vector< GaudiHandleArrayBase * > m_toolHandleArrays
Definition Algorithm.h:435
Gaudi::PluginService::Factory< IAlgorithm *(const std::string &, ISvcLocator *)> Factory
Definition Algorithm.h:90
std::string m_type
Algorithm's type.
Definition Algorithm.h:425
Algorithm & operator=(const Algorithm &)=delete
delete assignment operator: NO ASSIGNMENT ALLOWED
const std::string & version() const override
bool registerContext() const
register for Algorithm Context Service?
Definition Algorithm.h:280
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition Algorithm.h:181
Gaudi::Property< bool > m_auditorExecute
Definition Algorithm.h:480
const AlgResources_t & neededResources() const override
Definition Algorithm.h:400
StatusCode declareTool(ToolHandle< T > &handle, const std::string &toolTypeAndName, bool createIf=true)
Definition Algorithm.h:354
SmartIF< ISvcLocator > & serviceLocator() const override
The standard service locator.
Gaudi::Property< bool > m_auditorFinalize
Definition Algorithm.h:482
void declareInfo(const std::string &name, const T &var, const std::string &desc) const
Declare monitoring information.
Definition Algorithm.h:324
SmartIF< IDataProviderSvc > m_DDS
Detector data service.
Definition Algorithm.h:445
bool isReEntrant() const override
Definition Algorithm.h:421
SmartIF< IConversionSvc > m_DCS
Detector conversion service.
Definition Algorithm.h:446
StatusCode stop() override
the default (empty) implementation of IStateful::stop() method
Definition Algorithm.h:179
Gaudi::Property< bool > m_auditorRestart
Definition Algorithm.h:478
const std::string & name() const override
The identifying name of the algorithm object.
void addToolsArray(ToolHandleArray< T > &hndlArr)
Definition Algorithm.h:369
SmartIF< INTupleSvc > m_NTS
N tuple service.
Definition Algorithm.h:448
Gaudi::Property< bool > m_asynchronous
Definition Algorithm.h:501
friend AlgorithmManager
Definition Algorithm.h:92
Gaudi::StateMachine::State m_state
flag indicating whether ToolHandle tools have been added to m_tools
Definition Algorithm.h:510
Gaudi::Property< bool > m_filterCircDeps
Definition Algorithm.h:505
bool isFinalized() const override
Has the Algorithm already been finalized?
Definition Algorithm.h:407
SmartIF< IHiveWhiteBoard > m_WB
Event data service (whiteboard).
Definition Algorithm.h:443
StatusCode terminate() override
Dummy implementation of IStateful::terminate() method.
Definition Algorithm.h:172
SmartIF< IAlgExecStateSvc > m_aess
Alg execution state mgr.
Definition Algorithm.h:458
SmartIF< IAuditorSvc > m_pAuditorSvc
Auditor Service.
Definition Algorithm.h:452
std::vector< BaseToolHandle * > m_toolHandles
Definition Algorithm.h:434
Gaudi::Property< bool > m_auditorInitialize
Definition Algorithm.h:474
SmartIF< IMonitorSvc > & monitorSvc() const
Access the monitor service.
Definition Algorithm.h:310
const std::string & type() const override
The type of the algorithm object.
Definition Algorithm.h:162
SmartIF< IService > service(std::string_view name, const bool createIf=true, const bool quiet=false) const
Return a pointer to the service identified by name (or "type/name").
Gaudi::StateMachine::State targetFSMState() const override
returns the state the algorithm will be in after the ongoing transition
Definition Algorithm.h:189
SmartIF< IMessageSvc > m_MS
Message service.
Definition Algorithm.h:441
SmartIF< ITimelineSvc > m_timelineSvc
Timeline Service.
Definition Algorithm.h:457
bool isAsynchronous() const
Definition Algorithm.h:376
StatusCode start() override
the default (empty) implementation of IStateful::start() method
Definition Algorithm.h:177
Gaudi::Property< bool > m_auditorStop
Definition Algorithm.h:486
SmartIF< IHistogramSvc > m_HDS
Histogram data service.
Definition Algorithm.h:447
SmartIF< IDataProviderSvc > & evtSvc() const
shortcut for method eventSvc
Definition Algorithm.h:233
Gaudi::Property< bool > m_isEnabled
Definition Algorithm.h:472
Gaudi::Property< bool > m_auditorReinitialize
Definition Algorithm.h:476
bool isInitialized() const override
Has the Algorithm already been initialized?
Definition Algorithm.h:404
Gaudi::Property< bool > m_registerContext
Definition Algorithm.h:493
void setType(std::string type) override
Definition Algorithm.h:163
bool isSequence() const override
Are we a Sequence?
Definition Algorithm.h:195
SmartIF< ISvcLocator > m_pSvcLocator
Pointer to service locator service.
Definition Algorithm.h:460
std::string m_version
Algorithm's version.
Definition Algorithm.h:426
SmartIF< IToolSvc > m_ptoolSvc
ToolSvc Service.
Definition Algorithm.h:453
SmartIF< IMonitorSvc > m_pMonitorSvc
Online Monitoring Service.
Definition Algorithm.h:454
unsigned int cardinality() const override
Return the cardinality.
Definition Algorithm.h:398
std::vector< IAlgTool * > m_tools
Definition Algorithm.h:433
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Implementation of property with value of concrete type.
Definition Property.h:35
Helper class for efficient "key" access for strings.
Definition StringKey.h:68
Define general base for Gaudi exception.
The interface implemented by the AlgTool base class.
Definition IAlgTool.h:29
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition IAlgorithm.h:37
Definition of the IMonitorSvc interface, which publishes Gaudi variables to outside monitoring proces...
Definition IMonitorSvc.h:32
virtual void declareInfo(const std::string &name, const bool &var, const std::string &desc, const IInterface *owner)=0
Declare monitoring information.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition ISvcLocator.h:42
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Details::PropertyBase &prop)
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition ToolHandle.h:396
Handle to be used in lieu of naked pointers to tools.
Definition ToolHandle.h:132
std::string typeAndName() const override
Definition ToolHandle.h:276
StatusCode initialize(const std::string &toolTypeAndName, const IInterface *parent=nullptr, bool createIf=true)
Definition ToolHandle.h:216
General info and helper functions for toolhandles and arrays.
Definition ToolHandle.h:40
bool isPublic() const noexcept
Definition ToolHandle.h:49
bool getDefaultAuditorValue(ISvcLocator *loc)
Definition Algorithm.cpp:44
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
@ NIL
Definition IMessageSvc.h:22
STL namespace.