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 <string>
9 #include <vector>
10 #include <map>
11 #include <algorithm>
12 // ============================================================================
13 // GaudiKernel
14 // ============================================================================
15 #include "GaudiKernel/StatusCode.h"
16 #include "GaudiKernel/IMessageSvc.h"
17 #include "GaudiKernel/IToolSvc.h"
18 #include "GaudiKernel/IAlgTool.h"
19 #include "GaudiKernel/IAlgContextSvc.h"
20 #include "GaudiKernel/IDataProviderSvc.h"
21 #include "GaudiKernel/IAlgorithm.h"
22 #include "GaudiKernel/SmartDataPtr.h"
23 #include "GaudiKernel/System.h"
24 #include "GaudiKernel/GaudiException.h"
25 #include "GaudiKernel/IChronoStatSvc.h"
26 #include "GaudiKernel/StatEntity.h"
27 #include "GaudiKernel/ICounterSummarySvc.h"
28 #include "GaudiKernel/IUpdateManagerSvc.h"
29 #include "GaudiKernel/HashMap.h"
30 #include "GaudiKernel/CommonMessaging.h"
31 // ============================================================================
32 // forward declarations
33 // ============================================================================
34 class Algorithm ; // GaudiKernel
35 class AlgTool ; // GaudiKernel
36 namespace Gaudi { namespace Utils { template <class TYPE> struct GetData ; } }
37 
38 namespace GaudiCommon_details {
39  constexpr const struct svc_eq_t {
40  bool operator()(const std::string& n, const SmartIF<IService>& s) const { return n == s->name(); };
41  bool operator()(const SmartIF<IService>& s, const std::string& n) const { return s->name() == n; };
42  bool operator()(const SmartIF<IService>& s, const SmartIF<IService>& n) const { return s->name() == n->name(); };
43  } svc_eq { };
44  constexpr const struct svc_lt_t {
45  bool operator()(const std::string& n, const SmartIF<IService>& s) const { return n < s->name(); };
46  bool operator()(const SmartIF<IService>& s, const std::string& n) const { return s->name() < n; };
47  bool operator()(const SmartIF<IService>& s, const SmartIF<IService>& n) const { return s->name() < n->name(); };
48  } svc_lt { };
49 
50 }
51 // ============================================================================
52 /* @file GaudiCommon.h
53  *
54  * Header file for class : GaudiCommon
55  *
56  * @author Chris Jones Christopher.Rob.Jones@cern.ch
57  * @author Vanya BELYAEV Ivan.Belyaev@itep.ru
58  * @author Rob Lambert Rob.Lambert@cern.ch
59  * @date 2009-08-04
60  */
61 // ============================================================================
71 // ============================================================================
72 template < class PBASE >
73 class GAUDI_API GaudiCommon: public CommonMessaging<PBASE>
74 {
75 protected: // definitions
85  static const bool IgnoreRootInTES = false;
97  static const bool UseRootInTES = true;
98  // ==========================================================================
99 protected: // few actual data types
100  // ==========================================================================
102  typedef std::map<std::string,StatEntity> Statistics ;
104  typedef std::map<std::string,unsigned int> Counter ;
106  typedef std::vector<IAlgTool*> AlgTools ;
108  typedef std::vector<SmartIF<IService>> Services;
109 
110  // ==========================================================================
111  //protected members such that they can be used in the derived classes
113  SmartIF<ICounterSummarySvc> m_counterSummarySvc ;
115  std::vector<std::string> m_counterList = std::vector<std::string>(1,".*");
116  //list of stat entities to write. Set by property StatEntityList. This can be a regular expression.
117  std::vector<std::string> m_statEntityList = std::vector<std::string>(0);
118 public:
119  // ==========================================================================
153  template < class TYPE >
155  get ( IDataProviderSvc* svc ,
156  const std::string& location ,
157  const bool useRootInTES = true ) const ;
161  template < class TYPE >
163  getIfExists ( IDataProviderSvc* svc ,
164  const std::string& location ,
165  const bool useRootInTES = true ) const ;
192  template < class TYPE >
193  bool exist ( IDataProviderSvc* svc ,
194  const std::string& location ,
195  const bool useRootInTES = true ) const ;
223  template < class TYPE , class TYPE2 >
225  getOrCreate ( IDataProviderSvc* svc ,
226  const std::string& location ,
227  const bool useRootInTES = true ) const ;
253  DataObject* put ( IDataProviderSvc* svc ,
254  DataObject* object ,
255  const std::string& location ,
256  const bool useRootInTES = true ) const ;
282  template < class TOOL >
283  TOOL* tool ( const std::string& type ,
284  const std::string& name ,
285  const IInterface* parent = 0 ,
286  bool create = true ) const ;
309  template < class TOOL >
310  TOOL* tool ( const std::string& type ,
311  const IInterface* parent = 0 ,
312  bool create = true ) const ;
335  template < class SERVICE >
336  SmartIF<SERVICE> svc ( const std::string& name ,
337  const bool create = true ) const ;
339  inline IUpdateManagerSvc * updMgrSvc() const;
340 public:
364  StatusCode Error
365  ( const std::string& msg ,
366  const StatusCode st = StatusCode::FAILURE ,
367  const size_t mx = 10 ) const ;
391  StatusCode Warning
392  ( const std::string& msg ,
393  const StatusCode st = StatusCode::FAILURE ,
394  const size_t mx = 10 ) const ;
410  StatusCode Info
411  ( const std::string& msg ,
412  const StatusCode st = StatusCode::SUCCESS ,
413  const size_t mx = 10 ) const ;
425  StatusCode Print
426  ( const std::string& msg ,
427  const StatusCode st = StatusCode::SUCCESS ,
428  const MSG::Level lev = MSG::INFO ) const ;
437  inline void Assert
438  ( const bool ok ,
439  const std::string& message = "" ,
440  const StatusCode sc = StatusCode(StatusCode::FAILURE, true) ) const;
449  inline void Assert
450  ( const bool ok ,
451  const char* message ,
452  const StatusCode sc = StatusCode(StatusCode::FAILURE, true) ) const;
461  void Exception
462  ( const std::string & msg ,
463  const GaudiException & exc ,
464  const StatusCode sc = StatusCode(StatusCode::FAILURE, true) ) const ;
474  void Exception
475  ( const std::string & msg ,
476  const std::exception & exc ,
477  const StatusCode sc = StatusCode(StatusCode::FAILURE, true) ) const ;
486  void Exception
487  ( const std::string& msg = "no message" ,
488  const StatusCode sc = StatusCode(StatusCode::FAILURE, true) ) const ;
489 public:
490  // ==========================================================================
492  inline const Statistics& counters() const { return m_counters ; }
511  inline StatEntity& counter( const std::string& tag ) const { return m_counters[tag] ; }
512  // ==========================================================================
513 public:
514  void resetMsgStream() const;
516  inline bool typePrint () const { return m_typePrint ; }
518  inline bool propsPrint () const { return m_propsPrint ; }
520  inline bool statPrint () const { return m_statPrint ; }
522  inline bool errorsPrint () const { return m_errorsPrint ; }
523  // ==========================================================================
524 private:
525  // ==========================================================================
531  void msgLevelHandler ( Property& theProp );
532  // ==========================================================================
533 public:
538  long printStat ( const MSG::Level level = MSG::ALWAYS ) const ;
543  long printErrors ( const MSG::Level level = MSG::ALWAYS ) const ;
548  long printProps ( const MSG::Level level = MSG::ALWAYS ) const ;
564  template <class CallerClass>
565  inline void registerCondition(const std::string &condition, StatusCode (CallerClass::*mf)() = nullptr) {
566  updMgrSvc()->registerCondition(dynamic_cast<CallerClass*>(this),condition,mf);
567  }
598  template <class CallerClass, class CondType>
599  inline void registerCondition(const std::string &condition, CondType *&condPtrDest,
600  StatusCode (CallerClass::*mf)() = NULL) {
601  updMgrSvc()->registerCondition(dynamic_cast<CallerClass*>(this),condition,mf,condPtrDest);
602  }
604  template <class CallerClass>
605  inline void registerCondition(char *condition, StatusCode (CallerClass::*mf)() = NULL) {
606  updMgrSvc()->registerCondition(dynamic_cast<CallerClass*>(this),std::string(condition),mf);
607  }
623  template <class CallerClass,class TargetClass>
624  inline void registerCondition(TargetClass *condition, StatusCode (CallerClass::*mf)() = NULL) {
625  updMgrSvc()->registerCondition(dynamic_cast<CallerClass*>(this),condition,mf);
626  }
638  inline StatusCode runUpdate() { return updMgrSvc()->update(this); }
639 public:
641  GaudiCommon ( const std::string& name,
642  ISvcLocator * pSvcLocator );
644  GaudiCommon ( const std::string& type ,
645  const std::string& name ,
646  const IInterface* parent );
647 public:
651  StatusCode initialize() override
652 #ifdef __ICC
653  { return i_gcInitialize(); }
654  StatusCode i_gcInitialize()
655 #endif
656  ;
660  StatusCode finalize() override
661 #ifdef __ICC
662  { return i_gcFinalize(); }
663  StatusCode i_gcFinalize()
664 #endif
665  ;
666 protected:
668  ~GaudiCommon() override = default;
669 private :
670  GaudiCommon() = delete;
671  GaudiCommon ( const GaudiCommon& ) = delete;
672  GaudiCommon& operator=( const GaudiCommon& ) = delete;
673 protected:
675  StatusCode releaseTool ( const IAlgTool* tool ) const ;
677  StatusCode releaseSvc ( const IInterface* svc ) const ;
678 public:
698  StatusCode release ( const IInterface* interface ) const ;
700  using PBASE::release;
701  // ==========================================================================
702 public:
703  // ==========================================================================
705  const AlgTools& tools () const { return m_tools ; } // get all tools
707  const Services& services () const { return m_services ; } // get all services
708  // ==========================================================================
709 private:
710  // ==========================================================================
712  void printErrorHandler ( Property& /* theProp */ ) ; // "ErrorPrint"
714  void printPropsHandler ( Property& /* theProp */ ) ; // "PropertiesPrint"
716  void printStatHandler ( Property& /* theProp */ ) ; // "StatPrint"
717  // ==========================================================================
718 public:
719  // ==========================================================================
721  inline const std::string & context() const { return m_context; }
725  inline const std::string & rootInTES() const { return m_rootInTES; }
727  inline double globalTimeOffset() const { return m_globalTimeOffset; }
728  // ==========================================================================
729 public:
730  // ==========================================================================
732  const std::string fullTESLocation
733  ( const std::string & location ,
734  const bool useRootInTES ) const ;
735  // ==========================================================================
736 private:
737  // ==========================================================================
739  void addToToolList ( IAlgTool * tool ) const;
741  void addToServiceList ( SmartIF<IService> svc ) const;
743  void initGaudiCommonConstructor( const IInterface * parent = 0 );
744  // ==========================================================================
745 private:
747  MSG::Level m_msgLevel = MSG::NIL;
748 private:
750  mutable std::unique_ptr<MsgStream> m_msgStream ;
752  mutable AlgTools m_tools ;
754  mutable Services m_services ;
755  // ==========================================================================
757  mutable Counter m_errors ;
759  mutable Counter m_warnings ;
761  mutable Counter m_infos ;
763  mutable Counter m_exceptions ;
765  mutable Statistics m_counters ;
766  // ==========================================================================
768  mutable IUpdateManagerSvc* m_updMgrSvc = nullptr;
769  // ==========================================================================
771  bool m_typePrint = true ;
773  bool m_propsPrint = false ;
775  bool m_statPrint = true ;
777  bool m_errorsPrint = true ;
778  // ==========================================================================
780  std::string m_context;
782  std::string m_rootInTES;
786  std::string m_rootOnTES;
788  double m_globalTimeOffset = 0;
789  // ==========================================================================
790  // the header row
791  std::string m_header ;
792  // format for regular statistical printout rows
793  std::string m_format1 ;
794  // format for "efficiency" statistical printout rows
795  std::string m_format2 ;
796  // flag to use the special "efficiency" format
797  bool m_useEffFormat ;
798 } ;
799 // ============================================================================
800 #include "GaudiAlg/GaudiCommonImp.h"
801 // ============================================================================
802 
803 // ============================================================================
804 // The END
805 // ============================================================================
806 #endif // GAUDIALG_GAUDICOMMON_H
807 // ============================================================================
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:45
Define general base for Gaudi exception.
def initialize()
Definition: AnalysisTest.py:12
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
#define GAUDI_API
Definition: Kernel.h:107
Helper structure for implementation of "get"-functions for GaudiCommon
Definition: GaudiCommon.h:36
bool operator()(const std::string &n, const SmartIF< IService > &s) const
Definition: GaudiCommon.h:40
Data provider interface definition.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Definition of the basic interface.
Definition: IInterface.h:234
Interface class to the Update Manager service.
std::unique_ptr< MsgStream > m_msgStream
The predefined message stream.
constexpr const struct GaudiCommon_details::svc_eq_t svc_eq
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:61
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:34
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:22
string s
Definition: gaudirun.py:246
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:73
The basic counter used for Monitoring purposes.
Definition: StatEntity.h:64
constexpr const struct GaudiCommon_details::svc_lt_t svc_lt
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
string type
Definition: gaudirun.py:151