The Gaudi Framework  v38r1p1 (ae26267b)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
TestingAlgs.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 /*
12  * TestingAlg1.cpp
13  *
14  * Created on: Sep 7, 2009
15  * Author: Marco Clemencic
16  */
17 
18 #include "GaudiKernel/Algorithm.h"
21 #include "GaudiKernel/Incident.h"
22 #include "GaudiKernel/Memory.h"
23 #include "GaudiKernel/Sleep.h"
24 
25 #include <iostream>
26 
27 #include <csignal>
28 
29 namespace GaudiTesting {
30 
31  class DestructorCheckAlg : public Algorithm {
32  public:
34  ~DestructorCheckAlg() override {
35  // do not print messages if we are created in genconf
36  if ( System::cmdLineArgs()[0].find( "genconf" ) == std::string::npos ) {
37  std::cout << "Destructor of " << name() << std::endl;
38  }
39  }
40  StatusCode execute() override {
41  info() << "Executing " << name() << endmsg;
42  return StatusCode::SUCCESS;
43  }
44  };
45 
46  class SleepyAlg : public Algorithm {
47  public:
49  StatusCode execute() override {
50  info() << "Executing event " << ++m_counter << endmsg;
51  info() << "Sleeping for " << m_sleep.value() << " seconds" << endmsg;
53  info() << "Back from sleep" << endmsg;
54  return StatusCode::SUCCESS;
55  }
56 
57  private:
58  Gaudi::Property<int> m_sleep{ this, "SleepTime", 10, "Seconds to sleep during the execute" };
59  int m_counter = 0;
60  };
61 
65  class SignallingAlg : public Algorithm {
66  public:
68  StatusCode execute() override {
69  if ( m_eventCount <= 0 ) {
70  info() << "Raising signal now" << endmsg;
72  } else {
73  info() << m_eventCount.value() << " events to go" << endmsg;
74  }
75  --m_eventCount;
76  return StatusCode::SUCCESS;
77  }
78 
79  private:
80  Gaudi::Property<int> m_eventCount{ this, "EventCount", 3, "Number of events to let go before raising the signal" };
81  Gaudi::Property<int> m_signal{ this, "Signal", SIGINT, "Signal to raise" };
82  };
83 
84  class StopLoopAlg : public Algorithm {
85  public:
87  StatusCode execute() override {
88  if ( m_eventCount <= 0 ) {
89  info() << "Stopping loop with " << m_mode.value() << endmsg;
90  if ( m_mode == "exception" ) {
91  throw GaudiException( "Stopping loop", name(), StatusCode::FAILURE );
92  } else if ( m_mode == "stopRun" ) {
93  auto ep = serviceLocator()->as<IEventProcessor>();
94  if ( !ep ) {
95  error() << "Cannot get IEventProcessor" << endmsg;
96  return StatusCode::FAILURE;
97  }
98  if ( auto sc = ep->stopRun(); !sc ) return sc;
99  } else { // "failure"
100  return StatusCode::FAILURE;
101  }
102  } else {
103  info() << m_eventCount.value() << " events to go" << endmsg;
104  }
105  --m_eventCount;
106  return StatusCode::SUCCESS;
107  }
108 
109  private:
110  Gaudi::Property<int> m_eventCount{ this, "EventCount", 3,
111  "Number of events to let go before breaking the event loop" };
112  Gaudi::Property<std::string> m_mode{ this, "Mode", "failure",
113  "Type of interruption ['exception', 'stopRun', 'failure']" };
114  };
115 
116  class CustomIncidentAlg : public Algorithm {
117  public:
118  using Algorithm::Algorithm;
119  StatusCode initialize() override {
121  if ( sc.isFailure() ) return sc;
122 
123  if ( m_incident.empty() ) {
124  error() << "The incident type (property Incident) must be declared." << endmsg;
125  return StatusCode::FAILURE;
126  }
127 
128  m_incidentSvc = service( "IncidentSvc" );
129  if ( !m_incidentSvc ) return StatusCode::FAILURE;
130 
131  return StatusCode::SUCCESS;
132  }
133  StatusCode execute() override {
134  if ( m_eventCount == 0 ) {
135  info() << "Firing incident " << m_incident.value() << endmsg;
136  m_incidentSvc->fireIncident( Incident( name(), m_incident ) );
137  } else if ( m_eventCount > 0 ) {
138  info() << m_eventCount.value() << " events to go" << endmsg;
139  } else {
140  info() << "keep processing events..." << endmsg;
141  }
142  --m_eventCount;
143  return StatusCode::SUCCESS;
144  }
145  StatusCode finalize() override {
147  return Algorithm::finalize();
148  }
149 
150  private:
151  Gaudi::Property<int> m_eventCount{ this, "EventCount", 3,
152  "Number of events to let go before firing the incident." };
153  Gaudi::Property<std::string> m_incident{ this, "Incident", "", "Type of incident to fire." };
156  };
157 
161  class PutDataObjectAlg : public Algorithm {
162  public:
163  using Algorithm::Algorithm;
164 
165  StatusCode initialize() override {
167  if ( sc.isFailure() ) return sc;
168 
170  if ( !m_dataProvider ) return StatusCode::FAILURE;
171 
172  return StatusCode::SUCCESS;
173  }
174 
175  StatusCode execute() override {
177  info() << "Adding " << m_paths.size() << " objects to " << m_dataSvc.value() << endmsg;
178  for ( auto& p : m_paths ) {
179  info() << "Adding '" << p << "'" << endmsg;
180  DataObject* obj = new DataObject();
181  sc = m_dataProvider->registerObject( p, obj );
182  if ( sc.isFailure() ) warning() << "Cannot register object '" << p << "'" << endmsg;
183  }
184 
185  return sc;
186  }
187 
188  StatusCode finalize() override {
190  return Algorithm::finalize();
191  }
192 
193  private:
195  this, "Paths", {}, "List of paths in the transient store to load" };
196  Gaudi::Property<std::string> m_dataSvc{ this, "DataSvc", "EventDataSvc", "Name of the data service to use" };
198  };
199 
203  class GetDataObjectAlg : public Algorithm {
204  public:
205  using Algorithm::Algorithm;
206 
207  StatusCode initialize() override {
209  if ( sc.isFailure() ) return sc;
210 
212  if ( !m_dataProvider ) return StatusCode::FAILURE;
213 
214  return StatusCode::SUCCESS;
215  }
216 
217  StatusCode execute() override {
218  info() << "Getting " << m_paths.size() << " objects from " << m_dataSvc.value() << endmsg;
219  bool missing = false;
220  for ( auto& p : m_paths ) {
221  info() << "Getting '" << p << "'" << endmsg;
222  DataObject* obj;
223  StatusCode sc = m_dataProvider->retrieveObject( p, obj );
224  if ( sc.isFailure() ) {
225  warning() << "Cannot retrieve object '" << p << "'" << endmsg;
226  missing = true;
227  }
228  }
229 
230  return ( missing && !m_ignoreMissing ) ? StatusCode::FAILURE : StatusCode::SUCCESS;
231  }
232 
233  StatusCode finalize() override {
235  return Algorithm::finalize();
236  }
237 
238  private:
240  this, "Paths", {}, "List of paths in the transient store to load" };
241  Gaudi::Property<std::string> m_dataSvc{ this, "DataSvc", "EventDataSvc", "Name of the data service to use" };
242  Gaudi::Property<bool> m_ignoreMissing{ this, "IgnoreMissing", false,
243  "if True, missing objects will not beconsidered an error" };
245  };
246 
247  class OddEventsFilter : public Algorithm {
248  public:
249  using Algorithm::Algorithm;
250  StatusCode initialize() override {
251  m_counter = 0;
252  return Algorithm::initialize();
253  }
254  StatusCode execute() override {
255  setFilterPassed( ( ++m_counter ) % 2 );
256  return StatusCode::SUCCESS;
257  }
258 
259  protected:
260  int m_counter = 0;
261  };
262 
264  public:
265  using OddEventsFilter::OddEventsFilter;
266  StatusCode execute() override {
267  setFilterPassed( ( ( ++m_counter ) % 2 ) == 0 );
268  return StatusCode::SUCCESS;
269  }
270  };
271 
275  class ListTools : public Algorithm {
276  public:
277  using Algorithm::Algorithm;
278 
279  StatusCode execute() override {
281  info() << "All tool instances:" << endmsg;
282  for ( auto& tool : toolSvc()->getTools() ) { info() << " " << tool->name() << endmsg; }
283  return sc;
284  }
285  };
286 
290  class PrintMemoryUsage : public Algorithm {
291  public:
292  using Algorithm::Algorithm;
293  StatusCode initialize() override {
294  m_counter = 0;
295  return Algorithm::initialize();
296  }
297  StatusCode execute() override {
298  if ( ( m_frequency <= 1 ) || ( ( m_counter ) % m_frequency == 0 ) ) print();
299  return StatusCode::SUCCESS;
300  }
301  StatusCode finalize() override {
302  print();
303  return Algorithm::finalize();
304  }
305 
306  protected:
307  Gaudi::Property<int> m_frequency{ this, "Frequency", 1, "How often to print the memory usage (number of events)" };
308  int m_counter = 0;
309  void print() {
310  info() << "vmem: " << System::virtualMemory() << " kB" << endmsg;
311  info() << "rss: " << System::mappedMemory() << " kB" << endmsg;
312  }
313  };
314 } // namespace GaudiTesting
315 
316 namespace GaudiTesting {
317  DECLARE_COMPONENT( DestructorCheckAlg )
318  DECLARE_COMPONENT( SleepyAlg )
319  DECLARE_COMPONENT( SignallingAlg )
320  DECLARE_COMPONENT( StopLoopAlg )
321  DECLARE_COMPONENT( CustomIncidentAlg )
322  DECLARE_COMPONENT( PutDataObjectAlg )
323  DECLARE_COMPONENT( GetDataObjectAlg )
324  DECLARE_COMPONENT( OddEventsFilter )
325  DECLARE_COMPONENT( EvenEventsFilter )
326  DECLARE_COMPONENT( ListTools )
327  DECLARE_COMPONENT( PrintMemoryUsage )
328 } // namespace GaudiTesting
GaudiTesting::DestructorCheckAlg::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:40
IEventProcessor
Definition: IEventProcessor.h:24
GaudiTesting::PrintMemoryUsage::m_counter
int m_counter
Definition: TestingAlgs.cpp:308
GaudiTesting::PrintMemoryUsage::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:293
Gaudi::Algorithm::toolSvc
SmartIF< IToolSvc > & toolSvc() const
The standard ToolSvc service, Return a pointer to the service if present.
Definition: Algorithm.cpp:566
GaudiTesting::PutDataObjectAlg::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:175
GaudiTesting::CustomIncidentAlg::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:119
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:528
Gaudi::details::LegacyAlgorithmAdapter::setFilterPassed
void setFilterPassed(bool state) const
Set the filter passed flag to the specified state.
Definition: LegacyAlgorithm.cpp:30
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
GaudiTesting::GetDataObjectAlg::m_dataProvider
SmartIF< IDataProviderSvc > m_dataProvider
Definition: TestingAlgs.cpp:244
GaudiTesting
Definition: __init__.py:1
Gaudi::Algorithm::initialize
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:178
GaudiException
Definition: GaudiException.h:31
Algorithm
Alias for backward compatibility.
Definition: Algorithm.h:58
Memory.h
Algorithm.h
GaudiTesting::CustomIncidentAlg::m_incidentSvc
SmartIF< IIncidentSvc > m_incidentSvc
Incident service.
Definition: TestingAlgs.cpp:155
GaudiTesting::SignallingAlg::m_signal
Gaudi::Property< int > m_signal
Definition: TestingAlgs.cpp:81
std::raise
T raise(T... args)
Gaudi::Algorithm::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
The standard service locator.
Definition: Algorithm.cpp:572
GaudiTesting::SignallingAlg::m_eventCount
Gaudi::Property< int > m_eventCount
Definition: TestingAlgs.cpp:80
GaudiTesting::EvenEventsFilter
Definition: TestingAlgs.cpp:263
GaudiTesting::ListTools
Simple algorithm that creates dummy objects in the transient store.
Definition: TestingAlgs.cpp:275
GaudiTesting::CustomIncidentAlg
Definition: TestingAlgs.cpp:116
GaudiTesting::GetDataObjectAlg::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:207
GaudiTesting::DestructorCheckAlg::~DestructorCheckAlg
~DestructorCheckAlg() override
Definition: TestingAlgs.cpp:34
GaudiTesting::PrintMemoryUsage
Simple algorithm that prints the memory usage every N events (property "Frequency").
Definition: TestingAlgs.cpp:290
GaudiTesting::CustomIncidentAlg::m_eventCount
Gaudi::Property< int > m_eventCount
Definition: TestingAlgs.cpp:151
GaudiTesting::SleepyAlg::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:49
GaudiTesting::PutDataObjectAlg
Simple algorithm that creates dummy objects in the transient store.
Definition: TestingAlgs.cpp:161
GaudiTesting::PrintMemoryUsage::m_frequency
Gaudi::Property< int > m_frequency
Definition: TestingAlgs.cpp:307
IIncidentSvc.h
GaudiTesting::CustomIncidentAlg::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:133
GaudiTesting::GetDataObjectAlg::m_dataSvc
Gaudi::Property< std::string > m_dataSvc
Definition: TestingAlgs.cpp:241
bug_34121.tool
tool
Definition: bug_34121.py:17
GaudiTesting::DestructorCheckAlg
Definition: TestingAlgs.cpp:31
GaudiTesting::OddEventsFilter
Definition: TestingAlgs.cpp:247
StatusCode
Definition: StatusCode.h:65
GaudiTesting::SleepyAlg::m_sleep
Gaudi::Property< int > m_sleep
Definition: TestingAlgs.cpp:58
std::cout
GaudiTesting::PutDataObjectAlg::m_dataSvc
Gaudi::Property< std::string > m_dataSvc
Definition: TestingAlgs.cpp:196
GaudiTesting::CustomIncidentAlg::m_incident
Gaudi::Property< std::string > m_incident
Definition: TestingAlgs.cpp:153
GaudiTesting::CustomIncidentAlg::finalize
StatusCode finalize() override
Definition: TestingAlgs.cpp:145
GaudiTesting::StopLoopAlg::m_mode
Gaudi::Property< std::string > m_mode
Definition: TestingAlgs.cpp:112
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:239
GaudiTesting::ListTools::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:279
SmartIF< IIncidentSvc >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
GaudiTesting::SleepyAlg::m_counter
int m_counter
Definition: TestingAlgs.cpp:59
GaudiTesting::SleepyAlg
Definition: TestingAlgs.cpp:46
GaudiTesting::StopLoopAlg
Definition: TestingAlgs.cpp:84
GaudiTesting::OddEventsFilter::m_counter
int m_counter
Definition: TestingAlgs.cpp:260
GaudiTesting::StopLoopAlg::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:87
GaudiTesting::PutDataObjectAlg::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:165
GaudiTesting::StopLoopAlg::m_eventCount
Gaudi::Property< int > m_eventCount
Definition: TestingAlgs.cpp:110
GaudiTesting::SignallingAlg
Simple algorithm that raise a signal after N events.
Definition: TestingAlgs.cpp:65
SmartIF::as
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:117
GaudiTesting::PutDataObjectAlg::m_dataProvider
SmartIF< IDataProviderSvc > m_dataProvider
Definition: TestingAlgs.cpp:197
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
GaudiTesting::OddEventsFilter::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:254
Gaudi::Algorithm::finalize
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition: Algorithm.h:184
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
std::endl
T endl(T... args)
Sleep.h
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
GaudiTesting::GetDataObjectAlg
Simple algorithm that retrieves objects from the transient store.
Definition: TestingAlgs.cpp:203
System::cmdLineArgs
GAUDI_API const std::vector< std::string > cmdLineArgs()
Command line arguments including executable name as arg[0] as vector of strings.
Definition: System.cpp:363
GaudiTesting::PrintMemoryUsage::finalize
StatusCode finalize() override
Definition: TestingAlgs.cpp:301
Gaudi::Algorithm::service
StatusCode service(std::string_view name, T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Algorithm.h:205
GaudiTesting::PutDataObjectAlg::finalize
StatusCode finalize() override
Definition: TestingAlgs.cpp:188
DataObject
Definition: DataObject.h:40
GaudiTesting::GetDataObjectAlg::m_paths
Gaudi::Property< std::vector< std::string > > m_paths
Definition: TestingAlgs.cpp:239
System::mappedMemory
GAUDI_API long mappedMemory(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:197
GaudiTesting::PutDataObjectAlg::m_paths
Gaudi::Property< std::vector< std::string > > m_paths
Definition: TestingAlgs.cpp:194
Gaudi::Sleep
GAUDI_API void Sleep(int sec)
Simple sleep function.
Definition: Sleep.cpp:17
GaudiTesting::PrintMemoryUsage::print
void print()
Definition: TestingAlgs.cpp:309
Gaudi::Algorithm::Algorithm
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition: Algorithm.h:101
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Incident.h
GaudiTesting::GetDataObjectAlg::finalize
StatusCode finalize() override
Definition: TestingAlgs.cpp:233
Incident
Definition: Incident.h:27
GaudiTesting::GetDataObjectAlg::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:217
System::virtualMemory
GAUDI_API long virtualMemory(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition: Memory.cpp:228
GaudiTesting::SignallingAlg::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:68
IEventProcessor.h
GaudiTesting::PrintMemoryUsage::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:297
Gaudi::Property< int >
GaudiTesting::EvenEventsFilter::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:266
GaudiTesting::OddEventsFilter::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:250
GaudiTesting::GetDataObjectAlg::m_ignoreMissing
Gaudi::Property< bool > m_ignoreMissing
Definition: TestingAlgs.cpp:242