Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v38r0 (2143aa4c)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
GaudiCommon.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2021 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 #ifndef GAUDIALG_GAUDICOMMON_H
12 #define GAUDIALG_GAUDICOMMON_H 1
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 // from STL
17 // ============================================================================
18 #include <algorithm>
19 #include <functional>
20 #include <list>
21 #include <map>
22 #include <mutex>
23 #include <string>
24 #include <vector>
25 // ============================================================================
26 // GaudiKernel
27 // ============================================================================
28 #include "GaudiAlg/FixTESPath.h"
31 #include "GaudiKernel/HashMap.h"
33 #include "GaudiKernel/IAlgTool.h"
34 #include "GaudiKernel/IAlgorithm.h"
39 #include "GaudiKernel/IToolSvc.h"
42 #include "GaudiKernel/StatEntity.h"
43 #include "GaudiKernel/StatusCode.h"
44 #include "GaudiKernel/System.h"
45 
46 // ============================================================================
47 // forward declarations
48 // ============================================================================
49 namespace Gaudi {
50  class Algorithm; // GaudiKernel
51 }
52 class AlgTool; // GaudiKernel
53 class ISvcLocator;
54 namespace Gaudi {
55  namespace Utils {
56  template <class TYPE>
57  struct GetData;
58  }
59 } // namespace Gaudi
60 
62  constexpr const struct svc_eq_t {
63  bool operator()( std::string_view n, const SmartIF<IService>& s ) const { return n == s->name(); };
64  bool operator()( const SmartIF<IService>& s, std::string_view n ) const { return s->name() == n; };
65  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() == n->name(); };
66  } svc_eq{};
67  constexpr const struct svc_lt_t {
68  bool operator()( std::string_view n, const SmartIF<IService>& s ) const { return n < s->name(); };
69  bool operator()( const SmartIF<IService>& s, std::string_view n ) const { return s->name() < n; };
70  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() < n->name(); };
71  } svc_lt{};
72 } // namespace GaudiCommon_details
73 // ============================================================================
74 /* @file GaudiCommon.h
75  *
76  * Header file for class : GaudiCommon
77  *
78  * @author Chris Jones Christopher.Rob.Jones@cern.ch
79  * @author Vanya BELYAEV Ivan.Belyaev@itep.ru
80  * @author Rob Lambert Rob.Lambert@cern.ch
81  * @date 2009-08-04
82  */
83 // ============================================================================
93 // ============================================================================
94 template <class PBASE>
95 class GAUDI_API GaudiCommon : public FixTESPath<PBASE> {
96 protected: // definitions
98 
108  static const bool IgnoreRootInTES = false;
120  static const bool UseRootInTES = true;
121  // ==========================================================================
122 protected: // few actual data types
123  // ==========================================================================
133 
134  // ==========================================================================
135  // protected members such that they can be used in the derived classes
138 
139 public:
140  // ==========================================================================
174  template <class TYPE>
176  const bool useRootInTES = true ) const;
180  template <class TYPE>
181  typename Gaudi::Utils::GetData<TYPE>::return_type getIfExists( IDataProviderSvc* svc, std::string_view location,
182  const bool useRootInTES = true ) const;
209  template <class TYPE>
210  bool exist( IDataProviderSvc* svc, std::string_view location, const bool useRootInTES = true ) const;
238  template <class TYPE, class TYPE2>
239  typename Gaudi::Utils::GetData<TYPE>::return_type getOrCreate( IDataProviderSvc* svc, std::string_view location,
240  const bool useRootInTES = true ) const;
266  DataObject* put( IDataProviderSvc* svc, std::unique_ptr<DataObject> object, std::string_view location,
267  const bool useRootInTES = true ) const;
268  // [[deprecated( "please pass std::unique_ptr as 2nd argument" )]]
269  DataObject* put( IDataProviderSvc* svc, DataObject* object, std::string_view location,
270  const bool useRootInTES = true ) const {
271  return put( svc, std::unique_ptr<DataObject>( object ), location, useRootInTES );
272  }
298  template <class TOOL>
299  TOOL* tool( std::string_view type, std::string_view name, const IInterface* parent = 0, bool create = true ) const;
322  template <class TOOL>
323  TOOL* tool( std::string_view type, const IInterface* parent = 0, bool create = true ) const;
346  template <class SERVICE>
347  SmartIF<SERVICE> svc( std::string_view name, const bool create = true ) const;
349  IUpdateManagerSvc* updMgrSvc() const;
350 
351 public:
375  StatusCode Error( std::string_view msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
399  StatusCode Warning( std::string_view msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
415  StatusCode Info( std::string_view msg, const StatusCode st = StatusCode::SUCCESS, const size_t mx = 10 ) const;
427  StatusCode Print( std::string_view msg, const StatusCode st = StatusCode::SUCCESS,
428  const MSG::Level lev = MSG::INFO ) const;
437  void Assert( const bool ok, std::string_view message = "", const StatusCode sc = StatusCode::FAILURE ) const;
446  void Exception( std::string_view msg, const GaudiException& exc, const StatusCode sc = StatusCode::FAILURE ) const;
456  void Exception( std::string_view msg, const std::exception& exc, const StatusCode sc = StatusCode::FAILURE ) const;
465  void Exception( std::string_view msg = "no message", const StatusCode sc = StatusCode::FAILURE ) const;
466 
467 private:
469  StatisticsOwn countersOwn() const { return m_countersOwn; }
470 
471 public:
490  //[[deprecated( "see LHCBPS-1758" )]], on top no garantee about thread safety
491  StatEntity& counter( std::string_view tag ) const { return const_cast<GaudiCommon<PBASE>*>( this )->counter( tag ); }
492  StatEntity& counter( std::string_view tag ) {
493  auto lock = std::scoped_lock{ m_countersOwnMutex };
494  // Return referenced StatEntity if it already exists, else create it
495  auto p = m_countersOwn.find( tag );
496  if ( p == m_countersOwn.end() ) {
497  auto [iter, b] = m_countersOwn.try_emplace( std::string{ tag } );
498  assert( b );
499  this->serviceLocator()->monitoringHub().registerEntity( this->name(), iter->first, StatEntity::typeString,
500  iter->second );
501  p = iter;
502  }
503  return p->second;
504  }
505  // ==========================================================================
506 public:
511  long printErrors( const MSG::Level level = MSG::ALWAYS ) const;
516  long printProps( const MSG::Level level = MSG::ALWAYS ) const;
532  template <class CallerClass>
533  void registerCondition( const std::string& condition, StatusCode ( CallerClass::*mf )() = nullptr ) {
534  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
535  }
566  template <class CallerClass, class CondType>
567  void registerCondition( const std::string& condition, CondType*& condPtrDest,
568  StatusCode ( CallerClass::*mf )() = NULL ) {
569  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf, condPtrDest );
570  }
572  template <class CallerClass>
573  void registerCondition( char* condition, StatusCode ( CallerClass::*mf )() = NULL ) {
574  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), std::string( condition ), mf );
575  }
591  template <class CallerClass, class TargetClass>
592  void registerCondition( TargetClass* condition, StatusCode ( CallerClass::*mf )() = NULL ) {
593  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
594  }
606  StatusCode runUpdate() { return updMgrSvc()->update( this ); }
607 
608 public:
611  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of_v<Gaudi::Algorithm, PBASE>, U>>
612  GaudiCommon( std::string name, ISvcLocator* pSvcLocator ) : base_class( std::move( name ), pSvcLocator ) {
613  initGaudiCommonConstructor();
614  }
617  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of_v<AlgTool, PBASE>, U>>
619  : base_class( std::move( type ), std::move( name ), ancestor ) {
620  initGaudiCommonConstructor( this->parent() );
621  }
622 
623 public:
627  StatusCode initialize() override;
631  StatusCode finalize() override;
632 
633  GaudiCommon() = delete;
634  GaudiCommon( const GaudiCommon& ) = delete;
635  GaudiCommon& operator=( const GaudiCommon& ) = delete;
636 
637 protected:
639  StatusCode releaseTool( const IAlgTool* tool ) const;
641  StatusCode releaseSvc( const IInterface* svc ) const;
642 
643 public:
663  StatusCode release( const IInterface* interface ) const;
664 
666  using PBASE::release;
667  // ==========================================================================
668 public:
669  // ==========================================================================
671  const Services& services() const { return m_services; } // get all services
672  // ==========================================================================
673 public:
674  // ==========================================================================
676  const std::string& context() const { return m_context; }
677 
678 private:
679  // ==========================================================================
681  void addToServiceList( SmartIF<IService> svc ) const;
683  void initGaudiCommonConstructor( const IInterface* parent = nullptr );
684  // ==========================================================================
685 private:
690  // ==========================================================================
691  static auto increment( Counter& c, std::string_view which ) {
692  auto i = c.find( which );
693  return i != c.end() ? ++( i->second ) : c.emplace( which, 1 ).first->second;
694  }
696  mutable Counter m_errors;
700  mutable Counter m_infos;
707  // ==========================================================================
709  mutable IUpdateManagerSvc* m_updMgrSvc = nullptr;
710 
711 protected:
712  // ==========================================================================
713  // Properties
714  Gaudi::Property<bool> m_errorsPrint{ this, "ErrorsPrint", true,
715  [this]( auto& ) {
716  // no action if not yet initialized
717  if ( this->FSMState() >= Gaudi::StateMachine::INITIALIZED &&
718  this->m_errorsPrint.value() ) {
719  this->printErrors();
720  }
721  },
722  "print the statistics of errors/warnings/exceptions" };
723  Gaudi::Property<bool> m_propsPrint{ this, "PropertiesPrint", false,
724  [this]( auto& ) {
725  // no action if not yet initialized
726  if ( this->FSMState() >= Gaudi::StateMachine::INITIALIZED &&
727  this->m_propsPrint.value() ) {
728  this->printProps( MSG::ALWAYS );
729  }
730  },
731  "print the properties of the component" };
732  Gaudi::Property<bool> m_typePrint{ this, "TypePrint", true, "add the actual C++ component type into the messages" };
733 
734  Gaudi::Property<std::string> m_context{ this, "Context", {}, "note: overridden by parent settings" };
736  this, "CounterList", { ".*" }, "RegEx list, of simple integer counters for CounterSummary" };
737 };
738 // ============================================================================
739 #include "GaudiAlg/GaudiCommonImp.h"
740 // ============================================================================
741 
742 // ============================================================================
743 // The END
744 // ============================================================================
745 #endif // GAUDIALG_GAUDICOMMON_H
GaudiCommon::m_warnings
Counter m_warnings
counter of warnings
Definition: GaudiCommon.h:698
GaudiCommon_details::svc_eq_t::operator()
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:65
GaudiHive.precedence.message
message
Definition: precedence.py:19
GaudiCommon::StatisticsOwn
std::map< std::string, StatEntity, std::less<> > StatisticsOwn
the actual type of general counters
Definition: GaudiCommon.h:125
std::string
STL class.
GaudiCommon_details::svc_lt_t
Definition: GaudiCommon.h:67
IAlgTool
Definition: IAlgTool.h:33
GaudiCommon_details
Definition: GaudiCommon.h:61
GaudiCommon::Statistics
std::map< std::string, std::reference_wrapper< Gaudi::Accumulators::PrintableCounter >, std::less<> > Statistics
Definition: GaudiCommon.h:126
std::exception
STL class.
GaudiCommon::operator=
GaudiCommon & operator=(const GaudiCommon &)=delete
GaudiAlg.HistoUtils.location
location
Definition: HistoUtils.py:964
bug_34121.name
name
Definition: bug_34121.py:20
MSG::INFO
@ INFO
Definition: IMessageSvc.h:25
GaudiCommonImp.h
System.h
GaudiException.h
GaudiCommon::counter
StatEntity & counter(std::string_view tag)
Definition: GaudiCommon.h:492
GaudiCommon_details::svc_eq_t
Definition: GaudiCommon.h:62
GaudiCommon::services
const Services & services() const
get the list of aquired services
Definition: GaudiCommon.h:671
gaudirun.s
string s
Definition: gaudirun.py:346
std::vector< IAlgTool * >
GaudiCommon::m_countersOwn
StatisticsOwn m_countersOwn
General counters.
Definition: GaudiCommon.h:704
StatEntity
backward compatible StatEntity class.
Definition: StatEntity.h:23
ISvcLocator
Definition: ISvcLocator.h:46
Gaudi::Utils::GetData
Definition: GaudiCommon.h:57
GaudiException
Definition: GaudiException.h:31
Algorithm
Alias for backward compatibility.
Definition: Algorithm.h:58
GaudiPartProp.decorators.get
get
decorate the vector of properties
Definition: decorators.py:282
StatEntity.h
GaudiCommon::GaudiCommon
GaudiCommon()=delete
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
GaudiCommon::m_exceptions
Counter m_exceptions
Counter of exceptions.
Definition: GaudiCommon.h:702
GaudiCommon::m_managedTools
AlgTools m_managedTools
List of active tools.
Definition: GaudiCommon.h:687
GaudiCommon_details::svc_lt
constexpr const struct GaudiCommon_details::svc_lt_t svc_lt
conf.release
string release
Definition: conf.py:27
gaudirun.c
c
Definition: gaudirun.py:525
GaudiCommon::GaudiCommon
GaudiCommon(const GaudiCommon &)=delete
std::less
StatusCode.h
IMessageSvc.h
IDataProviderSvc.h
HashMap.h
GaudiCommon::registerCondition
void registerCondition(TargetClass *condition, StatusCode(CallerClass::*mf)()=NULL)
register the current instance to the UpdateManagerSvc as a consumer for a condition.
Definition: GaudiCommon.h:592
GaudiCommon::Services
std::vector< SmartIF< IService > > Services
storage for active services
Definition: GaudiCommon.h:132
GaudiCommon::m_infos
Counter m_infos
counter of infos
Definition: GaudiCommon.h:700
GaudiCommon_details::svc_lt_t::operator()
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:70
IToolSvc.h
GaudiCommon::m_countersOwnMutex
std::mutex m_countersOwnMutex
The mutex for m_countersOwn.
Definition: GaudiCommon.h:706
bug_34121.tool
tool
Definition: bug_34121.py:17
GaudiCommon::runUpdate
StatusCode runUpdate()
asks the UpdateManagerSvc to perform an update of the instance (if needed) without waiting the next B...
Definition: GaudiCommon.h:606
StatusCode
Definition: StatusCode.h:65
GaudiCommon_details::svc_eq_t::operator()
bool operator()(std::string_view n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:63
GaudiTesting.BaseTest.which
def which(executable)
Definition: BaseTest.py:739
IAlgTool.h
GaudiCommon_details::svc_lt_t::operator()
bool operator()(std::string_view n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:68
SmartDataPtr.h
GaudiCommon_details::svc_eq
constexpr const struct GaudiCommon_details::svc_eq_t svc_eq
SmartIF< IService >
FixTESPath::initialize
StatusCode initialize() override
Definition: FixTESPath.h:49
StatEntity::typeString
static const std::string typeString
Definition: StatEntity.h:25
std::map
STL class.
gaudirun.level
level
Definition: gaudirun.py:364
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
GaudiCommon::registerCondition
void registerCondition(const std::string &condition, CondType *&condPtrDest, StatusCode(CallerClass::*mf)()=NULL)
register the current instance to the UpdateManagerSvc as a consumer for a condition.
Definition: GaudiCommon.h:567
GaudiCommon::context
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:676
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:234
IUpdateManagerSvc.h
MSG::Level
Level
Definition: IMessageSvc.h:25
GaudiCommon::increment
static auto increment(Counter &c, std::string_view which)
Definition: GaudiCommon.h:691
gaudirun.type
type
Definition: gaudirun.py:160
GaudiCommon::registerCondition
void registerCondition(char *condition, StatusCode(CallerClass::*mf)()=NULL)
just to avoid conflicts with the version using a pointer to a template class.
Definition: GaudiCommon.h:573
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ICounterSummarySvc.h
IAlgContextSvc.h
AlgTool
Definition: AlgTool.h:62
IChronoStatSvc.h
GaudiCommon::countersOwn
StatisticsOwn countersOwn() const
accessor to all owned counters
Definition: GaudiCommon.h:469
MSG::ALWAYS
@ ALWAYS
Definition: IMessageSvc.h:25
IUpdateManagerSvc
Definition: IUpdateManagerSvc.h:142
std
STL namespace.
Gaudi::StateMachine::INITIALIZED
@ INITIALIZED
Definition: StateMachine.h:25
IInterface
Definition: IInterface.h:237
GaudiCommon::m_services
Services m_services
List of active services.
Definition: GaudiCommon.h:689
Gaudi::Utils::GetData::return_type
_GetType< Type >::return_type return_type
the actual return type
Definition: GetData.h:120
AlgTools
Definition: AlgTools.py:1
GaudiCommon::GaudiCommon
GaudiCommon(std::string name, ISvcLocator *pSvcLocator)
Algorithm constructor - the SFINAE constraint below ensures that this is constructor is only defined ...
Definition: GaudiCommon.h:612
DataObject
Definition: DataObject.h:40
GaudiCommon::put
DataObject * put(IDataProviderSvc *svc, DataObject *object, std::string_view location, const bool useRootInTES=true) const
Definition: GaudiCommon.h:269
GaudiCommon::m_counterSummarySvc
SmartIF< ICounterSummarySvc > m_counterSummarySvc
a pointer to the CounterSummarySvc
Definition: GaudiCommon.h:137
GaudiCommon::Counter
std::map< std::string, unsigned int, std::less<> > Counter
the actual type error/warning counter
Definition: GaudiCommon.h:128
GaudiCommon::counter
StatEntity & counter(std::string_view tag) const
accessor to certain counter by name
Definition: GaudiCommon.h:491
std::mutex
STL class.
GaudiCommon::GaudiCommon
GaudiCommon(std::string type, std::string name, const IInterface *ancestor)
Tool constructor - SFINAE-ed to insure this constructor is only defined if PBASE derives from AlgTool...
Definition: GaudiCommon.h:618
IDataProviderSvc
Definition: IDataProviderSvc.h:53
GaudiCommon_details::svc_lt_t::operator()
bool operator()(const SmartIF< IService > &s, std::string_view n) const
Definition: GaudiCommon.h:69
IAlgorithm.h
GaudiCommon::m_errors
Counter m_errors
Counter of errors.
Definition: GaudiCommon.h:696
GaudiCommon_details::svc_eq_t::operator()
bool operator()(const SmartIF< IService > &s, std::string_view n) const
Definition: GaudiCommon.h:64
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
GaudiCommon::registerCondition
void registerCondition(const std::string &condition, StatusCode(CallerClass::*mf)()=nullptr)
register the current instance to the UpdateManagerSvc as a consumer for a condition.
Definition: GaudiCommon.h:533
FixTESPath
Definition: FixTESPath.h:30
std::unique_ptr
STL class.
DataObjectHandle.h
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Property< bool >
FixTESPath.h
Gaudi::Functional::details::put
auto put(const DataObjectHandle< Out1 > &out_handle, Out2 &&out)
Definition: details.h:169
GaudiCommon::AlgTools
std::vector< IAlgTool * > AlgTools
storage for active tools
Definition: GaudiCommon.h:130
GaudiCommon
Definition: GaudiCommon.h:95