The Gaudi Framework  v30r4 (9b837755)
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 {
23 
25  template <Gaudi::StateMachine::Transition TR>
26  class OutStreamTransition
27  {
28  public:
30  OutStreamTransition( MsgStream& msg ) : m_msg( msg ), m_code( StatusCode::SUCCESS, false ) {}
31 
32  void operator()( ItemType& item );
33 
34  StatusCode result() const { return m_code; }
35 
36  private:
37  MsgStream& m_msg;
38  StatusCode m_code;
39  };
40 
41  template <>
43  {
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  {
53  const StatusCode sc = item.second->sysStart();
54  if ( sc.isFailure() ) {
55  m_msg << MSG::WARNING << "Failed to start " << item.first << endmsg;
56  m_code = sc;
57  }
58  }
59  template <>
61  {
62  const StatusCode sc = item.second->sysStop();
63  if ( sc.isFailure() ) {
64  m_msg << MSG::WARNING << "Failed to stop " << item.first << endmsg;
65  m_code = sc;
66  }
67  }
68  template <>
70  {
71  const StatusCode sc = item.second->sysFinalize();
72  if ( sc.isFailure() ) {
73  m_msg << MSG::WARNING << "Failed to finalize " << item.first << endmsg;
74  m_code = sc;
75  }
76  }
77 }
78 
79 template <Gaudi::StateMachine::Transition TR>
81 {
82  OutStreamTransition<TR> trans( msg() );
83  std::for_each( m_outputStreams.begin(), m_outputStreams.end(), trans );
84  return trans.result();
85 }
86 
87 // ============================================================================
88 // Initialization
89 // ============================================================================
91 {
92  StatusCode sc = GaudiAlgorithm::initialize(); // must be executed first
93  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
94 
95  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialize" << endmsg;
96 
97  m_algMgr = service( "ApplicationMgr" );
98  if ( UNLIKELY( !m_algMgr ) ) return Error( "cannot retrieve IAlgManager" );
99 
100  m_evtMgr = evtSvc();
101  if ( UNLIKELY( !m_evtMgr ) ) return Error( "cannot retrieve IDataManagerSvc " );
102 
103  std::for_each( m_outputStreamNames.begin(), m_outputStreamNames.end(), OutStreamAdder( this ) );
104 
105  return i_outStreamTransition<Gaudi::StateMachine::INITIALIZE>();
106 }
107 
109 {
110  StatusCode sc = GaudiAlgorithm::start(); // must be executed first
111  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
112 
113  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Start" << endmsg;
114 
115  return i_outStreamTransition<Gaudi::StateMachine::START>();
116 }
117 
118 // ============================================================================
119 // Main execution
120 // ============================================================================
122 {
123  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
124 
126  m_evtMgr->traverseSubTree( RecordOutputStream::locationRoot(), [&names]( IRegistry* pReg, int lvl ) {
127  if ( lvl > 0 ) names.push_back( pReg->name() );
128  return true;
129  } );
130 
131  std::for_each( names.begin(), names.end(), [this]( const std::string& name ) {
132  SmartIF<IAlgorithm>& alg = this->m_outputStreams[name];
133  if ( alg ) {
134  if ( !alg->isExecuted() ) {
136  } else {
137  this->warning() << name << " already executed for the current event" << endmsg;
138  }
139  } else {
140  this->warning() << "invalid OuputStream " << name << endmsg;
141  }
142  } );
143 
144  return StatusCode::SUCCESS;
145 }
146 
147 // ============================================================================
148 // Finalize
149 // ============================================================================
151 {
152  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Finalize" << endmsg;
153 
154  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::FINALIZE>();
155 
156  // release interfaces
157  m_outputStreams.clear();
158  m_algMgr.reset();
159  m_evtMgr.reset();
160 
161  StatusCode fsc = GaudiAlgorithm::finalize(); // must be called after all other actions
162  if ( sc.isSuccess() ) sc = fsc;
163  return sc;
164 }
165 
167 {
168  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Stop" << endmsg;
169 
170  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::STOP>();
171 
172  StatusCode ssc = GaudiAlgorithm::stop(); // must be called after all other actions
173  if ( sc.isSuccess() ) sc = ssc;
174  return sc;
175 }
176 
178 {
179  // we prepend '/' to the name of the algorithm to simplify the handling in
180  // OutputStreamsCollector
181  const std::string algId = "/" + outStream.name();
182  if ( m_outputStreams.find( algId ) == m_outputStreams.end() ) {
183  m_outputStreams[algId] = m_algMgr->algorithm( outStream );
184  if ( !m_outputStreams[algId] ) {
185  throw GaudiException( name(), "Could not get algorithm " + outStream.name(), StatusCode::FAILURE );
186  }
187  } else {
188  warning() << "OutputStream instance " << outStream.name() << " already added, ignoring " << outStream << endmsg;
189  }
190 }
191 
192 // ============================================================================
#define UNLIKELY(x)
Definition: Kernel.h:89
constexpr static const auto FAILURE
Definition: StatusCode.h:88
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Define general base for Gaudi exception.
StatusCode finalize() override
Algorithm finalization.
StatusCode initialize() override
standard initialization method
bool isSuccess() const
Definition: StatusCode.h:287
virtual const name_type & name() const =0
Name of the directory (or key)
T end(T...args)
bool isFailure() const
Definition: StatusCode.h:139
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:51
virtual bool isExecuted() const =0
check if the algorithm is already executed for the current event
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.
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
StatusCode execute() override
Algorithm execution.
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
T begin(T...args)
StatusCode start() override
Algorithm initialization.
StatusCode start() override
the default (empty) implementation of IStateful::start() method
Definition: Algorithm.h:182
StatusCode initialize() override
Algorithm initialization.
const std::string & name() const
StatusCode stop() override
the default (empty) implementation of IStateful::stop() method
Definition: Algorithm.h:184
T for_each(T...args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
static const std::string locationRoot()
Return the path in the Transient Store used to record the triggered instances.