ATLAS Offline Software
Loading...
Searching...
No Matches
PyAthenaComps.py
Go to the documentation of this file.
1# Copyright (C) 2002-2025 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 'AthFilterAlgorithm',
17 'algs',
18 'services',
19 ]
20
21
22from AthenaCommon.Configurable import * # noqa: F401, F403
23from AthenaPython.Configurables import (CfgPyAlgorithm,
24 CfgPyService,
25 CfgPyAlgTool,
26 CfgPyAud)
27
28
29import weakref
31 """A convenient class to access py-components by name
32 """
33 pass
34def __wk_setattr__(self, k, v):
35 self.__setattr__(k, weakref.ref(v))
36
37services = WeakRefHolder()
38services.__doc__ = """The list of PyAthena::Svc which have been instantiated"""
39services.__setattr__ = __wk_setattr__
40
42algs.__doc__ = """The list of PyAthena::Alg which have been instantiated"""
43algs.__setattr__ = __wk_setattr__
44
45del __wk_setattr__
46
47
48def _get_prop_value(pycomp, propname):
49 if propname in pycomp.properties() and pycomp.properties()[propname] != pycomp.propertyNoValue:
50 return pycomp.properties()[propname]
51 else:
52 return pycomp.getDefaultProperty(propname)
53
54
55
57 Recoverable = 2
58 Success = 1
59 Failure = 0
60
61
62class Alg( CfgPyAlgorithm ):
63 """
64 Base class from which all concrete algorithm classes should
65 be derived.
66
67 In order for a concrete algorithm class to do anything
68 useful the methods initialize(), execute() and finalize()
69 should be overridden.
70 """
71 def __init__(self, name = None, **kw):
72 if name is None: name = kw.get('name', self.__class__.__name__)
73 kw.setdefault('OutputLevel', 3) #INFO
74
75 super(Alg, self).__init__(name, **kw)
76 self._pyath_evtstore = None # handle to the evt store
77 self._pyath_detstore = None # handle to the det store
78 self._pyath_condstore = None # handle to the cond store
79 self._ctx = None
80 self.__component_type__="Algorithm"
81 self.name=name
82 return
83
84 @property
85 def evtStore(self):
86 if not self._pyath_evtstore:
87 import AthenaPython.PyAthena as PyAthena
88 self._pyath_evtstore = PyAthena.py_svc('StoreGateSvc/StoreGateSvc')
89 return self._pyath_evtstore
90
91 @property
92 def detStore(self):
93 if not self._pyath_detstore:
94 import AthenaPython.PyAthena as PyAthena
95 self._pyath_detstore = PyAthena.py_svc('StoreGateSvc/DetectorStore')
96 return self._pyath_detstore
97
98 @property
99 def condStore(self):
100 if not self._pyath_condstore:
101 import AthenaPython.PyAthena as PyAthena
102 self._pyath_condstore = PyAthena.py_svc('StoreGateSvc/ConditionStore')
103 return self._pyath_condstore
104
105 def sysInitialize(self):
106 if hasattr(self, 'OutputLevel'):
107 self.msg.setLevel(self.OutputLevel)
108 else:
109 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
110 return self.initialize()
111
112 def initialize(self):
113 return StatusCode.Success
114
116 if hasattr(self, 'OutputLevel'):
117 self.msg.setLevel(self.OutputLevel)
118 else:
119 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
120 return self.reinitialize()
121
122 def reinitialize(self):
123 return StatusCode.Success
124
125 def sysExecute(self, cppcontext):
126 import cppyy
127 self._ctx = cppyy.bind_object(cppcontext, "EventContext")
128 try:
129 ret = self.execute()
130 finally:
131 self._ctx = None
132 return ret
133
134 def execute(self):
135 return StatusCode.Success
136
137 def sysFinalize(self):
138 return self.finalize()
139
140 def finalize(self):
141 return StatusCode.Success
142
143 def sysStart(self):
144 return self.start()
145
146 def start(self):
147 return StatusCode.Success
148
149 def sysStop(self):
150 return self.stop()
151
152 def stop(self):
153 return StatusCode.Success
154
155 def filterPassed(self):
156 """Did this algorithm pass or fail its filter criterion for the last event?"""
157 return self._cppHandle.filterPassed()
158
159 def setFilterPassed(self, state):
160 """Set the filter passed flag to the specified state"""
161 return self._cppHandle.setFilterPassed(state)
162
163 def resetExecuted(self):
164 self.setExecuted(False)
165 return StatusCode.Success
166
167 def setExecuted(self,state):
168 return self._cppHandle.setExecuted(state)
169
170 def isExecuted(self):
171 return self._cppHandle.isExecuted()
172
173 def getContext(self):
174 return self._ctx
175
176 pass # PyAthena.Alg
177
178
179class Svc( CfgPyService ):
180 """Base class for all services
181 """
182 def __init__(self, name = None, **kw):
183 kw.setdefault('OutputLevel', 3) #INFO
184 if name is None: name = kw.get('name', self.__class__.__name__)
185
186 super(Svc, self).__init__(name, **kw)
187 self.__component_type__ = "Service"
188 return
189
190 def sysInitialize(self):
191 if hasattr(self, 'OutputLevel'):
192 self.msg.setLevel(self.OutputLevel)
193 else:
194 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
195 return self.initialize()
196
197 def initialize(self):
198 return StatusCode.Success
199
201 if hasattr(self, 'OutputLevel'):
202 self.msg.setLevel(self.OutputLevel)
203 else:
204 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
205 return self.reinitialize()
206
207 def reinitialize(self):
208 return StatusCode.Success
209
210 def sysFinalize(self):
211 return self.finalize()
212
213 def finalize(self):
214 return StatusCode.Success
215
216 def sysStart(self):
217 return self.start()
218
219 def start(self):
220 return StatusCode.Success
221
222 def sysStop(self):
223 return self.stop()
224
225 def stop(self):
226 return StatusCode.Success
227
228 pass # PyAthena.Svc
229
230
231class AlgTool( CfgPyAlgTool ):
232 """
233 Base class from which all concrete algtool classes should be derived.
234 """
235
236 def __init__(self, name=None, parent=None, **kw):
237 kw.setdefault('OutputLevel', 3) #INFO
238 if name is None: name = kw.get('name', self.__class__.__name__)
239 if not (parent is None):
240 if isinstance(parent, str): name = "%s.%s" % (parent,name)
241 else: name = "%s.%s" % (parent.name(),name)
242
243 super(AlgTool, self).__init__(name, **kw)
244 self._pyath_evtstore = None # handle to the evt store
245 self._pyath_detstore = None # handle to the det store
246 self._pyath_condstore = None # handle to the cond store
247 self.__component_type__ = "AlgTool"
248 return
249
250 @property
251 def evtStore(self):
252 if not self._pyath_evtstore:
253 import AthenaPython.PyAthena as PyAthena
254 self._pyath_evtstore = PyAthena.py_svc('StoreGateSvc/StoreGateSvc')
255 return self._pyath_evtstore
256
257 @property
258 def detStore(self):
259 if not self._pyath_detstore:
260 import AthenaPython.PyAthena as PyAthena
261 self._pyath_detstore = PyAthena.py_svc('StoreGateSvc/DetectorStore')
262 return self._pyath_detstore
263
264 @property
265 def condStore(self):
266 if not self._pyath_condstore:
267 import AthenaPython.PyAthena as PyAthena
268 self._pyath_condstore = PyAthena.py_svc('StoreGateSvc/ConditionStore')
269 return self._pyath_condstore
270
271 def sysInitialize(self):
272 if hasattr(self, 'OutputLevel'):
273 self.msg.setLevel(self.OutputLevel)
274 else:
275 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
276 return self.initialize()
277
278 def initialize(self):
279 raise NotImplementedError(
280 "You have to implement PyAthena.AlgTool.initialize() !"
281 )
282
284 if hasattr(self, 'OutputLevel'):
285 self.msg.setLevel(self.OutputLevel)
286 else:
287 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
288 return self.reinitialize()
289
290 def reinitialize(self):
291 self.msg.info( "==> re-initialize..." )
292 return StatusCode.Success
293
294 def sysFinalize(self):
295 return self.finalize()
296
297 def finalize(self):
298 raise NotImplementedError(
299 "You have to implement PyAthena.AlgTool.finalize() !"
300 )
301
302 pass # PyAthena.AlgTool
303
304
305class Aud( CfgPyAud ):
306 """
307 Base class from which all concrete auditor classes should be derived.
308 """
309 def __init__(self, name=None, **kw):
310 kw.setdefault('OutputLevel', 3) #INFO
311 if name is None: name = kw.get('name', self.__class__.__name__)
312
313 super(Aud, self).__init__(name, **kw)
314 return
315
316 def sysInitialize(self):
317 if hasattr(self, 'OutputLevel'):
318 self.msg.setLevel(self.OutputLevel)
319 else:
320 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
321 return self.initialize()
322
323 def initialize(self):
324 raise NotImplementedError(
325 "You have to implement PyAthena.Aud.initialize() !"
326 )
327
328 def sysFinalize(self):
329 return self.finalize()
330
331 def finalize(self):
332 raise NotImplementedError(
333 "You have to implement PyAthena.Aud.finalize() !"
334 )
335
336
337
338 def before(self, evt_name, comp_name):
339 return
340
341 def after(self, evt_name, comp_name, sc):
342 return
343
344 pass # PyAthena.Aud
345
346
348 """base class for a filter algorithm, making use of the cutflow-svc.
349
350 instances of this class (and its derived class) shall have:
351 - a `cutID` attribute of type `CutIdentifier*` from `initialize`
352 (included) and onwards,
353 - a `cutFlowSvc()` method from `initialize` (included) and onwards.
354
355 The `cutID` attribute is the CutIdentifier returned by the `ICutFlowSvc`
356 when `self` registered itself with the `ICutFlowSvc`.
357 """
358 _dflt_FilterDescription='N/A'
359
360 def __init__(self, name = None, **kw):
361 if name is None:
362 name = kw.get('name', self.__class__.__name__)
363 kw['name'] = name
364 kw.setdefault('OutputLevel', 3) #INFO
365
366
367 super(AthFilterAlgorithm, self).__init__(**kw)
368
369 # properties
370 self._filter_descr = kw.get('FilterDescription',
371 AthFilterAlgorithm._dflt_FilterDescription)
372 '''describes to the cutflowsvc what this filter does.'''
373 return
374
376 return self._filter_descr
377 def _set_filter_descr(self, descr):
378 self._filter_descr = descr
379
380 FilterDescription = property(_get_filter_descr,
381 _set_filter_descr,
382 doc='describes to the cutflowsvc what this filter does.')
383
384 def cutFlowSvc(self):
385 if not hasattr(self, '_cutflowsvc'):
386 import AthenaPython.PyAthena as PyAthena
387 self._cutflowsvc = PyAthena.py_svc('CutFlowSvc',
388 iface='ICutFlowSvc')
389 return self._cutflowsvc
390
391 def setFilterDescription(self,descr):
392 """This function updates self's CutFlow description if (and only if)
393 it has not been explicitely set during the python-level configuration
394 """
395 if hasattr(self,'cutID'):
396 if self.FilterDescription==AthFilterAlgorithm._dflt_FilterDescription:
398 else:
399 self.msg.error("AthFilterAlg has no self.cutID: could not set filter description.")
400 return
401
402 def sysInitialize(self):
403 if hasattr(self, 'OutputLevel'):
404 self.msg.setLevel(self.OutputLevel)
405 else:
406 self.msg.setLevel(_get_prop_value(self,'OutputLevel'))
407 myName=self.name() if callable(self.name) else self.name
408 self.cutID = self.cutFlowSvc().registerFilter(myName, self._filter_descr, True)
409 if not self.cutID:
410 self.msg.error("could not register filter-cut with cutflowsvc")
411 return StatusCode.Failure
412
413 return super(AthFilterAlgorithm, self).sysInitialize()
414
415 def setFilterPassed(self, state):
416 """Set the filter passed flag to the specified state"""
417 o = super(AthFilterAlgorithm, self).setFilterPassed(state)
418 if state:
419 # TODO: we should read a proper weight
420 self.cutFlowSvc().addEvent(self.cutID, 1)
421 return o
422
423 pass # PyAthena.AthFilterAlgorithm
__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()