The Gaudi Framework  v30r0 (c919700c)
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 #ifdef __CLING__
33 #define WARN_UNUSED
34 #elif __cplusplus > 201402L
35 #define WARN_UNUSED [[nodiscard]]
36 #else
37 #define WARN_UNUSED [[gnu::warn_unused_result]]
38 #endif
39 
40 // ============================================================================
41 // forward declarations
42 // ============================================================================
43 class Algorithm; // GaudiKernel
44 class AlgTool; // GaudiKernel
45 namespace Gaudi
46 {
47  namespace Utils
48  {
49  template <class TYPE>
50  struct GetData;
51  }
52 }
53 
55 {
56  constexpr const struct svc_eq_t {
57  bool operator()( const std::string& n, const SmartIF<IService>& s ) const { return n == s->name(); };
58  bool operator()( const SmartIF<IService>& s, const std::string& n ) const { return s->name() == n; };
59  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() == n->name(); };
60  } svc_eq{};
61  constexpr const struct svc_lt_t {
62  bool operator()( const std::string& n, const SmartIF<IService>& s ) const { return n < s->name(); };
63  bool operator()( const SmartIF<IService>& s, const std::string& n ) const { return s->name() < n; };
64  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() < n->name(); };
65  } svc_lt{};
66 }
67 // ============================================================================
68 /* @file GaudiCommon.h
69  *
70  * Header file for class : GaudiCommon
71  *
72  * @author Chris Jones Christopher.Rob.Jones@cern.ch
73  * @author Vanya BELYAEV Ivan.Belyaev@itep.ru
74  * @author Rob Lambert Rob.Lambert@cern.ch
75  * @date 2009-08-04
76  */
77 // ============================================================================
87 // ============================================================================
88 template <class PBASE>
89 class GAUDI_API GaudiCommon : public PBASE
90 {
91 protected: // definitions
92  using base_class = PBASE;
93 
103  static const bool IgnoreRootInTES = false;
115  static const bool UseRootInTES = true;
116  // ==========================================================================
117 protected: // few actual data types
118  // ==========================================================================
127 
128  // ==========================================================================
129  // protected members such that they can be used in the derived classes
132 
133 public:
134  // ==========================================================================
168  template <class TYPE>
170  const bool useRootInTES = true ) const;
174  template <class TYPE>
175  typename Gaudi::Utils::GetData<TYPE>::return_type getIfExists( IDataProviderSvc* svc, const std::string& location,
176  const bool useRootInTES = true ) const;
203  template <class TYPE>
204  bool exist( IDataProviderSvc* svc, const std::string& location, const bool useRootInTES = true ) const;
232  template <class TYPE, class TYPE2>
233  typename Gaudi::Utils::GetData<TYPE>::return_type getOrCreate( IDataProviderSvc* svc, const std::string& location,
234  const bool useRootInTES = true ) const;
260  DataObject* put( IDataProviderSvc* svc, DataObject* object, const std::string& location,
261  const bool useRootInTES = true ) const;
287  template <class TOOL>
288  TOOL* tool( const std::string& type, const std::string& name, const IInterface* parent = 0,
289  bool create = true ) const;
312  template <class TOOL>
313  TOOL* tool( const std::string& type, const IInterface* parent = 0, bool create = true ) const;
336  template <class SERVICE>
337  SmartIF<SERVICE> svc( const std::string& name, const bool create = true ) const;
339  inline IUpdateManagerSvc* updMgrSvc() const;
340 
341 public:
366  StatusCode Error( const std::string& msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
391  StatusCode Warning( const std::string& msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
408  StatusCode Info( const std::string& msg, const StatusCode st = StatusCode::SUCCESS, const size_t mx = 10 ) const;
421  StatusCode Print( const std::string& msg, const StatusCode st = StatusCode::SUCCESS,
422  const MSG::Level lev = MSG::INFO ) const;
431  inline void Assert( const bool ok, const std::string& message = "",
432  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
441  inline void Assert( const bool ok, const char* message,
442  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
451  void Exception( const std::string& msg, const GaudiException& exc,
452  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
462  void Exception( const std::string& msg, const std::exception& exc,
463  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
472  void Exception( const std::string& msg = "no message",
473  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
474 
475 public:
476  // ==========================================================================
478  inline const Statistics& counters() const { return m_counters; }
497  inline StatEntity& counter( const std::string& tag ) const { return m_counters[tag]; }
498  // ==========================================================================
499 public:
501  inline bool typePrint() const { return m_typePrint; }
503  inline bool propsPrint() const { return m_propsPrint; }
505  inline bool statPrint() const { return m_statPrint; }
507  inline bool errorsPrint() const { return m_errorsPrint; }
508  // ==========================================================================
509 public:
514  long printStat( const MSG::Level level = MSG::ALWAYS ) const;
519  long printErrors( const MSG::Level level = MSG::ALWAYS ) const;
524  long printProps( const MSG::Level level = MSG::ALWAYS ) const;
540  template <class CallerClass>
541  inline void registerCondition( const std::string& condition, StatusCode ( CallerClass::*mf )() = nullptr )
542  {
543  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
544  }
575  template <class CallerClass, class CondType>
576  inline void registerCondition( const std::string& condition, CondType*& condPtrDest,
577  StatusCode ( CallerClass::*mf )() = NULL )
578  {
579  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf, condPtrDest );
580  }
582  template <class CallerClass>
583  inline void registerCondition( char* condition, StatusCode ( CallerClass::*mf )() = NULL )
584  {
585  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), std::string( condition ), mf );
586  }
602  template <class CallerClass, class TargetClass>
603  inline void registerCondition( TargetClass* condition, StatusCode ( CallerClass::*mf )() = NULL )
604  {
605  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
606  }
618  inline StatusCode runUpdate() { return updMgrSvc()->update( this ); }
619 
620 public:
623  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of<Algorithm, PBASE>::value, U>>
624  GaudiCommon( const std::string& name, ISvcLocator* pSvcLocator ) : base_class( name, pSvcLocator )
625  {
626  initGaudiCommonConstructor();
627  }
630  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of<AlgTool, PBASE>::value, U>>
631  GaudiCommon( const std::string& type, const std::string& name, const IInterface* ancestor )
632  : base_class( type, name, ancestor )
633  {
634  initGaudiCommonConstructor( this->parent() );
635  }
636 
637 public:
641  StatusCode initialize() override
642 #ifdef __ICC
643  {
644  return i_gcInitialize();
645  }
646  StatusCode i_gcInitialize()
647 #endif
648  ;
652  StatusCode finalize() override
653 #ifdef __ICC
654  {
655  return i_gcFinalize();
656  }
657  StatusCode i_gcFinalize()
658 #endif
659  ;
660 protected:
662  ~GaudiCommon() override = default;
663 
664 private:
665  GaudiCommon() = delete;
666  GaudiCommon( const GaudiCommon& ) = delete;
667  GaudiCommon& operator=( const GaudiCommon& ) = delete;
668 
669 protected:
671  StatusCode releaseTool( const IAlgTool* tool ) const;
673  StatusCode releaseSvc( const IInterface* svc ) const;
674 
675 public:
695  StatusCode release( const IInterface* interface ) const;
696 
698  using PBASE::release;
699  // ==========================================================================
700 public:
701  // ==========================================================================
703  const Services& services() const { return m_services; } // get all services
704  // ==========================================================================
705 private:
706  // ==========================================================================
708  void printErrorHandler( Gaudi::Details::PropertyBase& /* theProp */ ); // "ErrorPrint"
710  void printPropsHandler( Gaudi::Details::PropertyBase& /* theProp */ ); // "PropertiesPrint"
712  void printStatHandler( Gaudi::Details::PropertyBase& /* theProp */ ); // "StatPrint"
713  // ==========================================================================
714 public:
715  // ==========================================================================
717  inline const std::string& context() const { return m_context; }
721  inline const std::string& rootInTES() const { return m_rootInTES; }
722  // ==========================================================================
723 public:
724  // ==========================================================================
726  const std::string fullTESLocation( const std::string& location, const bool useRootInTES ) const;
727  // ==========================================================================
728 private:
729  // ==========================================================================
731  void addToServiceList( SmartIF<IService> svc ) const;
733  void initGaudiCommonConstructor( const IInterface* parent = nullptr );
734  // ==========================================================================
735 private:
737  mutable AlgTools m_managedTools;
739  mutable Services m_services;
740  // ==========================================================================
742  mutable Counter m_errors;
744  mutable Counter m_warnings;
746  mutable Counter m_infos;
748  mutable Counter m_exceptions;
750  mutable Statistics m_counters;
751  // ==========================================================================
753  mutable IUpdateManagerSvc* m_updMgrSvc = nullptr;
754  // ==========================================================================
755  // Properties
756  Gaudi::Property<bool> m_errorsPrint{this, "ErrorsPrint", true, "print the statistics of errors/warnings/exceptions"};
757  Gaudi::Property<bool> m_propsPrint{this, "PropertiesPrint", false, "print the properties of the component"};
758  Gaudi::Property<bool> m_statPrint{this, "StatPrint", true, "print the table of counters"};
759  Gaudi::Property<bool> m_typePrint{this, "TypePrint", true, "add the actual C++ component type into the messages"};
760 
761  Gaudi::Property<std::string> m_context{this, "Context", {}, "note: overridden by parent settings"};
762  Gaudi::Property<std::string> m_rootInTES{this, "RootInTES", {}, "note: overridden by parent settings"};
763 
764  Gaudi::Property<std::string> m_header{this, "StatTableHeader",
765  " | Counter | # | "
766  " sum | mean/eff^* | rms/err^* | min | max |",
767  "the header row for the output Stat-table"};
769  this, "RegularRowFormat", " | %|-48.48s|%|50t||%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |",
770  "the format for regular row in the output Stat-table"};
772  this, "EfficiencyRowFormat",
773  " |*%|-48.48s|%|50t||%|10d| |%|11.5g| |(%|#9.6g| +- %|-#9.6g|)%%| ------- | ------- |",
774  "The format for \"efficiency\" row in the output Stat-table"};
775  Gaudi::Property<bool> m_useEffFormat{this, "UseEfficiencyRowFormat", true,
776  "use the special format for printout of efficiency counters"};
777 
779  this, "CounterList", {".*"}, "RegEx list, of simple integer counters for CounterSummary"};
781  this, "StatEntityList", {}, "RegEx list, of StatEntity counters for CounterSummary"};
782 };
783 // ============================================================================
784 #include "GaudiAlg/GaudiCommonImp.h"
785 // ============================================================================
786 
787 // ============================================================================
788 // The END
789 // ============================================================================
790 #endif // GAUDIALG_GAUDICOMMON_H
791 // ============================================================================
The implementation of inline/templated methods for class GaudiCommon.
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:63
std::map< std::string, StatEntity > Statistics
the actual type of general counters
Definition: GaudiCommon.h:120
Counter m_warnings
counter of warnings
Definition: GaudiCommon.h:744
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:62
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:748
Implementation of property with value of concrete type.
Definition: Property.h:319
const std::string & rootInTES() const
Returns the "rootInTES" string.
Definition: GaudiCommon.h:721
GaudiCommon(const std::string &name, ISvcLocator *pSvcLocator)
Algorithm constructor - the SFINAE constraint below ensures that this is constructor is only defined ...
Definition: GaudiCommon.h:624
std::vector< IAlgTool * > AlgTools
storage for active tools
Definition: GaudiCommon.h:124
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:603
std::map< std::string, unsigned int > Counter
the actual type error/warning counter
Definition: GaudiCommon.h:122
Services m_services
List of active services.
Definition: GaudiCommon.h:739
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:64
Statistics m_counters
General counters.
Definition: GaudiCommon.h:750
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:50
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:717
const Statistics & counters() const
accessor to all counters
Definition: GaudiCommon.h:478
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:57
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:583
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:541
const Services & services() const
get the list of aquired services
Definition: GaudiCommon.h:703
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
bool statPrint() const
Print statistical counters at finalization ?
Definition: GaudiCommon.h:505
StatEntity & counter(const std::string &tag) const
accessor to certain counter by name
Definition: GaudiCommon.h:497
constexpr const struct GaudiCommon_details::svc_lt_t svc_lt
Counter m_errors
Counter of errors.
Definition: GaudiCommon.h:742
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:503
Definition of the basic interface.
Definition: IInterface.h:277
Counter m_infos
counter of infos
Definition: GaudiCommon.h:746
bool errorsPrint() const
Print error counters at finalization ?
Definition: GaudiCommon.h:507
std::vector< SmartIF< IService > > Services
storage for active services
Definition: GaudiCommon.h:126
SmartIF< ICounterSummarySvc > m_counterSummarySvc
a pointer to the CounterSummarySvc
Definition: GaudiCommon.h:131
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:618
STL class.
#define WARN_UNUSED
Definition: GaudiCommon.h:37
bool typePrint() const
Insert the actual C++ type of the algorithm/tool in the messages ?
Definition: GaudiCommon.h:501
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:59
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
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:576
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:48
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:89
AlgTools m_managedTools
List of active tools.
Definition: GaudiCommon.h:737
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:58
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:631
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)