The Gaudi Framework  v29r0 (ff2e7097)
ReplayOutputStream.cpp
Go to the documentation of this file.
1 // Include files
2 
3 // From Gaudi
7 // local
8 #include "RecordOutputStream.h"
9 #include "ReplayOutputStream.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 {
24 
26  template <Gaudi::StateMachine::Transition TR>
27  class OutStreamTransition
28  {
29  public:
31  OutStreamTransition( MsgStream& msg ) : m_msg( msg ), m_code( StatusCode::SUCCESS, false ) {}
32 
33  void operator()( ItemType& item );
34 
35  StatusCode result() const { return m_code; }
36 
37  private:
38  MsgStream& m_msg;
39  StatusCode m_code;
40  };
41 
42  template <>
44  {
45  const StatusCode sc = item.second->sysInitialize();
46  if ( sc.isFailure() ) {
47  m_msg << MSG::WARNING << "Failed to initialize " << item.first << endmsg;
48  m_code = sc;
49  }
50  }
51  template <>
53  {
54  const StatusCode sc = item.second->sysStart();
55  if ( sc.isFailure() ) {
56  m_msg << MSG::WARNING << "Failed to start " << item.first << endmsg;
57  m_code = sc;
58  }
59  }
60  template <>
62  {
63  const StatusCode sc = item.second->sysStop();
64  if ( sc.isFailure() ) {
65  m_msg << MSG::WARNING << "Failed to stop " << item.first << endmsg;
66  m_code = sc;
67  }
68  }
69  template <>
71  {
72  const StatusCode sc = item.second->sysFinalize();
73  if ( sc.isFailure() ) {
74  m_msg << MSG::WARNING << "Failed to finalize " << item.first << endmsg;
75  m_code = sc;
76  }
77  }
78 }
79 
80 template <Gaudi::StateMachine::Transition TR>
82 {
83  OutStreamTransition<TR> trans( msg() );
84  std::for_each( m_outputStreams.begin(), m_outputStreams.end(), trans );
85  return trans.result();
86 }
87 
88 // ============================================================================
89 // Initialization
90 // ============================================================================
92 {
93  StatusCode sc = GaudiAlgorithm::initialize(); // must be executed first
94  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
95 
96  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialize" << endmsg;
97 
98  m_algMgr = service( "ApplicationMgr" );
99  if ( UNLIKELY( !m_algMgr ) ) return Error( "cannot retrieve IAlgManager" );
100 
101  m_evtMgr = evtSvc();
102  if ( UNLIKELY( !m_evtMgr ) ) return Error( "cannot retrieve IDataManagerSvc " );
103 
104  std::for_each( m_outputStreamNames.begin(), m_outputStreamNames.end(), OutStreamAdder( this ) );
105 
106  return i_outStreamTransition<Gaudi::StateMachine::INITIALIZE>();
107 }
108 
110 {
111  StatusCode sc = GaudiAlgorithm::start(); // must be executed first
112  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
113 
114  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Start" << endmsg;
115 
116  return i_outStreamTransition<Gaudi::StateMachine::START>();
117 }
118 
119 namespace
120 {
123  struct OutputStreamsCollector : public IDataStoreAgent {
125  bool analyse( IRegistry* pRegistry, int lvl ) override
126  {
127  if ( lvl > 0 ) names.push_back( pRegistry->name() );
128  return true;
129  }
130  };
131 }
132 
133 // ============================================================================
134 // Main execution
135 // ============================================================================
137 {
138  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
139 
140  OutputStreamsCollector collector;
141  m_evtMgr->traverseSubTree( RecordOutputStream::locationRoot(), &collector );
142 
143  std::for_each( collector.names.begin(), collector.names.end(), OutStreamTrigger( this ) );
144 
145  return StatusCode::SUCCESS;
146 }
147 
148 // ============================================================================
149 // Finalize
150 // ============================================================================
152 {
153  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Finalize" << endmsg;
154 
155  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::FINALIZE>();
156 
157  // release interfaces
158  m_outputStreams.clear();
159  m_algMgr.reset();
160  m_evtMgr.reset();
161 
162  StatusCode fsc = GaudiAlgorithm::finalize(); // must be called after all other actions
163  if ( sc.isSuccess() ) sc = fsc;
164  return sc;
165 }
166 
168 {
169  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Stop" << endmsg;
170 
171  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::STOP>();
172 
173  StatusCode ssc = GaudiAlgorithm::stop(); // must be called after all other actions
174  if ( sc.isSuccess() ) sc = ssc;
175  return sc;
176 }
177 
179 {
180  // we prepend '/' to the name of the algorithm to simplify the handling in
181  // OutputStreamsCollector
182  const std::string algId = "/" + outStream.name();
183  if ( m_outputStreams.find( algId ) == m_outputStreams.end() ) {
184  m_outputStreams[algId] = m_algMgr->algorithm( outStream );
185  if ( !m_outputStreams[algId] ) {
186  throw GaudiException( name(), "Could not get algorithm " + outStream.name(), StatusCode::FAILURE );
187  }
188  } else {
189  warning() << "OutputStream instance " << outStream.name() << " already added, ignoring " << outStream << endmsg;
190  }
191 }
192 
193 // ============================================================================
#define UNLIKELY(x)
Definition: Kernel.h:128
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:75
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:86
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:28
StatusCode finalize() override
standard finalization method
#define DECLARE_ALGORITHM_FACTORY(x)
Definition: Algorithm.h:629
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:181
StatusCode initialize() override
Algorithm initialization.
const std::string & name() const
StatusCode stop() override
the default (empty) implementation of IStateful::stop() method
Definition: Algorithm.h:183
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.