All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ReplayOutputStream.cpp
Go to the documentation of this file.
1 // Include files
2 
3 // From Gaudi
8 // local
9 #include "ReplayOutputStream.h"
10 #include "RecordOutputStream.h"
11 
12 #include <algorithm>
13 #include <functional>
14 #include <list>
15 
16 // ----------------------------------------------------------------------------
17 // Implementation file for class: ReplayOutputStream
18 //
19 // 30/08/2013: Marco Clemencic
20 // ----------------------------------------------------------------------------
22 
23 // ============================================================================
24 // Standard constructor, initializes variables
25 // ============================================================================
26 ReplayOutputStream::ReplayOutputStream(const std::string& name, ISvcLocator* pSvcLocator)
27  : GaudiAlgorithm(name, pSvcLocator)
28 {
29  declareProperty("OutputStreams", m_outputStreamNames,
30  "OutputStream instances that can be called.");
31 }
32 
33 // ============================================================================
34 // Destructor
35 // ============================================================================
37 
38 namespace {
39 
41  template <Gaudi::StateMachine::Transition TR>
42  class OutStreamTransition {
43  public:
45  OutStreamTransition(MsgStream &msg):
46  m_msg(msg),
47  m_code(StatusCode::SUCCESS, false)
48  {}
49 
50  void operator() (ItemType &item);
51 
52  StatusCode result() const { return m_code; }
53  private:
54  MsgStream &m_msg;
55  StatusCode m_code;
56  };
57 
58  template <>
59  void OutStreamTransition<Gaudi::StateMachine::INITIALIZE>::operator() (ItemType &item) {
60  const StatusCode sc = item.second->sysInitialize();
61  if (sc.isFailure()) {
62  m_msg << MSG::WARNING << "Failed to initialize " << item.first << endmsg;
63  m_code = sc;
64  }
65  }
66  template <>
67  void OutStreamTransition<Gaudi::StateMachine::START>::operator() (ItemType &item) {
68  const StatusCode sc = item.second->sysStart();
69  if (sc.isFailure()) {
70  m_msg << MSG::WARNING << "Failed to start " << item.first << endmsg;
71  m_code = sc;
72  }
73  }
74  template <>
75  void OutStreamTransition<Gaudi::StateMachine::STOP>::operator() (ItemType &item) {
76  const StatusCode sc = item.second->sysStop();
77  if (sc.isFailure()) {
78  m_msg << MSG::WARNING << "Failed to stop " << item.first << endmsg;
79  m_code = sc;
80  }
81  }
82  template <>
83  void OutStreamTransition<Gaudi::StateMachine::FINALIZE>::operator() (ItemType &item) {
84  const StatusCode sc = item.second->sysFinalize();
85  if (sc.isFailure()) {
86  m_msg << MSG::WARNING << "Failed to finalize " << item.first << endmsg;
87  m_code = sc;
88  }
89  }
90 
91 }
92 
93 template <Gaudi::StateMachine::Transition TR>
95  OutStreamTransition<TR> trans(msg());
96  std::for_each(m_outputStreams.begin(), m_outputStreams.end(), trans);
97  return trans.result();
98 }
99 
100 // ============================================================================
101 // Initialization
102 // ============================================================================
104  StatusCode sc = GaudiAlgorithm::initialize(); // must be executed first
105  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
106 
107  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Initialize" << endmsg;
108 
109  m_algMgr = service("ApplicationMgr");
110  if (UNLIKELY(!m_algMgr.isValid())) {
111  return Error("cannot retrieve IAlgManager");
112  }
113 
114  m_evtMgr = evtSvc();
115  if (UNLIKELY(!m_algMgr.isValid())) {
116  return Error("cannot retrieve IDataManagerSvc ");
117  }
118 
119  std::for_each(m_outputStreamNames.begin(), m_outputStreamNames.end(),
120  OutStreamAdder(this));
121 
122  return i_outStreamTransition<Gaudi::StateMachine::INITIALIZE>();
123 }
124 
126  StatusCode sc = GaudiAlgorithm::start(); // must be executed first
127  if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
128 
129  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Start" << endmsg;
130 
131  return i_outStreamTransition<Gaudi::StateMachine::START>();
132 }
133 
134 
135 namespace {
138  struct OutputStreamsCollector: public IDataStoreAgent {
139  std::list<std::string> names;
140  virtual bool analyse(IRegistry* pRegistry, int lvl) {
141  if (lvl > 0)
142  names.push_back(pRegistry->name());
143  return true;
144  }
145  };
146 }
147 
148 // ============================================================================
149 // Main execution
150 // ============================================================================
152  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Execute" << endmsg;
153 
154  OutputStreamsCollector collector;
155  m_evtMgr->traverseSubTree(RecordOutputStream::locationRoot(), &collector);
156 
157  std::for_each(collector.names.begin(), collector.names.end(),
158  OutStreamTrigger(this));
159 
160  return StatusCode::SUCCESS;
161 }
162 
163 // ============================================================================
164 // Finalize
165 // ============================================================================
167  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Finalize" << endmsg;
168 
169  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::FINALIZE>();
170 
171  // release interfaces
173  m_algMgr.reset();
174  m_evtMgr.reset();
175 
176  StatusCode fsc = GaudiAlgorithm::finalize(); // must be called after all other actions
177  if (sc.isSuccess()) sc = fsc;
178  return sc;
179 }
180 
182  if ( msgLevel(MSG::DEBUG) ) debug() << "==> Stop" << endmsg;
183 
184  StatusCode sc = i_outStreamTransition<Gaudi::StateMachine::STOP>();
185 
186  StatusCode ssc = GaudiAlgorithm::stop(); // must be called after all other actions
187  if (sc.isSuccess()) sc = ssc;
188  return sc;
189 }
190 
192  // we prepend '/' to the name of the algorithm to simplify the handling in
193  // OutputStreamsCollector
194  const std::string algId = "/" + outStream.name();
195  if (m_outputStreams.find(algId) == m_outputStreams.end()) {
196  if (!(m_outputStreams[algId] = m_algMgr->algorithm(outStream)).isValid()){
197  throw GaudiException(name(), "Could not get algorithm " + outStream.name(),
199  }
200  } else {
201  warning() << "OutputStream instance " << outStream.name()
202  << " already added, ignoring " << outStream << endmsg;
203  }
204 }
205 
206 // ============================================================================
MsgStream & warning() const
shortcut for the method msgStream ( MSG::WARNING )
Definition: GaudiCommon.h:495
#define UNLIKELY(x)
Definition: Kernel.h:127
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
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:26
virtual StatusCode start()
the default (empty) implementation of IStateful::start() method
Definition: Algorithm.h:159
virtual StatusCode finalize()
Algorithm finalization.
OutStreamsMapType m_outputStreams
Internal storage for the OutputStreams to call.
Helper class to call the required OutputStream.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:62
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.
virtual StatusCode initialize()
standard initialization method
virtual ~ReplayOutputStream()
Destructor.
virtual const name_type & name() const =0
Name of the directory (or key)
std::string names[100]
Definition: Node.cpp:19
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:72
virtual StatusCode stop()
the default (empty) implementation of IStateful::stop() method
Definition: Algorithm.h:161
MSG::Level msgLevel() const
The current message service output level.
Definition: GaudiCommon.h:532
StatusCode i_outStreamTransition()
Helper function to call the transition on the contained OutputStreams.
virtual StatusCode finalize()
standard finalization method
iterator end()
Definition: Map.h:131
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:9
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
virtual StatusCode execute()
Algorithm execution.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
std::pair< const std::string, SmartIF< IAlgorithm > > value_type
Definition: Map.h:94
iterator find(const key_type &key)
Definition: Map.h:148
#define DECLARE_ALGORITHM_FACTORY(x)
Definition: Algorithm.h:605
virtual const std::string & name() const
The identifying name of the algorithm object.
Definition: Algorithm.cpp:837
The useful base class for data processing algorithms.
MsgStream & msg() const
shortcut for the method msgStream ( MSG::INFO )
Definition: GaudiCommon.h:503
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
MsgStream & debug() const
shortcut for the method msgStream ( MSG::DEBUG )
Definition: GaudiCommon.h:499
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:130
SmartIF< IDataProviderSvc > & evtSvc() const
shortcut for method eventSvc
Definition: Algorithm.h:257
Generic data agent interface.
tuple item
print s1,s2
Definition: ana.py:146
void clear()
Definition: Map.h:176
const std::string & name() const
SmartIF< IDataManagerSvc > m_evtMgr
StatusCode service(const std::string &name, T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Algorithm.h:207
SmartIF< IAlgManager > m_algMgr
void reset(TYPE *ptr=0)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:74
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
static const std::string locationRoot()
Return the path in the Transient Store used to record the triggered instances.
virtual StatusCode initialize()
Algorithm initialization.