The Gaudi Framework  v33r1 (b1225454)
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 // ============================================================================
24 // GaudiAlg
25 // ============================================================================
27 #include "GaudiAlg/GaudiTool.h"
28 // ============================================================================
36 // ============================================================================
37 // templated methods
38 // ============================================================================
39 #include "GaudiCommon.icpp"
40 // ============================================================================
42 // ============================================================================
49 // ============================================================================
50 namespace GaudiToolServices {
52  const std::string s_EventDataSvc = "EventDataSvc";
54  const std::string s_DetectorDataSvc = "DetectorDataSvc";
56  const std::string s_ChronoStatSvc = "ChronoStatSvc";
58  const std::string s_IncidentSvc = "IncidentSvc";
60  const std::string s_HistoSvc = "HistogramDataSvc";
61 } // namespace GaudiToolServices
62 // ============================================================================
63 namespace GaudiToolLocal {
64  // ==========================================================================
68  class Counter final {
69  public:
70  // ========================================================================
71  // constructor
72  Counter( std::string msg = " Misbalance " ) : m_message( std::move( msg ) ){};
73  // destructor
74  ~Counter() { report(); }
75  // ========================================================================
76  public:
77  // ========================================================================
79  long increment( std::string_view object ) { return ++get( object ); }
81  long decrement( std::string_view object ) { return --get( object ); }
83  long counts( std::string_view object ) { return get( object ); }
85  void report() const {
87  if ( !GaudiTool::summaryEnabled() ) { return; } // RETURN
88  //
89  for ( const auto& entry : m_map ) {
90  if ( entry.second ) {
91  std::cout << "GaudiTool WARNING " << m_message << "'" << entry.first << "' Counts = " << entry.second
92  << std::endl;
93  }
94  }
95  }
96  // ========================================================================
97  private:
98  // ========================================================================
100  long& get( std::string_view which ) {
101  auto i = m_map.find( which );
102  if ( i == m_map.end() ) i = m_map.emplace( which, long{} ).first;
103  return i->second;
104  }
107  // ========================================================================
108  };
109  // ==========================================================================
115  static Counter s_InstanceCounter( " Create/Destroy (mis)balance " );
116  // ==========================================================================
122  static Counter s_FinalizeCounter( " Initialize/Finalize (mis)balance " );
123  // ==========================================================================
124 } // namespace GaudiToolLocal
125 // ============================================================================
127 // ============================================================================
128 bool GaudiTool::s_enableSummary = true; // summary is enabled
129 // ============================================================================
130 // enable/disable summary
131 // ============================================================================
132 bool GaudiTool::enableSummary( bool value ) // enable/disable summary
133 {
134  s_enableSummary = value;
135  return summaryEnabled();
136 }
137 // ============================================================================
138 // is summary enabled?
139 // ============================================================================
140 bool GaudiTool::summaryEnabled() // is summary enabled?
141 {
142  return s_enableSummary;
143 }
144 // ============================================================================
145 // Standard constructor
146 // ============================================================================
148  : GaudiCommon<CounterHolder<AlgTool>>( std::move( this_type ), std::move( this_name ), parent )
149  , 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 // ============================================================================
216  if ( UNLIKELY( !m_detSvc ) ) m_detSvc = service( GaudiToolServices::s_DetectorDataSvc, true );
217  return m_detSvc;
218 }
219 // ============================================================================
220 // The standard N-Tuple
221 // ============================================================================
223  if ( UNLIKELY( !m_ntupleSvc ) ) m_ntupleSvc = service( "NTupleSvc", true );
224  return m_ntupleSvc;
225 }
226 // ============================================================================
227 // The standard event collection service
228 // ============================================================================
230  if ( UNLIKELY( !m_evtColSvc ) ) m_evtColSvc = service( "EvtTupleSvc", true );
231  return m_evtColSvc;
232 }
233 // ============================================================================
234 // accessor to Incident Service
235 // ============================================================================
237  if ( UNLIKELY( !m_incSvc ) ) m_incSvc = service( GaudiToolServices::s_IncidentSvc, true );
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 // ============================================================================
251  if ( UNLIKELY( !m_histoSvc ) ) m_histoSvc = service( GaudiToolServices::s_HistoSvc, true );
252  return m_histoSvc;
253 }
254 // ============================================================================
255 // accessor to Algorithm Context Service
256 // ============================================================================
258  if ( UNLIKELY( !m_contextSvc ) ) m_contextSvc = service( m_contextSvcName, true );
259  return m_contextSvc;
260 }
261 // ============================================================================
262 // The END
263 // ============================================================================
#define UNLIKELY(x)
Definition: Kernel.h:106
Header file for class GaudiAlgorithm.
bool m_isPublic
Flag to say if the tool is a public or private tool.
Definition: GaudiTool.h:780
SmartIF< INTupleSvc > m_ntupleSvc
pointer to the N-Tuple service
Definition: GaudiTool.h:760
Counter(std::string msg=" Misbalance ")
Definition: GaudiTool.cpp:72
Header file for class GaudiAlgorithm.
T endl(T... args)
const std::string s_ChronoStatSvc
the default name for Chrono & Stat Service
Definition: GaudiTool.cpp:56
SmartIF< IAlgContextSvc > m_contextSvc
Algorithm Context Service.
Definition: GaudiTool.h:772
STL namespace.
static bool enableSummary(bool)
enable/disable summary
Definition: GaudiTool.cpp:132
GaudiTool(std::string type, std::string name, const IInterface *parent)
Standard constructor.
Definition: GaudiTool.cpp:147
T end(T... args)
SmartIF< IIncidentSvc > m_incSvc
pointer to Incident Service
Definition: GaudiTool.h:768
Data provider interface definition.
long increment(std::string_view object)
make the increment
Definition: GaudiTool.cpp:79
const std::string s_IncidentSvc
the default name for Incident Service
Definition: GaudiTool.cpp:58
IChronoStatSvc * chronoSvc() const
accessor to Chrono & Stat Service
Definition: GaudiTool.cpp:243
Collection of default services names to be used for class GaudiTool.
STL class.
The IChronoStatSvc is the interface implemented by the ChronoStatService.
const std::string s_HistoSvc
the default name for Histogram Service
Definition: GaudiTool.cpp:60
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
IDataProviderSvc * detSvc() const
accessor to detector service
Definition: GaudiTool.cpp:215
Definition of the basic interface.
Definition: IInterface.h:254
long counts(std::string_view object)
current count
Definition: GaudiTool.cpp:83
~GaudiTool() override
destructor, virtual and protected
Definition: GaudiTool.cpp:156
Definition of the IHistogramSvc interface class.
Definition: IHistogramSvc.h:56
const std::string s_DetectorDataSvc
the default name for Detector Data Service
Definition: GaudiTool.cpp:54
INTupleSvc * evtColSvc() const
Access the standard event collection service.
Definition: GaudiTool.cpp:229
T find(T... args)
static bool s_enableSummary
enable printout of summary?
Definition: GaudiTool.h:790
SmartIF< IChronoStatSvc > m_chronoSvc
pointer to Chrono & Stat Service
Definition: GaudiTool.h:766
INTupleSvc * ntupleSvc() const
Access the standard N-Tuple.
Definition: GaudiTool.cpp:222
def which(executable)
Definition: BaseTest.py:665
SmartIF< IHistogramSvc > m_histoSvc
pointer for histogram service
Definition: GaudiTool.h:770
simple local counter
Definition: GaudiTool.cpp:68
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
Gaudi::Property< std::string > m_contextSvcName
Definition: GaudiTool.h:774
T emplace(T... args)
StatusCode finalize() override
standard finalization method
Definition: GaudiTool.cpp:177
SmartIF< IDataProviderSvc > m_detSvc
pointer to Detector Data Service
Definition: GaudiTool.h:764
const Gaudi::Algorithm & parent
IAlgContextSvc * contextSvc() const
acessor to the Algorithm Context Service
Definition: GaudiTool.cpp:257
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:95
long & get(std::string_view which)
Definition: GaudiTool.cpp:100
const std::string s_EventDataSvc
the default name for Event Data Service
Definition: GaudiTool.cpp:52
An abstract interface for Algorithm Context Service.
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.
long decrement(std::string_view object)
make the decrement
Definition: GaudiTool.cpp:81
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
bool isFailure() const
Definition: StatusCode.h:145
const std::string m_local
full tool name "type/name"
Definition: GaudiTool.h:785
SmartIF< INTupleSvc > m_evtColSvc
pointer to the event tag collection service
Definition: GaudiTool.h:762
IHistogramSvc * histoSvc() const
acessor to the histogram service
Definition: GaudiTool.cpp:250
void report() const
make a report
Definition: GaudiTool.cpp:85
static bool summaryEnabled()
is summary enabled?
Definition: GaudiTool.cpp:140
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:33
StatusCode initialize() override
standard initialization method
Definition: GaudiTool.cpp:160
IIncidentSvc * incSvc() const
accessor to Incident Service
Definition: GaudiTool.cpp:236
bool isPublic() const
Determines if this tool is public or not (i.e. owned by the ToolSvc).
Definition: GaudiTool.cpp:199