ATLAS Offline Software
Loading...
Searching...
No Matches
PyAthenaComps.py
Go to the documentation of this file.
1# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3# @file: PyAthenaComps.py
4# @purpose: a set of Python classes for PyAthena
5# @author: Sebastien Binet <binet@cern.ch>
6
7__doc__ = """Module containing a set of Python base classes for PyAthena"""
8__author__ = "Sebastien Binet <binet@cern.ch>"
9
10
11__all__ = [ 'StatusCode',
12 'Alg',
13 'Svc',
14 'AlgTool',
15 'Aud',
16 'algs',
17 'services',
18 ]
19
20
21from AthenaCommon.Configurable import * # noqa: F401, F403
22from AthenaPython.Configurables import (CfgPyAlgorithm,
23 CfgPyService,
24 CfgPyAlgTool,
25 CfgPyAud)
26
27
28import weakref
29class WeakRefHolder(object):
30 """A convenient class to access py-components by name
31 """
32 pass
33def __wk_setattr__(self, k, v):
34 self.__setattr__(k, weakref.ref(v))
35
36services = WeakRefHolder()
37services.__doc__ = """The list of PyAthena::Svc which have been instantiated"""
38services.__setattr__ = __wk_setattr__
39
41algs.__doc__ = """The list of PyAthena::Alg which have been instantiated"""
42algs.__setattr__ = __wk_setattr__
43
44del __wk_setattr__
45
46
47def _get_prop_value(pycomp, propname):
48 if propname in pycomp.properties() and pycomp.properties()[propname] != pycomp.propertyNoValue:
49 return pycomp.properties()[propname]
50 else:
51 return pycomp.getDefaultProperty(propname)
52
53
54
56 Recoverable = 2
57 Success = 1
58 Failure = 0
59
60
61class Alg( CfgPyAlgorithm ):
62 """
63 Base class from which all concrete algorithm classes should
64 be derived.
65
66 In order for a concrete algorithm class to do anything
67 useful the methods initialize(), execute() and finalize()
68 should be overridden.
69 """
70 def __init__(self, name = None, **kw):
71 if name is None: name = kw.get('name', self.__class__.__name__)
72 kw.setdefault('OutputLevel', 3) #INFO
73
74 super(Alg, self).__init__(name, **kw)
75 self._pyath_evtstore = None # handle to the evt store
76 self._pyath_detstore = None # handle to the det store
77 self._pyath_condstore = None # handle to the cond store
78 self._ctx = None
79 self.__component_type__="Algorithm"
80 self.name=name
81 return
82
83 @property
84 def evtStore(self):
85 if not self._pyath_evtstore:
86 import AthenaPython.PyAthena as PyAthena
87 self._pyath_evtstore = PyAthena.py_svc('StoreGateSvc/StoreGateSvc')
88 return self._pyath_evtstore
89
90 @property
91 def detStore(self):
92 if not self._pyath_detstore:
93 import AthenaPython.PyAthena as PyAthena
94 self._pyath_detstore = PyAthena.py_svc('StoreGateSvc/DetectorStore')
95 return self._pyath_detstore
96
97 @property
98 def condStore(self):
99 if not self._pyath_condstore:
100 import AthenaPython.PyAthena as PyAthena
101 self._pyath_condstore = PyAthena.py_svc('StoreGateSvc/ConditionStore')
102 return self._pyath_condstore
103
104 def sysInitialize(self):
105 if hasattr(self, 'OutputLevel'):
106 self.msg.setLevel(self.OutputLevel)
107 else:
108 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
109 return self.initialize()
110
111 def initialize(self):
112 return StatusCode.Success
113
115 if hasattr(self, 'OutputLevel'):
116 self.msg.setLevel(self.OutputLevel)
117 else:
118 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
119 return self.reinitialize()
120
121 def reinitialize(self):
122 return StatusCode.Success
123
124 def sysExecute(self, cppcontext):
125 import cppyy
126 self._ctx = cppyy.bind_object(cppcontext, "EventContext")
127 try:
128 ret = self.execute()
129 finally:
130 self._ctx = None
131 return ret
132
133 def execute(self):
134 return StatusCode.Success
135
136 def sysFinalize(self):
137 return self.finalize()
138
139 def finalize(self):
140 return StatusCode.Success
141
142 def sysStart(self):
143 return self.start()
144
145 def start(self):
146 return StatusCode.Success
147
148 def sysStop(self):
149 return self.stop()
150
151 def stop(self):
152 return StatusCode.Success
153
154 def filterPassed(self):
155 """Did this algorithm pass or fail its filter criterion for the last event?"""
156 return self._cppHandle.filterPassed(self._ctx)
157
158 def setFilterPassed(self, state):
159 """Set the filter passed flag to the specified state"""
160 return self._cppHandle.setFilterPassed(state, self._ctx)
161
162 def resetExecuted(self):
163 self.setExecuted(False)
164 return StatusCode.Success
165
166 def setExecuted(self,state):
167 return self._cppHandle.setExecuted(state)
168
169 def isExecuted(self):
170 return self._cppHandle.isExecuted()
171
172 def getContext(self):
173 return self._ctx
174
175 pass # PyAthena.Alg
176
177
178class Svc( CfgPyService ):
179 """Base class for all services
180 """
181 def __init__(self, name = None, **kw):
182 kw.setdefault('OutputLevel', 3) #INFO
183 if name is None: name = kw.get('name', self.__class__.__name__)
184
185 super(Svc, self).__init__(name, **kw)
186 self.__component_type__ = "Service"
187 return
188
189 def sysInitialize(self):
190 if hasattr(self, 'OutputLevel'):
191 self.msg.setLevel(self.OutputLevel)
192 else:
193 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
194 return self.initialize()
195
196 def initialize(self):
197 return StatusCode.Success
198
200 if hasattr(self, 'OutputLevel'):
201 self.msg.setLevel(self.OutputLevel)
202 else:
203 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
204 return self.reinitialize()
205
206 def reinitialize(self):
207 return StatusCode.Success
208
209 def sysFinalize(self):
210 return self.finalize()
211
212 def finalize(self):
213 return StatusCode.Success
214
215 def sysStart(self):
216 return self.start()
217
218 def start(self):
219 return StatusCode.Success
220
221 def sysStop(self):
222 return self.stop()
223
224 def stop(self):
225 return StatusCode.Success
226
227 pass # PyAthena.Svc
228
229
230class AlgTool( CfgPyAlgTool ):
231 """
232 Base class from which all concrete algtool classes should be derived.
233 """
234
235 def __init__(self, name=None, parent=None, **kw):
236 kw.setdefault('OutputLevel', 3) #INFO
237 if name is None: name = kw.get('name', self.__class__.__name__)
238 if parent is not None:
239 if isinstance(parent, str): name = "%s.%s" % (parent,name)
240 else: name = "%s.%s" % (parent.name(),name)
241
242 super(AlgTool, self).__init__(name, **kw)
243 self._pyath_evtstore = None # handle to the evt store
244 self._pyath_detstore = None # handle to the det store
245 self._pyath_condstore = None # handle to the cond store
246 self.__component_type__ = "AlgTool"
247 return
248
249 @property
250 def evtStore(self):
251 if not self._pyath_evtstore:
252 import AthenaPython.PyAthena as PyAthena
253 self._pyath_evtstore = PyAthena.py_svc('StoreGateSvc/StoreGateSvc')
254 return self._pyath_evtstore
255
256 @property
257 def detStore(self):
258 if not self._pyath_detstore:
259 import AthenaPython.PyAthena as PyAthena
260 self._pyath_detstore = PyAthena.py_svc('StoreGateSvc/DetectorStore')
261 return self._pyath_detstore
262
263 @property
264 def condStore(self):
265 if not self._pyath_condstore:
266 import AthenaPython.PyAthena as PyAthena
267 self._pyath_condstore = PyAthena.py_svc('StoreGateSvc/ConditionStore')
268 return self._pyath_condstore
269
270 def sysInitialize(self):
271 if hasattr(self, 'OutputLevel'):
272 self.msg.setLevel(self.OutputLevel)
273 else:
274 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
275 return self.initialize()
276
277 def initialize(self):
278 raise NotImplementedError(
279 "You have to implement PyAthena.AlgTool.initialize() !"
280 )
281
283 if hasattr(self, 'OutputLevel'):
284 self.msg.setLevel(self.OutputLevel)
285 else:
286 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
287 return self.reinitialize()
288
289 def reinitialize(self):
290 self.msg.info( "==> re-initialize..." )
291 return StatusCode.Success
292
293 def sysFinalize(self):
294 return self.finalize()
295
296 def finalize(self):
297 raise NotImplementedError(
298 "You have to implement PyAthena.AlgTool.finalize() !"
299 )
300
301 pass # PyAthena.AlgTool
302
303
304class Aud( CfgPyAud ):
305 """
306 Base class from which all concrete auditor classes should be derived.
307 """
308 def __init__(self, name=None, **kw):
309 kw.setdefault('OutputLevel', 3) #INFO
310 if name is None: name = kw.get('name', self.__class__.__name__)
311
312 super(Aud, self).__init__(name, **kw)
313 return
314
315 def sysInitialize(self):
316 if hasattr(self, 'OutputLevel'):
317 self.msg.setLevel(self.OutputLevel)
318 else:
319 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
320 return self.initialize()
321
322 def initialize(self):
323 raise NotImplementedError(
324 "You have to implement PyAthena.Aud.initialize() !"
325 )
326
327 def sysFinalize(self):
328 return self.finalize()
329
330 def finalize(self):
331 raise NotImplementedError(
332 "You have to implement PyAthena.Aud.finalize() !"
333 )
334
335
336
337 def before(self, evt_name, comp_name):
338 return
339
340 def after(self, evt_name, comp_name, sc):
341 return
342
343 pass # PyAthena.Aud
__init__(self, name=None, parent=None, **kw)
sysExecute(self, cppcontext)
__init__(self, name=None, **kw)
_pyath_evtstore
init base class
__init__(self, name=None, **kw)
before(self, evt_name, comp_name)
default no-op implementation...
after(self, evt_name, comp_name, sc)
PyAthena.StatusCode ----------------------------------------------------—.
__init__(self, name=None, **kw)
str __component_type__
init base class
__wk_setattr__(self, k, v)
_get_prop_value(pycomp, propname)
helper methods ---------------------------------------------------------—
void initialize()