AlgExecStateSvc.cpp
Go to the documentation of this file.
1 #include "AlgExecStateSvc.h"
7 
9 
11 
12 //=============================================================================
13 
14 
15 AlgExecStateSvc::AlgExecStateSvc( const std::string& name, ISvcLocator* svcLoc ):
16 base_class(name,svcLoc), m_isInit(false)
17 {
18 
19 }
20 
21 //-----------------------------------------------------------------------------
22 
24 
25 }
26 
27 //-----------------------------------------------------------------------------
28 
29 void
31 
32  std::string wbn;
33 
34  // seriously? do we have no way of getting a Service by type???
36  for (auto &is : lst) {
37  IHiveWhiteBoard *iwb = dynamic_cast<IHiveWhiteBoard*>(is);
38  if (iwb != 0) {
39  wbn = is->name();
41  verbose() << "HiveWhiteBoard service name is " << wbn << endmsg;
42  break;
43  }
44  }
45 
47  wbs = serviceLocator()->service(wbn,false);
48 
49  if (wbs.isValid()) {
52  } else {
55  }
56 
57  if (msgLevel(MSG::DEBUG))
58  debug() << "resizing state containers to : " << m_algStates.size()
59  << endmsg;
60 
62  if (!algMan.isValid()) {
63  fatal() << "could not get the AlgManager" << endmsg;
64  throw GaudiException("In AlgExecStateSvc, unable to get the AlgManager!",
65  "AlgExecStateSvc",StatusCode::FAILURE);
66  }
67 
68  m_isInit = true;
69 
70  auto algos = algMan->getAlgorithms();
71  for (auto &alg : algos) {
72  addAlg( alg );
73  }
74 
75  for (auto &alg : m_preInitAlgs) {
76  addAlg( alg );
77  }
78 
79 
80  if (msgLevel( MSG::VERBOSE )) {
83  verbose() << "dumping state:\n" << ost.str() << endmsg;
84  }
85 
86 }
87 
88 //-----------------------------------------------------------------------------
89 
90 void
92 
93  if (!m_isInit) {
94  fatal() << "AlgExecStateSvc not initialized before first use" << endmsg;
95  throw GaudiException("AlgExecStateSvc not initialized before first use!",
96  "AlgExecStateSvc",StatusCode::FAILURE);
97  }
98 }
99 
100 //-----------------------------------------------------------------------------
101 
104 
105  // Initialise mother class (read properties, ...)
107  if (!sc.isSuccess()) {
108  warning () << "Base class could not be initialized" << endmsg;
109  return StatusCode::FAILURE;
110  }
111 
112  // moved all initialization to init(). hopefully it will get called in time....
113 
114  return StatusCode::SUCCESS;
115 
116 }
117 
118 //-----------------------------------------------------------------------------
119 
122 
123  return StatusCode::SUCCESS;
124 
125 }
126 
127 //-----------------------------------------------------------------------------
128 
129 void
131 
132  ost << "Event: " << trans(m_eventStatus.at(ctx.slot())) << std::endl;
133  ost << "Algs: " << m_algStates.at(ctx.slot()).size() << std::endl;
134 
135  size_t ml(0);
136  for (auto &e : m_algStates.at(ctx.slot())) {
137  if ( e.first.str().length() > ml ) {
138  ml = e.first.str().length();
139  }
140  }
141 
142  ost << " - Slot " << ctx.slot() << std::endl;
143  const AlgStateMap_t& m = m_algStates.at(ctx.slot());
144  for (auto &e : m) {
145  ost << " + " << std::setw(ml) << e.first.str() << " " << e.second
146  << std::endl;
147  }
148 
149 }
150 
151 //-----------------------------------------------------------------------------
152 
153 void
155  return addAlg(iAlg->nameKey());
156 }
157 
158 //-----------------------------------------------------------------------------
159 
160 void
162 
163 
164  if (!m_isInit) {
165  if (msgLevel(MSG::DEBUG))
166  debug() << "preInit: will add Alg " << alg.str() << " later" << endmsg;
167  m_preInitAlgs.push_back( alg );
168  return;
169  }
170 
171  if (m_algStates.size() > 0) {
172  if (m_algStates.at(0).find(alg) != m_algStates.at(0).end()) {
173  // already added
174  return;
175  }
176  }
177 
178  {
179  // in theory, this should only get called during initialization (serial)
180  // so shouldn't have to protect with a mutex...
182 
183  AlgExecState s;
184  for (size_t i = 0; i<m_algStates.size(); ++i) {
185  m_algStates.at(i)[alg] = s;
186  }
187  }
188 
189  if (msgLevel(MSG::DEBUG))
190  debug() << "adding alg " << alg.str() << " to "
191  << m_algStates.size() << " slots" << endmsg;
192 
193 }
194 
195 //-----------------------------------------------------------------------------
196 
197 const AlgExecState&
199  const EventContext& ctx) const {
200 
201  checkInit();
202 
203  AlgStateMap_t::const_iterator itr = m_algStates.at(ctx.slot()).find( algName );
204 
205  if (UNLIKELY( itr == m_algStates.at(ctx.slot()).end() ) ) {
206  throw GaudiException{std::string{"cannot find Alg "}
207  + algName.str() + " in AlgStateMap", name(), StatusCode::FAILURE};
208  }
209 
210  return itr->second;
211 
212 }
213 
214 //-----------------------------------------------------------------------------
215 
216 const AlgExecState&
218  const EventContext& ctx) const {
219 
220  return algExecState(iAlg->nameKey(), ctx);
221 
222 }
223 
224 //-----------------------------------------------------------------------------
225 
226 AlgExecState&
228  const EventContext& ctx) {
229 
231 
232  AlgStateMap_t::iterator itr = m_algStates.at(ctx.slot()).find( iAlg->nameKey() );
233 
234  if (UNLIKELY( itr == m_algStates.at(ctx.slot()).end() ) ) {
235  throw GaudiException{std::string{"cannot find Alg "}
236  + iAlg->name() + " in AlgStateMap", name(), StatusCode::FAILURE};
237  }
238 
239  return itr->second;
240 
241 }
242 
243 //-----------------------------------------------------------------------------
244 
247 
248  checkInit();
249 
250  return m_algStates.at(ctx.slot());
251 }
252 
253 //-----------------------------------------------------------------------------
254 
255 const EventStatus::Status&
257  checkInit();
258  return m_eventStatus.at(ctx.slot());
259 }
260 
261 //-----------------------------------------------------------------------------
262 
263 void
266  m_eventStatus.at(ctx.slot()) = sc;
267 }
268 
269 //-----------------------------------------------------------------------------
270 
271 void
272 AlgExecStateSvc::updateEventStatus(const bool& fail, const EventContext& ctx) {
274  if (m_eventStatus.at(ctx.slot()) == EventStatus::Success) {
275  if (fail)
277  } else if (m_eventStatus.at(ctx.slot()) == EventStatus::Invalid) {
278  if (! fail) {
280  } else {
282  }
283  }
284 }
285 
286 
287 //-----------------------------------------------------------------------------
288 
289 void
291 
292  if (msgLevel(MSG::DEBUG))
293  verbose() << "reset(" << ctx.slot() << ")" << endmsg;
294 
296  for (auto &e : m_algStates.at(ctx.slot())) {
297  e.second.reset();
298  }
299 
301 
302 }
303 
304 //-----------------------------------------------------------------------------
305 
308  switch (s) {
309  case EventStatus::Invalid :
310  return "Invalid";
311  case EventStatus::Success :
312  return "Success";
313  case EventStatus::AlgFail :
314  return "AlgFail";
315  case EventStatus::AlgStall :
316  return "AlgStall";
317  case EventStatus::Other :
318  return "Other";
319  default :
320  return "Should not happen";
321  }
322 
323 }
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
void dump(std::ostringstream &ost, const EventContext &ctx) const override
STL namespace.
T end(T...args)
std::vector< EventStatus::Status > m_eventStatus
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:25
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:78
T push_back(T...args)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
constexpr double m
Definition: SystemOfUnits.h:93
GAUDI_API const EventContext & currentContext()
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:50
#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
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:62
const EventStatus::Status & eventStatus(const EventContext &ctx) const 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
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:244
void addAlg(IAlgorithm *iAlg) override
std::vector< Gaudi::StringKey > m_preInitAlgs