The Gaudi Framework  master (1304469f)
Loading...
Searching...
No Matches
ReplayOutputStream.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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"
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
27namespace {
28
30 template <Gaudi::StateMachine::Transition TR>
31 class OutStreamTransition {
32 public:
33 typedef ReplayOutputStream::OutStreamsMapType::value_type ItemType;
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
79template <Gaudi::StateMachine::Transition TR>
81 OutStreamTransition<TR> trans( msg() );
82 std::for_each( m_outputStreams.begin(), m_outputStreams.end(), trans );
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;
99 }
100
101 m_evtMgr = evtSvc();
102 if ( !m_evtMgr ) {
103 error() << "cannot retrieve IDataManagerSvc" << endmsg;
104 return StatusCode::FAILURE;
105 }
106
107 std::for_each( m_outputStreamNames.begin(), m_outputStreamNames.end(), OutStreamAdder( this ) );
108
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
119}
120
121// ============================================================================
122// Main execution
123// ============================================================================
125 if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
126
127 std::vector<std::string> names;
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::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
160
161 // release interfaces
162 m_outputStreams.clear();
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
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// ============================================================================
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition Algorithm.h:175
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition Algorithm.h:181
StatusCode stop() override
the default (empty) implementation of IStateful::stop() method
Definition Algorithm.h:179
const std::string & name() const override
The identifying name of the algorithm object.
SmartIF< IService > service(std::string_view name, const bool createIf=true, const bool quiet=false) const
Return a pointer to the service identified by name (or "type/name")
StatusCode start() override
the default (empty) implementation of IStateful::start() method
Definition Algorithm.h:177
SmartIF< IDataProviderSvc > & evtSvc() const
shortcut for method eventSvc
Definition Algorithm.h:233
Helper class to parse a string of format "type/name".
const std::string & name() const
Define general base for Gaudi exception.
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition IRegistry.h:29
static const std::string locationRoot()
Return the path in the Transient Store used to record the triggered instances.
Helper class to fill the internal map of OutputStreams.
StatusCode stop() override
Algorithm finalization.
OutStreamsMapType m_outputStreams
Internal storage for the OutputStreams to call.
void i_addOutputStream(const Gaudi::Utils::TypeNameString &outStream)
Add a new algorithm to the list of OutputStreams.
StatusCode execute() override
Algorithm execution.
StatusCode i_outStreamTransition()
Helper function to call the transition on the contained OutputStreams.
StatusCode start() override
Algorithm initialization.
SmartIF< IDataManagerSvc > m_evtMgr
SmartIF< IAlgManager > m_algMgr
StatusCode initialize() override
Algorithm initialization.
StatusCode finalize() override
Algorithm finalization.
Gaudi::Property< std::vector< std::string > > m_outputStreamNames
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isFailure() const
Definition StatusCode.h:129
bool isSuccess() const
Definition StatusCode.h:314
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
@ WARNING
Definition IMessageSvc.h:22
@ DEBUG
Definition IMessageSvc.h:22