The Gaudi Framework  v33r0 (d5ea422b)
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( const std::string& object ) { return ++m_map[object]; }
81  long decrement( const std::string& object ) { return --m_map[object]; }
83  long counts( const std::string& object ) { return m_map[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  // ========================================================================
102  // ========================================================================
103  };
104  // ==========================================================================
110  static Counter s_InstanceCounter( " Create/Destroy (mis)balance " );
111  // ==========================================================================
117  static Counter s_FinalizeCounter( " Initialize/Finalize (mis)balance " );
118  // ==========================================================================
119 } // namespace GaudiToolLocal
120 // ============================================================================
122 // ============================================================================
123 bool GaudiTool::s_enableSummary = true; // summary is enabled
124 // ============================================================================
125 // enable/disable summary
126 // ============================================================================
127 bool GaudiTool::enableSummary( bool value ) // enable/disable summary
128 {
129  s_enableSummary = value;
130  return summaryEnabled();
131 }
132 // ============================================================================
133 // is summary enabled?
134 // ============================================================================
135 bool GaudiTool::summaryEnabled() // is summary enabled?
136 {
137  return s_enableSummary;
138 }
139 // ============================================================================
140 // Standard constructor
141 // ============================================================================
142 GaudiTool::GaudiTool( const std::string& this_type, const std::string& this_name, const IInterface* parent )
143  : GaudiCommon<CounterHolder<AlgTool>>( this_type, this_name, parent ), m_local( this_type + "/" + this_name ) {
144  // make instance counts
145  GaudiToolLocal::s_InstanceCounter.increment( m_local );
146 }
147 // ============================================================================
148 // destructor
149 // ============================================================================
150 GaudiTool::~GaudiTool() { GaudiToolLocal::s_InstanceCounter.decrement( m_local ); }
151 // ============================================================================
152 // standard initialization method
153 // ============================================================================
155  // initialize the base class
157  if ( sc.isFailure() ) { return sc; }
158 
159  // increment the counter
160  GaudiToolLocal::s_FinalizeCounter.increment( m_local );
161 
162  // are we a public tool ?
163  m_isPublic = isPublic();
164 
165  // return
166  return sc;
167 }
168 // ============================================================================
169 // standard finalization method
170 // ============================================================================
172  if ( msgLevel( MSG::DEBUG ) ) debug() << " ==> Finalize the base class GaudiTool " << endmsg;
173 
174  // clear "explicit services"
175  m_detSvc.reset();
176  m_chronoSvc.reset();
177  m_incSvc.reset();
178  m_histoSvc.reset();
179 
180  // finalize the base class
182  if ( sc.isFailure() ) { return sc; }
183 
184  // Decrement the counter
185  GaudiToolLocal::s_FinalizeCounter.decrement( m_local );
186 
187  // return
188  return sc;
189 }
190 // ============================================================================
191 // Determines if this tool is public or not (i.e. owned by the ToolSvc).
192 // ============================================================================
193 bool GaudiTool::isPublic() const {
194  const IAlgTool* tool = this;
195  // Recurse down the ownership tree, to see with we ever end up at the ToolSvc
196  bool ownedByToolSvc = false;
197  unsigned int sanityCheck( 0 );
198  while ( tool && ++sanityCheck < 99999 ) {
199  ownedByToolSvc = ( nullptr != dynamic_cast<const IToolSvc*>( tool->parent() ) );
200  if ( ownedByToolSvc ) { break; }
201  // if parent is also a tool, try again
202  tool = dynamic_cast<const IAlgTool*>( tool->parent() );
203  }
204  return ownedByToolSvc;
205 }
206 // ============================================================================
207 // accessor to detector service
208 // ============================================================================
210  if ( UNLIKELY( !m_detSvc ) ) m_detSvc = service( GaudiToolServices::s_DetectorDataSvc, true );
211  return m_detSvc;
212 }
213 // ============================================================================
214 // The standard N-Tuple
215 // ============================================================================
217  if ( UNLIKELY( !m_ntupleSvc ) ) m_ntupleSvc = service( "NTupleSvc", true );
218  return m_ntupleSvc;
219 }
220 // ============================================================================
221 // The standard event collection service
222 // ============================================================================
224  if ( UNLIKELY( !m_evtColSvc ) ) m_evtColSvc = service( "EvtTupleSvc", true );
225  return m_evtColSvc;
226 }
227 // ============================================================================
228 // accessor to Incident Service
229 // ============================================================================
231  if ( UNLIKELY( !m_incSvc ) ) m_incSvc = service( GaudiToolServices::s_IncidentSvc, true );
232  return m_incSvc;
233 }
234 // ============================================================================
235 // accessor to Chrono & Stat Service
236 // ============================================================================
239  return m_chronoSvc;
240 }
241 // ============================================================================
242 // accessor to histogram Service
243 // ============================================================================
245  if ( UNLIKELY( !m_histoSvc ) ) m_histoSvc = service( GaudiToolServices::s_HistoSvc, true );
246  return m_histoSvc;
247 }
248 // ============================================================================
249 // accessor to Algorithm Context Service
250 // ============================================================================
252  if ( UNLIKELY( !m_contextSvc ) ) m_contextSvc = service( m_contextSvcName, true );
253  return m_contextSvc;
254 }
255 // ============================================================================
256 // The END
257 // ============================================================================
#define UNLIKELY(x)
Definition: Kernel.h:106
Header file for class GaudiAlgorithm.
TOOL * tool(const std::string &type, const std::string &name, const IInterface *parent=0, bool create=true) const
Useful method for the easy location of tools.
bool m_isPublic
Flag to say if the tool is a public or private tool.
Definition: GaudiTool.h:781
SmartIF< INTupleSvc > m_ntupleSvc
pointer to the N-Tuple service
Definition: GaudiTool.h:761
long counts(const std::string &object)
current count
Definition: GaudiTool.cpp:83
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:773
STL namespace.
static bool enableSummary(bool)
enable/disable summary
Definition: GaudiTool.cpp:127
SmartIF< IIncidentSvc > m_incSvc
pointer to Incident Service
Definition: GaudiTool.h:769
Data provider interface definition.
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:237
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:209
Definition of the basic interface.
Definition: IInterface.h:254
GaudiTool()=delete
no default/copy constructor, no assignment
~GaudiTool() override
destructor, virtual and protected
Definition: GaudiTool.cpp:150
Definition of the IHistogramSvc interface class.
Definition: IHistogramSvc.h:56
long increment(const std::string &object)
make the increment
Definition: GaudiTool.cpp:79
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:223
static bool s_enableSummary
enable printout of summary?
Definition: GaudiTool.h:791
SmartIF< IChronoStatSvc > m_chronoSvc
pointer to Chrono & Stat Service
Definition: GaudiTool.h:767
INTupleSvc * ntupleSvc() const
Access the standard N-Tuple.
Definition: GaudiTool.cpp:216
SmartIF< IHistogramSvc > m_histoSvc
pointer for histogram service
Definition: GaudiTool.h:771
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:775
StatusCode finalize() override
standard finalization method
Definition: GaudiTool.cpp:171
SmartIF< IDataProviderSvc > m_detSvc
pointer to Detector Data Service
Definition: GaudiTool.h:765
IAlgContextSvc * contextSvc() const
acessor to the Algorithm Context Service
Definition: GaudiTool.cpp:251
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:101
const std::string s_EventDataSvc
the default name for Event Data Service
Definition: GaudiTool.cpp:52
An abstract interface for Algorithm Context Service.
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:141
const std::string m_local
full tool name "type/name"
Definition: GaudiTool.h:786
SmartIF< INTupleSvc > m_evtColSvc
pointer to the event tag collection service
Definition: GaudiTool.h:763
std::map< std::string, long > Map
Definition: GaudiTool.cpp:99
IHistogramSvc * histoSvc() const
acessor to the histogram service
Definition: GaudiTool.cpp:244
long decrement(const std::string &object)
make the decrement
Definition: GaudiTool.cpp:81
void report() const
make a report
Definition: GaudiTool.cpp:85
static bool summaryEnabled()
is summary enabled?
Definition: GaudiTool.cpp:135
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:154
IIncidentSvc * incSvc() const
accessor to Incident Service
Definition: GaudiTool.cpp:230
bool isPublic() const
Determines if this tool is public or not (i.e. owned by the ToolSvc).
Definition: GaudiTool.cpp:193