Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ReplayOutputStream.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 files
12 
13 // From Gaudi
16 // local
17 #include "RecordOutputStream.h"
18 #include "ReplayOutputStream.h"
19 
20 #include <algorithm>
21 #include <functional>
22 #include <list>
23 
24 // ----------------------------------------------------------------------------
25 // Implementation file for class: ReplayOutputStream
26 //
27 // 30/08/2013: Marco Clemencic
28 // ----------------------------------------------------------------------------
30 
31 namespace {
32 
34  template <Gaudi::StateMachine::Transition TR>
35  class OutStreamTransition {
36  public:
38  OutStreamTransition( MsgStream& msg ) : m_msg( msg ), m_code( StatusCode::SUCCESS ) {}
39 
40  void operator()( ItemType& item );
41 
42  StatusCode result() const { return m_code; }
43 
44  private:
45  MsgStream& m_msg;
46  StatusCode m_code;
47  };
48 
49  template <>
50  void OutStreamTransition<Gaudi::StateMachine::INITIALIZE>::operator()( ItemType& item ) {
51  const StatusCode sc = item.second->sysInitialize();
52  if ( sc.isFailure() ) {
53  m_msg << MSG::WARNING << "Failed to initialize " << item.first << endmsg;
54  m_code = sc;
55  }
56  }
57  template <>
58  void OutStreamTransition<Gaudi::StateMachine::START>::operator()( ItemType& item ) {
59  const StatusCode sc = item.second->sysStart();
60  if ( sc.isFailure() ) {
61  m_msg << MSG::WARNING << "Failed to start " << item.first << endmsg;
62  m_code = sc;
63  }
64  }
65  template <>
66  void OutStreamTransition<Gaudi::StateMachine::STOP>::operator()( ItemType& item ) {
67  const StatusCode sc = item.second->sysStop();
68  if ( sc.isFailure() ) {
69  m_msg << MSG::WARNING << "Failed to stop " << item.first << endmsg;
70  m_code = sc;
71  }
72  }
73  template <>
74  void OutStreamTransition<Gaudi::StateMachine::FINALIZE>::operator()( ItemType& item ) {
75  const StatusCode sc = item.second->sysFinalize();
76  if ( sc.isFailure() ) {
77  m_msg << MSG::WARNING << "Failed to finalize " << item.first << endmsg;
78  m_code = sc;
79  }
80  }
81 } // namespace
82 
83 template <Gaudi::StateMachine::Transition TR>
85  OutStreamTransition<TR> trans( msg() );
87  return trans.result();
88 }
89 
90 // ============================================================================
91 // Initialization
92 // ============================================================================
94  StatusCode sc = GaudiAlgorithm::initialize(); // must be executed first
95  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
96 
97  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialize" << endmsg;
98 
99  m_algMgr = service( "ApplicationMgr" );
100  if ( !m_algMgr ) return Error( "cannot retrieve IAlgManager" );
101 
102  m_evtMgr = evtSvc();
103  if ( !m_evtMgr ) return Error( "cannot retrieve IDataManagerSvc " );
104 
106 
107  return i_outStreamTransition<Gaudi::StateMachine::INITIALIZE>();
108 }
109 
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 // ============================================================================
120 // Main execution
121 // ============================================================================
123  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
124 
126  if ( auto sc = m_evtMgr->traverseSubTree( RecordOutputStream::locationRoot(),
127  [&names]( IRegistry* pReg, int lvl ) {
128  if ( lvl > 0 ) names.push_back( pReg->name() );
129  return true;
130  } );
131  !sc )
132  return sc;
133 
134  std::for_each( names.begin(), names.end(), [this]( const std::string& name ) {
135  SmartIF<IAlgorithm>& alg = this->m_outputStreams[name];
136  if ( alg ) {
137  const auto& ctx = Gaudi::Hive::currentContext();
138  if ( alg->execState( ctx ).state() != AlgExecState::State::Done ) {
139  alg->sysExecute( ctx ).ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
140  } else {
141  this->warning() << name << " already executed for the current event" << endmsg;
142  }
143  } else {
144  this->warning() << "invalid OuputStream " << name << endmsg;
145  }
146  } );
147 
148  return StatusCode::SUCCESS;
149 }
150 
151 // ============================================================================
152 // Finalize
153 // ============================================================================
155  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Finalize" << endmsg;
156 
157  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::FINALIZE>();
158 
159  // release interfaces
161  m_algMgr.reset();
162  m_evtMgr.reset();
163 
164  StatusCode fsc = GaudiAlgorithm::finalize(); // must be called after all other actions
165  if ( sc.isSuccess() ) sc = fsc;
166  return sc;
167 }
168 
170  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Stop" << endmsg;
171 
172  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::STOP>();
173 
174  StatusCode ssc = GaudiAlgorithm::stop(); // must be called after all other actions
175  if ( sc.isSuccess() ) sc = ssc;
176  return sc;
177 }
178 
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 // ============================================================================
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
ReplayOutputStream::execute
StatusCode execute() override
Algorithm execution.
Definition: ReplayOutputStream.cpp:122
std::for_each
T for_each(T... args)
IAlgManager.h
std::string
STL class.
GaudiAlgorithm::finalize
StatusCode finalize() override
standard finalization method
Definition: GaudiAlgorithm.cpp:65
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:528
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:84
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
GaudiException
Definition: GaudiException.h:31
ReplayOutputStream::OutStreamAdder
Helper class to fill the internal map of OutputStreams.
Definition: ReplayOutputStream.h:48
ReplayOutputStream::start
StatusCode start() override
Algorithm initialization.
Definition: ReplayOutputStream.cpp:110
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:29
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:179
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:66
ReplayOutputStream::m_outputStreamNames
Gaudi::Property< std::vector< std::string > > m_outputStreamNames
Definition: ReplayOutputStream.h:62
GaudiAlgorithm::initialize
StatusCode initialize() override
standard initialization method
Definition: GaudiAlgorithm.cpp:52
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
MsgStream
Definition: MsgStream.h:34
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:40
ReplayOutputStream::m_algMgr
SmartIF< IAlgManager > m_algMgr
Definition: ReplayOutputStream.h:68
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
ReplayOutputStream::stop
StatusCode stop() override
Algorithm finalization.
Definition: ReplayOutputStream.cpp:169
ReplayOutputStream::finalize
StatusCode finalize() override
Algorithm finalization.
Definition: ReplayOutputStream.cpp:154
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:248
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:205
ReplayOutputStream::initialize
StatusCode initialize() override
Algorithm initialization.
Definition: ReplayOutputStream.cpp:93
std::vector::end
T end(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
bug_38882.SUCCESS
SUCCESS
Definition: bug_38882.py:24
RecordOutputStream.h
Gaudi::Algorithm::stop
StatusCode stop() override
the default (empty) implementation of IStateful::stop() method
Definition: Algorithm.h:182
GaudiCommon< Algorithm >::Error
StatusCode Error(std::string_view msg, const StatusCode st=StatusCode::FAILURE, const size_t mx=10) const
Print the error message and return with the given StatusCode.
Definition: GaudiCommon.icpp:256
IDataManagerSvc.h
ReplayOutputStream::m_evtMgr
SmartIF< IDataManagerSvc > m_evtMgr
Definition: ReplayOutputStream.h:69