The Gaudi Framework  v30r1 (5d4f4ae2)
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 namespace Gaudi
57 {
58  namespace Utils
59  {
60  template <class TYPE>
61  struct GetData;
62  }
63 }
64 
66 {
67  constexpr const struct svc_eq_t {
68  bool operator()( const std::string& n, const SmartIF<IService>& s ) const { return n == s->name(); };
69  bool operator()( const SmartIF<IService>& s, const std::string& 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_eq{};
72  constexpr const struct svc_lt_t {
73  bool operator()( const std::string& n, const SmartIF<IService>& s ) const { return n < s->name(); };
74  bool operator()( const SmartIF<IService>& s, const std::string& n ) const { return s->name() < n; };
75  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() < n->name(); };
76  } svc_lt{};
77 }
78 // ============================================================================
79 /* @file GaudiCommon.h
80  *
81  * Header file for class : GaudiCommon
82  *
83  * @author Chris Jones Christopher.Rob.Jones@cern.ch
84  * @author Vanya BELYAEV Ivan.Belyaev@itep.ru
85  * @author Rob Lambert Rob.Lambert@cern.ch
86  * @date 2009-08-04
87  */
88 // ============================================================================
98 // ============================================================================
99 template <class PBASE>
100 class GAUDI_API GaudiCommon : public PBASE
101 {
102 protected: // definitions
103  using base_class = PBASE;
104 
114  static const bool IgnoreRootInTES = false;
126  static const bool UseRootInTES = true;
127  // ==========================================================================
128 protected: // few actual data types
129  // ==========================================================================
131  typedef tbb::concurrent_unordered_map<std::string, StatEntity> Statistics;
138 
139  // ==========================================================================
140  // protected members such that they can be used in the derived classes
143 
144 public:
145  // ==========================================================================
179  template <class TYPE>
181  const bool useRootInTES = true ) const;
185  template <class TYPE>
186  typename Gaudi::Utils::GetData<TYPE>::return_type getIfExists( IDataProviderSvc* svc, const std::string& location,
187  const bool useRootInTES = true ) const;
214  template <class TYPE>
215  bool exist( IDataProviderSvc* svc, const std::string& location, const bool useRootInTES = true ) const;
243  template <class TYPE, class TYPE2>
244  typename Gaudi::Utils::GetData<TYPE>::return_type getOrCreate( IDataProviderSvc* svc, const std::string& location,
245  const bool useRootInTES = true ) const;
271  DataObject* put( IDataProviderSvc* svc, DataObject* object, const std::string& location,
272  const bool useRootInTES = true ) const;
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 public:
487  // ==========================================================================
489  inline const Statistics& counters() const { return m_counters; }
508  inline StatEntity& counter( const std::string& tag ) const { return m_counters[tag]; }
509  // ==========================================================================
510 public:
512  inline bool typePrint() const { return m_typePrint; }
514  inline bool propsPrint() const { return m_propsPrint; }
516  inline bool statPrint() const { return m_statPrint; }
518  inline bool errorsPrint() const { return m_errorsPrint; }
519  // ==========================================================================
520 public:
525  long printStat( const MSG::Level level = MSG::ALWAYS ) const;
530  long printErrors( const MSG::Level level = MSG::ALWAYS ) const;
535  long printProps( const MSG::Level level = MSG::ALWAYS ) const;
551  template <class CallerClass>
552  inline void registerCondition( const std::string& condition, StatusCode ( CallerClass::*mf )() = nullptr )
553  {
554  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
555  }
586  template <class CallerClass, class CondType>
587  inline void registerCondition( const std::string& condition, CondType*& condPtrDest,
588  StatusCode ( CallerClass::*mf )() = NULL )
589  {
590  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf, condPtrDest );
591  }
593  template <class CallerClass>
594  inline void registerCondition( char* condition, StatusCode ( CallerClass::*mf )() = NULL )
595  {
596  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), std::string( condition ), mf );
597  }
613  template <class CallerClass, class TargetClass>
614  inline void registerCondition( TargetClass* condition, StatusCode ( CallerClass::*mf )() = NULL )
615  {
616  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
617  }
629  inline StatusCode runUpdate() { return updMgrSvc()->update( this ); }
630 
631 public:
634  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of<Algorithm, PBASE>::value, U>>
635  GaudiCommon( const std::string& name, ISvcLocator* pSvcLocator ) : base_class( name, pSvcLocator )
636  {
637  initGaudiCommonConstructor();
638  }
641  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of<AlgTool, PBASE>::value, U>>
642  GaudiCommon( const std::string& type, const std::string& name, const IInterface* ancestor )
643  : base_class( type, name, ancestor )
644  {
645  initGaudiCommonConstructor( this->parent() );
646  }
647 
648 public:
652  StatusCode initialize() override
653 #ifdef __ICC
654  {
655  return i_gcInitialize();
656  }
657  StatusCode i_gcInitialize()
658 #endif
659  ;
663  StatusCode finalize() override
664 #ifdef __ICC
665  {
666  return i_gcFinalize();
667  }
668  StatusCode i_gcFinalize()
669 #endif
670  ;
671 
672 private:
673  GaudiCommon() = delete;
674  GaudiCommon( const GaudiCommon& ) = delete;
675  GaudiCommon& operator=( const GaudiCommon& ) = delete;
676 
677 protected:
679  StatusCode releaseTool( const IAlgTool* tool ) const;
681  StatusCode releaseSvc( const IInterface* svc ) const;
682 
683 public:
703  StatusCode release( const IInterface* interface ) const;
704 
706  using PBASE::release;
707  // ==========================================================================
708 public:
709  // ==========================================================================
711  const Services& services() const { return m_services; } // get all services
712  // ==========================================================================
713 private:
714  // ==========================================================================
716  void printErrorHandler( Gaudi::Details::PropertyBase& /* theProp */ ); // "ErrorPrint"
718  void printPropsHandler( Gaudi::Details::PropertyBase& /* theProp */ ); // "PropertiesPrint"
720  void printStatHandler( Gaudi::Details::PropertyBase& /* theProp */ ); // "StatPrint"
721  // ==========================================================================
722 public:
723  // ==========================================================================
725  inline const std::string& context() const { return m_context; }
729  inline const std::string& rootInTES() const { return m_rootInTES; }
730  // ==========================================================================
731 public:
732  // ==========================================================================
734  const std::string fullTESLocation( const std::string& location, const bool useRootInTES ) const;
735  // ==========================================================================
736 private:
737  // ==========================================================================
739  void addToServiceList( SmartIF<IService> svc ) const;
741  void initGaudiCommonConstructor( const IInterface* parent = nullptr );
742  // ==========================================================================
743 private:
745  mutable AlgTools m_managedTools;
747  mutable Services m_services;
748  // ==========================================================================
750  mutable Counter m_errors;
752  mutable Counter m_warnings;
754  mutable Counter m_infos;
756  mutable Counter m_exceptions;
758  mutable Statistics m_counters;
759  // ==========================================================================
761  mutable IUpdateManagerSvc* m_updMgrSvc = nullptr;
762  // ==========================================================================
763  // Properties
764  Gaudi::Property<bool> m_errorsPrint{this, "ErrorsPrint", true, "print the statistics of errors/warnings/exceptions"};
765  Gaudi::Property<bool> m_propsPrint{this, "PropertiesPrint", false, "print the properties of the component"};
766  Gaudi::Property<bool> m_statPrint{this, "StatPrint", true, "print the table of counters"};
767  Gaudi::Property<bool> m_typePrint{this, "TypePrint", true, "add the actual C++ component type into the messages"};
768 
769  Gaudi::Property<std::string> m_context{this, "Context", {}, "note: overridden by parent settings"};
770  Gaudi::Property<std::string> m_rootInTES{this, "RootInTES", {}, "note: overridden by parent settings"};
771 
772  Gaudi::Property<std::string> m_header{this, "StatTableHeader",
773  " | Counter | # | "
774  " sum | mean/eff^* | rms/err^* | min | max |",
775  "the header row for the output Stat-table"};
777  this, "RegularRowFormat", " | %|-48.48s|%|50t||%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |",
778  "the format for regular row in the output Stat-table"};
780  this, "EfficiencyRowFormat",
781  " |*%|-48.48s|%|50t||%|10d| |%|11.5g| |(%|#9.6g| +- %|-#9.6g|)%%| ------- | ------- |",
782  "The format for \"efficiency\" row in the output Stat-table"};
783  Gaudi::Property<bool> m_useEffFormat{this, "UseEfficiencyRowFormat", true,
784  "use the special format for printout of efficiency counters"};
785 
787  this, "CounterList", {".*"}, "RegEx list, of simple integer counters for CounterSummary"};
789  this, "StatEntityList", {}, "RegEx list, of StatEntity counters for CounterSummary"};
790 };
791 // ============================================================================
792 #include "GaudiAlg/GaudiCommonImp.h"
793 // ============================================================================
794 
795 // ============================================================================
796 // The END
797 // ============================================================================
798 #endif // GAUDIALG_GAUDICOMMON_H
799 // ============================================================================
The implementation of inline/templated methods for class GaudiCommon.
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:74
Counter m_warnings
counter of warnings
Definition: GaudiCommon.h:752
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:73
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:756
Implementation of property with value of concrete type.
Definition: Property.h:319
const std::string & rootInTES() const
Returns the "rootInTES" string.
Definition: GaudiCommon.h:729
GaudiCommon(const std::string &name, ISvcLocator *pSvcLocator)
Algorithm constructor - the SFINAE constraint below ensures that this is constructor is only defined ...
Definition: GaudiCommon.h:635
std::vector< IAlgTool * > AlgTools
storage for active tools
Definition: GaudiCommon.h:135
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:614
std::map< std::string, unsigned int > Counter
the actual type error/warning counter
Definition: GaudiCommon.h:133
Services m_services
List of active services.
Definition: GaudiCommon.h:747
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:75
Statistics m_counters
General counters.
Definition: GaudiCommon.h:758
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:61
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:725
const Statistics & counters() const
accessor to all counters
Definition: GaudiCommon.h:489
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:68
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:594
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:552
const Services & services() const
get the list of aquired services
Definition: GaudiCommon.h:711
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
bool statPrint() const
Print statistical counters at finalization ?
Definition: GaudiCommon.h:516
StatEntity & counter(const std::string &tag) const
accessor to certain counter by name
Definition: GaudiCommon.h:508
constexpr const struct GaudiCommon_details::svc_lt_t svc_lt
Counter m_errors
Counter of errors.
Definition: GaudiCommon.h:750
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
bool propsPrint() const
Print properties at initialization ?
Definition: GaudiCommon.h:514
Definition of the basic interface.
Definition: IInterface.h:277
Counter m_infos
counter of infos
Definition: GaudiCommon.h:754
tbb::concurrent_unordered_map< std::string, StatEntity > Statistics
the actual type of general counters
Definition: GaudiCommon.h:131
bool errorsPrint() const
Print error counters at finalization ?
Definition: GaudiCommon.h:518
std::vector< SmartIF< IService > > Services
storage for active services
Definition: GaudiCommon.h:137
SmartIF< ICounterSummarySvc > m_counterSummarySvc
a pointer to the CounterSummarySvc
Definition: GaudiCommon.h:142
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
Interface class to the Update Manager service.
StatusCode runUpdate()
asks the UpdateManagerSvc to perform an update of the instance (if needed) without waiting the next B...
Definition: GaudiCommon.h:629
STL class.
#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:512
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:70
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:78
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:587
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:100
AlgTools m_managedTools
List of active tools.
Definition: GaudiCommon.h:745
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:69
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:642
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)