| The Gaudi Framework v32r1 (f65d50dc)

GaudiTool.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Include files
3 // ============================================================================
4 // GaudiKernel
5 // ============================================================================
6 #include "GaudiKernel/Bootstrap.h"
7 #include "GaudiKernel/IAlgContextSvc.h"
8 #include "GaudiKernel/IChronoStatSvc.h"
9 #include "GaudiKernel/IDataProviderSvc.h"
10 #include "GaudiKernel/IHistogramSvc.h"
11 #include "GaudiKernel/IIncidentSvc.h"
12 #include "GaudiKernel/INTupleSvc.h"
13 // ============================================================================
14 // GaudiAlg
15 // ============================================================================
16 #include "GaudiAlg/GaudiAlgorithm.h"
17 #include "GaudiAlg/GaudiTool.h"
18 // ============================================================================
26 // ============================================================================
27 // templated methods
28 // ============================================================================
29 #include "GaudiCommon.icpp"
30 // ============================================================================
31 template class GaudiCommon<CounterHolder<AlgTool>>;
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 // ========================================================================
89 typedef std::map<std::string, long> Map;
90 Map m_map;
91 std::string m_message;
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 // ============================================================================
144 StatusCode GaudiTool::initialize() {
145 // initialize the base class
146 const StatusCode sc = GaudiCommon<CounterHolder<AlgTool>>::initialize();
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 // ============================================================================
161 StatusCode GaudiTool::finalize() {
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
171 const StatusCode sc = GaudiCommon<CounterHolder<AlgTool>>::finalize();
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 // ============================================================================
199 IDataProviderSvc* GaudiTool::detSvc() const {
200 if ( UNLIKELY( !m_detSvc ) ) m_detSvc = service( GaudiToolServices::s_DetectorDataSvc, true );
201 return m_detSvc;
202 }
203 // ============================================================================
204 // The standard N-Tuple
205 // ============================================================================
206 INTupleSvc* GaudiTool::ntupleSvc() const {
207 if ( UNLIKELY( !m_ntupleSvc ) ) m_ntupleSvc = service( "NTupleSvc", true );
208 return m_ntupleSvc;
209 }
210 // ============================================================================
211 // The standard event collection service
212 // ============================================================================
213 INTupleSvc* GaudiTool::evtColSvc() const {
214 if ( UNLIKELY( !m_evtColSvc ) ) m_evtColSvc = service( "EvtTupleSvc", true );
215 return m_evtColSvc;
216 }
217 // ============================================================================
218 // accessor to Incident Service
219 // ============================================================================
220 IIncidentSvc* GaudiTool::incSvc() const {
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 // ============================================================================
227 IChronoStatSvc* GaudiTool::chronoSvc() const {
228 if ( UNLIKELY( !m_chronoSvc ) ) m_chronoSvc = service( GaudiToolServices::s_ChronoStatSvc, true );
229 return m_chronoSvc;
230 }
231 // ============================================================================
232 // accessor to histogram Service
233 // ============================================================================
234 IHistogramSvc* GaudiTool::histoSvc() const {
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 // ============================================================================
241 IAlgContextSvc* GaudiTool::contextSvc() const {
242 if ( UNLIKELY( !m_contextSvc ) ) m_contextSvc = service( m_contextSvcName, true );
243 return m_contextSvc;
244 }
245 // ============================================================================
246 // The END
247 // ============================================================================
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:96
GaudiTool.h
Header file for class GaudiAlgorithm.
GaudiCommon< CounterHolder< AlgTool > >::tool
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.
Definition: GaudiCommonImp.h:81
GaudiTool::m_isPublic
bool m_isPublic
Flag to say if the tool is a public or private tool.
Definition: GaudiTool.h:771
GaudiTool::m_ntupleSvc
SmartIF< INTupleSvc > m_ntupleSvc
pointer to the N-Tuple service
Definition: GaudiTool.h:751
GaudiToolLocal::Counter::counts
long counts(const std::string &object)
current count
Definition: GaudiTool.cpp:73
GaudiToolLocal::Counter::Counter
Counter(std::string msg=" Misbalance ")
Definition: GaudiTool.cpp:62
GaudiAlgorithm.h
Header file for class GaudiAlgorithm.
MSG::DEBUG
Definition: IMessageSvc.h:15
std::endl
T endl(T... args)
GaudiToolServices::s_ChronoStatSvc
const std::string s_ChronoStatSvc
the default name for Chrono & Stat Service
Definition: GaudiTool.cpp:46
Bootstrap.h
GaudiTool::m_contextSvc
SmartIF< IAlgContextSvc > m_contextSvc
Algorithm Context Service.
Definition: GaudiTool.h:763
std
STL namespace.
GaudiTool::enableSummary
static bool enableSummary(bool)
enable/disable summary
Definition: GaudiTool.cpp:117
GaudiTool::m_incSvc
SmartIF< IIncidentSvc > m_incSvc
pointer to Incident Service
Definition: GaudiTool.h:759
IDataProviderSvc
Data provider interface definition.
Definition: IDataProviderSvc.h:43
std::map< std::string, long >
GaudiToolServices::s_IncidentSvc
const std::string s_IncidentSvc
the default name for Incident Service
Definition: GaudiTool.cpp:48
GaudiTool::chronoSvc
IChronoStatSvc * chronoSvc() const
accessor to Chrono & Stat Service
Definition: GaudiTool.cpp:227
GaudiToolServices
Collection of default services names to be used for class GaudiTool.
std::string
STL class.
INTupleSvc
Definition: INTupleSvc.h:36
IChronoStatSvc
The IChronoStatSvc is the interface implemented by the ChronoStatService.
Definition: IChronoStatSvc.h:33
GaudiToolLocal::Counter::m_map
Map m_map
Definition: GaudiTool.cpp:90
GaudiToolLocal::Counter::m_message
std::string m_message
Definition: GaudiTool.cpp:91
IDataProviderSvc.h
GaudiToolServices::s_HistoSvc
const std::string s_HistoSvc
the default name for Histogram Service
Definition: GaudiTool.cpp:50
IIncidentSvc.h
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
GaudiTool::detSvc
IDataProviderSvc * detSvc() const
accessor to detector service
Definition: GaudiTool.cpp:199
IInterface
Definition of the basic interface.
Definition: IInterface.h:244
CounterHolder
Definition: CounterHolder.h:16
GaudiTool::GaudiTool
GaudiTool()=delete
no default/copy constructor, no assignment
GaudiTool::~GaudiTool
~GaudiTool() override
destructor, virtual and protected
Definition: GaudiTool.cpp:140
IHistogramSvc
Definition of the IHistogramSvc interface class.
Definition: IHistogramSvc.h:46
GaudiToolLocal::Counter::increment
long increment(const std::string &object)
make the increment
Definition: GaudiTool.cpp:69
GaudiToolServices::s_DetectorDataSvc
const std::string s_DetectorDataSvc
the default name for Detector Data Service
Definition: GaudiTool.cpp:44
GaudiCommon.icpp
GaudiTool::evtColSvc
INTupleSvc * evtColSvc() const
Access the standard event collection service.
Definition: GaudiTool.cpp:213
IAlgContextSvc.h
GaudiTool::s_enableSummary
static bool s_enableSummary
enable printout of summary?
Definition: GaudiTool.h:781
GaudiTool::m_chronoSvc
SmartIF< IChronoStatSvc > m_chronoSvc
pointer to Chrono & Stat Service
Definition: GaudiTool.h:757
GaudiTool::ntupleSvc
INTupleSvc * ntupleSvc() const
Access the standard N-Tuple.
Definition: GaudiTool.cpp:206
GaudiTool::m_histoSvc
SmartIF< IHistogramSvc > m_histoSvc
pointer for histogram service
Definition: GaudiTool.h:761
GaudiToolLocal::Counter
simple local counter
Definition: GaudiTool.cpp:58
AlgTool
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:47
IAlgTool
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
GaudiTool::m_contextSvcName
Gaudi::Property< std::string > m_contextSvcName
Definition: GaudiTool.h:765
GaudiTool::finalize
StatusCode finalize() override
standard finalization method
Definition: GaudiTool.cpp:161
GaudiToolLocal
Definition: GaudiTool.cpp:53
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:8
GaudiTool::m_detSvc
SmartIF< IDataProviderSvc > m_detSvc
pointer to Detector Data Service
Definition: GaudiTool.h:755
GaudiTool::contextSvc
IAlgContextSvc * contextSvc() const
acessor to the Algorithm Context Service
Definition: GaudiTool.cpp:241
GaudiCommon
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:91
GaudiToolServices::s_EventDataSvc
const std::string s_EventDataSvc
the default name for Event Data Service
Definition: GaudiTool.cpp:42
IAlgContextSvc
An abstract interface for Algorithm Context Service.
Definition: IAlgContextSvc.h:23
IChronoStatSvc.h
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:86
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
GaudiTool::m_local
const std::string m_local
full tool name "type/name"
Definition: GaudiTool.h:776
GaudiTool::m_evtColSvc
SmartIF< INTupleSvc > m_evtColSvc
pointer to the event tag collection service
Definition: GaudiTool.h:753
GaudiToolLocal::Counter::Map
std::map< std::string, long > Map
Definition: GaudiTool.cpp:89
GaudiTool::histoSvc
IHistogramSvc * histoSvc() const
acessor to the histogram service
Definition: GaudiTool.cpp:234
GaudiToolLocal::Counter::decrement
long decrement(const std::string &object)
make the decrement
Definition: GaudiTool.cpp:71
GaudiToolLocal::Counter::report
void report() const
make a report
Definition: GaudiTool.cpp:75
std::cout
GaudiTool::summaryEnabled
static bool summaryEnabled()
is summary enabled?
Definition: GaudiTool.cpp:125
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
IIncidentSvc
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:23
GaudiTool::initialize
StatusCode initialize() override
standard initialization method
Definition: GaudiTool.cpp:144
GaudiTool::incSvc
IIncidentSvc * incSvc() const
accessor to Incident Service
Definition: GaudiTool.cpp:220
GaudiTool::isPublic
bool isPublic() const
Determines if this tool is public or not (i.e. owned by the ToolSvc).
Definition: GaudiTool.cpp:183
GaudiToolLocal::Counter::~Counter
~Counter()
Definition: GaudiTool.cpp:64
INTupleSvc.h
IHistogramSvc.h