Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
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 // ============================================================================
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 // ============================================================================
32 // ============================================================================
39 // ============================================================================
40 namespace GaudiToolServices {
42  const std::string s_EventDataSvc = "EventDataSvc";
44  const std::string s_DetectorDataSvc = "DetectorDataSvc";
46  const std::string s_ChronoStatSvc = "ChronoStatSvc";
48  const std::string s_IncidentSvc = "IncidentSvc";
50  const std::string s_HistoSvc = "HistogramDataSvc";
51 } // namespace GaudiToolServices
52 // ============================================================================
53 namespace GaudiToolLocal {
54  // ==========================================================================
58  class Counter final {
59  public:
60  // ========================================================================
61  // constructor
62  Counter( std::string msg = " Misbalance " ) : m_message( std::move( msg ) ){};
63  // destructor
64  ~Counter() { report(); }
65  // ========================================================================
66  public:
67  // ========================================================================
69  long increment( const std::string& object ) { return ++m_map[object]; }
71  long decrement( const std::string& object ) { return --m_map[object]; }
73  long counts( const std::string& object ) { return m_map[object]; }
75  void report() const {
77  if ( !GaudiTool::summaryEnabled() ) { return; } // RETURN
78  //
79  for ( const auto& entry : m_map ) {
80  if ( entry.second ) {
81  std::cout << "GaudiTool WARNING " << m_message << "'" << entry.first << "' Counts = " << entry.second
82  << std::endl;
83  }
84  }
85  }
86  // ========================================================================
87  private:
88  // ========================================================================
90  Map m_map;
92  // ========================================================================
93  };
94  // ==========================================================================
100  static Counter s_InstanceCounter( " Create/Destroy (mis)balance " );
101  // ==========================================================================
107  static Counter s_FinalizeCounter( " Initialize/Finalize (mis)balance " );
108  // ==========================================================================
109 } // namespace GaudiToolLocal
110 // ============================================================================
112 // ============================================================================
113 bool GaudiTool::s_enableSummary = true; // summary is enabled
114 // ============================================================================
115 // enable/disable summary
116 // ============================================================================
117 bool GaudiTool::enableSummary( bool value ) // enable/disable summary
118 {
119  s_enableSummary = value;
120  return summaryEnabled();
121 }
122 // ============================================================================
123 // is summary enabled?
124 // ============================================================================
125 bool GaudiTool::summaryEnabled() // is summary enabled?
126 {
127  return s_enableSummary;
128 }
129 // ============================================================================
130 // Standard constructor
131 // ============================================================================
132 GaudiTool::GaudiTool( const std::string& this_type, const std::string& this_name, const IInterface* parent )
133  : GaudiCommon<CounterHolder<AlgTool>>( this_type, this_name, parent ), m_local( this_type + "/" + this_name ) {
134  // make instance counts
135  GaudiToolLocal::s_InstanceCounter.increment( m_local );
136 }
137 // ============================================================================
138 // destructor
139 // ============================================================================
140 GaudiTool::~GaudiTool() { GaudiToolLocal::s_InstanceCounter.decrement( m_local ); }
141 // ============================================================================
142 // standard initialization method
143 // ============================================================================
145  // initialize the base class
147  if ( sc.isFailure() ) { return sc; }
148 
149  // increment the counter
150  GaudiToolLocal::s_FinalizeCounter.increment( m_local );
151 
152  // are we a public tool ?
153  m_isPublic = isPublic();
154 
155  // return
156  return sc;
157 }
158 // ============================================================================
159 // standard finalization method
160 // ============================================================================
162  if ( msgLevel( MSG::DEBUG ) ) debug() << " ==> Finalize the base class GaudiTool " << endmsg;
163 
164  // clear "explicit services"
165  m_detSvc.reset();
166  m_chronoSvc.reset();
167  m_incSvc.reset();
168  m_histoSvc.reset();
169 
170  // finalize the base class
172  if ( sc.isFailure() ) { return sc; }
173 
174  // Decrement the counter
175  GaudiToolLocal::s_FinalizeCounter.decrement( m_local );
176 
177  // return
178  return sc;
179 }
180 // ============================================================================
181 // Determines if this tool is public or not (i.e. owned by the ToolSvc).
182 // ============================================================================
183 bool GaudiTool::isPublic() const {
184  const IAlgTool* tool = this;
185  // Recurse down the ownership tree, to see with we ever end up at the ToolSvc
186  bool ownedByToolSvc = false;
187  unsigned int sanityCheck( 0 );
188  while ( tool && ++sanityCheck < 99999 ) {
189  ownedByToolSvc = ( nullptr != dynamic_cast<const IToolSvc*>( tool->parent() ) );
190  if ( ownedByToolSvc ) { break; }
191  // if parent is also a tool, try again
192  tool = dynamic_cast<const IAlgTool*>( tool->parent() );
193  }
194  return ownedByToolSvc;
195 }
196 // ============================================================================
197 // accessor to detector service
198 // ============================================================================
200  if ( UNLIKELY( !m_detSvc ) ) m_detSvc = service( GaudiToolServices::s_DetectorDataSvc, true );
201  return m_detSvc;
202 }
203 // ============================================================================
204 // The standard N-Tuple
205 // ============================================================================
207  if ( UNLIKELY( !m_ntupleSvc ) ) m_ntupleSvc = service( "NTupleSvc", true );
208  return m_ntupleSvc;
209 }
210 // ============================================================================
211 // The standard event collection service
212 // ============================================================================
214  if ( UNLIKELY( !m_evtColSvc ) ) m_evtColSvc = service( "EvtTupleSvc", true );
215  return m_evtColSvc;
216 }
217 // ============================================================================
218 // accessor to Incident Service
219 // ============================================================================
221  if ( UNLIKELY( !m_incSvc ) ) m_incSvc = service( GaudiToolServices::s_IncidentSvc, true );
222  return m_incSvc;
223 }
224 // ============================================================================
225 // accessor to Chrono & Stat Service
226 // ============================================================================
229  return m_chronoSvc;
230 }
231 // ============================================================================
232 // accessor to histogram Service
233 // ============================================================================
235  if ( UNLIKELY( !m_histoSvc ) ) m_histoSvc = service( GaudiToolServices::s_HistoSvc, true );
236  return m_histoSvc;
237 }
238 // ============================================================================
239 // accessor to Algorithm Context Service
240 // ============================================================================
242  if ( UNLIKELY( !m_contextSvc ) ) m_contextSvc = service( m_contextSvcName, true );
243  return m_contextSvc;
244 }
245 // ============================================================================
246 // The END
247 // ============================================================================
IChronoStatSvc * chronoSvc() const
accessor to Chrono & Stat Service
Definition: GaudiTool.cpp:227
IIncidentSvc * incSvc() const
accessor to Incident Service
Definition: GaudiTool.cpp:220
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
#define UNLIKELY(x)
Definition: Kernel.h:89
IAlgContextSvc * contextSvc() const
acessor to the Algorithm Context Service
Definition: GaudiTool.cpp:241
Header file for class GaudiAlgorithm.
bool m_isPublic
Flag to say if the tool is a public or private tool.
Definition: GaudiTool.h:774
SmartIF< INTupleSvc > m_ntupleSvc
pointer to the N-Tuple service
Definition: GaudiTool.h:754
long counts(const std::string &object)
current count
Definition: GaudiTool.cpp:73
void report() const
make a report
Definition: GaudiTool.cpp:75
IHistogramSvc * histoSvc() const
acessor to the histogram service
Definition: GaudiTool.cpp:234
Counter(std::string msg=" Misbalance ")
Definition: GaudiTool.cpp:62
Header file for class GaudiAlgorithm.
T endl(T...args)
const std::string s_ChronoStatSvc
the default name for Chrono & Stat Service
Definition: GaudiTool.cpp:46
SmartIF< IAlgContextSvc > m_contextSvc
Algorithm Context Service.
Definition: GaudiTool.h:766
STL namespace.
static bool enableSummary(bool)
enable/disable summary
Definition: GaudiTool.cpp:117
SmartIF< IIncidentSvc > m_incSvc
pointer to Incident Service
Definition: GaudiTool.h:762
Data provider interface definition.
INTupleSvc * ntupleSvc() const
Access the standard N-Tuple.
Definition: GaudiTool.cpp:206
bool isFailure() const
Definition: StatusCode.h:130
const std::string s_IncidentSvc
the default name for Incident Service
Definition: GaudiTool.cpp:48
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:213
const std::string s_HistoSvc
the default name for Histogram Service
Definition: GaudiTool.cpp:50
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
Definition of the basic interface.
Definition: IInterface.h:244
GaudiTool()=delete
no default/copy constructor, no assignment
~GaudiTool() override
destructor, virtual and protected
Definition: GaudiTool.cpp:140
Definition of the IHistogramSvc interface class.
Definition: IHistogramSvc.h:46
bool isPublic() const
Determines if this tool is public or not (i.e. owned by the ToolSvc).
Definition: GaudiTool.cpp:183
long increment(const std::string &object)
make the increment
Definition: GaudiTool.cpp:69
const std::string s_DetectorDataSvc
the default name for Detector Data Service
Definition: GaudiTool.cpp:44
static bool s_enableSummary
enable printout of summary?
Definition: GaudiTool.h:784
SmartIF< IChronoStatSvc > m_chronoSvc
pointer to Chrono & Stat Service
Definition: GaudiTool.h:760
SmartIF< IHistogramSvc > m_histoSvc
pointer for histogram service
Definition: GaudiTool.h:764
simple local counter
Definition: GaudiTool.cpp:58
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:47
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
Gaudi::Property< std::string > m_contextSvcName
Definition: GaudiTool.h:768
StatusCode finalize() override
standard finalization method
Definition: GaudiTool.cpp:161
SmartIF< IDataProviderSvc > m_detSvc
pointer to Detector Data Service
Definition: GaudiTool.h:758
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:92
const std::string s_EventDataSvc
the default name for Event Data Service
Definition: GaudiTool.cpp:42
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:86
const std::string m_local
full tool name "type/name"
Definition: GaudiTool.h:779
SmartIF< INTupleSvc > m_evtColSvc
pointer to the event tag collection service
Definition: GaudiTool.h:756
std::map< std::string, long > Map
Definition: GaudiTool.cpp:89
long decrement(const std::string &object)
make the decrement
Definition: GaudiTool.cpp:71
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:125
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:23
StatusCode initialize() override
standard initialization method
Definition: GaudiTool.cpp:144
IDataProviderSvc * detSvc() const
accessor to detector service
Definition: GaudiTool.cpp:199