The Gaudi Framework  v30r2 (9eca68f7)
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 <map>
10 #include <string>
11 #include <vector>
12 // ============================================================================
13 // GaudiKernel
14 // ============================================================================
17 #include "GaudiKernel/HashMap.h"
19 #include "GaudiKernel/IAlgTool.h"
20 #include "GaudiKernel/IAlgorithm.h"
25 #include "GaudiKernel/IToolSvc.h"
28 #include "GaudiKernel/StatEntity.h"
29 #include "GaudiKernel/StatusCode.h"
30 #include "GaudiKernel/System.h"
31 // ============================================================================
32 // TBB
33 // ============================================================================
34 #if defined( __GNUC__ ) && __GNUC__ >= 5
35 #pragma GCC diagnostic push
36 #pragma GCC diagnostic ignored "-Wsuggest-override"
37 #endif
38 #include "tbb/concurrent_unordered_map.h"
39 #if defined( __GNUC__ ) && __GNUC__ >= 5
40 #pragma GCC diagnostic pop
41 #endif
42 
43 #ifdef __CLING__
44 #define WARN_UNUSED
45 #elif __cplusplus > 201402L
46 #define WARN_UNUSED [[nodiscard]]
47 #else
48 #define WARN_UNUSED [[gnu::warn_unused_result]]
49 #endif
50 
51 // ============================================================================
52 // forward declarations
53 // ============================================================================
54 class Algorithm; // GaudiKernel
55 class AlgTool; // GaudiKernel
56 class ISvcLocator;
57 namespace Gaudi
58 {
59  namespace Utils
60  {
61  template <class TYPE>
62  struct GetData;
63  }
64 }
65 
67 {
68  constexpr const struct svc_eq_t {
69  bool operator()( const std::string& n, const SmartIF<IService>& s ) const { return n == s->name(); };
70  bool operator()( const SmartIF<IService>& s, const std::string& n ) const { return s->name() == n; };
71  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() == n->name(); };
72  } svc_eq{};
73  constexpr const struct svc_lt_t {
74  bool operator()( const std::string& n, const SmartIF<IService>& s ) const { return n < s->name(); };
75  bool operator()( const SmartIF<IService>& s, const std::string& n ) const { return s->name() < n; };
76  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() < n->name(); };
77  } svc_lt{};
78 }
79 // ============================================================================
80 /* @file GaudiCommon.h
81  *
82  * Header file for class : GaudiCommon
83  *
84  * @author Chris Jones Christopher.Rob.Jones@cern.ch
85  * @author Vanya BELYAEV Ivan.Belyaev@itep.ru
86  * @author Rob Lambert Rob.Lambert@cern.ch
87  * @date 2009-08-04
88  */
89 // ============================================================================
99 // ============================================================================
100 template <class PBASE>
101 class GAUDI_API GaudiCommon : public PBASE
102 {
103 protected: // definitions
104  using base_class = PBASE;
105 
115  static const bool IgnoreRootInTES = false;
127  static const bool UseRootInTES = true;
128  // ==========================================================================
129 protected: // few actual data types
130  // ==========================================================================
132  typedef tbb::concurrent_unordered_map<std::string, StatEntity> Statistics;
139 
140  // ==========================================================================
141  // protected members such that they can be used in the derived classes
144 
145 public:
146  // ==========================================================================
180  template <class TYPE>
182  const bool useRootInTES = true ) const;
186  template <class TYPE>
187  typename Gaudi::Utils::GetData<TYPE>::return_type getIfExists( IDataProviderSvc* svc, const std::string& location,
188  const bool useRootInTES = true ) const;
215  template <class TYPE>
216  bool exist( IDataProviderSvc* svc, const std::string& location, const bool useRootInTES = true ) const;
244  template <class TYPE, class TYPE2>
245  typename Gaudi::Utils::GetData<TYPE>::return_type getOrCreate( IDataProviderSvc* svc, const std::string& location,
246  const bool useRootInTES = true ) const;
273  const bool useRootInTES = true ) const;
274  // [[deprecated( "please pass std::unique_ptr as 2nd argument" )]]
275  DataObject* put( IDataProviderSvc* svc, DataObject* object, const std::string& location,
276  const bool useRootInTES = true ) const
277  {
278  return put( svc, std::unique_ptr<DataObject>( object ), location, useRootInTES );
279  }
305  template <class TOOL>
306  TOOL* tool( const std::string& type, const std::string& name, const IInterface* parent = 0,
307  bool create = true ) const;
330  template <class TOOL>
331  TOOL* tool( const std::string& type, const IInterface* parent = 0, bool create = true ) const;
354  template <class SERVICE>
355  SmartIF<SERVICE> svc( const std::string& name, const bool create = true ) const;
357  inline IUpdateManagerSvc* updMgrSvc() const;
358 
359 public:
384  StatusCode Error( const std::string& msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
409  StatusCode Warning( const std::string& msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
426  StatusCode Info( const std::string& msg, const StatusCode st = StatusCode::SUCCESS, const size_t mx = 10 ) const;
439  StatusCode Print( const std::string& msg, const StatusCode st = StatusCode::SUCCESS,
440  const MSG::Level lev = MSG::INFO ) const;
449  inline void Assert( const bool ok, const std::string& message = "",
450  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
459  inline void Assert( const bool ok, const char* message,
460  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
469  void Exception( const std::string& msg, const GaudiException& exc,
470  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
480  void Exception( const std::string& msg, const std::exception& exc,
481  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
490  void Exception( const std::string& msg = "no message",
491  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
492 
493 public:
494  // ==========================================================================
496  inline const Statistics& counters() const { return m_counters; }
515  inline StatEntity& counter( const std::string& tag ) const { return m_counters[tag]; }
516  // ==========================================================================
517 public:
519  inline bool typePrint() const { return m_typePrint; }
521  inline bool propsPrint() const { return m_propsPrint; }
523  inline bool statPrint() const { return m_statPrint; }
525  inline bool errorsPrint() const { return m_errorsPrint; }
526  // ==========================================================================
527 public:
532  long printStat( const MSG::Level level = MSG::ALWAYS ) const;
537  long printErrors( const MSG::Level level = MSG::ALWAYS ) const;
542  long printProps( const MSG::Level level = MSG::ALWAYS ) const;
558  template <class CallerClass>
559  inline void registerCondition( const std::string& condition, StatusCode ( CallerClass::*mf )() = nullptr )
560  {
561  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
562  }
593  template <class CallerClass, class CondType>
594  inline void registerCondition( const std::string& condition, CondType*& condPtrDest,
595  StatusCode ( CallerClass::*mf )() = NULL )
596  {
597  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf, condPtrDest );
598  }
600  template <class CallerClass>
601  inline void registerCondition( char* condition, StatusCode ( CallerClass::*mf )() = NULL )
602  {
603  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), std::string( condition ), mf );
604  }
620  template <class CallerClass, class TargetClass>
621  inline void registerCondition( TargetClass* condition, StatusCode ( CallerClass::*mf )() = NULL )
622  {
623  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
624  }
636  inline StatusCode runUpdate() { return updMgrSvc()->update( this ); }
637 
638 public:
641  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of<Algorithm, PBASE>::value, U>>
642  GaudiCommon( const std::string& name, ISvcLocator* pSvcLocator ) : base_class( name, pSvcLocator )
643  {
644  initGaudiCommonConstructor();
645  }
648  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of<AlgTool, PBASE>::value, U>>
649  GaudiCommon( const std::string& type, const std::string& name, const IInterface* ancestor )
650  : base_class( type, name, ancestor )
651  {
652  initGaudiCommonConstructor( this->parent() );
653  }
654 
655 public:
659  StatusCode initialize() override
660 #ifdef __ICC
661  {
662  return i_gcInitialize();
663  }
664  StatusCode i_gcInitialize()
665 #endif
666  ;
670  StatusCode finalize() override
671 #ifdef __ICC
672  {
673  return i_gcFinalize();
674  }
675  StatusCode i_gcFinalize()
676 #endif
677  ;
678 
679 private:
680  GaudiCommon() = delete;
681  GaudiCommon( const GaudiCommon& ) = delete;
682  GaudiCommon& operator=( const GaudiCommon& ) = delete;
683 
684 protected:
686  StatusCode releaseTool( const IAlgTool* tool ) const;
688  StatusCode releaseSvc( const IInterface* svc ) const;
689 
690 public:
710  StatusCode release( const IInterface* interface ) const;
711 
713  using PBASE::release;
714  // ==========================================================================
715 public:
716  // ==========================================================================
718  const Services& services() const { return m_services; } // get all services
719  // ==========================================================================
720 private:
721  // ==========================================================================
723  void printErrorHandler( Gaudi::Details::PropertyBase& /* theProp */ ); // "ErrorPrint"
725  void printPropsHandler( Gaudi::Details::PropertyBase& /* theProp */ ); // "PropertiesPrint"
727  void printStatHandler( Gaudi::Details::PropertyBase& /* theProp */ ); // "StatPrint"
728  // ==========================================================================
729 public:
730  // ==========================================================================
732  inline const std::string& context() const { return m_context; }
736  inline const std::string& rootInTES() const { return m_rootInTES; }
737  // ==========================================================================
738 public:
739  // ==========================================================================
741  const std::string fullTESLocation( const std::string& location, const bool useRootInTES ) const;
742  // ==========================================================================
743 private:
744  // ==========================================================================
746  void addToServiceList( SmartIF<IService> svc ) const;
748  void initGaudiCommonConstructor( const IInterface* parent = nullptr );
749  // ==========================================================================
750 private:
752  mutable AlgTools m_managedTools;
754  mutable Services m_services;
755  // ==========================================================================
757  mutable Counter m_errors;
759  mutable Counter m_warnings;
761  mutable Counter m_infos;
763  mutable Counter m_exceptions;
765  mutable Statistics m_counters;
766  // ==========================================================================
768  mutable IUpdateManagerSvc* m_updMgrSvc = nullptr;
769  // ==========================================================================
770  // Properties
771  Gaudi::Property<bool> m_errorsPrint{this, "ErrorsPrint", true, "print the statistics of errors/warnings/exceptions"};
772  Gaudi::Property<bool> m_propsPrint{this, "PropertiesPrint", false, "print the properties of the component"};
773  Gaudi::Property<bool> m_statPrint{this, "StatPrint", true, "print the table of counters"};
774  Gaudi::Property<bool> m_typePrint{this, "TypePrint", true, "add the actual C++ component type into the messages"};
775 
776  Gaudi::Property<std::string> m_context{this, "Context", {}, "note: overridden by parent settings"};
777  Gaudi::Property<std::string> m_rootInTES{this, "RootInTES", {}, "note: overridden by parent settings"};
778 
779  Gaudi::Property<std::string> m_header{this, "StatTableHeader",
780  " | Counter | # | "
781  " sum | mean/eff^* | rms/err^* | min | max |",
782  "the header row for the output Stat-table"};
784  this, "RegularRowFormat", " | %|-48.48s|%|50t||%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |",
785  "the format for regular row in the output Stat-table"};
787  this, "EfficiencyRowFormat",
788  " |*%|-48.48s|%|50t||%|10d| |%|11.5g| |(%|#9.6g| +- %|-#9.6g|)%%| ------- | ------- |",
789  "The format for \"efficiency\" row in the output Stat-table"};
790  Gaudi::Property<bool> m_useEffFormat{this, "UseEfficiencyRowFormat", true,
791  "use the special format for printout of efficiency counters"};
792 
794  this, "CounterList", {".*"}, "RegEx list, of simple integer counters for CounterSummary"};
796  this, "StatEntityList", {}, "RegEx list, of StatEntity counters for CounterSummary"};
797 };
798 // ============================================================================
799 #include "GaudiAlg/GaudiCommonImp.h"
800 // ============================================================================
801 
802 // ============================================================================
803 // The END
804 // ============================================================================
805 #endif // GAUDIALG_GAUDICOMMON_H
806 // ============================================================================
The implementation of inline/templated methods for class GaudiCommon.
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:75
Counter m_warnings
counter of warnings
Definition: GaudiCommon.h:759
constexpr static const auto FAILURE
Definition: StatusCode.h:88
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:74
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:763
Implementation of property with value of concrete type.
Definition: Property.h:381
const std::string & rootInTES() const
Returns the "rootInTES" string.
Definition: GaudiCommon.h:736
GaudiCommon(const std::string &name, ISvcLocator *pSvcLocator)
Algorithm constructor - the SFINAE constraint below ensures that this is constructor is only defined ...
Definition: GaudiCommon.h:642
std::vector< IAlgTool * > AlgTools
storage for active tools
Definition: GaudiCommon.h:136
constexpr const struct GaudiCommon_details::svc_eq_t svc_eq
void registerCondition(TargetClass *condition, StatusCode(CallerClass::*mf)()=NULL)
register the current instance to the UpdateManagerSvc as a consumer for a condition.
Definition: GaudiCommon.h:621
std::map< std::string, unsigned int > Counter
the actual type error/warning counter
Definition: GaudiCommon.h:134
Services m_services
List of active services.
Definition: GaudiCommon.h:754
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:76
Statistics m_counters
General counters.
Definition: GaudiCommon.h:765
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:62
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:732
const Statistics & counters() const
accessor to all counters
Definition: GaudiCommon.h:496
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:69
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:601
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:559
const Services & services() const
get the list of aquired services
Definition: GaudiCommon.h:718
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
bool statPrint() const
Print statistical counters at finalization ?
Definition: GaudiCommon.h:523
StatEntity & counter(const std::string &tag) const
accessor to certain counter by name
Definition: GaudiCommon.h:515
constexpr const struct GaudiCommon_details::svc_lt_t svc_lt
Counter m_errors
Counter of errors.
Definition: GaudiCommon.h:757
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:521
Definition of the basic interface.
Definition: IInterface.h:277
Counter m_infos
counter of infos
Definition: GaudiCommon.h:761
tbb::concurrent_unordered_map< std::string, StatEntity > Statistics
the actual type of general counters
Definition: GaudiCommon.h:132
bool errorsPrint() const
Print error counters at finalization ?
Definition: GaudiCommon.h:525
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:138
SmartIF< ICounterSummarySvc > m_counterSummarySvc
a pointer to the CounterSummarySvc
Definition: GaudiCommon.h:143
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:275
StatusCode runUpdate()
asks the UpdateManagerSvc to perform an update of the instance (if needed) without waiting the next B...
Definition: GaudiCommon.h:636
STL class.
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
#define WARN_UNUSED
Definition: GaudiCommon.h:48
bool typePrint() const
Insert the actual C++ type of the algorithm/tool in the messages ?
Definition: GaudiCommon.h:519
StatusCode update(CallerClass *instance)
Update the given instance.
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:71
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:594
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:101
AlgTools m_managedTools
List of active tools.
Definition: GaudiCommon.h:752
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:70
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:649
The basic counter used for Monitoring purposes.
Definition: StatEntity.h:65
#define GAUDI_API
Definition: Kernel.h:110
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)