Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 // ============================================================================
20 #include "GaudiKernel/HashMap.h"
22 #include "GaudiKernel/IAlgTool.h"
23 #include "GaudiKernel/IAlgorithm.h"
28 #include "GaudiKernel/IToolSvc.h"
31 #include "GaudiKernel/StatEntity.h"
32 #include "GaudiKernel/StatusCode.h"
33 #include "GaudiKernel/System.h"
34 
35 #ifdef __CLING__
36 # define WARN_UNUSED
37 #elif __cplusplus > 201402L
38 # define WARN_UNUSED [[nodiscard]]
39 #else
40 # define WARN_UNUSED [[gnu::warn_unused_result]]
41 #endif
42 
43 // ============================================================================
44 // forward declarations
45 // ============================================================================
46 namespace Gaudi {
47  class Algorithm; // GaudiKernel
48 }
49 class AlgTool; // GaudiKernel
50 class ISvcLocator;
51 namespace Gaudi {
52  namespace Utils {
53  template <class TYPE>
54  struct GetData;
55  }
56 } // namespace Gaudi
57 
59  constexpr const struct svc_eq_t {
60  bool operator()( const std::string& n, const SmartIF<IService>& s ) const { return n == s->name(); };
61  bool operator()( const SmartIF<IService>& s, const std::string& n ) const { return s->name() == n; };
62  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() == n->name(); };
63  } svc_eq{};
64  constexpr const struct svc_lt_t {
65  bool operator()( const std::string& n, const SmartIF<IService>& s ) const { return n < s->name(); };
66  bool operator()( const SmartIF<IService>& s, const std::string& n ) const { return s->name() < n; };
67  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() < n->name(); };
68  } svc_lt{};
69 } // namespace GaudiCommon_details
70 // ============================================================================
71 /* @file GaudiCommon.h
72  *
73  * Header file for class : GaudiCommon
74  *
75  * @author Chris Jones Christopher.Rob.Jones@cern.ch
76  * @author Vanya BELYAEV Ivan.Belyaev@itep.ru
77  * @author Rob Lambert Rob.Lambert@cern.ch
78  * @date 2009-08-04
79  */
80 // ============================================================================
90 // ============================================================================
91 template <class PBASE>
92 class GAUDI_API GaudiCommon : public PBASE {
93 protected: // definitions
94  using base_class = PBASE;
95 
105  static const bool IgnoreRootInTES = false;
117  static const bool UseRootInTES = true;
118  // ==========================================================================
119 protected: // few actual data types
120  // ==========================================================================
130 
131  // ==========================================================================
132  // protected members such that they can be used in the derived classes
135 
136 public:
137  // ==========================================================================
171  template <class TYPE>
173  const bool useRootInTES = true ) const;
177  template <class TYPE>
178  typename Gaudi::Utils::GetData<TYPE>::return_type getIfExists( IDataProviderSvc* svc, const std::string& location,
179  const bool useRootInTES = true ) const;
206  template <class TYPE>
207  bool exist( IDataProviderSvc* svc, const std::string& location, const bool useRootInTES = true ) const;
235  template <class TYPE, class TYPE2>
236  typename Gaudi::Utils::GetData<TYPE>::return_type getOrCreate( IDataProviderSvc* svc, const std::string& location,
237  const bool useRootInTES = true ) const;
264  const bool useRootInTES = true ) const;
265  // [[deprecated( "please pass std::unique_ptr as 2nd argument" )]]
266  DataObject* put( IDataProviderSvc* svc, DataObject* object, const std::string& location,
267  const bool useRootInTES = true ) const {
268  return put( svc, std::unique_ptr<DataObject>( object ), location, useRootInTES );
269  }
295  template <class TOOL>
296  TOOL* tool( const std::string& type, const std::string& name, const IInterface* parent = 0,
297  bool create = true ) const;
320  template <class TOOL>
321  TOOL* tool( const std::string& type, const IInterface* parent = 0, bool create = true ) const;
344  template <class SERVICE>
345  SmartIF<SERVICE> svc( const std::string& name, const bool create = true ) const;
347  inline IUpdateManagerSvc* updMgrSvc() const;
348 
349 public:
374  StatusCode Error( const std::string& msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
399  StatusCode Warning( const std::string& msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
416  StatusCode Info( const std::string& msg, const StatusCode st = StatusCode::SUCCESS, const size_t mx = 10 ) const;
429  StatusCode Print( const std::string& msg, const StatusCode st = StatusCode::SUCCESS,
430  const MSG::Level lev = MSG::INFO ) const;
439  inline void Assert( const bool ok, const std::string& message = "",
440  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
449  inline void Assert( const bool ok, const char* message,
450  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
459  void Exception( const std::string& msg, const GaudiException& exc,
460  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
470  void Exception( const std::string& msg, const std::exception& exc,
471  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
480  void Exception( const std::string& msg = "no message",
481  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
482 
483 private:
485  inline StatisticsOwn countersOwn() const { return m_countersOwn; }
486 
487 public:
506  //[[deprecated( "see LHCBPS-1758" )]]
507  inline StatEntity& counter( const std::string& tag ) const {
508  return const_cast<GaudiCommon<PBASE>*>( this )->counter( tag );
509  }
510  inline StatEntity& counter( const std::string& tag ) {
511  std::lock_guard<std::mutex> lock( m_countersOwnMutex );
512  // Return referenced StatEntity if it already exists, else create it
513  auto p = this->findCounter( tag );
514  if ( !p ) {
515  auto& counter = m_countersOwn[tag];
516  this->declareCounter( tag, counter );
517  return counter;
518  }
519  return m_countersOwn[tag];
520  }
521  // ==========================================================================
522 public:
524  inline bool typePrint() const { return m_typePrint; }
526  inline bool propsPrint() const { return m_propsPrint; }
528  inline bool statPrint() const { return m_statPrint; }
530  inline bool errorsPrint() const { return m_errorsPrint; }
531  // ==========================================================================
532 public:
537  long printStat( const MSG::Level level = MSG::ALWAYS ) const;
542  long printErrors( const MSG::Level level = MSG::ALWAYS ) const;
547  long printProps( const MSG::Level level = MSG::ALWAYS ) const;
563  template <class CallerClass>
564  inline void registerCondition( const std::string& condition, StatusCode ( CallerClass::*mf )() = nullptr ) {
565  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
566  }
597  template <class CallerClass, class CondType>
598  inline void registerCondition( const std::string& condition, CondType*& condPtrDest,
599  StatusCode ( CallerClass::*mf )() = NULL ) {
600  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf, condPtrDest );
601  }
603  template <class CallerClass>
604  inline void registerCondition( char* condition, StatusCode ( CallerClass::*mf )() = NULL ) {
605  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), std::string( condition ), mf );
606  }
622  template <class CallerClass, class TargetClass>
623  inline void registerCondition( TargetClass* condition, StatusCode ( CallerClass::*mf )() = NULL ) {
624  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
625  }
637  inline StatusCode runUpdate() { return updMgrSvc()->update( this ); }
638 
639 public:
642  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of<Gaudi::Algorithm, PBASE>::value, U>>
643  GaudiCommon( const std::string& name, ISvcLocator* pSvcLocator ) : base_class( name, pSvcLocator ) {
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  initGaudiCommonConstructor( this->parent() );
652  }
653 
654 public:
658  StatusCode initialize() override;
662  StatusCode finalize() override;
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 public:
706  // ==========================================================================
708  inline const std::string& context() const { return m_context; }
712  inline const std::string& rootInTES() const { return m_rootInTES; }
713  // ==========================================================================
714 public:
715  // ==========================================================================
717  const std::string fullTESLocation( const std::string& location, const bool useRootInTES ) const;
718  // ==========================================================================
719 private:
720  // ==========================================================================
722  void addToServiceList( SmartIF<IService> svc ) const;
724  void initGaudiCommonConstructor( const IInterface* parent = nullptr );
725  // ==========================================================================
726 private:
728  mutable AlgTools m_managedTools;
730  mutable Services m_services;
731  // ==========================================================================
733  mutable Counter m_errors;
735  mutable Counter m_warnings;
737  mutable Counter m_infos;
739  mutable Counter m_exceptions;
741  StatisticsOwn m_countersOwn;
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_rootInTES{this, "RootInTES", {}, "note: overridden by parent settings"};
781  Gaudi::Property<std::string> m_header{this, "StatTableHeader",
782  " | Counter | # | "
783  " sum | mean/eff^* | rms/err^* | min | max |",
784  "the header row for the output Stat-table"};
786  this, "RegularRowFormat", " | %|-48.48s|%|50t||%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |",
787  "the format for regular row in the output Stat-table"};
789  this, "EfficiencyRowFormat",
790  " |*%|-48.48s|%|50t||%|10d| |%|11.5g| |(%|#9.6g| +- %|-#9.6g|)%%| ------- | ------- |",
791  "The format for \"efficiency\" row in the output Stat-table"};
792  Gaudi::Property<bool> m_useEffFormat{this, "UseEfficiencyRowFormat", true,
793  "use the special format for printout of efficiency counters"};
794 
796  this, "CounterList", {".*"}, "RegEx list, of simple integer counters for CounterSummary"};
798  this, "StatEntityList", {}, "RegEx list, of StatEntity counters for CounterSummary"};
799 };
800 // ============================================================================
801 #include "GaudiAlg/GaudiCommonImp.h"
802 // ============================================================================
803 
804 // ============================================================================
805 // The END
806 // ============================================================================
807 #endif // GAUDIALG_GAUDICOMMON_H
std::map< std::string, StatEntity > StatisticsOwn
the actual type of general counters
Definition: GaudiCommon.h:122
The implementation of inline/templated methods for class GaudiCommon.
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:66
Counter m_warnings
counter of warnings
Definition: GaudiCommon.h:735
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:65
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:739
Implementation of property with value of concrete type.
Definition: Property.h:352
const std::string & rootInTES() const
Returns the "rootInTES" string.
Definition: GaudiCommon.h:712
GaudiCommon(const std::string &name, ISvcLocator *pSvcLocator)
Algorithm constructor - the SFINAE constraint below ensures that this is constructor is only defined ...
Definition: GaudiCommon.h:643
StatEntity & counter(const std::string &tag)
Definition: GaudiCommon.h:510
std::vector< IAlgTool * > AlgTools
storage for active tools
Definition: GaudiCommon.h:127
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:623
std::map< std::string, unsigned int > Counter
the actual type error/warning counter
Definition: GaudiCommon.h:125
Services m_services
List of active services.
Definition: GaudiCommon.h:730
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:67
StatisticsOwn m_countersOwn
General counters.
Definition: GaudiCommon.h:741
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:54
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:708
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:60
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:604
StatisticsOwn countersOwn() const
accessor to all owned counters
Definition: GaudiCommon.h:485
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:564
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:528
StatEntity & counter(const std::string &tag) const
accessor to certain counter by name
Definition: GaudiCommon.h:507
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:50
bool propsPrint() const
Print properties at initialization ?
Definition: GaudiCommon.h:526
Definition of the basic interface.
Definition: IInterface.h:244
Counter m_infos
counter of infos
Definition: GaudiCommon.h:737
bool errorsPrint() const
Print error counters at finalization ?
Definition: GaudiCommon.h:530
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:129
SmartIF< ICounterSummarySvc > m_counterSummarySvc
a pointer to the CounterSummarySvc
Definition: GaudiCommon.h:134
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:266
StatusCode runUpdate()
asks the UpdateManagerSvc to perform an update of the instance (if needed) without waiting the next B...
Definition: GaudiCommon.h:637
std::mutex m_countersOwnMutex
The mutex for m_countersOwn.
Definition: GaudiCommon.h:743
STL class.
#define WARN_UNUSED
Definition: GaudiCommon.h:40
std::map< std::string, std::reference_wrapper< Gaudi::Accumulators::PrintableCounter > > Statistics
Definition: GaudiCommon.h:123
bool typePrint() const
Insert the actual C++ type of the algorithm/tool in the messages ?
Definition: GaudiCommon.h:524
StatusCode update(CallerClass *instance)
Update the given instance.
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:62
Alias for backward compatibility.
Definition: Algorithm.h:56
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:598
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:312
constexpr static const auto FAILURE
Definition: StatusCode.h:86
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:92
AlgTools m_managedTools
List of active tools.
Definition: GaudiCommon.h:728
bool operator()(const SmartIF< IService > &s, const std::string &n) const
Definition: GaudiCommon.h:61
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
backward compatible StatEntity class.
Definition: Counters.h:837
#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
Helper functions to set/get the application return code.
Definition: __init__.py:1
Out1 * put(DataObjectHandle< Out1 > &out_handle, Out2 &&out)