The Gaudi Framework  v33r1 (b1225454)
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, false ) {}
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 ( UNLIKELY( !m_algMgr ) ) return Error( "cannot retrieve IAlgManager" );
101 
102  m_evtMgr = evtSvc();
103  if ( UNLIKELY( !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 
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 ) {
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 // ============================================================================
virtual SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
#define UNLIKELY(x)
Definition: Kernel.h:106
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:34
Define general base for Gaudi exception.
OutStreamsMapType m_outputStreams
Internal storage for the OutputStreams to call.
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.
StatusCode finalize() override
Algorithm finalization.
StatusCode initialize() override
standard initialization method
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.
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
T end(T... args)
def start
Definition: IOTest.py:108
STL class.
#define DECLARE_COMPONENT(type)
StatusCode i_outStreamTransition()
Helper function to call the transition on the contained OutputStreams.
iterator end()
Definition: Map.h:140
Helper class to parse a string of format "type/name".
StatusCode stop() override
Algorithm finalization.
Gaudi::Property< std::vector< std::string > > m_outputStreamNames
const std::string & name() const
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
StatusCode finalize() override
standard finalization method
iterator find(const key_type &key)
Definition: Map.h:157
GAUDI_API const EventContext & currentContext()
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:32
void i_addOutputStream(const Gaudi::Utils::TypeNameString &outStream)
Add a new algorithm to the list of OutputStreams.
bool isSuccess() const
Definition: StatusCode.h:365
Helper class to fill the internal map of OutputStreams.
iterator begin()
Definition: Map.h:139
StatusCode execute() override
Algorithm execution.
T begin(T... args)
StatusCode start() override
Algorithm initialization.
constexpr static const auto FAILURE
Definition: StatusCode.h:101
StatusCode initialize() override
Algorithm initialization.
void clear()
Definition: Map.h:195
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
bool isFailure() const
Definition: StatusCode.h:145
SmartIF< IDataManagerSvc > m_evtMgr
T for_each(T... args)
SmartIF< IAlgManager > m_algMgr
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
static const std::string locationRoot()
Return the path in the Transient Store used to record the triggered instances.