AlgExecStateSvc.cpp
Go to the documentation of this file.
1 #include "AlgExecStateSvc.h"
6 
8 
10 
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 //-----------------------------------------------------------------------------
21 
23 
24 }
25 
26 //-----------------------------------------------------------------------------
27 
28 void
30 
31  std::string wbn;
32 
33  // seriously? do we have no way of getting a Service by type???
35  for (auto &is : lst) {
36  IHiveWhiteBoard *iwb = dynamic_cast<IHiveWhiteBoard*>(is);
37  if (iwb != 0) {
38  wbn = is->name();
40  verbose() << "HiveWhiteBoard service name is " << wbn << endmsg;
41  break;
42  }
43  }
44 
46  wbs = serviceLocator()->service(wbn,false);
47 
48  if (wbs.isValid()) {
51  } else {
54  }
55 
56  if (msgLevel(MSG::DEBUG))
57  debug() << "resizing state containers to : " << m_algStates.size()
58  << endmsg;
59 
61  if (!algMan.isValid()) {
62  fatal() << "could not get the AlgManager" << endmsg;
63  throw GaudiException("In AlgExecStateSvc, unable to get the AlgManager!",
64  "AlgExecStateSvc",StatusCode::FAILURE);
65  }
66 
67  m_isInit = true;
68 
69  auto algos = algMan->getAlgorithms();
70  for (auto &alg : algos) {
71  addAlg( alg );
72  }
73 
74  for (auto &alg : m_preInitAlgs) {
75  addAlg( alg );
76  }
77 
78 
79  if (msgLevel( MSG::VERBOSE )) {
81  dump(ost);
82  verbose() << "dumping state:\n" << ost.str() << endmsg;
83  }
84 
85 }
86 
87 //-----------------------------------------------------------------------------
88 
89 void
91 
92  if (!m_isInit) {
93  fatal() << "AlgExecStateSvc not initialized before first use" << endmsg;
94  throw GaudiException("AlgExecStateSvc not initialized before first use!",
95  "AlgExecStateSvc",StatusCode::FAILURE);
96  }
97 }
98 
99 //-----------------------------------------------------------------------------
100 
103 
104  // Initialise mother class (read properties, ...)
106  if (!sc.isSuccess()) {
107  warning () << "Base class could not be initialized" << endmsg;
108  return StatusCode::FAILURE;
109  }
110 
111  // moved all initialization to init(). hopefully it will get called in time....
112 
113  return StatusCode::SUCCESS;
114 
115 }
116 
117 //-----------------------------------------------------------------------------
118 
121 
122  return StatusCode::SUCCESS;
123 
124 }
125 
126 //-----------------------------------------------------------------------------
127 
128 void
130 
131  checkInit();
132 
133  ost << "Event: " << trans(m_eventStatus.at(0)) << std::endl;
134  ost << "Algs: " << m_algStates.at(0).size() << std::endl;
135 
136  size_t ml(0);
137  for (auto &e : m_algStates.at(0)) {
138  if ( e.first.str().length() > ml ) {
139  ml = e.first.str().length();
140  }
141  }
142 
143  for (size_t is =0; is < m_algStates.size(); ++is) {
144  ost << " - Slot " << is << std::endl;
145  const AlgStateMap_t& m = m_algStates.at(is);
146  for (auto &e : m) {
147  ost << " + " << std::setw(ml) << e.first.str() << " " << e.second
148  << std::endl;
149  }
150  }
151 
152 }
153 
154 //-----------------------------------------------------------------------------
155 
156 void
158 
159  ost << "Event: " << trans(m_eventStatus.at(ctx.slot())) << std::endl;
160  ost << "Algs: " << m_algStates.at(ctx.slot()).size() << std::endl;
161 
162  size_t ml(0);
163  for (auto &e : m_algStates.at(ctx.slot())) {
164  if ( e.first.str().length() > ml ) {
165  ml = e.first.str().length();
166  }
167  }
168 
169  ost << " - Slot " << ctx.slot() << std::endl;
170  const AlgStateMap_t& m = m_algStates.at(ctx.slot());
171  for (auto &e : m) {
172  ost << " + " << std::setw(ml) << e.first.str() << " " << e.second
173  << std::endl;
174  }
175 
176 }
177 
178 //-----------------------------------------------------------------------------
179 
180 void
182  return addAlg(iAlg->nameKey());
183 }
184 
185 //-----------------------------------------------------------------------------
186 
187 void
189 
190 
191  if (!m_isInit) {
192  if (msgLevel(MSG::DEBUG))
193  debug() << "preInit: will add Alg " << alg.str() << " later" << endmsg;
194  m_preInitAlgs.push_back( alg );
195  return;
196  }
197 
198  if (m_algStates.size() > 0) {
199  if (m_algStates.at(0).find(alg) != m_algStates.at(0).end()) {
200  // already added
201  return;
202  }
203  }
204 
205  {
206  // in theory, this should only get called during initialization (serial)
207  // so shouldn't have to protect with a mutex...
209 
210  AlgExecState s;
211  for (size_t i = 0; i<m_algStates.size(); ++i) {
212  m_algStates.at(i)[alg] = s;
213  }
214  }
215 
216  if (msgLevel(MSG::DEBUG))
217  debug() << "adding alg " << alg.str() << " to "
218  << m_algStates.size() << " slots" << endmsg;
219 
220 }
221 
222 //-----------------------------------------------------------------------------
223 
224 const AlgExecState&
226  const EventContext& ctx) const {
227 
228  checkInit();
229 
230  AlgStateMap_t::const_iterator itr = m_algStates.at(ctx.slot()).find( algName );
231 
232  if (UNLIKELY( itr == m_algStates.at(ctx.slot()).end() ) ) {
233  throw GaudiException{std::string{"cannot find Alg "}
234  + algName.str() + " in AlgStateMap", name(), StatusCode::FAILURE};
235  }
236 
237  return itr->second;
238 
239 }
240 
241 //-----------------------------------------------------------------------------
242 
243 const AlgExecState&
245  const EventContext& ctx) const {
246 
247  return algExecState(iAlg->nameKey(), ctx);
248 
249 }
250 
251 //-----------------------------------------------------------------------------
252 
253 AlgExecState&
255  const EventContext& ctx) {
256 
258 
259  AlgStateMap_t::iterator itr = m_algStates.at(ctx.slot()).find( iAlg->nameKey() );
260 
261  if (UNLIKELY( itr == m_algStates.at(ctx.slot()).end() ) ) {
262  throw GaudiException{std::string{"cannot find Alg "}
263  + iAlg->name() + " in AlgStateMap", name(), StatusCode::FAILURE};
264  }
265 
266  return itr->second;
267 
268 }
269 
270 //-----------------------------------------------------------------------------
271 
274 
275  checkInit();
276 
277  return m_algStates.at(ctx.slot());
278 }
279 
280 //-----------------------------------------------------------------------------
281 
282 const AlgExecState&
284 
285  checkInit();
286 
287  AlgStateMap_t::const_iterator itr = m_algStates.at(0).find( algName );
288 
289  if (UNLIKELY( itr == m_algStates.at(0).end() ) ) {
290  throw GaudiException{std::string{"cannot find Alg "}
291  + algName.str() + " in AlgStateMap", name(), StatusCode::FAILURE};
292  }
293 
294  return itr->second;
295 
296 }
297 
298 //-----------------------------------------------------------------------------
299 
300 AlgExecState&
302 
304 
305  AlgStateMap_t::iterator itr = m_algStates.at(0).find( iAlg->nameKey() );
306 
307  if (UNLIKELY( itr == m_algStates.at(0).end() ) ) {
308  throw GaudiException{std::string{"cannot find Alg "}
309  + iAlg->name() + " in AlgStateMap", name(), StatusCode::FAILURE};
310  }
311 
312  return itr->second;
313 
314 }
315 
316 //-----------------------------------------------------------------------------
317 
318 const AlgExecState&
320  checkInit();
321  return algExecState(iAlg->nameKey());
322 }
323 
324 //-----------------------------------------------------------------------------
325 
328 
329  checkInit();
330 
331  return m_algStates.at(0);
332 }
333 
334 //-----------------------------------------------------------------------------
335 
336 const EventStatus::Status&
338  checkInit();
339  return m_eventStatus.at(ctx.slot());
340 }
341 
342 //-----------------------------------------------------------------------------
343 
344 const EventStatus::Status&
346  checkInit();
347  return m_eventStatus.at(0);
348 }
349 
350 //-----------------------------------------------------------------------------
351 
352 void
355  m_eventStatus.at(ctx.slot()) = sc;
356 }
357 
358 //-----------------------------------------------------------------------------
359 
360 void
363  m_eventStatus.at(0) = sc;
364 }
365 
366 //-----------------------------------------------------------------------------
367 
368 void
372  if (fail)
374  } else if (m_eventStatus.at(0) == EventStatus::Invalid) {
375  if (! fail) {
377  } else {
379  }
380  }
381 }
382 
383 //-----------------------------------------------------------------------------
384 
385 void
386 AlgExecStateSvc::updateEventStatus(const bool& fail, const EventContext& ctx) {
388  if (m_eventStatus.at(ctx.slot()) == EventStatus::Success) {
389  if (fail)
391  } else if (m_eventStatus.at(ctx.slot()) == EventStatus::Invalid) {
392  if (! fail) {
394  } else {
396  }
397  }
398 }
399 
400 
401 //-----------------------------------------------------------------------------
402 
403 void
405 
406  if (msgLevel(MSG::DEBUG))
407  verbose() << "reset(" << ctx.slot() << ")" << endmsg;
408 
410  for (auto &e : m_algStates.at(ctx.slot())) {
411  e.second.reset();
412  }
413 
415 
416 }
417 
418 //-----------------------------------------------------------------------------
419 
420 void
422 
423  if (msgLevel(MSG::DEBUG))
424  verbose() << "reset()" << endmsg;
425 
427  for (auto &e : m_algStates.at(0)) {
428  e.second.reset();
429  }
430 
432 
433 }
434 
435 //-----------------------------------------------------------------------------
436 
439  switch (s) {
440  case EventStatus::Invalid :
441  return "Invalid";
442  case EventStatus::Success :
443  return "Success";
444  case EventStatus::AlgFail :
445  return "AlgFail";
446  case EventStatus::AlgStall :
447  return "AlgStall";
448  case EventStatus::Other :
449  return "Other";
450  default :
451  return "Should not happen";
452  }
453 
454 }
void reset() override
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:41
AlgStates_t m_algStates
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
T endl(T...args)
#define UNLIKELY(x)
Definition: Kernel.h:126
STL namespace.
T end(T...args)
std::vector< EventStatus::Status > m_eventStatus
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:25
STL class.
The helper class to represent the efficient "key" for access.
Definition: StringKey.h:35
void updateEventStatus(const bool &b) override
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:78
T push_back(T...args)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
void dump(std::ostringstream &ost) const override
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
constexpr double m
Definition: SystemOfUnits.h:93
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:49
#define DECLARE_SERVICE_FACTORY(x)
Definition: Service.h:242
std::string trans(const EventStatus::Status &sc) const
virtual const Gaudi::StringKey & nameKey() const =0
StringKey rep of name.
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:27
std::mutex m_mut
T find(T...args)
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:62
void setEventStatus(const EventStatus::Status &sc) override
~AlgExecStateSvc()
Destructor.
string s
Definition: gaudirun.py:245
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.
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
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:244
void addAlg(IAlgorithm *iAlg) override
std::vector< Gaudi::StringKey > m_preInitAlgs
const EventStatus::Status & eventStatus() const override
const AlgStateMap_t & algExecStates() const override