The Gaudi Framework  v29r0 (ff2e7097)
GaudiTool.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Include files
3 // ============================================================================
4 // GaudiKernel
5 // ============================================================================
12 #include "GaudiKernel/INTupleSvc.h"
13 // ============================================================================
14 // GaudiAlg
15 // ============================================================================
17 #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 " ) : m_message( std::move( msg ) ){};
66  // destructor
67  ~Counter() { report(); }
68  // ========================================================================
69  public:
70  // ========================================================================
72  long increment( const std::string& object ) { return ++m_map[object]; }
74  long decrement( const std::string& object ) { return --m_map[object]; }
76  long counts( const std::string& object ) { return m_map[object]; }
78  void report() const
79  {
81  if ( !GaudiTool::summaryEnabled() ) {
82  return;
83  } // RETURN
84  //
85  for ( const auto& entry : m_map ) {
86  if ( entry.second ) {
87  std::cout << "GaudiTool WARNING " << m_message << "'" << entry.first << "' Counts = " << entry.second
88  << std::endl;
89  }
90  }
91  }
92  // ========================================================================
93  private:
94  // ========================================================================
96  Map m_map;
98  // ========================================================================
99  };
100  // ==========================================================================
106  static Counter s_InstanceCounter( " Create/Destroy (mis)balance " );
107  // ==========================================================================
113  static Counter s_FinalizeCounter( " Initialize/Finalize (mis)balance " );
114  // ==========================================================================
115 }
116 // ============================================================================
118 // ============================================================================
119 bool GaudiTool::s_enableSummary = true; // summary is enabled
120 // ============================================================================
121 // enable/disable summary
122 // ============================================================================
123 bool GaudiTool::enableSummary( bool value ) // enable/disable summary
124 {
125  s_enableSummary = value;
126  return summaryEnabled();
127 }
128 // ============================================================================
129 // is summary enabled?
130 // ============================================================================
131 bool GaudiTool::summaryEnabled() // is summary enabled?
132 {
133  return s_enableSummary;
134 }
135 // ============================================================================
136 // Standard constructor
137 // ============================================================================
138 GaudiTool::GaudiTool( const std::string& this_type, const std::string& this_name, const IInterface* parent )
139  : GaudiCommon<AlgTool>( this_type, this_name, parent ), m_local( this_type + "/" + this_name )
140 {
141  // make instance counts
142  GaudiToolLocal::s_InstanceCounter.increment( m_local );
143 }
144 // ============================================================================
145 // destructor
146 // ============================================================================
147 GaudiTool::~GaudiTool() { GaudiToolLocal::s_InstanceCounter.decrement( m_local ); }
148 // ============================================================================
149 // standard initialization method
150 // ============================================================================
152 {
153  // initialize the base class
155  if ( sc.isFailure() ) {
156  return sc;
157  }
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 {
173  if ( msgLevel( MSG::DEBUG ) ) debug() << " ==> Finalize the base class GaudiTool " << endmsg;
174 
175  // clear "explicit services"
176  m_detSvc.reset();
177  m_chronoSvc.reset();
178  m_incSvc.reset();
179  m_histoSvc.reset();
180 
181  // finalize the base class
183  if ( sc.isFailure() ) {
184  return sc;
185  }
186 
187  // Decrement the counter
188  GaudiToolLocal::s_FinalizeCounter.decrement( m_local );
189 
190  // return
191  return sc;
192 }
193 // ============================================================================
194 // Determines if this tool is public or not (i.e. owned by the ToolSvc).
195 // ============================================================================
197 {
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 ) {
205  break;
206  }
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 {
218  return m_detSvc;
219 }
220 // ============================================================================
221 // The standard N-Tuple
222 // ============================================================================
224 {
225  if ( UNLIKELY( !m_ntupleSvc ) ) m_ntupleSvc = service( "NTupleSvc", true );
226  return m_ntupleSvc;
227 }
228 // ============================================================================
229 // The standard event collection service
230 // ============================================================================
232 {
233  if ( UNLIKELY( !m_evtColSvc ) ) m_evtColSvc = service( "EvtTupleSvc", true );
234  return m_evtColSvc;
235 }
236 // ============================================================================
237 // accessor to Incident Service
238 // ============================================================================
240 {
242  return m_incSvc;
243 }
244 // ============================================================================
245 // accessor to Chrono & Stat Service
246 // ============================================================================
248 {
250  return m_chronoSvc;
251 }
252 // ============================================================================
253 // accessor to histogram Service
254 // ============================================================================
256 {
258  return m_histoSvc;
259 }
260 // ============================================================================
261 // accessor to Algorithm Context Service
262 // ============================================================================
264 {
266  return m_contextSvc;
267 }
268 // ============================================================================
269 // The END
270 // ============================================================================
IChronoStatSvc * chronoSvc() const
accessor to Chrono & Stat Service
Definition: GaudiTool.cpp:247
IIncidentSvc * incSvc() const
accessor to Incident Service
Definition: GaudiTool.cpp:239
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
#define UNLIKELY(x)
Definition: Kernel.h:128
IAlgContextSvc * contextSvc() const
acessor to the Algorithm Context Service
Definition: GaudiTool.cpp:263
Header file for class GaudiAlgorithm.
bool m_isPublic
Flag to say if the tool is a public or private tool.
Definition: GaudiTool.h:786
SmartIF< INTupleSvc > m_ntupleSvc
pointer to the N-Tuple service
Definition: GaudiTool.h:766
long counts(const std::string &object)
current count
Definition: GaudiTool.cpp:76
void report() const
make a report
Definition: GaudiTool.cpp:78
IHistogramSvc * histoSvc() const
acessor to the histogram service
Definition: GaudiTool.cpp:255
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
SmartIF< IAlgContextSvc > m_contextSvc
Algorithm Context Service.
Definition: GaudiTool.h:778
STL namespace.
static bool enableSummary(bool)
enable/disable summary
Definition: GaudiTool.cpp:123
StatusCode finalize() override
standard finalization method
SmartIF< IIncidentSvc > m_incSvc
pointer to Incident Service
Definition: GaudiTool.h:774
Data provider interface definition.
INTupleSvc * ntupleSvc() const
Access the standard N-Tuple.
Definition: GaudiTool.cpp:223
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
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:231
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:28
Definition of the basic interface.
Definition: IInterface.h:277
StatusCode service(const std::string &name, T *&svc, bool createIf=true) const
Access a service by name, creating it if it doesn&#39;t already exist.
Definition: AlgTool.h:132
GaudiTool()=delete
no default/copy constructor, no assignment
~GaudiTool() override
destructor, virtual and protected
Definition: GaudiTool.cpp:147
Definition of the IHistogramSvc interface class.
Definition: IHistogramSvc.h:47
bool isPublic() const
Determines if this tool is public or not (i.e. owned by the ToolSvc).
Definition: GaudiTool.cpp:196
long increment(const std::string &object)
make the increment
Definition: GaudiTool.cpp:72
const std::string s_DetectorDataSvc
the default name for Detector Data Service
Definition: GaudiTool.cpp:45
StatusCode initialize() override
standard initialization method
static bool s_enableSummary
enable printout of summary?
Definition: GaudiTool.h:796
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
SmartIF< IChronoStatSvc > m_chronoSvc
pointer to Chrono & Stat Service
Definition: GaudiTool.h:772
SmartIF< IHistogramSvc > m_histoSvc
pointer for histogram service
Definition: GaudiTool.h:776
simple local counter
Definition: GaudiTool.cpp:60
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:48
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
Gaudi::Property< std::string > m_contextSvcName
Definition: GaudiTool.h:780
const IInterface * parent() const override
Retrieve parent of the sub-algtool.
Definition: AlgTool.cpp:77
StatusCode finalize() override
standard finalization method
Definition: GaudiTool.cpp:171
SmartIF< IDataProviderSvc > m_detSvc
pointer to Detector Data Service
Definition: GaudiTool.h:770
const std::string s_EventDataSvc
the default name for Event Data Service
Definition: GaudiTool.cpp:43
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:92
const std::string m_local
full tool name "type/name"
Definition: GaudiTool.h:791
SmartIF< INTupleSvc > m_evtColSvc
pointer to the event tag collection service
Definition: GaudiTool.h:768
std::map< std::string, long > Map
Definition: GaudiTool.cpp:95
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
long decrement(const std::string &object)
make the decrement
Definition: GaudiTool.cpp:74
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:131
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:23
StatusCode initialize() override
standard initialization method
Definition: GaudiTool.cpp:151
IDataProviderSvc * detSvc() const
accessor to detector service
Definition: GaudiTool.cpp:215