The Gaudi Framework  master (37c0b60a)
ReplayOutputStream.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2023 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "ReplayOutputStream.h"
12 #include "RecordOutputStream.h"
15 #include <GaudiKernel/IRegistry.h>
16 #include <algorithm>
17 #include <functional>
18 #include <list>
19 
20 // ----------------------------------------------------------------------------
21 // Implementation file for class: ReplayOutputStream
22 //
23 // 30/08/2013: Marco Clemencic
24 // ----------------------------------------------------------------------------
26 
27 namespace {
28 
30  template <Gaudi::StateMachine::Transition TR>
31  class OutStreamTransition {
32  public:
34  OutStreamTransition( MsgStream& msg ) : m_msg( msg ), m_code( StatusCode::SUCCESS ) {}
35 
36  void operator()( ItemType& item );
37 
38  StatusCode result() const { return m_code; }
39 
40  private:
41  MsgStream& m_msg;
42  StatusCode m_code;
43  };
44 
45  template <>
46  void OutStreamTransition<Gaudi::StateMachine::INITIALIZE>::operator()( ItemType& item ) {
47  const StatusCode sc = item.second->sysInitialize();
48  if ( sc.isFailure() ) {
49  m_msg << MSG::WARNING << "Failed to initialize " << item.first << endmsg;
50  m_code = sc;
51  }
52  }
53  template <>
54  void OutStreamTransition<Gaudi::StateMachine::START>::operator()( ItemType& item ) {
55  const StatusCode sc = item.second->sysStart();
56  if ( sc.isFailure() ) {
57  m_msg << MSG::WARNING << "Failed to start " << item.first << endmsg;
58  m_code = sc;
59  }
60  }
61  template <>
62  void OutStreamTransition<Gaudi::StateMachine::STOP>::operator()( ItemType& item ) {
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 <>
70  void OutStreamTransition<Gaudi::StateMachine::FINALIZE>::operator()( ItemType& item ) {
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 } // namespace
78 
79 template <Gaudi::StateMachine::Transition TR>
81  OutStreamTransition<TR> trans( msg() );
83  return trans.result();
84 }
85 
86 // ============================================================================
87 // Initialization
88 // ============================================================================
90  StatusCode sc = Algorithm::initialize(); // must be executed first
91  if ( sc.isFailure() ) return sc; // error printed already by Algorithm
92 
93  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialize" << endmsg;
94 
95  m_algMgr = service( "ApplicationMgr" );
96  if ( !m_algMgr ) {
97  error() << "cannot retrieve IAlgManager" << endmsg;
98  return StatusCode::FAILURE;
99  }
100 
101  m_evtMgr = evtSvc();
102  if ( !m_evtMgr ) {
103  error() << "cannot retrieve IDataManagerSvc" << endmsg;
104  return StatusCode::FAILURE;
105  }
106 
108 
109  return i_outStreamTransition<Gaudi::StateMachine::INITIALIZE>();
110 }
111 
113  StatusCode sc = Algorithm::start(); // must be executed first
114  if ( sc.isFailure() ) return sc; // error printed already by Algorithm
115 
116  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Start" << endmsg;
117 
118  return i_outStreamTransition<Gaudi::StateMachine::START>();
119 }
120 
121 // ============================================================================
122 // Main execution
123 // ============================================================================
125  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
126 
128  if ( auto sc = m_evtMgr->traverseSubTree( RecordOutputStream::locationRoot(),
129  [&names]( IRegistry* pReg, int lvl ) {
130  if ( lvl > 0 ) names.push_back( pReg->name() );
131  return true;
132  } );
133  !sc )
134  return sc;
135 
136  std::for_each( names.begin(), names.end(), [this]( const std::string& name ) {
137  SmartIF<IAlgorithm>& alg = this->m_outputStreams[name];
138  if ( alg ) {
139  const auto& ctx = Gaudi::Hive::currentContext();
140  if ( alg->execState( ctx ).state() != AlgExecState::State::Done ) {
141  alg->sysExecute( ctx ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
142  } else {
143  this->warning() << name << " already executed for the current event" << endmsg;
144  }
145  } else {
146  this->warning() << "invalid OuputStream " << name << endmsg;
147  }
148  } );
149 
150  return StatusCode::SUCCESS;
151 }
152 
153 // ============================================================================
154 // Finalize
155 // ============================================================================
157  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Finalize" << endmsg;
158 
159  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::FINALIZE>();
160 
161  // release interfaces
163  m_algMgr.reset();
164  m_evtMgr.reset();
165 
166  StatusCode fsc = Algorithm::finalize(); // must be called after all other actions
167  if ( sc.isSuccess() ) sc = fsc;
168  return sc;
169 }
170 
172  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Stop" << endmsg;
173 
174  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::STOP>();
175 
176  StatusCode ssc = Algorithm::stop(); // must be called after all other actions
177  if ( sc.isSuccess() ) sc = ssc;
178  return sc;
179 }
180 
182  // we prepend '/' to the name of the algorithm to simplify the handling in
183  // OutputStreamsCollector
184  const std::string algId = "/" + outStream.name();
185  if ( m_outputStreams.find( algId ) == m_outputStreams.end() ) {
186  m_outputStreams[algId] = m_algMgr->algorithm( outStream );
187  if ( !m_outputStreams[algId] ) {
188  throw GaudiException( name(), "Could not get algorithm " + outStream.name(), StatusCode::FAILURE );
189  }
190  } else {
191  warning() << "OutputStream instance " << outStream.name() << " already added, ignoring " << outStream << endmsg;
192  }
193 }
194 
195 // ============================================================================
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
ReplayOutputStream::execute
StatusCode execute() override
Algorithm execution.
Definition: ReplayOutputStream.cpp:124
std::for_each
T for_each(T... args)
IAlgManager.h
std::string
STL class.
Gaudi::Utils::TypeNameString::name
const std::string & name() const
Definition: TypeNameString.h:49
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:526
GaudiUtils::Map::find
iterator find(const key_type &key)
Definition: Map.h:157
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
std::pair
ReplayOutputStream::i_outStreamTransition
StatusCode i_outStreamTransition()
Helper function to call the transition on the contained OutputStreams.
Definition: ReplayOutputStream.cpp:80
std::vector< std::string >
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
Gaudi::Algorithm::initialize
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:178
GaudiException
Definition: GaudiException.h:31
ReplayOutputStream::OutStreamAdder
Helper class to fill the internal map of OutputStreams.
Definition: ReplayOutputStream.h:46
ReplayOutputStream::start
StatusCode start() override
Algorithm initialization.
Definition: ReplayOutputStream.cpp:112
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
MSG::WARNING
@ WARNING
Definition: IMessageSvc.h:25
ReplayOutputStream.h
IRegistry
Definition: IRegistry.h:32
Gaudi::Algorithm::start
StatusCode start() override
the default (empty) implementation of IStateful::start() method
Definition: Algorithm.h:180
ReplayOutputStream
Definition: ReplayOutputStream.h:27
CommonMessaging< implements< IAlgorithm, IDataHandleHolder, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
GaudiUtils::Map::begin
iterator begin()
Definition: Map.h:139
ReplayOutputStream::i_addOutputStream
void i_addOutputStream(const Gaudi::Utils::TypeNameString &outStream)
Add a new algorithm to the list of OutputStreams.
Definition: ReplayOutputStream.cpp:181
GaudiUtils::Map::clear
void clear()
Definition: Map.h:195
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:20
StatusCode
Definition: StatusCode.h:65
ReplayOutputStream::m_outputStreams
OutStreamsMapType m_outputStreams
Internal storage for the OutputStreams to call.
Definition: ReplayOutputStream.h:64
ReplayOutputStream::m_outputStreamNames
Gaudi::Property< std::vector< std::string > > m_outputStreamNames
Definition: ReplayOutputStream.h:60
GaudiPython.Bindings.SUCCESS
SUCCESS
Definition: Bindings.py:83
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
IRegistry.h
MsgStream
Definition: MsgStream.h:33
GaudiUtils::Map::end
iterator end()
Definition: Map.h:140
RecordOutputStream::locationRoot
static const std::string locationRoot()
Return the path in the Transient Store used to record the triggered instances.
Definition: RecordOutputStream.h:37
ReplayOutputStream::m_algMgr
SmartIF< IAlgManager > m_algMgr
Definition: ReplayOutputStream.h:66
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
ReplayOutputStream::stop
StatusCode stop() override
Algorithm finalization.
Definition: ReplayOutputStream.cpp:171
ReplayOutputStream::finalize
StatusCode finalize() override
Algorithm finalization.
Definition: ReplayOutputStream.cpp:156
Gaudi::Algorithm::finalize
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition: Algorithm.h:184
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
Gaudi::Algorithm::evtSvc
SmartIF< IDataProviderSvc > & evtSvc() const
shortcut for method eventSvc
Definition: Algorithm.h:250
std::vector::begin
T begin(T... args)
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
Gaudi::Algorithm::service
StatusCode service(std::string_view name, T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Algorithm.h:206
ReplayOutputStream::initialize
StatusCode initialize() override
Algorithm initialization.
Definition: ReplayOutputStream.cpp:89
std::vector::end
T end(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
RecordOutputStream.h
Gaudi::Algorithm::stop
StatusCode stop() override
the default (empty) implementation of IStateful::stop() method
Definition: Algorithm.h:182
IDataManagerSvc.h
ReplayOutputStream::m_evtMgr
SmartIF< IDataManagerSvc > m_evtMgr
Definition: ReplayOutputStream.h:67