The Gaudi Framework  v32r2 (46d42edc)
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 // ============================================================================
18 #include "GaudiAlg/FixTESPath.h"
21 #include "GaudiKernel/HashMap.h"
23 #include "GaudiKernel/IAlgTool.h"
24 #include "GaudiKernel/IAlgorithm.h"
29 #include "GaudiKernel/IToolSvc.h"
32 #include "GaudiKernel/StatEntity.h"
33 #include "GaudiKernel/StatusCode.h"
34 #include "GaudiKernel/System.h"
35 
36 #ifdef __CLING__
37 # define WARN_UNUSED
38 #else
39 # define WARN_UNUSED [[nodiscard]]
40 #endif
41 
42 // ============================================================================
43 // forward declarations
44 // ============================================================================
45 namespace Gaudi {
46  class Algorithm; // GaudiKernel
47 }
48 class AlgTool; // GaudiKernel
49 class ISvcLocator;
50 namespace Gaudi {
51  namespace Utils {
52  template <class TYPE>
53  struct GetData;
54  }
55 } // namespace Gaudi
56 
58  constexpr const struct svc_eq_t {
59  bool operator()( const std::string& n, const SmartIF<IService>& s ) const { return n == s->name(); };
60  bool operator()( const SmartIF<IService>& s, const std::string& n ) const { return s->name() == n; };
61  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() == n->name(); };
62  } svc_eq{};
63  constexpr const struct svc_lt_t {
64  bool operator()( const std::string& n, const SmartIF<IService>& s ) const { return n < s->name(); };
65  bool operator()( const SmartIF<IService>& s, const std::string& n ) const { return s->name() < n; };
66  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() < n->name(); };
67  } svc_lt{};
68 } // namespace GaudiCommon_details
69 // ============================================================================
70 /* @file GaudiCommon.h
71  *
72  * Header file for class : GaudiCommon
73  *
74  * @author Chris Jones Christopher.Rob.Jones@cern.ch
75  * @author Vanya BELYAEV Ivan.Belyaev@itep.ru
76  * @author Rob Lambert Rob.Lambert@cern.ch
77  * @date 2009-08-04
78  */
79 // ============================================================================
89 // ============================================================================
90 template <class PBASE>
91 class GAUDI_API GaudiCommon : public FixTESPath<PBASE> {
92 protected: // definitions
94 
104  static const bool IgnoreRootInTES = false;
116  static const bool UseRootInTES = true;
117  // ==========================================================================
118 protected: // few actual data types
119  // ==========================================================================
129 
130  // ==========================================================================
131  // protected members such that they can be used in the derived classes
134 
135 public:
136  // ==========================================================================
170  template <class TYPE>
172  const bool useRootInTES = true ) const;
176  template <class TYPE>
178  const bool useRootInTES = true ) const;
205  template <class TYPE>
206  bool exist( IDataProviderSvc* svc, const std::string& location, const bool useRootInTES = true ) const;
234  template <class TYPE, class TYPE2>
236  const bool useRootInTES = true ) const;
263  const bool useRootInTES = true ) const;
264  // [[deprecated( "please pass std::unique_ptr as 2nd argument" )]]
266  const bool useRootInTES = true ) const {
267  return put( svc, std::unique_ptr<DataObject>( object ), location, useRootInTES );
268  }
294  template <class TOOL>
295  TOOL* tool( const std::string& type, const std::string& name, const IInterface* parent = 0,
296  bool create = true ) const;
319  template <class TOOL>
320  TOOL* tool( const std::string& type, const IInterface* parent = 0, bool create = true ) const;
343  template <class SERVICE>
344  SmartIF<SERVICE> svc( const std::string& name, const bool create = true ) const;
346  inline IUpdateManagerSvc* updMgrSvc() const;
347 
348 public:
373  StatusCode Error( const std::string& msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
398  StatusCode Warning( const std::string& msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
415  StatusCode Info( const std::string& msg, const StatusCode st = StatusCode::SUCCESS, const size_t mx = 10 ) const;
428  StatusCode Print( const std::string& msg, const StatusCode st = StatusCode::SUCCESS,
429  const MSG::Level lev = MSG::INFO ) const;
438  inline void Assert( const bool ok, const std::string& message = "",
439  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
448  inline void Assert( const bool ok, const char* message,
449  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
458  void Exception( const std::string& msg, const GaudiException& exc,
459  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
469  void Exception( const std::string& msg, const std::exception& exc,
470  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
479  void Exception( const std::string& msg = "no message",
480  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
481 
482 private:
484  inline StatisticsOwn countersOwn() const { return m_countersOwn; }
485 
486 public:
505  //[[deprecated( "see LHCBPS-1758" )]]
506  inline StatEntity& counter( const std::string& tag ) const {
507  return const_cast<GaudiCommon<PBASE>*>( this )->counter( tag );
508  }
509  inline StatEntity& counter( const std::string& tag ) {
510  std::lock_guard<std::mutex> lock( m_countersOwnMutex );
511  // Return referenced StatEntity if it already exists, else create it
512  auto p = this->findCounter( tag );
513  if ( !p ) {
514  auto& counter = m_countersOwn[tag];
515  this->declareCounter( tag, counter );
516  return counter;
517  }
518  return m_countersOwn[tag];
519  }
520  // ==========================================================================
521 public:
523  inline bool typePrint() const { return m_typePrint; }
525  inline bool propsPrint() const { return m_propsPrint; }
527  inline bool statPrint() const { return m_statPrint; }
529  inline bool errorsPrint() const { return m_errorsPrint; }
530  // ==========================================================================
531 public:
536  long printStat( const MSG::Level level = MSG::ALWAYS ) const;
541  long printErrors( const MSG::Level level = MSG::ALWAYS ) const;
546  long printProps( const MSG::Level level = MSG::ALWAYS ) const;
562  template <class CallerClass>
563  inline void registerCondition( const std::string& condition, StatusCode ( CallerClass::*mf )() = nullptr ) {
564  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
565  }
596  template <class CallerClass, class CondType>
597  inline void registerCondition( const std::string& condition, CondType*& condPtrDest,
598  StatusCode ( CallerClass::*mf )() = NULL ) {
599  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf, condPtrDest );
600  }
602  template <class CallerClass>
603  inline void registerCondition( char* condition, StatusCode ( CallerClass::*mf )() = NULL ) {
604  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), std::string( condition ), mf );
605  }
621  template <class CallerClass, class TargetClass>
622  inline void registerCondition( TargetClass* condition, StatusCode ( CallerClass::*mf )() = NULL ) {
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_v<Gaudi::Algorithm, PBASE>, U>>
642  GaudiCommon( const std::string& name, ISvcLocator* pSvcLocator ) : base_class( name, pSvcLocator ) {
643  initGaudiCommonConstructor();
644  }
647  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of_v<AlgTool, PBASE>, U>>
648  GaudiCommon( const std::string& type, const std::string& name, const IInterface* ancestor )
649  : base_class( type, name, ancestor ) {
650  initGaudiCommonConstructor( this->parent() );
651  }
652 
653 public:
657  StatusCode initialize() override;
661  StatusCode finalize() override;
662 
663 private:
664  GaudiCommon() = delete;
665  GaudiCommon( const GaudiCommon& ) = delete;
666  GaudiCommon& operator=( const GaudiCommon& ) = delete;
667 
668 protected:
670  StatusCode releaseTool( const IAlgTool* tool ) const;
672  StatusCode releaseSvc( const IInterface* svc ) const;
673 
674 public:
694  StatusCode release( const IInterface* interface ) const;
695 
697  using PBASE::release;
698  // ==========================================================================
699 public:
700  // ==========================================================================
702  const Services& services() const { return m_services; } // get all services
703  // ==========================================================================
704 public:
705  // ==========================================================================
707  inline const std::string& context() const { return m_context; }
708 
709 private:
710  // ==========================================================================
712  void addToServiceList( SmartIF<IService> svc ) const;
714  void initGaudiCommonConstructor( const IInterface* parent = nullptr );
715  // ==========================================================================
716 private:
721  // ==========================================================================
723  mutable Counter m_errors;
727  mutable Counter m_infos;
734  // ==========================================================================
736  mutable IUpdateManagerSvc* m_updMgrSvc = nullptr;
737  // ==========================================================================
738  // Properties
739  Gaudi::Property<bool> m_errorsPrint{this, "ErrorsPrint", true,
740  [this]( auto& ) {
741  // no action if not yet initialized
742  if ( this->FSMState() >= Gaudi::StateMachine::INITIALIZED &&
743  this->errorsPrint() ) {
744  this->printErrors();
745  }
746  },
747  "print the statistics of errors/warnings/exceptions"};
748  Gaudi::Property<bool> m_propsPrint{this, "PropertiesPrint", false,
749  [this]( auto& ) {
750  // no action if not yet initialized
751  if ( this->FSMState() >= Gaudi::StateMachine::INITIALIZED &&
752  this->propsPrint() ) {
753  this->printProps( MSG::ALWAYS );
754  }
755  },
756  "print the properties of the component"};
757  Gaudi::Property<bool> m_statPrint{this, "StatPrint", true,
758  [this]( auto& ) {
759  // no action if not yet initialized
760  if ( this->FSMState() >= Gaudi::StateMachine::INITIALIZED && this->statPrint() ) {
761  this->printStat( MSG::ALWAYS );
762  }
763  },
764  "print the table of counters"};
765  Gaudi::Property<bool> m_printEmptyCounters{this, "PrintEmptyCounters", false,
766  "force printing of empty counters, otherwise only printed in DEBUG mode"};
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_header{this, "StatTableHeader",
771  " | Counter | # | "
772  " sum | mean/eff^* | rms/err^* | min | max |",
773  "the header row for the output Stat-table"};
775  this, "RegularRowFormat", " | %|-48.48s|%|50t||%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |",
776  "the format for regular row in the output Stat-table"};
778  this, "EfficiencyRowFormat",
779  " |*%|-48.48s|%|50t||%|10d| |%|11.5g| |(%|#9.6g| +- %|-#9.6g|)%%| ------- | ------- |",
780  "The format for \"efficiency\" row in the output Stat-table"};
781  Gaudi::Property<bool> m_useEffFormat{this, "UseEfficiencyRowFormat", true,
782  "use the special format for printout of efficiency counters"};
783 
785  this, "CounterList", {".*"}, "RegEx list, of simple integer counters for CounterSummary"};
787  this, "StatEntityList", {}, "RegEx list, of StatEntity counters for CounterSummary"};
788 };
789 // ============================================================================
790 #include "GaudiAlg/GaudiCommonImp.h"
791 // ============================================================================
792 
793 // ============================================================================
794 // The END
795 // ============================================================================
796 #endif // GAUDIALG_GAUDICOMMON_H
std::map< std::string, StatEntity > StatisticsOwn
the actual type of general counters
Definition: GaudiCommon.h:121
The implementation of inline/templated methods for class GaudiCommon.
Counter m_warnings
counter of warnings
Definition: GaudiCommon.h:725
Out1 * put(const DataObjectHandle< Out1 > &out_handle, Out2 &&out)
Define general base for Gaudi exception.
bool statPrint() const
Print statistical counters at finalization ?
Definition: GaudiCommon.h:527
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:729
bool errorsPrint() const
Print error counters at finalization ?
Definition: GaudiCommon.h:529
Implementation of property with value of concrete type.
Definition: Property.h:352
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
StatEntity & counter(const std::string &tag)
Definition: GaudiCommon.h:509
std::vector< IAlgTool * > AlgTools
storage for active tools
Definition: GaudiCommon.h:126
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:622
std::map< std::string, unsigned int > Counter
the actual type error/warning counter
Definition: GaudiCommon.h:124
Services m_services
List of active services.
Definition: GaudiCommon.h:720
const Services & services() const
get the list of aquired services
Definition: GaudiCommon.h:702
StatisticsOwn m_countersOwn
General counters.
Definition: GaudiCommon.h:731
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:53
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:59
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:603
StatisticsOwn countersOwn() const
accessor to all owned counters
Definition: GaudiCommon.h:484
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
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:563
DataObject * put(IDataProviderSvc *svc, DataObject *object, const std::string &location, const bool useRootInTES=true) const
Definition: GaudiCommon.h:265
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:60
STL class.
constexpr const struct GaudiCommon_details::svc_lt_t svc_lt
Counter m_errors
Counter of errors.
Definition: GaudiCommon.h:723
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
Definition of the basic interface.
Definition: IInterface.h:244
Counter m_infos
counter of infos
Definition: GaudiCommon.h:727
std::vector< SmartIF< IService > > Services
storage for active services
Definition: GaudiCommon.h:128
SmartIF< ICounterSummarySvc > m_counterSummarySvc
a pointer to the CounterSummarySvc
Definition: GaudiCommon.h:133
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:636
std::mutex m_countersOwnMutex
The mutex for m_countersOwn.
Definition: GaudiCommon.h:733
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:707
STL class.
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:65
#define WARN_UNUSED
Definition: GaudiCommon.h:39
StatEntity & counter(const std::string &tag) const
accessor to certain counter by name
Definition: GaudiCommon.h:506
std::map< std::string, std::reference_wrapper< Gaudi::Accumulators::PrintableCounter > > Statistics
Definition: GaudiCommon.h:122
Alias for backward compatibility.
Definition: Algorithm.h:56
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:66
STL class.
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:61
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:64
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:597
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:318
constexpr static const auto FAILURE
Definition: StatusCode.h:86
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:91
bool propsPrint() const
Print properties at initialization ?
Definition: GaudiCommon.h:525
AlgTools m_managedTools
List of active tools.
Definition: GaudiCommon.h:718
StatusCode initialize() override
Definition: FixTESPath.h:39
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:648
#define GAUDI_API
Definition: Kernel.h:71
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
bool typePrint() const
Insert the actual C++ type of the algorithm/tool in the messages ?
Definition: GaudiCommon.h:523