Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
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
6 // local
7 #include "RecordOutputStream.h"
8 #include "ReplayOutputStream.h"
9 
10 #include <algorithm>
11 #include <functional>
12 #include <list>
13 
14 // ----------------------------------------------------------------------------
15 // Implementation file for class: ReplayOutputStream
16 //
17 // 30/08/2013: Marco Clemencic
18 // ----------------------------------------------------------------------------
20 
21 namespace {
22 
24  template <Gaudi::StateMachine::Transition TR>
25  class OutStreamTransition {
26  public:
28  OutStreamTransition( MsgStream& msg ) : m_msg( msg ), m_code( StatusCode::SUCCESS, false ) {}
29 
30  void operator()( ItemType& item );
31 
32  StatusCode result() const { return m_code; }
33 
34  private:
35  MsgStream& m_msg;
36  StatusCode m_code;
37  };
38 
39  template <>
41  const StatusCode sc = item.second->sysInitialize();
42  if ( sc.isFailure() ) {
43  m_msg << MSG::WARNING << "Failed to initialize " << item.first << endmsg;
44  m_code = sc;
45  }
46  }
47  template <>
49  const StatusCode sc = item.second->sysStart();
50  if ( sc.isFailure() ) {
51  m_msg << MSG::WARNING << "Failed to start " << item.first << endmsg;
52  m_code = sc;
53  }
54  }
55  template <>
57  const StatusCode sc = item.second->sysStop();
58  if ( sc.isFailure() ) {
59  m_msg << MSG::WARNING << "Failed to stop " << item.first << endmsg;
60  m_code = sc;
61  }
62  }
63  template <>
65  const StatusCode sc = item.second->sysFinalize();
66  if ( sc.isFailure() ) {
67  m_msg << MSG::WARNING << "Failed to finalize " << item.first << endmsg;
68  m_code = sc;
69  }
70  }
71 } // namespace
72 
73 template <Gaudi::StateMachine::Transition TR>
75  OutStreamTransition<TR> trans( msg() );
76  std::for_each( m_outputStreams.begin(), m_outputStreams.end(), trans );
77  return trans.result();
78 }
79 
80 // ============================================================================
81 // Initialization
82 // ============================================================================
84  StatusCode sc = GaudiAlgorithm::initialize(); // must be executed first
85  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
86 
87  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialize" << endmsg;
88 
89  m_algMgr = service( "ApplicationMgr" );
90  if ( UNLIKELY( !m_algMgr ) ) return Error( "cannot retrieve IAlgManager" );
91 
92  m_evtMgr = evtSvc();
93  if ( UNLIKELY( !m_evtMgr ) ) return Error( "cannot retrieve IDataManagerSvc " );
94 
95  std::for_each( m_outputStreamNames.begin(), m_outputStreamNames.end(), OutStreamAdder( this ) );
96 
97  return i_outStreamTransition<Gaudi::StateMachine::INITIALIZE>();
98 }
99 
101  StatusCode sc = GaudiAlgorithm::start(); // must be executed first
102  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
103 
104  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Start" << endmsg;
105 
106  return i_outStreamTransition<Gaudi::StateMachine::START>();
107 }
108 
109 // ============================================================================
110 // Main execution
111 // ============================================================================
113  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
114 
116  m_evtMgr->traverseSubTree( RecordOutputStream::locationRoot(), [&names]( IRegistry* pReg, int lvl ) {
117  if ( lvl > 0 ) names.push_back( pReg->name() );
118  return true;
119  } );
120 
121  std::for_each( names.begin(), names.end(), [this]( const std::string& name ) {
122  SmartIF<IAlgorithm>& alg = this->m_outputStreams[name];
123  if ( alg ) {
124  const auto& ctx = Gaudi::Hive::currentContext();
125  if ( alg->execState( ctx ).state() != AlgExecState::State::Done ) {
126  alg->sysExecute( ctx );
127  } else {
128  this->warning() << name << " already executed for the current event" << endmsg;
129  }
130  } else {
131  this->warning() << "invalid OuputStream " << name << endmsg;
132  }
133  } );
134 
135  return StatusCode::SUCCESS;
136 }
137 
138 // ============================================================================
139 // Finalize
140 // ============================================================================
142  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Finalize" << endmsg;
143 
144  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::FINALIZE>();
145 
146  // release interfaces
147  m_outputStreams.clear();
148  m_algMgr.reset();
149  m_evtMgr.reset();
150 
151  StatusCode fsc = GaudiAlgorithm::finalize(); // must be called after all other actions
152  if ( sc.isSuccess() ) sc = fsc;
153  return sc;
154 }
155 
157  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Stop" << endmsg;
158 
159  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::STOP>();
160 
161  StatusCode ssc = GaudiAlgorithm::stop(); // must be called after all other actions
162  if ( sc.isSuccess() ) sc = ssc;
163  return sc;
164 }
165 
167  // we prepend '/' to the name of the algorithm to simplify the handling in
168  // OutputStreamsCollector
169  const std::string algId = "/" + outStream.name();
170  if ( m_outputStreams.find( algId ) == m_outputStreams.end() ) {
171  m_outputStreams[algId] = m_algMgr->algorithm( outStream );
172  if ( !m_outputStreams[algId] ) {
173  throw GaudiException( name(), "Could not get algorithm " + outStream.name(), StatusCode::FAILURE );
174  }
175  } else {
176  warning() << "OutputStream instance " << outStream.name() << " already added, ignoring " << outStream << endmsg;
177  }
178 }
179 
180 // ============================================================================
#define UNLIKELY(x)
Definition: Kernel.h:89
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Define general base for Gaudi exception.
StatusCode start() override
the default (empty) implementation of IStateful::start() method
Definition: Algorithm.h:182
StatusCode finalize() override
Algorithm finalization.
StatusCode initialize() override
standard initialization method
bool isSuccess() const
Definition: StatusCode.h:267
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
virtual const name_type & name() const =0
Name of the directory (or key)
T end(T...args)
bool isFailure() const
Definition: StatusCode.h:130
STL class.
#define DECLARE_COMPONENT(type)
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:50
StatusCode finalize() override
standard finalization method
virtual StatusCode sysExecute(const EventContext &)=0
System execution. This method invokes the execute() method of a concrete algorithm.
GAUDI_API const EventContext & currentContext()
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.
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
T begin(T...args)
virtual AlgExecState & execState(const EventContext &ctx) const =0
reference to AlgExecState of Alg
StatusCode start() override
Algorithm initialization.
constexpr static const auto FAILURE
Definition: StatusCode.h:86
StatusCode initialize() override
Algorithm initialization.
const std::string & name() const
T for_each(T...args)
State state() const
StatusCode stop() override
the default (empty) implementation of IStateful::stop() method
Definition: Algorithm.h:184
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
static const std::string locationRoot()
Return the path in the Transient Store used to record the triggered instances.