The Gaudi Framework  v36r1 (3e2fb5a8)
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, "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 
162  public:
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 GaudiAlgorithm::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 
204  public:
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 GaudiAlgorithm::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 
248  public:
250  StatusCode initialize() override {
251  m_counter = 0;
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 GaudiAlgorithm {
276  public:
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 
291  public:
293  StatusCode initialize() override {
294  m_counter = 0;
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 GaudiAlgorithm::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
standard execution method
Definition: TestingAlgs.cpp:41
IEventProcessor
Definition: IEventProcessor.h:24
GaudiTesting::PrintMemoryUsage::m_counter
int m_counter
Definition: TestingAlgs.cpp:308
std::string
STL class.
GaudiTesting::PrintMemoryUsage::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:293
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:175
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:244
GaudiTesting
Definition: MyServiceWithTool.cpp:15
Memory.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: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:263
GaudiTesting::ListTools
Simple algorithm that creates dummy objects in the transient store.
Definition: TestingAlgs.cpp:275
GaudiTesting::CustomIncidentAlg
Definition: TestingAlgs.cpp:117
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: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:161
GaudiTesting::PrintMemoryUsage::m_frequency
Gaudi::Property< int > m_frequency
Definition: TestingAlgs.cpp:307
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:241
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:59
std::cout
GaudiAlgorithm
Definition: GaudiAlgorithm.h:104
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
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: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:60
GaudiTesting::SleepyAlg
Definition: TestingAlgs.cpp:47
GaudiTesting::StopLoopAlg
Definition: TestingAlgs.cpp:85
GaudiTesting::OddEventsFilter::m_counter
int m_counter
Definition: TestingAlgs.cpp:260
GaudiTesting::StopLoopAlg::execute
StatusCode execute() override
standard execution method
Definition: TestingAlgs.cpp:88
GaudiTesting::PutDataObjectAlg::initialize
StatusCode initialize() override
Definition: TestingAlgs.cpp:165
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:197
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:142
GaudiTesting::OddEventsFilter::execute
StatusCode execute() override
standard execution method
Definition: TestingAlgs.cpp:254
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:344
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: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:354
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
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
standard execution method
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
standard execution method
Definition: TestingAlgs.cpp:69
IEventProcessor.h
GaudiTesting::PrintMemoryUsage::execute
StatusCode execute() override
standard execution method
Definition: TestingAlgs.cpp:297
Gaudi::Property< int >
GaudiTesting::EvenEventsFilter::execute
StatusCode execute() override
standard execution method
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