All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ReplayOutputStream.cpp
Go to the documentation of this file.
1 // Include files
2 
3 // From Gaudi
7 // local
8 #include "ReplayOutputStream.h"
9 #include "RecordOutputStream.h"
10 
11 #include <algorithm>
12 #include <functional>
13 #include <list>
14 
15 // ----------------------------------------------------------------------------
16 // Implementation file for class: ReplayOutputStream
17 //
18 // 30/08/2013: Marco Clemencic
19 // ----------------------------------------------------------------------------
21 
22 namespace {
23 
25  template <Gaudi::StateMachine::Transition TR>
26  class OutStreamTransition {
27  public:
29  OutStreamTransition(MsgStream &msg):
30  m_msg(msg),
31  m_code(StatusCode::SUCCESS, false)
32  {}
33 
34  void operator() (ItemType &item);
35 
36  StatusCode result() const { return m_code; }
37  private:
38  MsgStream &m_msg;
39  StatusCode m_code;
40  };
41 
42  template <>
44  const StatusCode sc = item.second->sysInitialize();
45  if (sc.isFailure()) {
46  m_msg << MSG::WARNING << "Failed to initialize " << item.first << endmsg;
47  m_code = sc;
48  }
49  }
50  template <>
52  const StatusCode sc = item.second->sysStart();
53  if (sc.isFailure()) {
54  m_msg << MSG::WARNING << "Failed to start " << item.first << endmsg;
55  m_code = sc;
56  }
57  }
58  template <>
60  const StatusCode sc = item.second->sysStop();
61  if (sc.isFailure()) {
62  m_msg << MSG::WARNING << "Failed to stop " << item.first << endmsg;
63  m_code = sc;
64  }
65  }
66  template <>
68  const StatusCode sc = item.second->sysFinalize();
69  if (sc.isFailure()) {
70  m_msg << MSG::WARNING << "Failed to finalize " << item.first << endmsg;
71  m_code = sc;
72  }
73  }
74 
75 }
76 
77 template <Gaudi::StateMachine::Transition TR>
79  OutStreamTransition<TR> trans(msg());
80  std::for_each(m_outputStreams.begin(), m_outputStreams.end(), trans);
81  return trans.result();
82 }
83 
84 // ============================================================================
85 // Initialization
86 // ============================================================================
88  StatusCode sc = GaudiAlgorithm::initialize(); // must be executed first
89  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
90 
91  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Initialize" << endmsg;
92 
93  m_algMgr = service("ApplicationMgr");
94  if (UNLIKELY(!m_algMgr)) return Error("cannot retrieve IAlgManager");
95 
96  m_evtMgr = evtSvc();
97  if (UNLIKELY(!m_evtMgr)) return Error("cannot retrieve IDataManagerSvc ");
98 
99  std::for_each(m_outputStreamNames.begin(), m_outputStreamNames.end(),
100  OutStreamAdder(this));
101 
102  return i_outStreamTransition<Gaudi::StateMachine::INITIALIZE>();
103 }
104 
106  StatusCode sc = GaudiAlgorithm::start(); // must be executed first
107  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
108 
109  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Start" << endmsg;
110 
111  return i_outStreamTransition<Gaudi::StateMachine::START>();
112 }
113 
114 
115 namespace {
118  struct OutputStreamsCollector: public IDataStoreAgent {
120  bool analyse(IRegistry* pRegistry, int lvl) override {
121  if (lvl > 0)
122  names.push_back(pRegistry->name());
123  return true;
124  }
125  };
126 }
127 
128 // ============================================================================
129 // Main execution
130 // ============================================================================
132  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Execute" << endmsg;
133 
134  OutputStreamsCollector collector;
135  m_evtMgr->traverseSubTree(RecordOutputStream::locationRoot(), &collector);
136 
137  std::for_each(collector.names.begin(), collector.names.end(),
138  OutStreamTrigger(this));
139 
140  return StatusCode::SUCCESS;
141 }
142 
143 // ============================================================================
144 // Finalize
145 // ============================================================================
147  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Finalize" << endmsg;
148 
149  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::FINALIZE>();
150 
151  // release interfaces
152  m_outputStreams.clear();
153  m_algMgr.reset();
154  m_evtMgr.reset();
155 
156  StatusCode fsc = GaudiAlgorithm::finalize(); // must be called after all other actions
157  if (sc.isSuccess()) sc = fsc;
158  return sc;
159 }
160 
162  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Stop" << endmsg;
163 
164  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::STOP>();
165 
166  StatusCode ssc = GaudiAlgorithm::stop(); // must be called after all other actions
167  if (sc.isSuccess()) sc = ssc;
168  return sc;
169 }
170 
172  // we prepend '/' to the name of the algorithm to simplify the handling in
173  // OutputStreamsCollector
174  const std::string algId = "/" + outStream.name();
175  if (m_outputStreams.find(algId) == m_outputStreams.end()) {
176  m_outputStreams[algId] = m_algMgr->algorithm(outStream);
177  if (!m_outputStreams[algId]) {
178  throw GaudiException(name(), "Could not get algorithm " + outStream.name(),
180  }
181  } else {
182  warning() << "OutputStream instance " << outStream.name()
183  << " already added, ignoring " << outStream << endmsg;
184  }
185 }
186 
187 // ============================================================================
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Define general base for Gaudi exception.
Helper class to call the required OutputStream.
StatusCode finalize() override
Algorithm finalization.
StatusCode initialize() override
standard initialization method
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
#define UNLIKELY(x)
Definition: Kernel.h:126
virtual const name_type & name() const =0
Name of the directory (or key)
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:84
STL class.
StatusCode i_outStreamTransition()
Helper function to call the transition on the contained OutputStreams.
T push_back(T...args)
Helper class to parse a string of format "type/name".
StatusCode stop() override
Algorithm finalization.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
StatusCode finalize() override
standard finalization method
#define DECLARE_ALGORITHM_FACTORY(x)
Definition: Algorithm.h:620
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
void i_addOutputStream(const Gaudi::Utils::TypeNameString &outStream)
Add a new algorithm to the list of OutputStreams.
Helper class to fill the internal map of OutputStreams.
StatusCode execute() override
Algorithm execution.
Generic data agent interface.
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
StatusCode start() override
Algorithm initialization.
StatusCode start() override
the default (empty) implementation of IStateful::start() method
Definition: Algorithm.h:180
StatusCode initialize() override
Algorithm initialization.
const std::string & name() const
StatusCode stop() override
the default (empty) implementation of IStateful::stop() method
Definition: Algorithm.h:182
T for_each(T...args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
static const std::string locationRoot()
Return the path in the Transient Store used to record the triggered instances.