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 #include "GaudiKernel/IDataStoreAgent.h"
7 // local
8 #include "ReplayOutputStream.h"
9 #include "RecordOutputStream.h"
10 
11 #include <algorithm>
12 #include <functional>
13 #include <list>
14 
15 // ----------------------------------------------------------------------------
16 // Implementation file for class: ReplayOutputStream
17 //
18 // 30/08/2013: Marco Clemencic
19 // ----------------------------------------------------------------------------
21 
22 // ============================================================================
23 // Standard constructor, initializes variables
24 // ============================================================================
26  : GaudiAlgorithm(name, pSvcLocator)
27 {
28  declareProperty("OutputStreams", m_outputStreamNames,
29  "OutputStream instances that can be called.");
30 }
31 
32 // ============================================================================
33 // Destructor
34 // ============================================================================
36 
37 namespace {
38 
40  template <Gaudi::StateMachine::Transition TR>
41  class OutStreamTransition {
42  public:
44  OutStreamTransition(MsgStream &msg):
45  m_msg(msg),
46  m_code(StatusCode::SUCCESS, false)
47  {}
48 
49  void operator() (ItemType &item);
50 
51  StatusCode result() const { return m_code; }
52  private:
53  MsgStream &m_msg;
54  StatusCode m_code;
55  };
56 
57  template <>
58  void OutStreamTransition<Gaudi::StateMachine::INITIALIZE>::operator() (ItemType &item) {
59  const StatusCode sc = item.second->sysInitialize();
60  if (sc.isFailure()) {
61  m_msg << MSG::WARNING << "Failed to initialize " << item.first << endmsg;
62  m_code = sc;
63  }
64  }
65  template <>
66  void OutStreamTransition<Gaudi::StateMachine::START>::operator() (ItemType &item) {
67  const StatusCode sc = item.second->sysStart();
68  if (sc.isFailure()) {
69  m_msg << MSG::WARNING << "Failed to start " << item.first << endmsg;
70  m_code = sc;
71  }
72  }
73  template <>
74  void OutStreamTransition<Gaudi::StateMachine::STOP>::operator() (ItemType &item) {
75  const StatusCode sc = item.second->sysStop();
76  if (sc.isFailure()) {
77  m_msg << MSG::WARNING << "Failed to stop " << item.first << endmsg;
78  m_code = sc;
79  }
80  }
81  template <>
82  void OutStreamTransition<Gaudi::StateMachine::FINALIZE>::operator() (ItemType &item) {
83  const StatusCode sc = item.second->sysFinalize();
84  if (sc.isFailure()) {
85  m_msg << MSG::WARNING << "Failed to finalize " << item.first << endmsg;
86  m_code = sc;
87  }
88  }
89 
90 }
91 
92 template <Gaudi::StateMachine::Transition TR>
94  OutStreamTransition<TR> trans(msg());
95  std::for_each(m_outputStreams.begin(), m_outputStreams.end(), trans);
96  return trans.result();
97 }
98 
99 // ============================================================================
100 // Initialization
101 // ============================================================================
103  StatusCode sc = GaudiAlgorithm::initialize(); // must be executed first
104  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
105 
106  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Initialize" << endmsg;
107 
108  m_algMgr = service("ApplicationMgr");
109  if (UNLIKELY(!m_algMgr)) return Error("cannot retrieve IAlgManager");
110 
111  m_evtMgr = evtSvc();
112  if (UNLIKELY(!m_evtMgr)) return Error("cannot retrieve IDataManagerSvc ");
113 
114  std::for_each(m_outputStreamNames.begin(), m_outputStreamNames.end(),
115  OutStreamAdder(this));
116 
117  return i_outStreamTransition<Gaudi::StateMachine::INITIALIZE>();
118 }
119 
121  StatusCode sc = GaudiAlgorithm::start(); // must be executed first
122  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
123 
124  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Start" << endmsg;
125 
126  return i_outStreamTransition<Gaudi::StateMachine::START>();
127 }
128 
129 
130 namespace {
133  struct OutputStreamsCollector: public IDataStoreAgent {
134  std::list<std::string> names;
135  virtual bool analyse(IRegistry* pRegistry, int lvl) {
136  if (lvl > 0)
137  names.push_back(pRegistry->name());
138  return true;
139  }
140  };
141 }
142 
143 // ============================================================================
144 // Main execution
145 // ============================================================================
147  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Execute" << endmsg;
148 
149  OutputStreamsCollector collector;
151 
152  std::for_each(collector.names.begin(), collector.names.end(),
153  OutStreamTrigger(this));
154 
155  return StatusCode::SUCCESS;
156 }
157 
158 // ============================================================================
159 // Finalize
160 // ============================================================================
162  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Finalize" << endmsg;
163 
164  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::FINALIZE>();
165 
166  // release interfaces
168  m_algMgr.reset();
169  m_evtMgr.reset();
170 
171  StatusCode fsc = GaudiAlgorithm::finalize(); // must be called after all other actions
172  if (sc.isSuccess()) sc = fsc;
173  return sc;
174 }
175 
177  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Stop" << endmsg;
178 
179  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::STOP>();
180 
181  StatusCode ssc = GaudiAlgorithm::stop(); // must be called after all other actions
182  if (sc.isSuccess()) sc = ssc;
183  return sc;
184 }
185 
187  // we prepend '/' to the name of the algorithm to simplify the handling in
188  // OutputStreamsCollector
189  const std::string algId = "/" + outStream.name();
190  if (m_outputStreams.find(algId) == m_outputStreams.end()) {
191  m_outputStreams[algId] = m_algMgr->algorithm(outStream);
192  if (!m_outputStreams[algId]) {
193  throw GaudiException(name(), "Could not get algorithm " + outStream.name(),
195  }
196  } else {
197  warning() << "OutputStream instance " << outStream.name()
198  << " already added, ignoring " << outStream << endmsg;
199  }
200 }
201 
202 // ============================================================================
virtual SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
#define DECLARE_ALGORITHM_FACTORY(x)
Definition: Algorithm.h:946
Define general base for Gaudi exception.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
virtual StatusCode finalize()
Algorithm finalization.
OutStreamsMapType m_outputStreams
Internal storage for the OutputStreams to call.
Helper class to call the required OutputStream.
StatusCode initialize() override
standard initialization method
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
virtual bool analyse(IRegistry *pObject, int level)=0
Analyse the data object.
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.
virtual StatusCode stop()
Algorithm finalization.
virtual StatusCode start()
Algorithm initialization.
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
virtual ~ReplayOutputStream()
Destructor.
STL namespace.
virtual const name_type & name() const =0
Name of the directory (or key)
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
StatusCode i_outStreamTransition()
Helper function to call the transition on the contained OutputStreams.
iterator end()
Definition: Map.h:132
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:9
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
virtual StatusCode execute()
Algorithm execution.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
std::pair< const K, T > value_type
Definition: Map.h:94
StatusCode finalize() override
standard finalization method
iterator find(const key_type &key)
Definition: Map.h:149
The useful base class for data processing algorithms.
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
void i_addOutputStream(const Gaudi::Utils::TypeNameString &outStream)
Add a new algorithm to the list of OutputStreams.
std::vector< std::string > m_outputStreamNames
(property) Type/Name list of OutputStream we have to call.
Helper class to fill the internal map of OutputStreams.
iterator begin()
Definition: Map.h:131
Generic data agent interface.
tuple item
print s1,s2
Definition: ana.py:146
#define UNLIKELY(x)
Definition: Kernel.h:126
void clear()
Definition: Map.h:178
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:88
const std::string & name() const
SmartIF< IDataManagerSvc > m_evtMgr
virtual StatusCode traverseSubTree(const std::string &sub_path, IDataStoreAgent *pAgent)=0
Analyse by traversing all data objects below the sub tree identified by its full path name...
SmartIF< IAlgManager > m_algMgr
tuple start
Definition: IOTest.py:88
static const std::string locationRoot()
Return the path in the Transient Store used to record the triggered instances.
virtual StatusCode initialize()
Algorithm initialization.
MSG::Level msgLevel() const
get the output level from the embedded MsgStream