The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
GaudiTool.cpp
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 // ============================================================================
12 // Include files
13 // ============================================================================
14 // GaudiKernel
15 // ============================================================================
16 #include "GaudiKernel/Bootstrap.h"
22 #include "GaudiKernel/INTupleSvc.h"
23 #include "GaudiKernel/System.h"
24 // ============================================================================
25 // GaudiAlg
26 // ============================================================================
28 #include "GaudiAlg/GaudiTool.h"
29 // ============================================================================
37 // ============================================================================
38 // templated methods
39 // ============================================================================
40 #include "GaudiCommon.icpp"
41 // ============================================================================
42 template class GaudiCommon<AlgTool>;
43 // ============================================================================
50 // ============================================================================
51 namespace GaudiToolServices {
53  const std::string s_EventDataSvc = "EventDataSvc";
55  const std::string s_DetectorDataSvc = "DetectorDataSvc";
57  const std::string s_ChronoStatSvc = "ChronoStatSvc";
59  const std::string s_IncidentSvc = "IncidentSvc";
61  const std::string s_HistoSvc = "HistogramDataSvc";
62 } // namespace GaudiToolServices
63 // ============================================================================
64 namespace GaudiToolLocal {
65  // ==========================================================================
69  class Counter final {
70  public:
71  // ========================================================================
72  // constructor
73  Counter( std::string msg = " Misbalance " ) : m_message( std::move( msg ) ){};
74  // destructor
75  ~Counter() { report(); }
76  // ========================================================================
77  public:
78  // ========================================================================
80  long increment( std::string_view object ) { return ++get( object ); }
82  long decrement( std::string_view object ) { return --get( object ); }
84  long counts( std::string_view object ) { return get( object ); }
86  void report() const {
88  if ( !GaudiTool::summaryEnabled() ) { return; } // RETURN
89  //
90  for ( const auto& entry : m_map ) {
91  if ( entry.second ) {
92  std::cout << "GaudiTool WARNING " << m_message << "'" << entry.first << "' Counts = " << entry.second
93  << std::endl;
94  }
95  }
96  }
97  // ========================================================================
98  private:
99  // ========================================================================
101  long& get( std::string_view which ) {
102  auto i = m_map.find( which );
103  if ( i == m_map.end() ) i = m_map.emplace( which, long{} ).first;
104  return i->second;
105  }
108  // ========================================================================
109  };
110  // ==========================================================================
116  static Counter s_InstanceCounter( " Create/Destroy (mis)balance " );
117  // ==========================================================================
123  static Counter s_FinalizeCounter( " Initialize/Finalize (mis)balance " );
124  // ==========================================================================
125 } // namespace GaudiToolLocal
126 // ============================================================================
128 // ============================================================================
129 bool GaudiTool::s_enableSummary = System::isEnvSet( "ENABLE_GAUDITOOL_SUMMARY" );
130 // ============================================================================
131 // enable/disable summary
132 // ============================================================================
133 bool GaudiTool::enableSummary( bool value ) // enable/disable summary
134 {
135  s_enableSummary = value;
136  return summaryEnabled();
137 }
138 // ============================================================================
139 // is summary enabled?
140 // ============================================================================
141 bool GaudiTool::summaryEnabled() // is summary enabled?
142 {
143  return s_enableSummary;
144 }
145 // ============================================================================
146 // Standard constructor
147 // ============================================================================
148 GaudiTool::GaudiTool( std::string this_type, std::string this_name, const IInterface* parent )
149  : GaudiCommon<AlgTool>( std::move( this_type ), std::move( this_name ), parent ), m_local( type() + "/" + name() ) {
150  // make instance counts
151  GaudiToolLocal::s_InstanceCounter.increment( m_local );
152 }
153 // ============================================================================
154 // destructor
155 // ============================================================================
156 GaudiTool::~GaudiTool() { GaudiToolLocal::s_InstanceCounter.decrement( m_local ); }
157 // ============================================================================
158 // standard initialization method
159 // ============================================================================
161  // initialize the base class
163  if ( sc.isFailure() ) { return sc; }
164 
165  // increment the counter
166  GaudiToolLocal::s_FinalizeCounter.increment( m_local );
167 
168  // are we a public tool ?
169  m_isPublic = isPublic();
170 
171  // return
172  return sc;
173 }
174 // ============================================================================
175 // standard finalization method
176 // ============================================================================
178  if ( msgLevel( MSG::DEBUG ) ) debug() << " ==> Finalize the base class GaudiTool " << endmsg;
179 
180  // clear "explicit services"
181  m_detSvc.reset();
182  m_chronoSvc.reset();
183  m_incSvc.reset();
184  m_histoSvc.reset();
185 
186  // finalize the base class
188  if ( sc.isFailure() ) { return sc; }
189 
190  // Decrement the counter
191  GaudiToolLocal::s_FinalizeCounter.decrement( m_local );
192 
193  // return
194  return sc;
195 }
196 // ============================================================================
197 // Determines if this tool is public or not (i.e. owned by the ToolSvc).
198 // ============================================================================
199 bool GaudiTool::isPublic() const {
200  const IAlgTool* tool = this;
201  // Recurse down the ownership tree, to see with we ever end up at the ToolSvc
202  bool ownedByToolSvc = false;
203  unsigned int sanityCheck( 0 );
204  while ( tool && ++sanityCheck < 99999 ) {
205  ownedByToolSvc = ( nullptr != dynamic_cast<const IToolSvc*>( tool->parent() ) );
206  if ( ownedByToolSvc ) { break; }
207  // if parent is also a tool, try again
208  tool = dynamic_cast<const IAlgTool*>( tool->parent() );
209  }
210  return ownedByToolSvc;
211 }
212 // ============================================================================
213 // accessor to detector service
214 // ============================================================================
217  return m_detSvc;
218 }
219 // ============================================================================
220 // The standard N-Tuple
221 // ============================================================================
223  if ( !m_ntupleSvc ) m_ntupleSvc = service( "NTupleSvc", true );
224  return m_ntupleSvc;
225 }
226 // ============================================================================
227 // The standard event collection service
228 // ============================================================================
230  if ( !m_evtColSvc ) m_evtColSvc = service( "EvtTupleSvc", true );
231  return m_evtColSvc;
232 }
233 // ============================================================================
234 // accessor to Incident Service
235 // ============================================================================
238  return m_incSvc;
239 }
240 // ============================================================================
241 // accessor to Chrono & Stat Service
242 // ============================================================================
245  return m_chronoSvc;
246 }
247 // ============================================================================
248 // accessor to histogram Service
249 // ============================================================================
252  return m_histoSvc;
253 }
254 // ============================================================================
255 // accessor to Algorithm Context Service
256 // ============================================================================
259  return m_contextSvc;
260 }
261 // ============================================================================
262 // The END
263 // ============================================================================
GaudiTool::m_histoSvc
SmartIF< IHistogramSvc > m_histoSvc
pointer for histogram service
Definition: GaudiTool.h:769
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
GaudiTool::m_evtColSvc
SmartIF< INTupleSvc > m_evtColSvc
pointer to the event tag collection service
Definition: GaudiTool.h:761
GaudiTool.h
GaudiToolLocal::Counter::Counter
Counter(std::string msg=" Misbalance ")
Definition: GaudiTool.cpp:73
GaudiTool::m_detSvc
SmartIF< IDataProviderSvc > m_detSvc
pointer to Detector Data Service
Definition: GaudiTool.h:763
GaudiCommon.icpp
std::string
STL class.
IAlgTool
Definition: IAlgTool.h:33
IHistogramSvc
Definition: IHistogramSvc.h:56
GaudiTool::enableSummary
static bool enableSummary(bool)
enable/disable summary
Definition: GaudiTool.cpp:133
GaudiTool::m_local
const std::string m_local
full tool name "type/name"
Definition: GaudiTool.h:784
GaudiToolServices::s_HistoSvc
const std::string s_HistoSvc
the default name for Histogram Service
Definition: GaudiTool.cpp:61
System.h
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
GaudiToolLocal::Counter::increment
long increment(std::string_view object)
make the increment
Definition: GaudiTool.cpp:80
GaudiTool::summaryEnabled
static bool summaryEnabled()
is summary enabled?
Definition: GaudiTool.cpp:141
GaudiTool::initialize
StatusCode initialize() override
standard initialization method
Definition: GaudiTool.cpp:160
GaudiToolServices::s_DetectorDataSvc
const std::string s_DetectorDataSvc
the default name for Detector Data Service
Definition: GaudiTool.cpp:55
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
GaudiCommon< AlgTool >::tool
TOOL * tool(std::string_view type, std::string_view name, const IInterface *parent=0, bool create=true) const
Useful method for the easy location of tools.
Definition: GaudiCommonImp.h:88
GaudiToolLocal::Counter::m_message
std::string m_message
Definition: GaudiTool.cpp:107
Counter
Definition: Counter.py:1
CommonMessaging< implements< IAlgTool, IDataHandleHolder, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
GaudiToolServices::s_IncidentSvc
const std::string s_IncidentSvc
the default name for Incident Service
Definition: GaudiTool.cpp:59
IDataProviderSvc.h
IIncidentSvc.h
GaudiTool::GaudiTool
GaudiTool(std::string type, std::string name, const IInterface *parent)
Standard constructor.
Definition: GaudiTool.cpp:148
Gaudi::Functional::details::get
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
Definition: FunctionalDetails.h:444
TimingHistograms.name
name
Definition: TimingHistograms.py:25
StatusCode
Definition: StatusCode.h:65
GaudiTool::m_chronoSvc
SmartIF< IChronoStatSvc > m_chronoSvc
pointer to Chrono & Stat Service
Definition: GaudiTool.h:765
std::cout
GaudiTool::evtColSvc
INTupleSvc * evtColSvc() const
Access the standard event collection service.
Definition: GaudiTool.cpp:229
GaudiToolLocal::Counter::counts
long counts(std::string_view object)
current count
Definition: GaudiTool.cpp:84
GaudiTesting.BaseTest.which
def which(executable)
Definition: BaseTest.py:746
IChronoStatSvc
Definition: IChronoStatSvc.h:43
GaudiTool::finalize
StatusCode finalize() override
standard finalization method
Definition: GaudiTool.cpp:177
GaudiTool::s_enableSummary
static bool s_enableSummary
enable printout of summary?
Definition: GaudiTool.h:789
GaudiToolLocal::Counter::m_map
Map m_map
Definition: GaudiTool.cpp:106
GaudiTool::m_incSvc
SmartIF< IIncidentSvc > m_incSvc
pointer to Incident Service
Definition: GaudiTool.h:767
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
System::isEnvSet
GAUDI_API bool isEnvSet(const char *var)
Check if an environment variable is set or not.
Definition: System.cpp:408
std::map< std::string, long, std::less<> >
GaudiTool::histoSvc
IHistogramSvc * histoSvc() const
acessor to the histogram service
Definition: GaudiTool.cpp:250
GaudiTool::chronoSvc
IChronoStatSvc * chronoSvc() const
accessor to Chrono & Stat Service
Definition: GaudiTool.cpp:243
GaudiToolServices::s_ChronoStatSvc
const std::string s_ChronoStatSvc
the default name for Chrono & Stat Service
Definition: GaudiTool.cpp:57
GaudiToolServices
GaudiTool::detSvc
IDataProviderSvc * detSvc() const
accessor to detector service
Definition: GaudiTool.cpp:215
GaudiCommon::initialize
StatusCode initialize() override
standard initialization method
Definition: GaudiCommon.icpp:76
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
GaudiToolLocal::Counter::get
long & get(std::string_view which)
Definition: GaudiTool.cpp:101
gaudirun.type
type
Definition: gaudirun.py:162
GaudiTool::incSvc
IIncidentSvc * incSvc() const
accessor to Incident Service
Definition: GaudiTool.cpp:236
GaudiCommon::finalize
StatusCode finalize() override
standard finalization method
Definition: GaudiCommon.icpp:112
std::endl
T endl(T... args)
IAlgContextSvc.h
AlgTool::service
StatusCode service(std::string_view name, T *&svc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: AlgTool.h:137
AlgTool
Definition: AlgTool.h:62
IChronoStatSvc.h
GaudiAlgorithm.h
std
STL namespace.
IInterface
Definition: IInterface.h:237
IAlgContextSvc
Definition: IAlgContextSvc.h:33
Bootstrap.h
GaudiToolServices::s_EventDataSvc
const std::string s_EventDataSvc
the default name for Event Data Service
Definition: GaudiTool.cpp:53
GaudiTool::isPublic
bool isPublic() const
Determines if this tool is public or not (i.e. owned by the ToolSvc).
Definition: GaudiTool.cpp:199
GaudiTool::~GaudiTool
~GaudiTool() override
destructor, virtual and protected
Definition: GaudiTool.cpp:156
GaudiTool::m_isPublic
bool m_isPublic
Flag to say if the tool is a public or private tool.
Definition: GaudiTool.h:779
GaudiToolLocal
Definition: GaudiTool.cpp:64
GaudiTool::m_ntupleSvc
SmartIF< INTupleSvc > m_ntupleSvc
pointer to the N-Tuple service
Definition: GaudiTool.h:759
IDataProviderSvc
Definition: IDataProviderSvc.h:53
INTupleSvc
Definition: INTupleSvc.h:46
GaudiTool::ntupleSvc
INTupleSvc * ntupleSvc() const
Access the standard N-Tuple.
Definition: GaudiTool.cpp:222
IIncidentSvc
Definition: IIncidentSvc.h:33
IToolSvc
Definition: IToolSvc.h:29
GaudiTool::m_contextSvcName
Gaudi::Property< std::string > m_contextSvcName
Definition: GaudiTool.h:773
GaudiToolLocal::Counter::~Counter
~Counter()
Definition: GaudiTool.cpp:75
GaudiTool::contextSvc
IAlgContextSvc * contextSvc() const
acessor to the Algorithm Context Service
Definition: GaudiTool.cpp:257
IHistogramSvc.h
GaudiToolLocal::Counter::report
void report() const
make a report
Definition: GaudiTool.cpp:86
INTupleSvc.h
GaudiToolLocal::Counter::decrement
long decrement(std::string_view object)
make the decrement
Definition: GaudiTool.cpp:82
GaudiTool::m_contextSvc
SmartIF< IAlgContextSvc > m_contextSvc
Algorithm Context Service.
Definition: GaudiTool.h:771
GaudiCommon< AlgTool >