The Gaudi Framework  v30r3 (a5ef0a68)
GaudiCommon.h
Go to the documentation of this file.
1 #ifndef GAUDIALG_GAUDICOMMON_H
2 #define GAUDIALG_GAUDICOMMON_H 1
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 // from STL
7 // ============================================================================
8 #include <algorithm>
9 #include <functional>
10 #include <list>
11 #include <map>
12 #include <mutex>
13 #include <string>
14 #include <vector>
15 // ============================================================================
16 // GaudiKernel
17 // ============================================================================
20 #include "GaudiKernel/HashMap.h"
22 #include "GaudiKernel/IAlgTool.h"
23 #include "GaudiKernel/IAlgorithm.h"
28 #include "GaudiKernel/IToolSvc.h"
31 #include "GaudiKernel/StatEntity.h"
32 #include "GaudiKernel/StatusCode.h"
33 #include "GaudiKernel/System.h"
34 
35 #ifdef __CLING__
36 #define WARN_UNUSED
37 #elif __cplusplus > 201402L
38 #define WARN_UNUSED [[nodiscard]]
39 #else
40 #define WARN_UNUSED [[gnu::warn_unused_result]]
41 #endif
42 
43 // ============================================================================
44 // forward declarations
45 // ============================================================================
46 class Algorithm; // GaudiKernel
47 class AlgTool; // GaudiKernel
48 class ISvcLocator;
49 namespace Gaudi
50 {
51  namespace Utils
52  {
53  template <class TYPE>
54  struct GetData;
55  }
56 }
57 
59 {
60  constexpr const struct svc_eq_t {
61  bool operator()( const std::string& n, const SmartIF<IService>& s ) const { return n == s->name(); };
62  bool operator()( const SmartIF<IService>& s, const std::string& n ) const { return s->name() == n; };
63  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() == n->name(); };
64  } svc_eq{};
65  constexpr const struct svc_lt_t {
66  bool operator()( const std::string& n, const SmartIF<IService>& s ) const { return n < s->name(); };
67  bool operator()( const SmartIF<IService>& s, const std::string& n ) const { return s->name() < n; };
68  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() < n->name(); };
69  } svc_lt{};
70 }
71 // ============================================================================
72 /* @file GaudiCommon.h
73  *
74  * Header file for class : GaudiCommon
75  *
76  * @author Chris Jones Christopher.Rob.Jones@cern.ch
77  * @author Vanya BELYAEV Ivan.Belyaev@itep.ru
78  * @author Rob Lambert Rob.Lambert@cern.ch
79  * @date 2009-08-04
80  */
81 // ============================================================================
91 // ============================================================================
92 template <class PBASE>
93 class GAUDI_API GaudiCommon : public PBASE
94 {
95 protected: // definitions
96  using base_class = PBASE;
97 
107  static const bool IgnoreRootInTES = false;
119  static const bool UseRootInTES = true;
120  // ==========================================================================
121 protected: // few actual data types
122  // ==========================================================================
132 
133  // ==========================================================================
134  // protected members such that they can be used in the derived classes
137 
138 public:
139  // ==========================================================================
173  template <class TYPE>
175  const bool useRootInTES = true ) const;
179  template <class TYPE>
180  typename Gaudi::Utils::GetData<TYPE>::return_type getIfExists( IDataProviderSvc* svc, const std::string& location,
181  const bool useRootInTES = true ) const;
208  template <class TYPE>
209  bool exist( IDataProviderSvc* svc, const std::string& location, const bool useRootInTES = true ) const;
237  template <class TYPE, class TYPE2>
238  typename Gaudi::Utils::GetData<TYPE>::return_type getOrCreate( IDataProviderSvc* svc, const std::string& location,
239  const bool useRootInTES = true ) const;
266  const bool useRootInTES = true ) const;
267  // [[deprecated( "please pass std::unique_ptr as 2nd argument" )]]
268  DataObject* put( IDataProviderSvc* svc, DataObject* object, const std::string& location,
269  const bool useRootInTES = true ) const
270  {
271  return put( svc, std::unique_ptr<DataObject>( object ), location, useRootInTES );
272  }
298  template <class TOOL>
299  TOOL* tool( const std::string& type, const std::string& name, const IInterface* parent = 0,
300  bool create = true ) const;
323  template <class TOOL>
324  TOOL* tool( const std::string& type, const IInterface* parent = 0, bool create = true ) const;
347  template <class SERVICE>
348  SmartIF<SERVICE> svc( const std::string& name, const bool create = true ) const;
350  inline IUpdateManagerSvc* updMgrSvc() const;
351 
352 public:
377  StatusCode Error( const std::string& msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
402  StatusCode Warning( const std::string& msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
419  StatusCode Info( const std::string& msg, const StatusCode st = StatusCode::SUCCESS, const size_t mx = 10 ) const;
432  StatusCode Print( const std::string& msg, const StatusCode st = StatusCode::SUCCESS,
433  const MSG::Level lev = MSG::INFO ) const;
442  inline void Assert( const bool ok, const std::string& message = "",
443  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
452  inline void Assert( const bool ok, const char* message,
453  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
462  void Exception( const std::string& msg, const GaudiException& exc,
463  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
473  void Exception( const std::string& msg, const std::exception& exc,
474  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
483  void Exception( const std::string& msg = "no message",
484  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
485 
486 private:
488  inline StatisticsOwn countersOwn() const { return m_countersOwn; }
489 public:
490  // ==========================================================================
492  inline const Statistics counters() const { return m_counters; }
511  //[[deprecated( "see LHCBPS-1758" )]]
512  inline StatEntity& counter( const std::string& tag ) const
513  {
514  return const_cast<GaudiCommon<PBASE>*>( this )->counter( tag );
515  }
516  inline StatEntity& counter( const std::string& tag )
517  {
518  std::lock_guard<std::mutex> lock( m_countersMutex );
519  // Return referenced StatEntity if it already exists, else create it
520  auto p = m_counters.find( tag );
521  if ( p == end( m_counters ) ) {
522  auto& counter = m_countersOwn[tag];
523  p = m_counters.emplace( tag, counter ).first;
524  }
525  return m_countersOwn[tag];
526  }
528  {
529  std::lock_guard<std::mutex> lock( m_countersMutex );
530  m_counters.emplace( tag, r );
531  }
532  // ==========================================================================
533 public:
535  inline bool typePrint() const { return m_typePrint; }
537  inline bool propsPrint() const { return m_propsPrint; }
539  inline bool statPrint() const { return m_statPrint; }
541  inline bool errorsPrint() const { return m_errorsPrint; }
542  // ==========================================================================
543 public:
548  long printStat( const MSG::Level level = MSG::ALWAYS ) const;
553  long printErrors( const MSG::Level level = MSG::ALWAYS ) const;
558  long printProps( const MSG::Level level = MSG::ALWAYS ) const;
574  template <class CallerClass>
575  inline void registerCondition( const std::string& condition, StatusCode ( CallerClass::*mf )() = nullptr )
576  {
577  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
578  }
609  template <class CallerClass, class CondType>
610  inline void registerCondition( const std::string& condition, CondType*& condPtrDest,
611  StatusCode ( CallerClass::*mf )() = NULL )
612  {
613  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf, condPtrDest );
614  }
616  template <class CallerClass>
617  inline void registerCondition( char* condition, StatusCode ( CallerClass::*mf )() = NULL )
618  {
619  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), std::string( condition ), mf );
620  }
636  template <class CallerClass, class TargetClass>
637  inline void registerCondition( TargetClass* condition, StatusCode ( CallerClass::*mf )() = NULL )
638  {
639  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
640  }
652  inline StatusCode runUpdate() { return updMgrSvc()->update( this ); }
653 
654 public:
657  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of<Algorithm, PBASE>::value, U>>
658  GaudiCommon( const std::string& name, ISvcLocator* pSvcLocator ) : base_class( name, pSvcLocator )
659  {
660  initGaudiCommonConstructor();
661  }
664  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of<AlgTool, PBASE>::value, U>>
665  GaudiCommon( const std::string& type, const std::string& name, const IInterface* ancestor )
666  : base_class( type, name, ancestor )
667  {
668  initGaudiCommonConstructor( this->parent() );
669  }
670 
671 public:
675  StatusCode initialize() override
676 #ifdef __ICC
677  {
678  return i_gcInitialize();
679  }
680  StatusCode i_gcInitialize()
681 #endif
682  ;
686  StatusCode finalize() override
687 #ifdef __ICC
688  {
689  return i_gcFinalize();
690  }
691  StatusCode i_gcFinalize()
692 #endif
693  ;
694 
695 private:
696  GaudiCommon() = delete;
697  GaudiCommon( const GaudiCommon& ) = delete;
698  GaudiCommon& operator=( const GaudiCommon& ) = delete;
699 
700 protected:
702  StatusCode releaseTool( const IAlgTool* tool ) const;
704  StatusCode releaseSvc( const IInterface* svc ) const;
705 
706 public:
726  StatusCode release( const IInterface* interface ) const;
727 
729  using PBASE::release;
730  // ==========================================================================
731 public:
732  // ==========================================================================
734  const Services& services() const { return m_services; } // get all services
735  // ==========================================================================
736 private:
737  // ==========================================================================
739  void printErrorHandler( Gaudi::Details::PropertyBase& /* theProp */ ); // "ErrorPrint"
741  void printPropsHandler( Gaudi::Details::PropertyBase& /* theProp */ ); // "PropertiesPrint"
743  void printStatHandler( Gaudi::Details::PropertyBase& /* theProp */ ); // "StatPrint"
744  // ==========================================================================
745 public:
746  // ==========================================================================
748  inline const std::string& context() const { return m_context; }
752  inline const std::string& rootInTES() const { return m_rootInTES; }
753  // ==========================================================================
754 public:
755  // ==========================================================================
757  const std::string fullTESLocation( const std::string& location, const bool useRootInTES ) const;
758  // ==========================================================================
759 private:
760  // ==========================================================================
762  void addToServiceList( SmartIF<IService> svc ) const;
764  void initGaudiCommonConstructor( const IInterface* parent = nullptr );
765  // ==========================================================================
766 private:
768  mutable AlgTools m_managedTools;
770  mutable Services m_services;
771  // ==========================================================================
773  mutable Counter m_errors;
775  mutable Counter m_warnings;
777  mutable Counter m_infos;
779  mutable Counter m_exceptions;
781  StatisticsOwn m_countersOwn;
782  Statistics m_counters;
785  // ==========================================================================
787  mutable IUpdateManagerSvc* m_updMgrSvc = nullptr;
788  // ==========================================================================
789  // Properties
790  Gaudi::Property<bool> m_errorsPrint{this, "ErrorsPrint", true, "print the statistics of errors/warnings/exceptions"};
791  Gaudi::Property<bool> m_propsPrint{this, "PropertiesPrint", false, "print the properties of the component"};
792  Gaudi::Property<bool> m_statPrint{this, "StatPrint", true, "print the table of counters"};
793  Gaudi::Property<bool> m_typePrint{this, "TypePrint", true, "add the actual C++ component type into the messages"};
794 
795  Gaudi::Property<std::string> m_context{this, "Context", {}, "note: overridden by parent settings"};
796  Gaudi::Property<std::string> m_rootInTES{this, "RootInTES", {}, "note: overridden by parent settings"};
797 
798  Gaudi::Property<std::string> m_header{this, "StatTableHeader",
799  " | Counter | # | "
800  " sum | mean/eff^* | rms/err^* | min | max |",
801  "the header row for the output Stat-table"};
803  this, "RegularRowFormat", " | %|-48.48s|%|50t||%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |",
804  "the format for regular row in the output Stat-table"};
806  this, "EfficiencyRowFormat",
807  " |*%|-48.48s|%|50t||%|10d| |%|11.5g| |(%|#9.6g| +- %|-#9.6g|)%%| ------- | ------- |",
808  "The format for \"efficiency\" row in the output Stat-table"};
809  Gaudi::Property<bool> m_useEffFormat{this, "UseEfficiencyRowFormat", true,
810  "use the special format for printout of efficiency counters"};
811 
813  this, "CounterList", {".*"}, "RegEx list, of simple integer counters for CounterSummary"};
815  this, "StatEntityList", {}, "RegEx list, of StatEntity counters for CounterSummary"};
816 };
817 // ============================================================================
818 #include "GaudiAlg/GaudiCommonImp.h"
819 // ============================================================================
820 
821 // ============================================================================
822 // The END
823 // ============================================================================
824 #endif // GAUDIALG_GAUDICOMMON_H
825 // ============================================================================
std::map< std::string, StatEntity > StatisticsOwn
the actual type of general counters
Definition: GaudiCommon.h:124
The implementation of inline/templated methods for class GaudiCommon.
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:67
Counter m_warnings
counter of warnings
Definition: GaudiCommon.h:775
constexpr static const auto FAILURE
Definition: StatusCode.h:88
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:66
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
Counter m_exceptions
Counter of exceptions.
Definition: GaudiCommon.h:779
Implementation of property with value of concrete type.
Definition: Property.h:381
const std::string & rootInTES() const
Returns the "rootInTES" string.
Definition: GaudiCommon.h:752
GaudiCommon(const std::string &name, ISvcLocator *pSvcLocator)
Algorithm constructor - the SFINAE constraint below ensures that this is constructor is only defined ...
Definition: GaudiCommon.h:658
StatEntity & counter(const std::string &tag)
Definition: GaudiCommon.h:516
std::vector< IAlgTool * > AlgTools
storage for active tools
Definition: GaudiCommon.h:129
constexpr const struct GaudiCommon_details::svc_eq_t svc_eq
AttribStringParser::Iterator end(const AttribStringParser &)
void registerCondition(TargetClass *condition, StatusCode(CallerClass::*mf)()=NULL)
register the current instance to the UpdateManagerSvc as a consumer for a condition.
Definition: GaudiCommon.h:637
std::map< std::string, unsigned int > Counter
the actual type error/warning counter
Definition: GaudiCommon.h:127
Services m_services
List of active services.
Definition: GaudiCommon.h:770
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:68
StatisticsOwn m_countersOwn
General counters.
Definition: GaudiCommon.h:781
Statistics m_counters
Definition: GaudiCommon.h:782
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:54
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:748
An empty ancester of all counters that knows how to print themselves.
Definition: Counters.h:610
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:61
std::mutex m_countersMutex
The counters mutex.
Definition: GaudiCommon.h:784
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:617
StatisticsOwn countersOwn() const
accessor to all owned counters
Definition: GaudiCommon.h:488
Data provider interface definition.
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:575
const Services & services() const
get the list of aquired services
Definition: GaudiCommon.h:734
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
bool statPrint() const
Print statistical counters at finalization ?
Definition: GaudiCommon.h:539
StatEntity & counter(const std::string &tag) const
accessor to certain counter by name
Definition: GaudiCommon.h:512
constexpr const struct GaudiCommon_details::svc_lt_t svc_lt
Counter m_errors
Counter of errors.
Definition: GaudiCommon.h:773
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
bool propsPrint() const
Print properties at initialization ?
Definition: GaudiCommon.h:537
Definition of the basic interface.
Definition: IInterface.h:277
Counter m_infos
counter of infos
Definition: GaudiCommon.h:777
bool errorsPrint() const
Print error counters at finalization ?
Definition: GaudiCommon.h:541
void registerCondition(CallerClass *instance, const std::string &condition="", typename ObjectMemberFunction< CallerClass >::MemberFunctionType mf=nullptr)
Register an object (algorithm instance) to the service.
std::vector< SmartIF< IService > > Services
storage for active services
Definition: GaudiCommon.h:131
SmartIF< ICounterSummarySvc > m_counterSummarySvc
a pointer to the CounterSummarySvc
Definition: GaudiCommon.h:136
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
Interface class to the Update Manager service.
DataObject * put(IDataProviderSvc *svc, DataObject *object, const std::string &location, const bool useRootInTES=true) const
Definition: GaudiCommon.h:268
StatusCode runUpdate()
asks the UpdateManagerSvc to perform an update of the instance (if needed) without waiting the next B...
Definition: GaudiCommon.h:652
STL class.
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
#define WARN_UNUSED
Definition: GaudiCommon.h:40
std::map< std::string, std::reference_wrapper< Gaudi::Accumulators::PrintableCounter > > Statistics
Definition: GaudiCommon.h:125
bool typePrint() const
Insert the actual C++ type of the algorithm/tool in the messages ?
Definition: GaudiCommon.h:535
StatusCode update(CallerClass *instance)
Update the given instance.
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:63
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
STL class.
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:610
void registerCounter(const std::string &tag, Gaudi::Accumulators::PrintableCounter &r)
Definition: GaudiCommon.h:527
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:47
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
string s
Definition: gaudirun.py:253
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:93
AlgTools m_managedTools
List of active tools.
Definition: GaudiCommon.h:768
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:62
GaudiCommon(const std::string &type, const 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:665
backward compatible StatEntity class.
Definition: Counters.h:777
#define GAUDI_API
Definition: Kernel.h:104
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
Helper functions to set/get the application return code.
Definition: __init__.py:1
Out1 * put(DataObjectHandle< Out1 > &out_handle, Out2 &&out)
const Statistics counters() const
accessor to all counters
Definition: GaudiCommon.h:492