The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
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>
16#include <GaudiKernel/Memory.h>
17#include <GaudiKernel/Sleep.h>
18#include <csignal>
19#include <iostream>
20
21namespace GaudiTesting {
22
24 public:
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;
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;
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;
62 std::raise( m_signal );
63 } else {
64 info() << m_eventCount.value() << " events to go" << endmsg;
65 }
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;
88 }
89 if ( auto sc = ep->stopRun(); !sc ) return sc;
90 } else { // "failure"
92 }
93 } else {
94 info() << m_eventCount.value() << " events to go" << endmsg;
95 }
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
108 public:
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 {
137 m_incidentSvc.reset();
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
153 public:
155
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 {
180 m_dataProvider.reset();
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
195 public:
197
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
222 }
223
224 StatusCode finalize() override {
225 m_dataProvider.reset();
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:
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:
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
282 public:
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
307namespace GaudiTesting {
319} // namespace GaudiTesting
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
A DataObject is the base class of any identifiable object on any data store.
Definition DataObject.h:37
This class represents an entry point to all the event specific data.
Base class from which all concrete algorithm classes should be derived.
Definition Algorithm.h:87
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition Algorithm.h:98
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition Algorithm.h:175
StatusCode finalize() override
the default (empty) implementation of IStateful::finalize() method
Definition Algorithm.h:181
SmartIF< ISvcLocator > & serviceLocator() const override
The standard service locator.
const std::string & name() const override
The identifying name of the algorithm object.
SmartIF< IService > service(std::string_view name, const bool createIf=true, const bool quiet=false) const
Return a pointer to the service identified by name (or "type/name")
SmartIF< IToolSvc > & toolSvc() const
The standard ToolSvc service, Return a pointer to the service if present.
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27
void setFilterPassed(bool state) const
Set the filter passed flag to the specified state.
Define general base for Gaudi exception.
SmartIF< IIncidentSvc > m_incidentSvc
Incident service.
StatusCode finalize() override
StatusCode execute() override
StatusCode initialize() override
Gaudi::Property< int > m_eventCount
Gaudi::Property< std::string > m_incident
StatusCode execute() override
StatusCode execute() override
Simple algorithm that retrieves objects from the transient store.
Gaudi::Property< std::vector< std::string > > m_paths
StatusCode finalize() override
Gaudi::Property< std::string > m_dataSvc
SmartIF< IDataProviderSvc > m_dataProvider
StatusCode execute() override
StatusCode initialize() override
Gaudi::Property< bool > m_ignoreMissing
Simple algorithm that creates dummy objects in the transient store.
StatusCode execute() override
StatusCode execute() override
StatusCode initialize() override
Simple algorithm that prints the memory usage every N events (property "Frequency").
StatusCode initialize() override
StatusCode execute() override
StatusCode finalize() override
Gaudi::Property< int > m_frequency
Simple algorithm that creates dummy objects in the transient store.
Gaudi::Property< std::string > m_dataSvc
StatusCode initialize() override
StatusCode finalize() override
StatusCode execute() override
Gaudi::Property< std::vector< std::string > > m_paths
SmartIF< IDataProviderSvc > m_dataProvider
Simple algorithm that raise a signal after N events.
StatusCode execute() override
Gaudi::Property< int > m_signal
Gaudi::Property< int > m_eventCount
Gaudi::Property< int > m_sleep
StatusCode execute(EventContext const &ctx) const override
Gaudi::Property< std::string > m_mode
StatusCode execute() override
Gaudi::Property< int > m_eventCount
The IEventProcessor is the interface to process events.
SmartIF< IFace > as()
Definition ISvcLocator.h:64
Base class for all Incidents (computing events).
Definition Incident.h:24
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isFailure() const
Definition StatusCode.h:129
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
GAUDI_API void Sleep(int sec)
Simple sleep function.
Definition Sleep.cpp:17
GAUDI_API long mappedMemory(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition Memory.cpp:172
GAUDI_API const std::vector< std::string > cmdLineArgs()
Command line arguments including executable name as arg[0] as vector of strings.
Definition System.cpp:310
GAUDI_API long virtualMemory(MemoryUnit unit=kByte, InfoType fetch=Memory, long pid=-1)
Basic Process Information: priority boost.
Definition Memory.cpp:203