The Gaudi Framework  v33r0 (d5ea422b)
GaudiCommon.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIALG_GAUDICOMMON_H
12 #define GAUDIALG_GAUDICOMMON_H 1
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 // from STL
17 // ============================================================================
18 #include <algorithm>
19 #include <functional>
20 #include <list>
21 #include <map>
22 #include <mutex>
23 #include <string>
24 #include <vector>
25 // ============================================================================
26 // GaudiKernel
27 // ============================================================================
28 #include "GaudiAlg/FixTESPath.h"
31 #include "GaudiKernel/HashMap.h"
33 #include "GaudiKernel/IAlgTool.h"
34 #include "GaudiKernel/IAlgorithm.h"
39 #include "GaudiKernel/IToolSvc.h"
42 #include "GaudiKernel/StatEntity.h"
43 #include "GaudiKernel/StatusCode.h"
44 #include "GaudiKernel/System.h"
45 
46 #ifdef __CLING__
47 # define WARN_UNUSED
48 #else
49 # define WARN_UNUSED [[nodiscard]]
50 #endif
51 
52 // ============================================================================
53 // forward declarations
54 // ============================================================================
55 namespace Gaudi {
56  class Algorithm; // GaudiKernel
57 }
58 class AlgTool; // GaudiKernel
59 class ISvcLocator;
60 namespace Gaudi {
61  namespace Utils {
62  template <class TYPE>
63  struct GetData;
64  }
65 } // namespace Gaudi
66 
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 } // namespace GaudiCommon_details
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 FixTESPath<PBASE> {
102 protected: // definitions
104 
114  static const bool IgnoreRootInTES = false;
126  static const bool UseRootInTES = true;
127  // ==========================================================================
128 protected: // few actual data types
129  // ==========================================================================
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>
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>
246  const bool useRootInTES = true ) const;
273  const bool useRootInTES = true ) const;
274  // [[deprecated( "please pass std::unique_ptr as 2nd argument" )]]
276  const bool useRootInTES = true ) const {
277  return put( svc, std::unique_ptr<DataObject>( object ), location, useRootInTES );
278  }
304  template <class TOOL>
305  TOOL* tool( const std::string& type, const std::string& name, const IInterface* parent = 0,
306  bool create = true ) const;
329  template <class TOOL>
330  TOOL* tool( const std::string& type, const IInterface* parent = 0, bool create = true ) const;
353  template <class SERVICE>
354  SmartIF<SERVICE> svc( const std::string& name, const bool create = true ) const;
356  inline IUpdateManagerSvc* updMgrSvc() const;
357 
358 public:
383  StatusCode Error( const std::string& msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
408  StatusCode Warning( const std::string& msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
425  StatusCode Info( const std::string& msg, const StatusCode st = StatusCode::SUCCESS, const size_t mx = 10 ) const;
438  StatusCode Print( const std::string& msg, const StatusCode st = StatusCode::SUCCESS,
439  const MSG::Level lev = MSG::INFO ) const;
448  inline void Assert( const bool ok, const std::string& message = "",
449  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
458  inline void Assert( const bool ok, const char* message,
459  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
468  void Exception( const std::string& msg, const GaudiException& exc,
469  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
479  void Exception( const std::string& msg, const std::exception& exc,
480  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
489  void Exception( const std::string& msg = "no message",
490  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
491 
492 private:
494  inline StatisticsOwn countersOwn() const { return m_countersOwn; }
495 
496 public:
515  //[[deprecated( "see LHCBPS-1758" )]]
516  inline StatEntity& counter( const std::string& tag ) const {
517  return const_cast<GaudiCommon<PBASE>*>( this )->counter( tag );
518  }
519  inline StatEntity& counter( const std::string& tag ) {
520  std::lock_guard<std::mutex> lock( m_countersOwnMutex );
521  // Return referenced StatEntity if it already exists, else create it
522  auto p = this->findCounter( tag );
523  if ( !p ) {
524  auto& counter = m_countersOwn[tag];
525  this->declareCounter( tag, counter );
526  return counter;
527  }
528  return m_countersOwn[tag];
529  }
530  // ==========================================================================
531 public:
533  inline bool typePrint() const { return m_typePrint; }
535  inline bool propsPrint() const { return m_propsPrint; }
537  inline bool statPrint() const { return m_statPrint; }
539  inline bool errorsPrint() const { return m_errorsPrint; }
540  // ==========================================================================
541 public:
546  long printStat( const MSG::Level level = MSG::ALWAYS ) const;
551  long printErrors( const MSG::Level level = MSG::ALWAYS ) const;
556  long printProps( const MSG::Level level = MSG::ALWAYS ) const;
572  template <class CallerClass>
573  inline void registerCondition( const std::string& condition, StatusCode ( CallerClass::*mf )() = nullptr ) {
574  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
575  }
606  template <class CallerClass, class CondType>
607  inline void registerCondition( const std::string& condition, CondType*& condPtrDest,
608  StatusCode ( CallerClass::*mf )() = NULL ) {
609  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf, condPtrDest );
610  }
612  template <class CallerClass>
613  inline void registerCondition( char* condition, StatusCode ( CallerClass::*mf )() = NULL ) {
614  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), std::string( condition ), mf );
615  }
631  template <class CallerClass, class TargetClass>
632  inline void registerCondition( TargetClass* condition, StatusCode ( CallerClass::*mf )() = NULL ) {
633  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
634  }
646  inline StatusCode runUpdate() { return updMgrSvc()->update( this ); }
647 
648 public:
651  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of_v<Gaudi::Algorithm, PBASE>, U>>
652  GaudiCommon( const std::string& name, ISvcLocator* pSvcLocator ) : base_class( name, pSvcLocator ) {
653  initGaudiCommonConstructor();
654  }
657  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of_v<AlgTool, PBASE>, U>>
658  GaudiCommon( const std::string& type, const std::string& name, const IInterface* ancestor )
659  : base_class( type, name, ancestor ) {
660  initGaudiCommonConstructor( this->parent() );
661  }
662 
663 public:
667  StatusCode initialize() override;
671  StatusCode finalize() override;
672 
673 private:
674  GaudiCommon() = delete;
675  GaudiCommon( const GaudiCommon& ) = delete;
676  GaudiCommon& operator=( const GaudiCommon& ) = delete;
677 
678 protected:
680  StatusCode releaseTool( const IAlgTool* tool ) const;
682  StatusCode releaseSvc( const IInterface* svc ) const;
683 
684 public:
704  StatusCode release( const IInterface* interface ) const;
705 
707  using PBASE::release;
708  // ==========================================================================
709 public:
710  // ==========================================================================
712  const Services& services() const { return m_services; } // get all services
713  // ==========================================================================
714 public:
715  // ==========================================================================
717  inline const std::string& context() const { return m_context; }
718 
719 private:
720  // ==========================================================================
722  void addToServiceList( SmartIF<IService> svc ) const;
724  void initGaudiCommonConstructor( const IInterface* parent = nullptr );
725  // ==========================================================================
726 private:
731  // ==========================================================================
733  mutable Counter m_errors;
737  mutable Counter m_infos;
744  // ==========================================================================
746  mutable IUpdateManagerSvc* m_updMgrSvc = nullptr;
747  // ==========================================================================
748  // Properties
749  Gaudi::Property<bool> m_errorsPrint{this, "ErrorsPrint", true,
750  [this]( auto& ) {
751  // no action if not yet initialized
752  if ( this->FSMState() >= Gaudi::StateMachine::INITIALIZED &&
753  this->errorsPrint() ) {
754  this->printErrors();
755  }
756  },
757  "print the statistics of errors/warnings/exceptions"};
758  Gaudi::Property<bool> m_propsPrint{this, "PropertiesPrint", false,
759  [this]( auto& ) {
760  // no action if not yet initialized
761  if ( this->FSMState() >= Gaudi::StateMachine::INITIALIZED &&
762  this->propsPrint() ) {
763  this->printProps( MSG::ALWAYS );
764  }
765  },
766  "print the properties of the component"};
767  Gaudi::Property<bool> m_statPrint{this, "StatPrint", true,
768  [this]( auto& ) {
769  // no action if not yet initialized
770  if ( this->FSMState() >= Gaudi::StateMachine::INITIALIZED && this->statPrint() ) {
771  this->printStat( MSG::ALWAYS );
772  }
773  },
774  "print the table of counters"};
775  Gaudi::Property<bool> m_printEmptyCounters{this, "PrintEmptyCounters", false,
776  "force printing of empty counters, otherwise only printed in DEBUG mode"};
777  Gaudi::Property<bool> m_typePrint{this, "TypePrint", true, "add the actual C++ component type into the messages"};
778 
779  Gaudi::Property<std::string> m_context{this, "Context", {}, "note: overridden by parent settings"};
780  Gaudi::Property<std::string> m_header{this, "StatTableHeader",
781  " | Counter | # | "
782  " sum | mean/eff^* | rms/err^* | min | max |",
783  "the header row for the output Stat-table"};
785  this, "RegularRowFormat", " | %|-48.48s|%|50t||%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |",
786  "the format for regular row in the output Stat-table"};
788  this, "EfficiencyRowFormat",
789  " |*%|-48.48s|%|50t||%|10d| |%|11.5g| |(%|#9.6g| +- %|-#9.6g|)%%| ------- | ------- |",
790  "The format for \"efficiency\" row in the output Stat-table"};
791  Gaudi::Property<bool> m_useEffFormat{this, "UseEfficiencyRowFormat", true,
792  "use the special format for printout of efficiency counters"};
793 
795  this, "CounterList", {".*"}, "RegEx list, of simple integer counters for CounterSummary"};
797  this, "StatEntityList", {}, "RegEx list, of StatEntity counters for CounterSummary"};
798 };
799 // ============================================================================
800 #include "GaudiAlg/GaudiCommonImp.h"
801 // ============================================================================
802 
803 // ============================================================================
804 // The END
805 // ============================================================================
806 #endif // GAUDIALG_GAUDICOMMON_H
std::map< std::string, StatEntity > StatisticsOwn
the actual type of general counters
Definition: GaudiCommon.h:131
The implementation of inline/templated methods for class GaudiCommon.
Counter m_warnings
counter of warnings
Definition: GaudiCommon.h:735
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:537
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:35
Counter m_exceptions
Counter of exceptions.
Definition: GaudiCommon.h:739
bool errorsPrint() const
Print error counters at finalization ?
Definition: GaudiCommon.h:539
Implementation of property with value of concrete type.
Definition: Property.h:370
GaudiCommon(const std::string &name, ISvcLocator *pSvcLocator)
Algorithm constructor - the SFINAE constraint below ensures that this is constructor is only defined ...
Definition: GaudiCommon.h:652
StatEntity & counter(const std::string &tag)
Definition: GaudiCommon.h:519
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:632
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:730
const Services & services() const
get the list of aquired services
Definition: GaudiCommon.h:712
StatisticsOwn m_countersOwn
General counters.
Definition: GaudiCommon.h:741
constexpr static const auto SUCCESS
Definition: StatusCode.h:96
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:63
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:613
StatisticsOwn countersOwn() const
accessor to all owned counters
Definition: GaudiCommon.h:494
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:573
DataObject * put(IDataProviderSvc *svc, DataObject *object, const std::string &location, const bool useRootInTES=true) const
Definition: GaudiCommon.h:275
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:70
STL class.
constexpr const struct GaudiCommon_details::svc_lt_t svc_lt
Counter m_errors
Counter of errors.
Definition: GaudiCommon.h:733
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
Definition of the basic interface.
Definition: IInterface.h:254
Counter m_infos
counter of infos
Definition: GaudiCommon.h:737
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
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:646
std::mutex m_countersOwnMutex
The mutex for m_countersOwn.
Definition: GaudiCommon.h:743
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:717
STL class.
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:75
#define WARN_UNUSED
Definition: GaudiCommon.h:49
StatEntity & counter(const std::string &tag) const
accessor to certain counter by name
Definition: GaudiCommon.h:516
std::map< std::string, std::reference_wrapper< Gaudi::Accumulators::PrintableCounter > > Statistics
Definition: GaudiCommon.h:132
Alias for backward compatibility.
Definition: Algorithm.h:66
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:76
STL class.
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:71
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:74
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:607
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:57
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:33
string s
Definition: gaudirun.py:328
constexpr static const auto FAILURE
Definition: StatusCode.h:97
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:101
bool propsPrint() const
Print properties at initialization ?
Definition: GaudiCommon.h:535
AlgTools m_managedTools
List of active tools.
Definition: GaudiCommon.h:728
StatusCode initialize() override
Definition: FixTESPath.h:49
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:658
backward compatible StatEntity class.
Definition: StatEntity.h:18
#define GAUDI_API
Definition: Kernel.h:81
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:40
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:533