The Gaudi Framework  v36r1 (3e2fb5a8)
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 // ============================================================================
41 template class GaudiCommon<AlgTool>;
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( std::string_view object ) { return ++get( object ); }
81  long decrement( std::string_view object ) { return --get( object ); }
83  long counts( std::string_view object ) { return get( 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  // ========================================================================
100  long& get( std::string_view which ) {
101  auto i = m_map.find( which );
102  if ( i == m_map.end() ) i = m_map.emplace( which, long{} ).first;
103  return i->second;
104  }
107  // ========================================================================
108  };
109  // ==========================================================================
115  static Counter s_InstanceCounter( " Create/Destroy (mis)balance " );
116  // ==========================================================================
122  static Counter s_FinalizeCounter( " Initialize/Finalize (mis)balance " );
123  // ==========================================================================
124 } // namespace GaudiToolLocal
125 // ============================================================================
127 // ============================================================================
128 bool GaudiTool::s_enableSummary = true; // summary is enabled
129 // ============================================================================
130 // enable/disable summary
131 // ============================================================================
132 bool GaudiTool::enableSummary( bool value ) // enable/disable summary
133 {
134  s_enableSummary = value;
135  return summaryEnabled();
136 }
137 // ============================================================================
138 // is summary enabled?
139 // ============================================================================
140 bool GaudiTool::summaryEnabled() // is summary enabled?
141 {
142  return s_enableSummary;
143 }
144 // ============================================================================
145 // Standard constructor
146 // ============================================================================
147 GaudiTool::GaudiTool( std::string this_type, std::string this_name, const IInterface* parent )
148  : GaudiCommon<AlgTool>( std::move( this_type ), std::move( this_name ), parent ), m_local( type() + "/" + name() ) {
149  // make instance counts
150  GaudiToolLocal::s_InstanceCounter.increment( m_local );
151 }
152 // ============================================================================
153 // destructor
154 // ============================================================================
155 GaudiTool::~GaudiTool() { GaudiToolLocal::s_InstanceCounter.decrement( m_local ); }
156 // ============================================================================
157 // standard initialization method
158 // ============================================================================
160  // initialize the base class
162  if ( sc.isFailure() ) { return sc; }
163 
164  // increment the counter
165  GaudiToolLocal::s_FinalizeCounter.increment( m_local );
166 
167  // are we a public tool ?
168  m_isPublic = isPublic();
169 
170  // return
171  return sc;
172 }
173 // ============================================================================
174 // standard finalization method
175 // ============================================================================
177  if ( msgLevel( MSG::DEBUG ) ) debug() << " ==> Finalize the base class GaudiTool " << endmsg;
178 
179  // clear "explicit services"
180  m_detSvc.reset();
181  m_chronoSvc.reset();
182  m_incSvc.reset();
183  m_histoSvc.reset();
184 
185  // finalize the base class
187  if ( sc.isFailure() ) { return sc; }
188 
189  // Decrement the counter
190  GaudiToolLocal::s_FinalizeCounter.decrement( m_local );
191 
192  // return
193  return sc;
194 }
195 // ============================================================================
196 // Determines if this tool is public or not (i.e. owned by the ToolSvc).
197 // ============================================================================
198 bool GaudiTool::isPublic() const {
199  const IAlgTool* tool = this;
200  // Recurse down the ownership tree, to see with we ever end up at the ToolSvc
201  bool ownedByToolSvc = false;
202  unsigned int sanityCheck( 0 );
203  while ( tool && ++sanityCheck < 99999 ) {
204  ownedByToolSvc = ( nullptr != dynamic_cast<const IToolSvc*>( tool->parent() ) );
205  if ( ownedByToolSvc ) { break; }
206  // if parent is also a tool, try again
207  tool = dynamic_cast<const IAlgTool*>( tool->parent() );
208  }
209  return ownedByToolSvc;
210 }
211 // ============================================================================
212 // accessor to detector service
213 // ============================================================================
216  return m_detSvc;
217 }
218 // ============================================================================
219 // The standard N-Tuple
220 // ============================================================================
222  if ( UNLIKELY( !m_ntupleSvc ) ) m_ntupleSvc = service( "NTupleSvc", true );
223  return m_ntupleSvc;
224 }
225 // ============================================================================
226 // The standard event collection service
227 // ============================================================================
229  if ( UNLIKELY( !m_evtColSvc ) ) m_evtColSvc = service( "EvtTupleSvc", true );
230  return m_evtColSvc;
231 }
232 // ============================================================================
233 // accessor to Incident Service
234 // ============================================================================
237  return m_incSvc;
238 }
239 // ============================================================================
240 // accessor to Chrono & Stat Service
241 // ============================================================================
244  return m_chronoSvc;
245 }
246 // ============================================================================
247 // accessor to histogram Service
248 // ============================================================================
251  return m_histoSvc;
252 }
253 // ============================================================================
254 // accessor to Algorithm Context Service
255 // ============================================================================
258  return m_contextSvc;
259 }
260 // ============================================================================
261 // The END
262 // ============================================================================
GaudiTool::m_histoSvc
SmartIF< IHistogramSvc > m_histoSvc
pointer for histogram service
Definition: GaudiTool.h:769
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
GaudiTool::m_evtColSvc
SmartIF< INTupleSvc > m_evtColSvc
pointer to the event tag collection service
Definition: GaudiTool.h:761
GaudiTool.h
GaudiToolLocal::Counter::Counter
Counter(std::string msg=" Misbalance ")
Definition: GaudiTool.cpp:72
GaudiTool::m_detSvc
SmartIF< IDataProviderSvc > m_detSvc
pointer to Detector Data Service
Definition: GaudiTool.h:763
GaudiCommon.icpp
std::string
STL class.
IAlgTool
Definition: IAlgTool.h:33
IHistogramSvc
Definition: IHistogramSvc.h:56
GaudiTool::enableSummary
static bool enableSummary(bool)
enable/disable summary
Definition: GaudiTool.cpp:132
GaudiTool::m_local
const std::string m_local
full tool name "type/name"
Definition: GaudiTool.h:784
GaudiToolServices::s_HistoSvc
const std::string s_HistoSvc
the default name for Histogram Service
Definition: GaudiTool.cpp:60
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
GaudiToolLocal::Counter::increment
long increment(std::string_view object)
make the increment
Definition: GaudiTool.cpp:79
GaudiTool::summaryEnabled
static bool summaryEnabled()
is summary enabled?
Definition: GaudiTool.cpp:140
GaudiTool::initialize
StatusCode initialize() override
standard initialization method
Definition: GaudiTool.cpp:159
GaudiToolServices::s_DetectorDataSvc
const std::string s_DetectorDataSvc
the default name for Detector Data Service
Definition: GaudiTool.cpp:54
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:18
GaudiCommon< AlgTool >::tool
TOOL * tool(std::string_view type, std::string_view name, const IInterface *parent=0, bool create=true) const
Useful method for the easy location of tools.
Definition: GaudiCommonImp.h:88
GaudiToolLocal::Counter::m_message
std::string m_message
Definition: GaudiTool.cpp:106
Counter
Definition: Counter.py:1
CommonMessaging< implements< IAlgTool, IDataHandleHolder, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
GaudiToolServices::s_IncidentSvc
const std::string s_IncidentSvc
the default name for Incident Service
Definition: GaudiTool.cpp:58
IDataProviderSvc.h
IIncidentSvc.h
GaudiTool::GaudiTool
GaudiTool(std::string type, std::string name, const IInterface *parent)
Standard constructor.
Definition: GaudiTool.cpp:147
Gaudi::Functional::details::get
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
Definition: FunctionalDetails.h:391
TimingHistograms.name
name
Definition: TimingHistograms.py:23
StatusCode
Definition: StatusCode.h:65
GaudiTool::m_chronoSvc
SmartIF< IChronoStatSvc > m_chronoSvc
pointer to Chrono & Stat Service
Definition: GaudiTool.h:765
std::cout
GaudiTool::evtColSvc
INTupleSvc * evtColSvc() const
Access the standard event collection service.
Definition: GaudiTool.cpp:228
GaudiToolLocal::Counter::counts
long counts(std::string_view object)
current count
Definition: GaudiTool.cpp:83
GaudiTesting.BaseTest.which
def which(executable)
Definition: BaseTest.py:674
IChronoStatSvc
Definition: IChronoStatSvc.h:43
GaudiTool::finalize
StatusCode finalize() override
standard finalization method
Definition: GaudiTool.cpp:176
GaudiTool::s_enableSummary
static bool s_enableSummary
enable printout of summary?
Definition: GaudiTool.h:789
GaudiToolLocal::Counter::m_map
Map m_map
Definition: GaudiTool.cpp:105
GaudiTool::m_incSvc
SmartIF< IIncidentSvc > m_incSvc
pointer to Incident Service
Definition: GaudiTool.h:767
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
std::map< std::string, long, std::less<> >
GaudiTool::histoSvc
IHistogramSvc * histoSvc() const
acessor to the histogram service
Definition: GaudiTool.cpp:249
GaudiTool::chronoSvc
IChronoStatSvc * chronoSvc() const
accessor to Chrono & Stat Service
Definition: GaudiTool.cpp:242
GaudiToolServices::s_ChronoStatSvc
const std::string s_ChronoStatSvc
the default name for Chrono & Stat Service
Definition: GaudiTool.cpp:56
GaudiToolServices
GaudiTool::detSvc
IDataProviderSvc * detSvc() const
accessor to detector service
Definition: GaudiTool.cpp:214
GaudiCommon::initialize
StatusCode initialize() override
standard initialization method
Definition: GaudiCommon.icpp:76
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:142
GaudiToolLocal::Counter::get
long & get(std::string_view which)
Definition: GaudiTool.cpp:100
gaudirun.type
type
Definition: gaudirun.py:154
GaudiTool::incSvc
IIncidentSvc * incSvc() const
accessor to Incident Service
Definition: GaudiTool.cpp:235
GaudiCommon::finalize
StatusCode finalize() override
standard finalization method
Definition: GaudiCommon.icpp:112
std::endl
T endl(T... args)
IAlgContextSvc.h
AlgTool::service
StatusCode service(std::string_view name, T *&svc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: AlgTool.h:143
AlgTool
Definition: AlgTool.h:62
IChronoStatSvc.h
GaudiAlgorithm.h
std
STL namespace.
IInterface
Definition: IInterface.h:237
IAlgContextSvc
Definition: IAlgContextSvc.h:33
Bootstrap.h
GaudiToolServices::s_EventDataSvc
const std::string s_EventDataSvc
the default name for Event Data Service
Definition: GaudiTool.cpp:52
GaudiTool::isPublic
bool isPublic() const
Determines if this tool is public or not (i.e. owned by the ToolSvc).
Definition: GaudiTool.cpp:198
GaudiTool::~GaudiTool
~GaudiTool() override
destructor, virtual and protected
Definition: GaudiTool.cpp:155
GaudiTool::m_isPublic
bool m_isPublic
Flag to say if the tool is a public or private tool.
Definition: GaudiTool.h:779
GaudiToolLocal
Definition: GaudiTool.cpp:63
GaudiTool::m_ntupleSvc
SmartIF< INTupleSvc > m_ntupleSvc
pointer to the N-Tuple service
Definition: GaudiTool.h:759
IDataProviderSvc
Definition: IDataProviderSvc.h:53
INTupleSvc
Definition: INTupleSvc.h:46
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:106
GaudiTool::ntupleSvc
INTupleSvc * ntupleSvc() const
Access the standard N-Tuple.
Definition: GaudiTool.cpp:221
IIncidentSvc
Definition: IIncidentSvc.h:33
IToolSvc
Definition: IToolSvc.h:29
GaudiTool::m_contextSvcName
Gaudi::Property< std::string > m_contextSvcName
Definition: GaudiTool.h:773
GaudiToolLocal::Counter::~Counter
~Counter()
Definition: GaudiTool.cpp:74
GaudiTool::contextSvc
IAlgContextSvc * contextSvc() const
acessor to the Algorithm Context Service
Definition: GaudiTool.cpp:256
IHistogramSvc.h
GaudiToolLocal::Counter::report
void report() const
make a report
Definition: GaudiTool.cpp:85
INTupleSvc.h
GaudiToolLocal::Counter::decrement
long decrement(std::string_view object)
make the decrement
Definition: GaudiTool.cpp:81
GaudiTool::m_contextSvc
SmartIF< IAlgContextSvc > m_contextSvc
Algorithm Context Service.
Definition: GaudiTool.h:771
GaudiCommon< AlgTool >