The Gaudi Framework  v29r0 (ff2e7097)
AlgExecStateSvc.cpp
Go to the documentation of this file.
1 #include "AlgExecStateSvc.h"
7 
9 
11 
12 //=============================================================================
13 
14 AlgExecStateSvc::AlgExecStateSvc( const std::string& name, ISvcLocator* svcLoc )
15  : base_class( name, svcLoc ), m_isInit( false )
16 {
17 }
18 
19 //-----------------------------------------------------------------------------
20 
22 
23 //-----------------------------------------------------------------------------
24 
26 {
27 
28  std::string wbn;
29 
30  // seriously? do we have no way of getting a Service by type???
32  for ( auto& is : lst ) {
33  IHiveWhiteBoard* iwb = dynamic_cast<IHiveWhiteBoard*>( is );
34  if ( iwb != 0 ) {
35  wbn = is->name();
36  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "HiveWhiteBoard service name is " << wbn << endmsg;
37  break;
38  }
39  }
40 
42  wbs = serviceLocator()->service( wbn, false );
43 
44  if ( wbs.isValid() ) {
47  } else {
48  m_algStates.resize( 1 );
49  m_eventStatus.resize( 1 );
50  }
51 
52  if ( msgLevel( MSG::DEBUG ) ) debug() << "resizing state containers to : " << m_algStates.size() << endmsg;
53 
55  if ( !algMan.isValid() ) {
56  fatal() << "could not get the AlgManager" << endmsg;
57  throw GaudiException( "In AlgExecStateSvc, unable to get the AlgManager!", "AlgExecStateSvc", StatusCode::FAILURE );
58  }
59 
60  m_isInit = true;
61 
62  auto algos = algMan->getAlgorithms();
63  for ( auto& alg : algos ) {
64  addAlg( alg );
65  }
66 
67  for ( auto& alg : m_preInitAlgs ) {
68  addAlg( alg );
69  }
70 
71  if ( msgLevel( MSG::VERBOSE ) ) {
74  verbose() << "dumping state:\n" << ost.str() << endmsg;
75  }
76 }
77 
78 //-----------------------------------------------------------------------------
79 
81 {
82 
83  if ( !m_isInit ) {
84  fatal() << "AlgExecStateSvc not initialized before first use" << endmsg;
85  throw GaudiException( "AlgExecStateSvc not initialized before first use!", "AlgExecStateSvc", StatusCode::FAILURE );
86  }
87 }
88 
89 //-----------------------------------------------------------------------------
90 
92 {
93 
94  // Initialise mother class (read properties, ...)
96  if ( !sc.isSuccess() ) {
97  warning() << "Base class could not be initialized" << endmsg;
98  return StatusCode::FAILURE;
99  }
100 
101  // moved all initialization to init(). hopefully it will get called in time....
102 
103  return StatusCode::SUCCESS;
104 }
105 
106 //-----------------------------------------------------------------------------
107 
109 
110 //-----------------------------------------------------------------------------
111 
113 {
114 
115  ost << "Event: " << trans( m_eventStatus.at( ctx.slot() ) ) << std::endl;
116  ost << "Algs: " << m_algStates.at( ctx.slot() ).size() << std::endl;
117 
118  size_t ml( 0 );
119  for ( auto& e : m_algStates.at( ctx.slot() ) ) {
120  if ( e.first.str().length() > ml ) {
121  ml = e.first.str().length();
122  }
123  }
124 
125  ost << " - Slot " << ctx.slot() << std::endl;
126  const AlgStateMap_t& m = m_algStates.at( ctx.slot() );
127  for ( auto& e : m ) {
128  ost << " + " << std::setw( ml ) << e.first.str() << " " << e.second << std::endl;
129  }
130 }
131 
132 //-----------------------------------------------------------------------------
133 
134 void AlgExecStateSvc::addAlg( IAlgorithm* iAlg ) { return addAlg( iAlg->nameKey() ); }
135 
136 //-----------------------------------------------------------------------------
137 
139 {
140 
141  if ( !m_isInit ) {
142  if ( msgLevel( MSG::DEBUG ) ) debug() << "preInit: will add Alg " << alg.str() << " later" << endmsg;
143  m_preInitAlgs.push_back( alg );
144  return;
145  }
146 
147  if ( m_algStates.size() > 0 ) {
148  if ( m_algStates.at( 0 ).find( alg ) != m_algStates.at( 0 ).end() ) {
149  // already added
150  return;
151  }
152  }
153 
154  {
155  // in theory, this should only get called during initialization (serial)
156  // so shouldn't have to protect with a mutex...
158 
159  AlgExecState s;
160  for ( size_t i = 0; i < m_algStates.size(); ++i ) {
161  m_algStates.at( i )[alg] = s;
162  }
163 
164  m_errorCount[alg] = 0;
165  }
166 
167  if ( msgLevel( MSG::DEBUG ) )
168  debug() << "adding alg " << alg.str() << " to " << m_algStates.size() << " slots" << endmsg;
169 }
170 
171 //-----------------------------------------------------------------------------
172 
174 {
175 
176  checkInit();
177 
178  AlgStateMap_t::const_iterator itr = m_algStates.at( ctx.slot() ).find( algName );
179 
180  if ( UNLIKELY( itr == m_algStates.at( ctx.slot() ).end() ) ) {
181  throw GaudiException{std::string{"cannot find Alg "} + algName.str() + " in AlgStateMap", name(),
183  }
184 
185  return itr->second;
186 }
187 
188 //-----------------------------------------------------------------------------
189 
191 {
192 
193  return algExecState( iAlg->nameKey(), ctx );
194 }
195 
196 //-----------------------------------------------------------------------------
197 
199 {
200 
202 
203  AlgStateMap_t::iterator itr = m_algStates.at( ctx.slot() ).find( iAlg->nameKey() );
204 
205  if ( UNLIKELY( itr == m_algStates.at( ctx.slot() ).end() ) ) {
206  throw GaudiException{std::string{"cannot find Alg "} + iAlg->name() + " in AlgStateMap", name(),
208  }
209 
210  return itr->second;
211 }
212 
213 //-----------------------------------------------------------------------------
214 
216 {
217 
218  checkInit();
219 
220  return m_algStates.at( ctx.slot() );
221 }
222 
223 //-----------------------------------------------------------------------------
224 
226 {
227  checkInit();
228  return m_eventStatus.at( ctx.slot() );
229 }
230 
231 //-----------------------------------------------------------------------------
232 
234 {
236  m_eventStatus.at( ctx.slot() ) = sc;
237 }
238 
239 //-----------------------------------------------------------------------------
240 
241 void AlgExecStateSvc::updateEventStatus( const bool& fail, const EventContext& ctx )
242 {
244  if ( m_eventStatus.at( ctx.slot() ) == EventStatus::Success ) {
245  if ( fail ) m_eventStatus.at( ctx.slot() ) = EventStatus::AlgFail;
246  } else if ( m_eventStatus.at( ctx.slot() ) == EventStatus::Invalid ) {
247  if ( !fail ) {
249  } else {
251  }
252  }
253 }
254 
255 //-----------------------------------------------------------------------------
256 
258 {
259 
260  if ( msgLevel( MSG::DEBUG ) ) verbose() << "reset(" << ctx.slot() << ")" << endmsg;
261 
263  for ( auto& e : m_algStates.at( ctx.slot() ) ) {
264  e.second.reset();
265  }
266 
268 }
269 
270 //-----------------------------------------------------------------------------
271 
272 unsigned int AlgExecStateSvc::algErrorCount( const IAlgorithm* iAlg ) const
273 {
274  AlgErrorMap_t::const_iterator itr = m_errorCount.find( iAlg->nameKey() );
275 
276  if ( itr != m_errorCount.end() ) {
277  return itr->second;
278  } else {
279  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
280  << " of ErrorCounts" << endmsg;
281  return 0;
282  }
283 }
284 
285 //-----------------------------------------------------------------------------
286 
288 {
289  AlgErrorMap_t::iterator itr = m_errorCount.find( iAlg->nameKey() );
290  if ( itr != m_errorCount.end() ) {
291  itr->second = 0;
292  } else {
293  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
294  << " of ErrorCounts" << endmsg;
295  }
296 }
297 
298 //-----------------------------------------------------------------------------
299 
301 {
302  AlgErrorMap_t::iterator itr = m_errorCount.find( iAlg->nameKey() );
303  if ( itr != m_errorCount.end() ) {
304  return ( ++( itr->second ) );
305  } else {
306  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
307  << " of ErrorCounts" << endmsg;
308  return 0;
309  }
310 }
311 
312 //-----------------------------------------------------------------------------
313 
315 {
316  switch ( s ) {
318  return "Invalid";
320  return "Success";
322  return "AlgFail";
324  return "AlgStall";
325  case EventStatus::Other:
326  return "Other";
327  default:
328  return "Should not happen";
329  }
330 }
void resetErrorCount(const IAlgorithm *iAlg) override
#define UNLIKELY(x)
Definition: Kernel.h:128
StatusCode initialize() override
Definition: Service.cpp:64
A service that keeps track of the execution state of Algorithm *.
Define general base for Gaudi exception.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
virtual StatusCode finalize() override final
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:289
const AlgExecState & algExecState(const Gaudi::StringKey &algName, const EventContext &ctx) const override
ContextID_t slot() const
Definition: EventContext.h:40
AlgStates_t m_algStates
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
T endl(T...args)
void dump(std::ostringstream &ost, const EventContext &ctx) const override
STL namespace.
T end(T...args)
void updateEventStatus(const bool &b, const EventContext &ctx) override
std::once_flag m_initFlag
T call_once(T...args)
This class represents an entry point to all the event specific data.
Definition: EventContext.h:24
STL class.
The helper class to represent the efficient "key" for access.
Definition: StringKey.h:35
T setw(T...args)
T resize(T...args)
STL class.
void checkInit() const
T at(T...args)
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:79
T push_back(T...args)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
constexpr double m
Definition: SystemOfUnits.h:94
AlgErrorMap_t m_errorCount
GAUDI_API const EventContext & currentContext()
unsigned int incrementErrorCount(const IAlgorithm *iAlg) override
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
#define DECLARE_SERVICE_FACTORY(x)
Definition: Service.h:211
std::vector< Gaudi::StringKey > m_preInitAlgs
std::string trans(const EventStatus::Status &sc) const
virtual const Gaudi::StringKey & nameKey() const =0
StringKey rep of name.
unsigned int algErrorCount(const IAlgorithm *iAlg) const override
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:28
const AlgStateMap_t & algExecStates(const EventContext &ctx) const override
std::mutex m_mut
T find(T...args)
void reset(const EventContext &ctx) override
T size(T...args)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:68
const EventStatus::Status & eventStatus(const EventContext &ctx) const override
~AlgExecStateSvc()
Destructor.
string s
Definition: gaudirun.py:253
virtual const std::list< IService * > & getServices() const =0
Get a reference to a service and create it if it does not exists.
virtual StatusCode initialize() override final
virtual const std::vector< IAlgorithm * > & getAlgorithms() const =0
Return the list of Algorithms.
std::vector< EventStatus::Status > m_eventStatus
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
virtual size_t getNumberOfStores() const =0
Get the number of &#39;slots&#39;.
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
void setEventStatus(const EventStatus::Status &sc, const EventContext &ctx) override
const std::string & str() const
the actual string
Definition: StringKey.h:47
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:292
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
void addAlg(IAlgorithm *iAlg) override