Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v38r0 (2143aa4c)
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_DetectorDataSvc = "DetectorDataSvc";
55  const std::string s_ChronoStatSvc = "ChronoStatSvc";
57  const std::string s_IncidentSvc = "IncidentSvc";
59  const std::string s_HistoSvc = "HistogramDataSvc";
60 } // namespace GaudiToolServices
61 // ============================================================================
62 namespace GaudiToolLocal {
63  // ==========================================================================
67  class Counter final {
68  public:
69  // ========================================================================
70  // constructor
71  Counter( std::string msg = " Misbalance " ) : m_message( std::move( msg ) ){};
72  // destructor
73  ~Counter() { report(); }
74  // ========================================================================
75  public:
76  // ========================================================================
78  long increment( std::string_view object ) { return ++get( object ); }
80  long decrement( std::string_view object ) { return --get( object ); }
82  long counts( std::string_view object ) { return get( object ); }
84  void report() const {
86  if ( !GaudiTool::summaryEnabled() ) { return; } // RETURN
87  //
88  for ( const auto& entry : m_map ) {
89  if ( entry.second ) {
90  std::cout << "GaudiTool WARNING " << m_message << "'" << entry.first << "' Counts = " << entry.second
91  << std::endl;
92  }
93  }
94  }
95  // ========================================================================
96  private:
97  // ========================================================================
99  long& get( std::string_view which ) {
100  auto i = m_map.find( which );
101  if ( i == m_map.end() ) i = m_map.emplace( which, long{} ).first;
102  return i->second;
103  }
106  // ========================================================================
107  };
108  // ==========================================================================
114  static Counter s_InstanceCounter( " Create/Destroy (mis)balance " );
115  // ==========================================================================
121  static Counter s_FinalizeCounter( " Initialize/Finalize (mis)balance " );
122  // ==========================================================================
123 } // namespace GaudiToolLocal
124 // ============================================================================
126 // ============================================================================
127 bool GaudiTool::s_enableSummary = System::isEnvSet( "ENABLE_GAUDITOOL_SUMMARY" );
128 // ============================================================================
129 // enable/disable summary
130 // ============================================================================
131 bool GaudiTool::enableSummary( bool value ) // enable/disable summary
132 {
133  s_enableSummary = value;
134  return summaryEnabled();
135 }
136 // ============================================================================
137 // is summary enabled?
138 // ============================================================================
139 bool GaudiTool::summaryEnabled() // is summary enabled?
140 {
141  return s_enableSummary;
142 }
143 // ============================================================================
144 // Standard constructor
145 // ============================================================================
146 GaudiTool::GaudiTool( std::string this_type, std::string this_name, const IInterface* parent )
147  : GaudiCommon<AlgTool>( std::move( this_type ), std::move( this_name ), parent ), m_local( type() + "/" + name() ) {
148  // make instance counts
149  GaudiToolLocal::s_InstanceCounter.increment( m_local );
150 }
151 // ============================================================================
152 // destructor
153 // ============================================================================
154 GaudiTool::~GaudiTool() { GaudiToolLocal::s_InstanceCounter.decrement( m_local ); }
155 // ============================================================================
156 // standard initialization method
157 // ============================================================================
159  // initialize the base class
161  if ( sc.isFailure() ) { return sc; }
162 
163  // increment the counter
164  GaudiToolLocal::s_FinalizeCounter.increment( m_local );
165 
166  // are we a public tool ?
167  m_isPublic = isPublic();
168 
169  // return
170  return sc;
171 }
172 // ============================================================================
173 // standard finalization method
174 // ============================================================================
176  if ( msgLevel( MSG::DEBUG ) ) debug() << " ==> Finalize the base class GaudiTool " << endmsg;
177 
178  // clear "explicit services"
179  m_detSvc.reset();
180  m_chronoSvc.reset();
181  m_incSvc.reset();
182  m_histoSvc.reset();
183 
184  // finalize the base class
186  if ( sc.isFailure() ) { return sc; }
187 
188  // Decrement the counter
189  GaudiToolLocal::s_FinalizeCounter.decrement( m_local );
190 
191  // return
192  return sc;
193 }
194 // ============================================================================
195 // Determines if this tool is public or not (i.e. owned by the ToolSvc).
196 // ============================================================================
197 bool GaudiTool::isPublic() const {
198  const IAlgTool* tool = this;
199  // Recurse down the ownership tree, to see with we ever end up at the ToolSvc
200  bool ownedByToolSvc = false;
201  unsigned int sanityCheck( 0 );
202  while ( tool && ++sanityCheck < 99999 ) {
203  ownedByToolSvc = ( nullptr != dynamic_cast<const IToolSvc*>( tool->parent() ) );
204  if ( ownedByToolSvc ) { break; }
205  // if parent is also a tool, try again
206  tool = dynamic_cast<const IAlgTool*>( tool->parent() );
207  }
208  return ownedByToolSvc;
209 }
210 // ============================================================================
211 // accessor to detector service
212 // ============================================================================
215  return m_detSvc;
216 }
217 // ============================================================================
218 // The standard N-Tuple
219 // ============================================================================
221  if ( !m_ntupleSvc ) m_ntupleSvc = service( "NTupleSvc", true );
222  return m_ntupleSvc;
223 }
224 // ============================================================================
225 // The standard event collection service
226 // ============================================================================
228  if ( !m_evtColSvc ) m_evtColSvc = service( "EvtTupleSvc", true );
229  return m_evtColSvc;
230 }
231 // ============================================================================
232 // accessor to Incident Service
233 // ============================================================================
236  return m_incSvc;
237 }
238 // ============================================================================
239 // accessor to Chrono & Stat Service
240 // ============================================================================
243  return m_chronoSvc;
244 }
245 // ============================================================================
246 // accessor to histogram Service
247 // ============================================================================
250  return m_histoSvc;
251 }
252 // ============================================================================
253 // accessor to Algorithm Context Service
254 // ============================================================================
257  return m_contextSvc;
258 }
259 // ============================================================================
260 // The END
261 // ============================================================================
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:71
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:131
GaudiTool::m_local
const std::string m_local
full tool name "type/name"
Definition: GaudiTool.h:784
bug_34121.name
name
Definition: bug_34121.py:20
GaudiToolServices::s_HistoSvc
const std::string s_HistoSvc
the default name for Histogram Service
Definition: GaudiTool.cpp:59
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:78
GaudiTool::summaryEnabled
static bool summaryEnabled()
is summary enabled?
Definition: GaudiTool.cpp:139
GaudiPartProp.decorators.get
get
decorate the vector of properties
Definition: decorators.py:282
GaudiTool::initialize
StatusCode initialize() override
standard initialization method
Definition: GaudiTool.cpp:158
GaudiToolServices::s_DetectorDataSvc
const std::string s_DetectorDataSvc
the default name for Detector Data Service
Definition: GaudiTool.cpp:53
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:105
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:57
IDataProviderSvc.h
IIncidentSvc.h
GaudiToolLocal::Counter
Definition: GaudiTool.cpp:67
GaudiTool::GaudiTool
GaudiTool(std::string type, std::string name, const IInterface *parent)
Standard constructor.
Definition: GaudiTool.cpp:146
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:227
GaudiToolLocal::Counter::counts
long counts(std::string_view object)
current count
Definition: GaudiTool.cpp:82
GaudiTesting.BaseTest.which
def which(executable)
Definition: BaseTest.py:739
IChronoStatSvc
Definition: IChronoStatSvc.h:43
GaudiTool::finalize
StatusCode finalize() override
standard finalization method
Definition: GaudiTool.cpp:175
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:104
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:248
GaudiTool::chronoSvc
IChronoStatSvc * chronoSvc() const
accessor to Chrono & Stat Service
Definition: GaudiTool.cpp:241
GaudiToolServices::s_ChronoStatSvc
const std::string s_ChronoStatSvc
the default name for Chrono & Stat Service
Definition: GaudiTool.cpp:55
GaudiToolServices
GaudiTool::detSvc
IDataProviderSvc * detSvc() const
accessor to detector service
Definition: GaudiTool.cpp:213
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:99
gaudirun.type
type
Definition: gaudirun.py:160
GaudiTool::incSvc
IIncidentSvc * incSvc() const
accessor to Incident Service
Definition: GaudiTool.cpp:234
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
GaudiTool::isPublic
bool isPublic() const
Determines if this tool is public or not (i.e. owned by the ToolSvc).
Definition: GaudiTool.cpp:197
GaudiTool::~GaudiTool
~GaudiTool() override
destructor, virtual and protected
Definition: GaudiTool.cpp:154
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:62
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:220
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:73
GaudiTool::contextSvc
IAlgContextSvc * contextSvc() const
acessor to the Algorithm Context Service
Definition: GaudiTool.cpp:255
IHistogramSvc.h
GaudiToolLocal::Counter::report
void report() const
make a report
Definition: GaudiTool.cpp:84
INTupleSvc.h
GaudiToolLocal::Counter::decrement
long decrement(std::string_view object)
make the decrement
Definition: GaudiTool.cpp:80
GaudiTool::m_contextSvc
SmartIF< IAlgContextSvc > m_contextSvc
Algorithm Context Service.
Definition: GaudiTool.h:771
GaudiCommon< AlgTool >