The Gaudi Framework  v36r7 (7f57a304)
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 
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 
32  public:
34  ~DestructorCheckAlg() override {
35  // do not print messages if we are created in genconf
36  const std::string cmd = System::cmdLineArgs()[0];
37  if ( cmd.find( "genconf" ) != std::string::npos ) return;
38 
39  std::cout << "Destructor of " << name() << std::endl;
40  }
41  StatusCode execute() override {
42  info() << "Executing " << name() << endmsg;
43  return StatusCode::SUCCESS;
44  }
45  };
46 
47  class SleepyAlg : public GaudiAlgorithm {
48  public:
50  StatusCode execute() override {
51  info() << "Executing event " << ++m_counter << endmsg;
52  info() << "Sleeping for " << m_sleep.value() << " seconds" << endmsg;
54  info() << "Back from sleep" << endmsg;
55  return StatusCode::SUCCESS;
56  }
57 
58  private:
59  Gaudi::Property<int> m_sleep{ this, "SleepTime", 10, "Seconds to sleep during the execute" };
60  int m_counter = 0;
61  };
62 
66  class SignallingAlg : public GaudiAlgorithm {
67  public:
69  StatusCode execute() override {
70  if ( m_eventCount <= 0 ) {
71  info() << "Raising signal now" << endmsg;
73  } else {
74  info() << m_eventCount.value() << " events to go" << endmsg;
75  }
76  --m_eventCount;
77  return StatusCode::SUCCESS;
78  }
79 
80  private:
81  Gaudi::Property<int> m_eventCount{ this, "EventCount", 3, "Number of events to let go before raising the signal" };
82  Gaudi::Property<int> m_signal{ this, "Signal", SIGINT, "Signal to raise" };
83  };
84 
85  class StopLoopAlg : public GaudiAlgorithm {
86  public:
88  StatusCode execute() override {
89  if ( m_eventCount <= 0 ) {
90  info() << "Stopping loop with " << m_mode.value() << endmsg;
91  if ( m_mode == "exception" ) {
92  Exception( "Stopping loop" );
93  } else if ( m_mode == "stopRun" ) {
94  auto ep = serviceLocator()->as<IEventProcessor>();
95  if ( !ep ) {
96  error() << "Cannot get IEventProcessor" << endmsg;
97  return StatusCode::FAILURE;
98  }
99  if ( auto sc = ep->stopRun(); !sc ) return sc;
100  } else { // "failure"
101  return StatusCode::FAILURE;
102  }
103  } else {
104  info() << m_eventCount.value() << " events to go" << endmsg;
105  }
106  --m_eventCount;
107  return StatusCode::SUCCESS;
108  }
109 
110  private:
111  Gaudi::Property<int> m_eventCount{ this, "EventCount", 3,
112  "Number of events to let go before breaking the event loop" };
113  Gaudi::Property<std::string> m_mode{ this, "Mode", "failure",
114  "Type of interruption ['exception', 'stopRun', 'failure']" };
115  };
116 
118  public:
120  StatusCode initialize() override {
122  if ( sc.isFailure() ) return sc;
123 
124  if ( m_incident.empty() ) {
125  error() << "The incident type (property Incident) must be declared." << endmsg;
126  return StatusCode::FAILURE;
127  }
128 
129  m_incidentSvc = service( "IncidentSvc" );
130  if ( !m_incidentSvc ) return StatusCode::FAILURE;
131 
132  return StatusCode::SUCCESS;
133  }
134  StatusCode execute() override {
135  if ( m_eventCount == 0 ) {
136  info() << "Firing incident " << m_incident.value() << endmsg;
137  m_incidentSvc->fireIncident( Incident( name(), m_incident ) );
138  } else if ( m_eventCount > 0 ) {
139  info() << m_eventCount.value() << " events to go" << endmsg;
140  } else {
141  info() << "keep processing events..." << endmsg;
142  }
143  --m_eventCount;
144  return StatusCode::SUCCESS;
145  }
146  StatusCode finalize() override {
148  return GaudiAlgorithm::finalize();
149  }
150 
151  private:
152  Gaudi::Property<int> m_eventCount{ this, "EventCount", 3,
153  "Number of events to let go before firing the incident." };
154  Gaudi::Property<std::string> m_incident{ this, "Incident", "", "Type of incident to fire." };
157  };
158 
163  public:
165 
166  StatusCode initialize() override {
168  if ( sc.isFailure() ) return sc;
169 
171  if ( !m_dataProvider ) return StatusCode::FAILURE;
172 
173  return StatusCode::SUCCESS;
174  }
175 
176  StatusCode execute() override {
178  info() << "Adding " << m_paths.size() << " objects to " << m_dataSvc.value() << endmsg;
179  for ( auto& p : m_paths ) {
180  info() << "Adding '" << p << "'" << endmsg;
181  DataObject* obj = new DataObject();
182  sc = m_dataProvider->registerObject( p, obj );
183  if ( sc.isFailure() ) warning() << "Cannot register object '" << p << "'" << endmsg;
184  }
185 
186  return sc;
187  }
188 
189  StatusCode finalize() override {
191  return GaudiAlgorithm::finalize();
192  }
193 
194  private:
196  this, "Paths", {}, "List of paths in the transient store to load" };
197  Gaudi::Property<std::string> m_dataSvc{ this, "DataSvc", "EventDataSvc", "Name of the data service to use" };
199  };
200 
205  public:
207 
208  StatusCode initialize() override {
210  if ( sc.isFailure() ) return sc;
211 
213  if ( !m_dataProvider ) return StatusCode::FAILURE;
214 
215  return StatusCode::SUCCESS;
216  }
217 
218  StatusCode execute() override {
219  info() << "Getting " << m_paths.size() << " objects from " << m_dataSvc.value() << endmsg;
220  bool missing = false;
221  for ( auto& p : m_paths ) {
222  info() << "Getting '" << p << "'" << endmsg;
223  DataObject* obj;
224  StatusCode sc = m_dataProvider->retrieveObject( p, obj );
225  if ( sc.isFailure() ) {
226  warning() << "Cannot retrieve object '" << p << "'" << endmsg;
227  missing = true;
228  }
229  }
230 
231  return ( missing && !m_ignoreMissing ) ? StatusCode::FAILURE : StatusCode::SUCCESS;
232  }
233 
234  StatusCode finalize() override {
236  return GaudiAlgorithm::finalize();
237  }
238 
239  private:
241  this, "Paths", {}, "List of paths in the transient store to load" };
242  Gaudi::Property<std::string> m_dataSvc{ this, "DataSvc", "EventDataSvc", "Name of the data service to use" };
243  Gaudi::Property<bool> m_ignoreMissing{ this, "IgnoreMissing", false,
244  "if True, missing objects will not beconsidered an error" };
246  };
247 
249  public:
251  StatusCode initialize() override {
252  m_counter = 0;
254  }
255  StatusCode execute() override {
256  setFilterPassed( ( ++m_counter ) % 2 );
257  return StatusCode::SUCCESS;
258  }
259 
260  protected:
261  int m_counter = 0;
262  };
263 
265  public:
266  using OddEventsFilter::OddEventsFilter;
267  StatusCode execute() override {
268  setFilterPassed( ( ( ++m_counter ) % 2 ) == 0 );
269  return StatusCode::SUCCESS;
270  }
271  };
272 
276  class ListTools : public GaudiAlgorithm {
277  public:
279 
280  StatusCode execute() override {
282  info() << "All tool instances:" << endmsg;
283  for ( auto& tool : toolSvc()->getTools() ) { info() << " " << tool->name() << endmsg; }
284  return sc;
285  }
286  };
287 
292  public:
294  StatusCode initialize() override {
295  m_counter = 0;
297  }
298  StatusCode execute() override {
299  if ( ( m_frequency <= 1 ) || ( ( m_counter ) % m_frequency == 0 ) ) print();
300  return StatusCode::SUCCESS;
301  }
302  StatusCode finalize() override {
303  print();
304  return GaudiAlgorithm::finalize();
305  }
306 
307  protected:
308  Gaudi::Property<int> m_frequency{ this, "Frequency", 1, "How often to print the memory usage (number of events)" };
309  int m_counter = 0;
310  void print() {
311  info() << "vmem: " << System::virtualMemory() << " kB" << endmsg;
312  info() << "rss: " << System::mappedMemory() << " kB" << endmsg;
313  }
314  };
315 } // namespace GaudiTesting
316 
317 namespace GaudiTesting {
318  DECLARE_COMPONENT( DestructorCheckAlg )
319  DECLARE_COMPONENT( SleepyAlg )
320  DECLARE_COMPONENT( SignallingAlg )
321  DECLARE_COMPONENT( StopLoopAlg )
322  DECLARE_COMPONENT( CustomIncidentAlg )
323  DECLARE_COMPONENT( PutDataObjectAlg )
324  DECLARE_COMPONENT( GetDataObjectAlg )
325  DECLARE_COMPONENT( OddEventsFilter )
326  DECLARE_COMPONENT( EvenEventsFilter )
327  DECLARE_COMPONENT( ListTools )
328  DECLARE_COMPONENT( PrintMemoryUsage )
329 } // namespace GaudiTesting
GaudiTesting::DestructorCheckAlg::execute
StatusCode execute() override
standard execution method
Definition: TestingAlgs.cpp:41
IEventProcessor
Definition: IEventProcessor.h:24
GaudiTesting::PrintMemoryUsage::m_counter
int m_counter
Definition: TestingAlgs.cpp:309
std::string
STL class.
GaudiTesting::PrintMemoryUsage::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:294
GaudiAlgorithm::GaudiAlgorithm
GaudiAlgorithm(std::string name, ISvcLocator *pSvcLocator)
Standard constructor.
Definition: GaudiAlgorithm.cpp:45
Gaudi::Algorithm::toolSvc
SmartIF< IToolSvc > & toolSvc() const
The standard ToolSvc service, Return a pointer to the service if present.
Definition: Algorithm.cpp:580
GaudiAlgorithm::finalize
StatusCode finalize() override
standard finalization method
Definition: GaudiAlgorithm.cpp:65
GaudiTesting::PutDataObjectAlg::execute
StatusCode execute() override
standard execution method
Definition: TestingAlgs.cpp:176
GaudiTesting::CustomIncidentAlg::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:120
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:542
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
std::string::find
T find(T... args)
GaudiTesting::GetDataObjectAlg::m_dataProvider
SmartIF< IDataProviderSvc > m_dataProvider
Definition: TestingAlgs.cpp:245
GaudiTesting
Definition: MyServiceWithTool.cpp:15
Memory.h
GaudiTesting::CustomIncidentAlg::m_incidentSvc
SmartIF< IIncidentSvc > m_incidentSvc
Incident service.
Definition: TestingAlgs.cpp:156
GaudiTesting::SignallingAlg::m_signal
Gaudi::Property< int > m_signal
Definition: TestingAlgs.cpp:82
std::raise
T raise(T... args)
Gaudi::Algorithm::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
The standard service locator.
Definition: Algorithm.cpp:586
GaudiCommon< Algorithm >::tool
TOOL * tool(std::string_view type, std::string_view name, const IInterface *parent=0, bool create=true) const
Useful method for the easy location of tools.
Definition: GaudiCommonImp.h:88
GaudiTesting::SignallingAlg::m_eventCount
Gaudi::Property< int > m_eventCount
Definition: TestingAlgs.cpp:81
GaudiTesting::EvenEventsFilter
Definition: TestingAlgs.cpp:264
GaudiTesting::ListTools
Simple algorithm that creates dummy objects in the transient store.
Definition: TestingAlgs.cpp:276
GaudiTesting::CustomIncidentAlg
Definition: TestingAlgs.cpp:117
GaudiTesting::GetDataObjectAlg::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:208
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:291
GaudiTesting::CustomIncidentAlg::m_eventCount
Gaudi::Property< int > m_eventCount
Definition: TestingAlgs.cpp:152
GaudiTesting::SleepyAlg::execute
StatusCode execute() override
standard execution method
Definition: TestingAlgs.cpp:50
GaudiTesting::PutDataObjectAlg
Simple algorithm that creates dummy objects in the transient store.
Definition: TestingAlgs.cpp:162
GaudiTesting::PrintMemoryUsage::m_frequency
Gaudi::Property< int > m_frequency
Definition: TestingAlgs.cpp:308
IIncidentSvc.h
GaudiTesting::CustomIncidentAlg::execute
StatusCode execute() override
standard execution method
Definition: TestingAlgs.cpp:134
GaudiTesting::GetDataObjectAlg::m_dataSvc
Gaudi::Property< std::string > m_dataSvc
Definition: TestingAlgs.cpp:242
GaudiTesting::DestructorCheckAlg
Definition: TestingAlgs.cpp:31
GaudiTesting::OddEventsFilter
Definition: TestingAlgs.cpp:248
StatusCode
Definition: StatusCode.h:65
GaudiTesting::SleepyAlg::m_sleep
Gaudi::Property< int > m_sleep
Definition: TestingAlgs.cpp:59
std::cout
GaudiAlgorithm
Definition: GaudiAlgorithm.h:104
GaudiTesting::PutDataObjectAlg::m_dataSvc
Gaudi::Property< std::string > m_dataSvc
Definition: TestingAlgs.cpp:197
GaudiTesting::CustomIncidentAlg::m_incident
Gaudi::Property< std::string > m_incident
Definition: TestingAlgs.cpp:154
GaudiAlgorithm::initialize
StatusCode initialize() override
standard initialization method
Definition: GaudiAlgorithm.cpp:52
GaudiTesting::CustomIncidentAlg::finalize
StatusCode finalize() override
Definition: TestingAlgs.cpp:146
GaudiTesting::StopLoopAlg::m_mode
Gaudi::Property< std::string > m_mode
Definition: TestingAlgs.cpp:113
Gaudi::Property::value
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:240
GaudiTesting::ListTools::execute
StatusCode execute() override
standard execution method
Definition: TestingAlgs.cpp:280
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:60
GaudiTesting::SleepyAlg
Definition: TestingAlgs.cpp:47
GaudiTesting::StopLoopAlg
Definition: TestingAlgs.cpp:85
GaudiTesting::OddEventsFilter::m_counter
int m_counter
Definition: TestingAlgs.cpp:261
GaudiTesting::StopLoopAlg::execute
StatusCode execute() override
standard execution method
Definition: TestingAlgs.cpp:88
GaudiTesting::PutDataObjectAlg::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:166
GaudiTesting::StopLoopAlg::m_eventCount
Gaudi::Property< int > m_eventCount
Definition: TestingAlgs.cpp:111
GaudiTesting::SignallingAlg
Simple algorithm that raise a signal after N events.
Definition: TestingAlgs.cpp:66
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:198
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
GaudiTesting::OddEventsFilter::execute
StatusCode execute() override
standard execution method
Definition: TestingAlgs.cpp:255
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
std::endl
T endl(T... args)
GaudiCommon< Algorithm >::Exception
void Exception(std::string_view msg, const GaudiException &exc, const StatusCode sc=StatusCode::FAILURE) const
Create and (re)-throw a given GaudiException.
Definition: GaudiCommon.icpp:345
Sleep.h
GaudiAlgorithm.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:204
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:354
GaudiTesting::PrintMemoryUsage::finalize
StatusCode finalize() override
Definition: TestingAlgs.cpp:302
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:189
DataObject
Definition: DataObject.h:40
GaudiTesting::GetDataObjectAlg::m_paths
Gaudi::Property< std::vector< std::string > > m_paths
Definition: TestingAlgs.cpp:240
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:195
Gaudi::Sleep
GAUDI_API void Sleep(int sec)
Simple sleep function.
Definition: Sleep.cpp:17
GaudiTesting::PrintMemoryUsage::print
void print()
Definition: TestingAlgs.cpp:310
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Incident.h
GaudiTesting::GetDataObjectAlg::finalize
StatusCode finalize() override
Definition: TestingAlgs.cpp:234
Incident
Definition: Incident.h:27
GaudiTesting::GetDataObjectAlg::execute
StatusCode execute() override
standard execution method
Definition: TestingAlgs.cpp:218
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
standard execution method
Definition: TestingAlgs.cpp:69
IEventProcessor.h
GaudiTesting::PrintMemoryUsage::execute
StatusCode execute() override
standard execution method
Definition: TestingAlgs.cpp:298
Gaudi::Property< int >
GaudiTesting::EvenEventsFilter::execute
StatusCode execute() override
standard execution method
Definition: TestingAlgs.cpp:267
GaudiTesting::OddEventsFilter::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:251
GaudiTesting::GetDataObjectAlg::m_ignoreMissing
Gaudi::Property< bool > m_ignoreMissing
Definition: TestingAlgs.cpp:243