The Gaudi Framework  master (37c0b60a)
TestingAlgs.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 <Gaudi/Algorithm.h>
12 #include <GaudiKernel/Algorithm.h>
15 #include <GaudiKernel/Incident.h>
16 #include <GaudiKernel/Memory.h>
17 #include <GaudiKernel/Sleep.h>
18 #include <csignal>
19 #include <iostream>
20 
21 namespace GaudiTesting {
22 
23  class DestructorCheckAlg : public Algorithm {
24  public:
26  ~DestructorCheckAlg() override {
27  // do not print messages if we are created in genconf
28  if ( System::cmdLineArgs()[0].find( "genconf" ) == std::string::npos ) {
29  std::cout << "Destructor of " << name() << std::endl;
30  }
31  }
32  StatusCode execute() override {
33  info() << "Executing " << name() << endmsg;
34  return StatusCode::SUCCESS;
35  }
36  };
37 
38  class SleepyAlg : public Gaudi::Algorithm {
39  public:
41  StatusCode execute( EventContext const& ctx ) const override {
42  info() << "Executing event " << ctx.evt() + 1 << endmsg;
43  info() << "Sleeping for " << m_sleep.value() << " seconds" << endmsg;
45  info() << "Back from sleep" << endmsg;
46  return StatusCode::SUCCESS;
47  }
48 
49  private:
50  Gaudi::Property<int> m_sleep{ this, "SleepTime", 10, "Seconds to sleep during the execute" };
51  };
52 
56  class SignallingAlg : public Algorithm {
57  public:
59  StatusCode execute() override {
60  if ( m_eventCount <= 0 ) {
61  info() << "Raising signal now" << endmsg;
63  } else {
64  info() << m_eventCount.value() << " events to go" << endmsg;
65  }
66  --m_eventCount;
67  return StatusCode::SUCCESS;
68  }
69 
70  private:
71  Gaudi::Property<int> m_eventCount{ this, "EventCount", 3, "Number of events to let go before raising the signal" };
72  Gaudi::Property<int> m_signal{ this, "Signal", SIGINT, "Signal to raise" };
73  };
74 
75  class StopLoopAlg : public Algorithm {
76  public:
78  StatusCode execute() override {
79  if ( m_eventCount <= 0 ) {
80  info() << "Stopping loop with " << m_mode.value() << endmsg;
81  if ( m_mode == "exception" ) {
82  throw GaudiException( "Stopping loop", name(), StatusCode::FAILURE );
83  } else if ( m_mode == "stopRun" ) {
84  auto ep = serviceLocator()->as<IEventProcessor>();
85  if ( !ep ) {
86  error() << "Cannot get IEventProcessor" << endmsg;
87  return StatusCode::FAILURE;
88  }
89  if ( auto sc = ep->stopRun(); !sc ) return sc;
90  } else { // "failure"
91  return StatusCode::FAILURE;
92  }
93  } else {
94  info() << m_eventCount.value() << " events to go" << endmsg;
95  }
96  --m_eventCount;
97  return StatusCode::SUCCESS;
98  }
99 
100  private:
101  Gaudi::Property<int> m_eventCount{ this, "EventCount", 3,
102  "Number of events to let go before breaking the event loop" };
103  Gaudi::Property<std::string> m_mode{ this, "Mode", "failure",
104  "Type of interruption ['exception', 'stopRun', 'failure']" };
105  };
106 
107  class CustomIncidentAlg : public Algorithm {
108  public:
109  using Algorithm::Algorithm;
110  StatusCode initialize() override {
112  if ( sc.isFailure() ) return sc;
113 
114  if ( m_incident.empty() ) {
115  error() << "The incident type (property Incident) must be declared." << endmsg;
116  return StatusCode::FAILURE;
117  }
118 
119  m_incidentSvc = service( "IncidentSvc" );
120  if ( !m_incidentSvc ) return StatusCode::FAILURE;
121 
122  return StatusCode::SUCCESS;
123  }
124  StatusCode execute() override {
125  if ( m_eventCount == 0 ) {
126  info() << "Firing incident " << m_incident.value() << endmsg;
127  m_incidentSvc->fireIncident( Incident( name(), m_incident ) );
128  } else if ( m_eventCount > 0 ) {
129  info() << m_eventCount.value() << " events to go" << endmsg;
130  } else {
131  info() << "keep processing events..." << endmsg;
132  }
133  --m_eventCount;
134  return StatusCode::SUCCESS;
135  }
136  StatusCode finalize() override {
138  return Algorithm::finalize();
139  }
140 
141  private:
142  Gaudi::Property<int> m_eventCount{ this, "EventCount", 3,
143  "Number of events to let go before firing the incident." };
144  Gaudi::Property<std::string> m_incident{ this, "Incident", "", "Type of incident to fire." };
147  };
148 
152  class PutDataObjectAlg : public Algorithm {
153  public:
154  using Algorithm::Algorithm;
155 
156  StatusCode initialize() override {
158  if ( sc.isFailure() ) return sc;
159 
161  if ( !m_dataProvider ) return StatusCode::FAILURE;
162 
163  return StatusCode::SUCCESS;
164  }
165 
166  StatusCode execute() override {
168  info() << "Adding " << m_paths.size() << " objects to " << m_dataSvc.value() << endmsg;
169  for ( auto& p : m_paths ) {
170  info() << "Adding '" << p << "'" << endmsg;
171  DataObject* obj = new DataObject();
172  sc = m_dataProvider->registerObject( p, obj );
173  if ( sc.isFailure() ) warning() << "Cannot register object '" << p << "'" << endmsg;
174  }
175 
176  return sc;
177  }
178 
179  StatusCode finalize() override {
181  return Algorithm::finalize();
182  }
183 
184  private:
186  this, "Paths", {}, "List of paths in the transient store to load" };
187  Gaudi::Property<std::string> m_dataSvc{ this, "DataSvc", "EventDataSvc", "Name of the data service to use" };
189  };
190 
194  class GetDataObjectAlg : public Algorithm {
195  public:
196  using Algorithm::Algorithm;
197 
198  StatusCode initialize() override {
200  if ( sc.isFailure() ) return sc;
201 
203  if ( !m_dataProvider ) return StatusCode::FAILURE;
204 
205  return StatusCode::SUCCESS;
206  }
207 
208  StatusCode execute() override {
209  info() << "Getting " << m_paths.size() << " objects from " << m_dataSvc.value() << endmsg;
210  bool missing = false;
211  for ( auto& p : m_paths ) {
212  info() << "Getting '" << p << "'" << endmsg;
213  DataObject* obj;
214  StatusCode sc = m_dataProvider->retrieveObject( p, obj );
215  if ( sc.isFailure() ) {
216  warning() << "Cannot retrieve object '" << p << "'" << endmsg;
217  missing = true;
218  }
219  }
220 
221  return ( missing && !m_ignoreMissing ) ? StatusCode::FAILURE : StatusCode::SUCCESS;
222  }
223 
224  StatusCode finalize() override {
226  return Algorithm::finalize();
227  }
228 
229  private:
231  this, "Paths", {}, "List of paths in the transient store to load" };
232  Gaudi::Property<std::string> m_dataSvc{ this, "DataSvc", "EventDataSvc", "Name of the data service to use" };
233  Gaudi::Property<bool> m_ignoreMissing{ this, "IgnoreMissing", false,
234  "if True, missing objects will not beconsidered an error" };
236  };
237 
238  class OddEventsFilter : public Algorithm {
239  public:
240  using Algorithm::Algorithm;
241  StatusCode initialize() override {
242  m_counter = 0;
243  return Algorithm::initialize();
244  }
245  StatusCode execute() override {
246  setFilterPassed( ( ++m_counter ) % 2 );
247  return StatusCode::SUCCESS;
248  }
249 
250  protected:
251  int m_counter = 0;
252  };
253 
255  public:
256  using OddEventsFilter::OddEventsFilter;
257  StatusCode execute() override {
258  setFilterPassed( ( ( ++m_counter ) % 2 ) == 0 );
259  return StatusCode::SUCCESS;
260  }
261  };
262 
266  class ListTools : public Algorithm {
267  public:
268  using Algorithm::Algorithm;
269 
270  StatusCode execute() override {
272  info() << "All tool instances:" << endmsg;
273  for ( auto& tool : toolSvc()->getTools() ) { info() << " " << tool->name() << endmsg; }
274  return sc;
275  }
276  };
277 
281  class PrintMemoryUsage : public Algorithm {
282  public:
283  using Algorithm::Algorithm;
284  StatusCode initialize() override {
285  m_counter = 0;
286  return Algorithm::initialize();
287  }
288  StatusCode execute() override {
289  if ( ( m_frequency <= 1 ) || ( ( m_counter ) % m_frequency == 0 ) ) print();
290  return StatusCode::SUCCESS;
291  }
292  StatusCode finalize() override {
293  print();
294  return Algorithm::finalize();
295  }
296 
297  protected:
298  Gaudi::Property<int> m_frequency{ this, "Frequency", 1, "How often to print the memory usage (number of events)" };
299  int m_counter = 0;
300  void print() {
301  info() << "vmem: " << System::virtualMemory() << " kB" << endmsg;
302  info() << "rss: " << System::mappedMemory() << " kB" << endmsg;
303  }
304  };
305 } // namespace GaudiTesting
306 
307 namespace GaudiTesting {
308  DECLARE_COMPONENT( DestructorCheckAlg )
309  DECLARE_COMPONENT( SleepyAlg )
310  DECLARE_COMPONENT( SignallingAlg )
311  DECLARE_COMPONENT( StopLoopAlg )
312  DECLARE_COMPONENT( CustomIncidentAlg )
313  DECLARE_COMPONENT( PutDataObjectAlg )
314  DECLARE_COMPONENT( GetDataObjectAlg )
315  DECLARE_COMPONENT( OddEventsFilter )
316  DECLARE_COMPONENT( EvenEventsFilter )
317  DECLARE_COMPONENT( ListTools )
318  DECLARE_COMPONENT( PrintMemoryUsage )
319 } // namespace GaudiTesting
GaudiTesting::DestructorCheckAlg::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:32
IEventProcessor
Definition: IEventProcessor.h:24
GaudiTesting::PrintMemoryUsage::m_counter
int m_counter
Definition: TestingAlgs.cpp:299
GaudiTesting::PrintMemoryUsage::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:284
Gaudi::Algorithm::toolSvc
SmartIF< IToolSvc > & toolSvc() const
The standard ToolSvc service, Return a pointer to the service if present.
Definition: Algorithm.cpp:564
GaudiTesting::PutDataObjectAlg::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:166
GaudiTesting::SleepyAlg::execute
StatusCode execute(EventContext const &ctx) const override
Definition: TestingAlgs.cpp:41
GaudiTesting::CustomIncidentAlg::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:110
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:526
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:235
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:146
GaudiTesting::SignallingAlg::m_signal
Gaudi::Property< int > m_signal
Definition: TestingAlgs.cpp:72
std::raise
T raise(T... args)
Gaudi::Algorithm::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
The standard service locator.
Definition: Algorithm.cpp:570
GaudiTesting::SignallingAlg::m_eventCount
Gaudi::Property< int > m_eventCount
Definition: TestingAlgs.cpp:71
GaudiTesting::EvenEventsFilter
Definition: TestingAlgs.cpp:254
GaudiTesting::ListTools
Simple algorithm that creates dummy objects in the transient store.
Definition: TestingAlgs.cpp:266
GaudiTesting::CustomIncidentAlg
Definition: TestingAlgs.cpp:107
GaudiTesting::GetDataObjectAlg::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:198
GaudiTesting::DestructorCheckAlg::~DestructorCheckAlg
~DestructorCheckAlg() override
Definition: TestingAlgs.cpp:26
GaudiTesting::PrintMemoryUsage
Simple algorithm that prints the memory usage every N events (property "Frequency").
Definition: TestingAlgs.cpp:281
GaudiTesting::CustomIncidentAlg::m_eventCount
Gaudi::Property< int > m_eventCount
Definition: TestingAlgs.cpp:142
GaudiTesting::PutDataObjectAlg
Simple algorithm that creates dummy objects in the transient store.
Definition: TestingAlgs.cpp:152
GaudiTesting::PrintMemoryUsage::m_frequency
Gaudi::Property< int > m_frequency
Definition: TestingAlgs.cpp:298
IIncidentSvc.h
GaudiTesting::CustomIncidentAlg::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:124
GaudiTesting::GetDataObjectAlg::m_dataSvc
Gaudi::Property< std::string > m_dataSvc
Definition: TestingAlgs.cpp:232
bug_34121.tool
tool
Definition: bug_34121.py:18
GaudiTesting::DestructorCheckAlg
Definition: TestingAlgs.cpp:23
GaudiTesting::OddEventsFilter
Definition: TestingAlgs.cpp:238
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:578
StatusCode
Definition: StatusCode.h:65
GaudiTesting::SleepyAlg::m_sleep
Gaudi::Property< int > m_sleep
Definition: TestingAlgs.cpp:50
std::cout
GaudiTesting::PutDataObjectAlg::m_dataSvc
Gaudi::Property< std::string > m_dataSvc
Definition: TestingAlgs.cpp:187
GaudiTesting::CustomIncidentAlg::m_incident
Gaudi::Property< std::string > m_incident
Definition: TestingAlgs.cpp:144
Gaudi::Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:90
GaudiTesting::CustomIncidentAlg::finalize
StatusCode finalize() override
Definition: TestingAlgs.cpp:136
GaudiTesting::StopLoopAlg::m_mode
Gaudi::Property< std::string > m_mode
Definition: TestingAlgs.cpp:103
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:237
GaudiTesting::ListTools::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:270
Algorithm.h
SmartIF< IIncidentSvc >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
GaudiTesting::SleepyAlg
Definition: TestingAlgs.cpp:38
GaudiTesting::StopLoopAlg
Definition: TestingAlgs.cpp:75
GaudiTesting::OddEventsFilter::m_counter
int m_counter
Definition: TestingAlgs.cpp:251
GaudiTesting::StopLoopAlg::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:78
GaudiTesting::PutDataObjectAlg::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:156
GaudiTesting::StopLoopAlg::m_eventCount
Gaudi::Property< int > m_eventCount
Definition: TestingAlgs.cpp:101
GaudiTesting::SignallingAlg
Simple algorithm that raise a signal after N events.
Definition: TestingAlgs.cpp:56
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:188
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
GaudiTesting::OddEventsFilter::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:245
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:194
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:365
GaudiTesting::PrintMemoryUsage::finalize
StatusCode finalize() override
Definition: TestingAlgs.cpp:292
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:206
EventContext
Definition: EventContext.h:34
GaudiTesting::PutDataObjectAlg::finalize
StatusCode finalize() override
Definition: TestingAlgs.cpp:179
DataObject
Definition: DataObject.h:36
GaudiTesting::GetDataObjectAlg::m_paths
Gaudi::Property< std::vector< std::string > > m_paths
Definition: TestingAlgs.cpp:230
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:185
Gaudi::Sleep
GAUDI_API void Sleep(int sec)
Simple sleep function.
Definition: Sleep.cpp:17
GaudiTesting::PrintMemoryUsage::print
void print()
Definition: TestingAlgs.cpp:300
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:224
Incident
Definition: Incident.h:27
GaudiTesting::GetDataObjectAlg::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:208
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:59
IEventProcessor.h
GaudiTesting::PrintMemoryUsage::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:288
Gaudi::Property< int >
GaudiTesting::EvenEventsFilter::execute
StatusCode execute() override
Definition: TestingAlgs.cpp:257
GaudiTesting::OddEventsFilter::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:241
GaudiTesting::GetDataObjectAlg::m_ignoreMissing
Gaudi::Property< bool > m_ignoreMissing
Definition: TestingAlgs.cpp:233