| The Gaudi Framework v32r1 (f65d50dc)

ReplayOutputStream.cpp
Go to the documentation of this file.
1 // Include files
2
3 // From Gaudi
4 #include "GaudiKernel/IAlgManager.h"
5 #include "GaudiKernel/IDataManagerSvc.h"
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 // ----------------------------------------------------------------------------
19 DECLARE_COMPONENT( ReplayOutputStream )
20
21 namespace {
22
24 template <Gaudi::StateMachine::Transition TR>
25 class OutStreamTransition {
26 public:
27 typedef ReplayOutputStream::OutStreamsMapType::value_type ItemType;
28 OutStreamTransition( MsgStream& msg ) : m_msg( msg ), m_code( StatusCode::SUCCESS, false ) {}
29
30 void operator()( ItemType& item );
31
32 StatusCode result() const { return m_code; }
33
34 private:
35 MsgStream& m_msg;
36 StatusCode m_code;
37 };
38
39 template <>
40 void OutStreamTransition<Gaudi::StateMachine::INITIALIZE>::operator()( ItemType& item ) {
41 const StatusCode sc = item.second->sysInitialize();
42 if ( sc.isFailure() ) {
43 m_msg << MSG::WARNING << "Failed to initialize " << item.first << endmsg;
44 m_code = sc;
45 }
46 }
47 template <>
48 void OutStreamTransition<Gaudi::StateMachine::START>::operator()( ItemType& item ) {
49 const StatusCode sc = item.second->sysStart();
50 if ( sc.isFailure() ) {
51 m_msg << MSG::WARNING << "Failed to start " << item.first << endmsg;
52 m_code = sc;
53 }
54 }
55 template <>
56 void OutStreamTransition<Gaudi::StateMachine::STOP>::operator()( ItemType& item ) {
57 const StatusCode sc = item.second->sysStop();
58 if ( sc.isFailure() ) {
59 m_msg << MSG::WARNING << "Failed to stop " << item.first << endmsg;
60 m_code = sc;
61 }
62 }
63 template <>
64 void OutStreamTransition<Gaudi::StateMachine::FINALIZE>::operator()( ItemType& item ) {
65 const StatusCode sc = item.second->sysFinalize();
66 if ( sc.isFailure() ) {
67 m_msg << MSG::WARNING << "Failed to finalize " << item.first << endmsg;
68 m_code = sc;
69 }
70 }
71 } // namespace
72
73 template <Gaudi::StateMachine::Transition TR>
74 StatusCode ReplayOutputStream::i_outStreamTransition() {
75 OutStreamTransition<TR> trans( msg() );
76 std::for_each( m_outputStreams.begin(), m_outputStreams.end(), trans );
77 return trans.result();
78 }
79
80 // ============================================================================
81 // Initialization
82 // ============================================================================
83 StatusCode ReplayOutputStream::initialize() {
84 StatusCode sc = GaudiAlgorithm::initialize(); // must be executed first
85 if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
86
87 if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Initialize" << endmsg;
88
89 m_algMgr = service( "ApplicationMgr" );
90 if ( UNLIKELY( !m_algMgr ) ) return Error( "cannot retrieve IAlgManager" );
91
92 m_evtMgr = evtSvc();
93 if ( UNLIKELY( !m_evtMgr ) ) return Error( "cannot retrieve IDataManagerSvc " );
94
95 std::for_each( m_outputStreamNames.begin(), m_outputStreamNames.end(), OutStreamAdder( this ) );
96
97 return i_outStreamTransition<Gaudi::StateMachine::INITIALIZE>();
98 }
99
100 StatusCode ReplayOutputStream::start() {
101 StatusCode sc = GaudiAlgorithm::start(); // must be executed first
102 if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
103
104 if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Start" << endmsg;
105
106 return i_outStreamTransition<Gaudi::StateMachine::START>();
107 }
108
109 // ============================================================================
110 // Main execution
111 // ============================================================================
112 StatusCode ReplayOutputStream::execute() {
113 if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
114
115 std::vector<std::string> names;
116 m_evtMgr->traverseSubTree( RecordOutputStream::locationRoot(), [&names]( IRegistry* pReg, int lvl ) {
117 if ( lvl > 0 ) names.push_back( pReg->name() );
118 return true;
119 } );
120
121 std::for_each( names.begin(), names.end(), [this]( const std::string& name ) {
122 SmartIF<IAlgorithm>& alg = this->m_outputStreams[name];
123 if ( alg ) {
124 const auto& ctx = Gaudi::Hive::currentContext();
125 if ( alg->execState( ctx ).state() != AlgExecState::State::Done ) {
126 alg->sysExecute( ctx );
127 } else {
128 this->warning() << name << " already executed for the current event" << endmsg;
129 }
130 } else {
131 this->warning() << "invalid OuputStream " << name << endmsg;
132 }
133 } );
134
135 return StatusCode::SUCCESS;
136 }
137
138 // ============================================================================
139 // Finalize
140 // ============================================================================
141 StatusCode ReplayOutputStream::finalize() {
142 if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Finalize" << endmsg;
143
144 StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::FINALIZE>();
145
146 // release interfaces
147 m_outputStreams.clear();
148 m_algMgr.reset();
149 m_evtMgr.reset();
150
151 StatusCode fsc = GaudiAlgorithm::finalize(); // must be called after all other actions
152 if ( sc.isSuccess() ) sc = fsc;
153 return sc;
154 }
155
156 StatusCode ReplayOutputStream::stop() {
157 if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Stop" << endmsg;
158
159 StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::STOP>();
160
161 StatusCode ssc = GaudiAlgorithm::stop(); // must be called after all other actions
162 if ( sc.isSuccess() ) sc = ssc;
163 return sc;
164 }
165
166 void ReplayOutputStream::i_addOutputStream( const Gaudi::Utils::TypeNameString& outStream ) {
167 // we prepend '/' to the name of the algorithm to simplify the handling in
168 // OutputStreamsCollector
169 const std::string algId = "/" + outStream.name();
170 if ( m_outputStreams.find( algId ) == m_outputStreams.end() ) {
171 m_outputStreams[algId] = m_algMgr->algorithm( outStream );
172 if ( !m_outputStreams[algId] ) {
173 throw GaudiException( name(), "Could not get algorithm " + outStream.name(), StatusCode::FAILURE );
174 }
175 } else {
176 warning() << "OutputStream instance " << outStream.name() << " already added, ignoring " << outStream << endmsg;
177 }
178 }
179
180 // ============================================================================
IAlgManager::algorithm
virtual SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:96
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
SmartIF< IAlgorithm >
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
ManySmallAlgs.alg
alg
Definition: ManySmallAlgs.py:69
ReplayOutputStream
Definition: ReplayOutputStream.h:19
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
ReplayOutputStream::m_outputStreams
OutStreamsMapType m_outputStreams
Internal storage for the OutputStreams to call.
Definition: ReplayOutputStream.h:56
IDataManagerSvc::traverseSubTree
virtual StatusCode traverseSubTree(std::string_view sub_tree_path, IDataStoreAgent *pAgent)=0
Analyse by traversing all data objects below the sub tree identified by its full path name.
ReplayOutputStream::finalize
StatusCode finalize() override
Algorithm finalization.
Definition: ReplayOutputStream.cpp:141
GaudiAlgorithm::initialize
StatusCode initialize() override
standard initialization method
Definition: GaudiAlgorithm.cpp:42
GaudiPython.Bindings.SUCCESS
SUCCESS
Definition: Bindings.py:56
MSG::DEBUG
Definition: IMessageSvc.h:15
GaudiCommon< Algorithm >::Error
WARN_UNUSED StatusCode Error(const std::string &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:286
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
IRegistry::name
virtual const name_type & name() const =0
Name of the directory (or key)
std::vector::end
T end(T... args)
IOTest.start
def start
Definition: IOTest.py:97
std::string
STL class.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
ReplayOutputStream::i_outStreamTransition
StatusCode i_outStreamTransition()
Helper function to call the transition on the contained OutputStreams.
Definition: ReplayOutputStream.cpp:74
std::vector::push_back
T push_back(T... args)
IDataManagerSvc.h
GaudiUtils::Map::end
iterator end()
Definition: Map.h:130
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:10
ReplayOutputStream::stop
StatusCode stop() override
Algorithm finalization.
Definition: ReplayOutputStream.cpp:156
ReplayOutputStream::m_outputStreamNames
Gaudi::Property< std::vector< std::string > > m_outputStreamNames
Definition: ReplayOutputStream.h:52
Gaudi::Utils::TypeNameString::name
const std::string & name() const
Definition: TypeNameString.h:37
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
GaudiAlgorithm::finalize
StatusCode finalize() override
standard finalization method
Definition: GaudiAlgorithm.cpp:55
GaudiUtils::Map::find
iterator find(const key_type &key)
Definition: Map.h:147
Gaudi::Hive::currentContext
GAUDI_API const EventContext & currentContext()
Definition: ThreadLocalContext.cpp:20
ReplayOutputStream.h
IRegistry
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
ReplayOutputStream::i_addOutputStream
void i_addOutputStream(const Gaudi::Utils::TypeNameString &outStream)
Add a new algorithm to the list of OutputStreams.
Definition: ReplayOutputStream.cpp:166
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:267
ReplayOutputStream::OutStreamAdder
Helper class to fill the internal map of OutputStreams.
Definition: ReplayOutputStream.h:38
GaudiUtils::Map::begin
iterator begin()
Definition: Map.h:129
std::pair
RecordOutputStream.h
std::vector< std::string >
ReplayOutputStream::execute
StatusCode execute() override
Algorithm execution.
Definition: ReplayOutputStream.cpp:112
std::vector::begin
T begin(T... args)
ReplayOutputStream::start
StatusCode start() override
Algorithm initialization.
Definition: ReplayOutputStream.cpp:100
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:8
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
ReplayOutputStream::initialize
StatusCode initialize() override
Algorithm initialization.
Definition: ReplayOutputStream.cpp:83
IAlgManager.h
GaudiUtils::Map::clear
void clear()
Definition: Map.h:185
MSG::WARNING
Definition: IMessageSvc.h:15
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:86
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
ReplayOutputStream::m_evtMgr
SmartIF< IDataManagerSvc > m_evtMgr
Definition: ReplayOutputStream.h:59
std::for_each
T for_each(T... args)
ReplayOutputStream::m_algMgr
SmartIF< IAlgManager > m_algMgr
Definition: ReplayOutputStream.h:58
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
RecordOutputStream::locationRoot
static const std::string locationRoot()
Return the path in the Transient Store used to record the triggered instances.
Definition: RecordOutputStream.h:30