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 // Include files
3 // ============================================================================
4 // GaudiKernel
5 // ============================================================================
10 #include "GaudiKernel/INTupleSvc.h"
12 #include "GaudiKernel/Bootstrap.h"
13 // ============================================================================
14 // GaudiAlg
15 // ============================================================================
16 #include "GaudiAlg/GaudiTool.h"
18 // ============================================================================
26 // ============================================================================
27 // templated methods
28 // ============================================================================
29 #include "GaudiCommon.icpp"
30 // ============================================================================
31 template class GaudiCommon<AlgTool>;
32 // ============================================================================
39 // ============================================================================
40 namespace GaudiToolServices
41 {
43  const std::string s_EventDataSvc = "EventDataSvc" ;
45  const std::string s_DetectorDataSvc = "DetectorDataSvc" ;
47  const std::string s_ChronoStatSvc = "ChronoStatSvc" ;
49  const std::string s_IncidentSvc = "IncidentSvc" ;
51  const std::string s_HistoSvc = "HistogramDataSvc" ;
52 }
53 // ============================================================================
54 namespace GaudiToolLocal
55 {
56  // ==========================================================================
60  class Counter final
61  {
62  public:
63  // ========================================================================
64  // constructor
65  Counter ( std::string msg = " Misbalance ")
66  : m_message ( std::move(msg) )
67  {};
68  // destructor
69  ~Counter() { report(); }
70  // ========================================================================
71  public:
72  // ========================================================================
74  long increment ( const std::string& object ) { return ++m_map[object] ; }
76  long decrement ( const std::string& object ) { return --m_map[object] ; }
78  long counts ( const std::string& object ) { return m_map[object] ; }
80  void report() const
81  {
83  if ( !GaudiTool::summaryEnabled() ) { return ; } // RETURN
84  //
85  for ( const auto& entry : m_map )
86  {
87  if( entry.second ) {
88  std::cout << "GaudiTool WARNING " << m_message
89  << "'" << entry.first << "' Counts = " << entry.second
90  << std::endl ;
91  }
92  }
93  }
94  // ========================================================================
95  private:
96  // ========================================================================
98  Map m_map ;
100  // ========================================================================
101  };
102  // ==========================================================================
108  static Counter s_InstanceCounter ( " Create/Destroy (mis)balance " ) ;
109  // ==========================================================================
115  static Counter s_FinalizeCounter ( " Initialize/Finalize (mis)balance " ) ;
116  // ==========================================================================
117 }
118 // ============================================================================
120 // ============================================================================
121 bool GaudiTool::s_enableSummary = true ; // summary is enabled
122 // ============================================================================
123 // enable/disable summary
124 // ============================================================================
125 bool GaudiTool::enableSummary ( bool value ) // enable/disable summary
126 {
127  s_enableSummary = value ;
128  return summaryEnabled () ;
129 }
130 // ============================================================================
131 // is summary enabled?
132 // ============================================================================
133 bool GaudiTool::summaryEnabled () // is summary enabled?
134 { return s_enableSummary ; }
135 // ============================================================================
136 // Standard constructor
137 // ============================================================================
138 GaudiTool::GaudiTool ( const std::string& this_type ,
139  const std::string& this_name ,
140  const IInterface* parent )
141  : GaudiCommon<AlgTool> ( this_type , this_name , parent )
142  , m_local ( this_type + "/" + this_name )
143 {
144  // make instance counts
145  GaudiToolLocal::s_InstanceCounter.increment ( m_local ) ;
146 }
147 // ============================================================================
148 // destructor
149 // ============================================================================
151 {
152  GaudiToolLocal::s_InstanceCounter.decrement ( m_local ) ;
153 }
154 // ============================================================================
155 // standard initialization method
156 // ============================================================================
158 {
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 {
177  if ( msgLevel(MSG::DEBUG) )
178  debug() << " ==> Finalize the base class GaudiTool " << endmsg;
179 
180  // clear "explicit services"
181  m_detSvc = nullptr ;
182  m_chronoSvc = nullptr ;
183  m_incSvc = nullptr ;
184  m_histoSvc = nullptr ;
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 // ============================================================================
200 {
201  const IAlgTool * tool = this;
202  // Recurse down the ownership tree, to see with we ever end up at the ToolSvc
203  bool ownedByToolSvc = false;
204  unsigned int sanityCheck(0);
205  while ( tool && ++sanityCheck < 99999 )
206  {
207  ownedByToolSvc = ( nullptr != dynamic_cast<const IToolSvc*>(tool->parent()) );
208  if ( ownedByToolSvc ) { break; }
209  // if parent is also a tool, try again
210  tool = dynamic_cast<const IAlgTool*>(tool->parent());
211  }
212  return ownedByToolSvc;
213 }
214 // ============================================================================
215 // accessor to detector service
216 // ============================================================================
218 {
219  if ( !m_detSvc )
220  {
221  m_detSvc =
222  svc<IDataProviderSvc>( GaudiToolServices::s_DetectorDataSvc , true ) ;
223  }
224  return m_detSvc ;
225 }
226 // ============================================================================
227 // The standard N-Tuple
228 // ============================================================================
230 {
231  if ( !m_ntupleSvc )
232  {
233  m_ntupleSvc = svc<INTupleSvc>( "NTupleSvc" , true ) ;
234  }
235  return m_ntupleSvc ;
236 }
237 // ============================================================================
238 // The standard event collection service
239 // ============================================================================
241 {
242  if ( !m_evtColSvc )
243  {
244  m_evtColSvc = svc< INTupleSvc > ( "EvtTupleSvc" , true ) ;
245  }
246  return m_evtColSvc ;
247 }
248 // ============================================================================
249 // accessor to Incident Service
250 // ============================================================================
252 {
253  if ( !m_incSvc )
254  {
255  m_incSvc =
256  svc<IIncidentSvc> ( GaudiToolServices::s_IncidentSvc , true ) ;
257  }
258  return m_incSvc ;
259 }
260 // ============================================================================
261 // accessor to Chrono & Stat Service
262 // ============================================================================
264 {
265  if ( !m_chronoSvc )
266  {
267  m_chronoSvc =
268  svc<IChronoStatSvc> ( GaudiToolServices::s_ChronoStatSvc , true ) ;
269  }
270  return m_chronoSvc ;
271 }
272 // ============================================================================
273 // accessor to histogram Service
274 // ============================================================================
276 {
277  if ( !m_histoSvc )
278  {
279  m_histoSvc = svc<IHistogramSvc> ( GaudiToolServices::s_HistoSvc, true ) ;
280  }
281  return m_histoSvc;
282 }
283 // ============================================================================
284 // accessor to Algorithm Context Service
285 // ============================================================================
287 {
288  if ( !m_contextSvc )
289  {
290  m_contextSvc = svc<IAlgContextSvc> ( m_contextSvcName , true ) ;
291  }
292  return m_contextSvc;
293 }
294 // ============================================================================
295 // The END
296 // ============================================================================
IChronoStatSvc * chronoSvc() const
accessor to Chrono & Stat Service
Definition: GaudiTool.cpp:263
IIncidentSvc * incSvc() const
accessor to Incident Service
Definition: GaudiTool.cpp:251
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:18
IAlgContextSvc * contextSvc() const
acessor to the Algorithm Context Service
Definition: GaudiTool.cpp:286
Header file for class GaudiAlgorithm.
bool m_isPublic
Flag to say if the tool is a public or private tool.
Definition: GaudiTool.h:785
long counts(const std::string &object)
current count
Definition: GaudiTool.cpp:78
IChronoStatSvc * m_chronoSvc
pointer to Chrono & Stat Service
Definition: GaudiTool.h:771
void report() const
make a report
Definition: GaudiTool.cpp:80
IHistogramSvc * histoSvc() const
acessor to the histogram service
Definition: GaudiTool.cpp:275
Counter(std::string msg=" Misbalance ")
Definition: GaudiTool.cpp:65
Header file for class GaudiAlgorithm.
T endl(T...args)
const std::string s_ChronoStatSvc
the default name for Chrono & Stat Service
Definition: GaudiTool.cpp:47
IDataProviderSvc * m_detSvc
pointer to Detector Data Service
Definition: GaudiTool.h:769
STL namespace.
static bool enableSummary(bool)
enable/disable summary
Definition: GaudiTool.cpp:125
StatusCode finalize() override
standard finalization method
Data provider interface definition.
INTupleSvc * ntupleSvc() const
Access the standard N-Tuple.
Definition: GaudiTool.cpp:229
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:84
const std::string s_IncidentSvc
the default name for Incident Service
Definition: GaudiTool.cpp:49
Collection of default services names to be used for class GaudiTool.
STL class.
The IChronoStatSvc is the interface implemented by the ChronoStatService.
INTupleSvc * evtColSvc() const
Access the standard event collection service.
Definition: GaudiTool.cpp:240
IAlgContextSvc * m_contextSvc
Algorithm Context Service.
Definition: GaudiTool.h:777
const std::string s_HistoSvc
the default name for Histogram Service
Definition: GaudiTool.cpp:51
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Definition of the basic interface.
Definition: IInterface.h:234
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:47
IHistogramSvc * m_histoSvc
pointer for histogram service
Definition: GaudiTool.h:775
bool isPublic() const
Determines if this tool is public or not (i.e. owned by the ToolSvc).
Definition: GaudiTool.cpp:199
long increment(const std::string &object)
make the increment
Definition: GaudiTool.cpp:74
const std::string s_DetectorDataSvc
the default name for Detector Data Service
Definition: GaudiTool.cpp:45
StatusCode initialize() override
standard initialization method
std::map< std::string, long > Map
Definition: GaudiTool.cpp:97
static bool s_enableSummary
enable printout of summary?
Definition: GaudiTool.h:795
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
IIncidentSvc * m_incSvc
pointer to Incident Service
Definition: GaudiTool.h:773
INTupleSvc * m_ntupleSvc
pointer to the N-Tuple service
Definition: GaudiTool.h:765
simple local counter
Definition: GaudiTool.cpp:60
INTupleSvc * m_evtColSvc
pointer to the event tag collection service
Definition: GaudiTool.h:767
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:46
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
Gaudi::Property< std::string > m_contextSvcName
Definition: GaudiTool.h:779
const IInterface * parent() const override
Retrieve parent of the sub-algtool.
Definition: AlgTool.cpp:77
StatusCode finalize() override
standard finalization method
Definition: GaudiTool.cpp:175
const std::string s_EventDataSvc
the default name for Event Data Service
Definition: GaudiTool.cpp:43
An abstract interface for Algorithm Context Service.
const std::string m_local
full tool name "type/name"
Definition: GaudiTool.h:790
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
long decrement(const std::string &object)
make the decrement
Definition: GaudiTool.cpp:76
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.
virtual const IInterface * parent() const =0
The parent of the concrete AlgTool.
static bool summaryEnabled()
is summary enabled?
Definition: GaudiTool.cpp:133
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:23
StatusCode initialize() override
standard initialization method
Definition: GaudiTool.cpp:157
IDataProviderSvc * detSvc() const
accessor to detector service
Definition: GaudiTool.cpp:217