The Gaudi Framework  v33r1 (b1225454)
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 // ============================================================================
47 // forward declarations
48 // ============================================================================
49 namespace Gaudi {
50  class Algorithm; // GaudiKernel
51 }
52 class AlgTool; // GaudiKernel
53 class ISvcLocator;
54 namespace Gaudi {
55  namespace Utils {
56  template <class TYPE>
57  struct GetData;
58  }
59 } // namespace Gaudi
60 
62  constexpr const struct svc_eq_t {
63  bool operator()( std::string_view n, const SmartIF<IService>& s ) const { return n == s->name(); };
64  bool operator()( const SmartIF<IService>& s, std::string_view n ) const { return s->name() == n; };
65  bool operator()( const SmartIF<IService>& s, const SmartIF<IService>& n ) const { return s->name() == n->name(); };
66  } svc_eq{};
67  constexpr const struct svc_lt_t {
68  bool operator()( std::string_view n, const SmartIF<IService>& s ) const { return n < s->name(); };
69  bool operator()( const SmartIF<IService>& s, std::string_view 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_lt{};
72 } // namespace GaudiCommon_details
73 // ============================================================================
74 /* @file GaudiCommon.h
75  *
76  * Header file for class : GaudiCommon
77  *
78  * @author Chris Jones Christopher.Rob.Jones@cern.ch
79  * @author Vanya BELYAEV Ivan.Belyaev@itep.ru
80  * @author Rob Lambert Rob.Lambert@cern.ch
81  * @date 2009-08-04
82  */
83 // ============================================================================
93 // ============================================================================
94 template <class PBASE>
95 class GAUDI_API GaudiCommon : public FixTESPath<PBASE> {
96 protected: // definitions
98 
108  static const bool IgnoreRootInTES = false;
120  static const bool UseRootInTES = true;
121  // ==========================================================================
122 protected: // few actual data types
123  // ==========================================================================
133 
134  // ==========================================================================
135  // protected members such that they can be used in the derived classes
138 
139 public:
140  // ==========================================================================
174  template <class TYPE>
176  const bool useRootInTES = true ) const;
180  template <class TYPE>
181  typename Gaudi::Utils::GetData<TYPE>::return_type getIfExists( IDataProviderSvc* svc, std::string_view location,
182  const bool useRootInTES = true ) const;
209  template <class TYPE>
210  bool exist( IDataProviderSvc* svc, std::string_view location, const bool useRootInTES = true ) const;
238  template <class TYPE, class TYPE2>
239  typename Gaudi::Utils::GetData<TYPE>::return_type getOrCreate( IDataProviderSvc* svc, std::string_view location,
240  const bool useRootInTES = true ) const;
266  DataObject* put( IDataProviderSvc* svc, std::unique_ptr<DataObject> object, std::string_view location,
267  const bool useRootInTES = true ) const;
268  // [[deprecated( "please pass std::unique_ptr as 2nd argument" )]]
269  DataObject* put( IDataProviderSvc* svc, DataObject* object, std::string_view location,
270  const bool useRootInTES = true ) const {
271  return put( svc, std::unique_ptr<DataObject>( object ), location, useRootInTES );
272  }
298  template <class TOOL>
299  TOOL* tool( std::string_view type, std::string_view name, const IInterface* parent = 0, bool create = true ) const;
322  template <class TOOL>
323  TOOL* tool( std::string_view type, const IInterface* parent = 0, bool create = true ) const;
346  template <class SERVICE>
347  SmartIF<SERVICE> svc( std::string_view name, const bool create = true ) const;
349  IUpdateManagerSvc* updMgrSvc() const;
350 
351 public:
375  StatusCode Error( std::string_view msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
399  StatusCode Warning( std::string_view msg, const StatusCode st = StatusCode::FAILURE, const size_t mx = 10 ) const;
415  StatusCode Info( std::string_view msg, const StatusCode st = StatusCode::SUCCESS, const size_t mx = 10 ) const;
427  StatusCode Print( std::string_view msg, const StatusCode st = StatusCode::SUCCESS,
428  const MSG::Level lev = MSG::INFO ) const;
437  void Assert( const bool ok, std::string_view message = "",
438  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
447  void Exception( std::string_view msg, const GaudiException& exc,
448  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
458  void Exception( std::string_view msg, const std::exception& exc,
459  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
468  void Exception( std::string_view msg = "no message",
469  const StatusCode sc = StatusCode( StatusCode::FAILURE, true ) ) const;
470 
471 private:
473  StatisticsOwn countersOwn() const { return m_countersOwn; }
474 
475 public:
494  //[[deprecated( "see LHCBPS-1758" )]]
495  StatEntity& counter( std::string_view tag ) const { return const_cast<GaudiCommon<PBASE>*>( this )->counter( tag ); }
496  StatEntity& counter( std::string_view tag ) {
497  auto lock = std::scoped_lock{m_countersOwnMutex};
498  // Return referenced StatEntity if it already exists, else create it
499  auto p = m_countersOwn.find( tag );
500  if ( UNLIKELY( p == m_countersOwn.end() ) ) {
501  auto [iter, b] = m_countersOwn.try_emplace( std::string{tag} );
502  assert( b );
503  this->declareCounter( iter->first, iter->second );
504  p = iter;
505  }
506  return p->second;
507  }
508  // ==========================================================================
509 public:
511  bool typePrint() const { return m_typePrint; }
513  bool propsPrint() const { return m_propsPrint; }
515  bool statPrint() const { return m_statPrint; }
517  bool errorsPrint() const { return m_errorsPrint; }
518  // ==========================================================================
519 public:
524  long printStat( const MSG::Level level = MSG::ALWAYS ) const;
529  long printErrors( const MSG::Level level = MSG::ALWAYS ) const;
534  long printProps( const MSG::Level level = MSG::ALWAYS ) const;
550  template <class CallerClass>
551  void registerCondition( const std::string& condition, StatusCode ( CallerClass::*mf )() = nullptr ) {
552  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
553  }
584  template <class CallerClass, class CondType>
585  void registerCondition( const std::string& condition, CondType*& condPtrDest,
586  StatusCode ( CallerClass::*mf )() = NULL ) {
587  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf, condPtrDest );
588  }
590  template <class CallerClass>
591  void registerCondition( char* condition, StatusCode ( CallerClass::*mf )() = NULL ) {
592  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), std::string( condition ), mf );
593  }
609  template <class CallerClass, class TargetClass>
610  void registerCondition( TargetClass* condition, StatusCode ( CallerClass::*mf )() = NULL ) {
611  updMgrSvc()->registerCondition( dynamic_cast<CallerClass*>( this ), condition, mf );
612  }
624  StatusCode runUpdate() { return updMgrSvc()->update( this ); }
625 
626 public:
629  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of_v<Gaudi::Algorithm, PBASE>, U>>
630  GaudiCommon( std::string name, ISvcLocator* pSvcLocator ) : base_class( std::move( name ), pSvcLocator ) {
631  initGaudiCommonConstructor();
632  }
635  template <typename U = PBASE, typename = std::enable_if_t<std::is_base_of_v<AlgTool, PBASE>, U>>
637  : base_class( std::move( type ), std::move( name ), ancestor ) {
638  initGaudiCommonConstructor( this->parent() );
639  }
640 
641 public:
645  StatusCode initialize() override;
649  StatusCode finalize() override;
650 
651  GaudiCommon() = delete;
652  GaudiCommon( const GaudiCommon& ) = delete;
653  GaudiCommon& operator=( const GaudiCommon& ) = delete;
654 
655 protected:
657  StatusCode releaseTool( const IAlgTool* tool ) const;
659  StatusCode releaseSvc( const IInterface* svc ) const;
660 
661 public:
681  StatusCode release( const IInterface* interface ) const;
682 
684  using PBASE::release;
685  // ==========================================================================
686 public:
687  // ==========================================================================
689  const Services& services() const { return m_services; } // get all services
690  // ==========================================================================
691 public:
692  // ==========================================================================
694  const std::string& context() const { return m_context; }
695 
696 private:
697  // ==========================================================================
699  void addToServiceList( SmartIF<IService> svc ) const;
701  void initGaudiCommonConstructor( const IInterface* parent = nullptr );
702  // ==========================================================================
703 private:
708  // ==========================================================================
709  static auto increment( Counter& c, std::string_view which ) {
710  auto i = c.find( which );
711  return i != c.end() ? ++( i->second ) : c.emplace( which, 1 ).first->second;
712  }
714  mutable Counter m_errors;
718  mutable Counter m_infos;
725  // ==========================================================================
727  mutable IUpdateManagerSvc* m_updMgrSvc = nullptr;
728  // ==========================================================================
729  // Properties
730  Gaudi::Property<bool> m_errorsPrint{this, "ErrorsPrint", true,
731  [this]( auto& ) {
732  // no action if not yet initialized
733  if ( this->FSMState() >= Gaudi::StateMachine::INITIALIZED &&
734  this->errorsPrint() ) {
735  this->printErrors();
736  }
737  },
738  "print the statistics of errors/warnings/exceptions"};
739  Gaudi::Property<bool> m_propsPrint{this, "PropertiesPrint", false,
740  [this]( auto& ) {
741  // no action if not yet initialized
742  if ( this->FSMState() >= Gaudi::StateMachine::INITIALIZED &&
743  this->propsPrint() ) {
744  this->printProps( MSG::ALWAYS );
745  }
746  },
747  "print the properties of the component"};
748  Gaudi::Property<bool> m_statPrint{this, "StatPrint", true,
749  [this]( auto& ) {
750  // no action if not yet initialized
751  if ( this->FSMState() >= Gaudi::StateMachine::INITIALIZED && this->statPrint() ) {
752  this->printStat( MSG::ALWAYS );
753  }
754  },
755  "print the table of counters"};
756  Gaudi::Property<bool> m_printEmptyCounters{this, "PrintEmptyCounters", false,
757  "force printing of empty counters, otherwise only printed in DEBUG mode"};
758  Gaudi::Property<bool> m_typePrint{this, "TypePrint", true, "add the actual C++ component type into the messages"};
759 
760  Gaudi::Property<std::string> m_context{this, "Context", {}, "note: overridden by parent settings"};
761  Gaudi::Property<std::string> m_header{this, "StatTableHeader",
762  " | Counter | # | "
763  " sum | mean/eff^* | rms/err^* | min | max |",
764  "the header row for the output Stat-table"};
766  this, "RegularRowFormat", " | %|-48.48s|%|50t||%|10d| |%|11.7g| |%|#11.5g| |%|#11.5g| |%|#12.5g| |%|#12.5g| |",
767  "the format for regular row in the output Stat-table"};
769  this, "EfficiencyRowFormat",
770  " |*%|-48.48s|%|50t||%|10d| |%|11.5g| |(%|#9.6g| +- %|-#9.6g|)%%| ------- | ------- |",
771  "The format for \"efficiency\" row in the output Stat-table"};
772  Gaudi::Property<bool> m_useEffFormat{this, "UseEfficiencyRowFormat", true,
773  "use the special format for printout of efficiency counters"};
774 
776  this, "CounterList", {".*"}, "RegEx list, of simple integer counters for CounterSummary"};
778  this, "StatEntityList", {}, "RegEx list, of StatEntity counters for CounterSummary"};
779 };
780 // ============================================================================
781 #include "GaudiAlg/GaudiCommonImp.h"
782 // ============================================================================
783 
784 // ============================================================================
785 // The END
786 // ============================================================================
787 #endif // GAUDIALG_GAUDICOMMON_H
std::map< std::string, unsigned int, std::less<> > Counter
the actual type error/warning counter
Definition: GaudiCommon.h:128
The implementation of templated methods for class GaudiCommon.
Counter m_warnings
counter of warnings
Definition: GaudiCommon.h:716
#define UNLIKELY(x)
Definition: Kernel.h:106
Out1 * put(const DataObjectHandle< Out1 > &out_handle, Out2 &&out)
StatEntity & counter(std::string_view tag) const
accessor to certain counter by name
Definition: GaudiCommon.h:495
Define general base for Gaudi exception.
bool statPrint() const
Print statistical counters at finalization ?
Definition: GaudiCommon.h:515
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:720
bool errorsPrint() const
Print error counters at finalization ?
Definition: GaudiCommon.h:517
Implementation of property with value of concrete type.
Definition: Property.h:370
StatEntity & counter(std::string_view tag)
Definition: GaudiCommon.h:496
std::vector< IAlgTool * > AlgTools
storage for active tools
Definition: GaudiCommon.h:130
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:610
Services m_services
List of active services.
Definition: GaudiCommon.h:707
const Services & services() const
get the list of aquired services
Definition: GaudiCommon.h:689
StatisticsOwn m_countersOwn
General counters.
Definition: GaudiCommon.h:722
GaudiCommon(std::string type, 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:636
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:57
STL namespace.
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:591
StatisticsOwn countersOwn() const
accessor to all owned counters
Definition: GaudiCommon.h:473
static auto increment(Counter &c, std::string_view which)
Definition: GaudiCommon.h:709
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:551
STL class.
STL class.
constexpr const struct GaudiCommon_details::svc_lt_t svc_lt
Counter m_errors
Counter of errors.
Definition: GaudiCommon.h:714
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
bool operator()(std::string_view n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:63
Definition of the basic interface.
Definition: IInterface.h:254
Counter m_infos
counter of infos
Definition: GaudiCommon.h:718
std::vector< SmartIF< IService > > Services
storage for active services
Definition: GaudiCommon.h:132
SmartIF< ICounterSummarySvc > m_counterSummarySvc
a pointer to the CounterSummarySvc
Definition: GaudiCommon.h:137
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:624
std::mutex m_countersOwnMutex
The mutex for m_countersOwn.
Definition: GaudiCommon.h:724
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:694
STL class.
bool operator()(const SmartIF< IService > &s, std::string_view n) const
Definition: GaudiCommon.h:64
Alias for backward compatibility.
Definition: Algorithm.h:58
std::map< std::string, StatEntity, std::less<> > StatisticsOwn
the actual type of general counters
Definition: GaudiCommon.h:125
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:70
STL class.
bool operator()(const SmartIF< IService > &s, const SmartIF< IService > &n) const
Definition: GaudiCommon.h:65
def which(executable)
Definition: BaseTest.py:665
bool operator()(std::string_view n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:68
GaudiCommon(std::string name, ISvcLocator *pSvcLocator)
Algorithm constructor - the SFINAE constraint below ensures that this is constructor is only defined ...
Definition: GaudiCommon.h:630
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:585
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:101
const Gaudi::Algorithm & parent
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:95
bool propsPrint() const
Print properties at initialization ?
Definition: GaudiCommon.h:513
AlgTools m_managedTools
List of active tools.
Definition: GaudiCommon.h:705
StatusCode initialize() override
Definition: FixTESPath.h:49
backward compatible StatEntity class.
Definition: StatEntity.h:18
bool operator()(const SmartIF< IService > &s, std::string_view n) const
Definition: GaudiCommon.h:69
#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
std::map< std::string, std::reference_wrapper< Gaudi::Accumulators::PrintableCounter >, std::less<> > Statistics
Definition: GaudiCommon.h:126
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
DataObject * put(IDataProviderSvc *svc, DataObject *object, std::string_view location, const bool useRootInTES=true) const
Definition: GaudiCommon.h:269
bool typePrint() const
Insert the actual C++ type of the algorithm/tool in the messages ?
Definition: GaudiCommon.h:511