ATLAS Offline Software
PyAthenaComps.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 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 
22 from AthenaCommon.Configurable import * # noqa: F401, F403
23 from AthenaPython.Configurables import (CfgPyAlgorithm,
24  CfgPyService,
25  CfgPyAlgTool,
26  CfgPyAud)
27 
28 
29 import weakref
31  """A convenient class to access py-components by name
32  """
33  pass
34 def __wk_setattr__(self, k, v):
35  self.__setattr__(k, weakref.ref(v))
36 
37 services = WeakRefHolder()
38 services.__doc__ = """The list of PyAthena::Svc which have been instantiated"""
39 services.__setattr__ = __wk_setattr__
40 
41 algs = WeakRefHolder()
42 algs.__doc__ = """The list of PyAthena::Alg which have been instantiated"""
43 algs.__setattr__ = __wk_setattr__
44 
45 del __wk_setattr__
46 
47 
48 def _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 
56 class StatusCode:
57  Recoverable = 2
58  Success = 1
59  Failure = 0
60 
61 
62 class 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 
115  def sysReinitialize(self):
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 
179 class 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 
200  def sysReinitialize(self):
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 
231 class 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 
283  def sysReinitialize(self):
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 
305 class 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):
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 
375  def _get_filter_descr(self):
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:
397  self.cutFlowSvc().setFilterDescription(self.cutID,descr)
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
grepfile.info
info
Definition: grepfile.py:38
python.PyAthenaComps.Svc.sysReinitialize
def sysReinitialize(self)
Definition: PyAthenaComps.py:200
python.PyAthenaComps.AthFilterAlgorithm._cutflowsvc
_cutflowsvc
Definition: PyAthenaComps.py:387
python.PyAthenaComps.Alg.sysExecute
def sysExecute(self, cppcontext)
Definition: PyAthenaComps.py:125
python.PyAthenaComps.StatusCode
PyAthena.StatusCode ----------------------------------------------------—.
Definition: PyAthenaComps.py:56
python.PyAthenaComps.AlgTool.sysReinitialize
def sysReinitialize(self)
Definition: PyAthenaComps.py:283
python.PyAthenaComps.Alg.resetExecuted
def resetExecuted(self)
Definition: PyAthenaComps.py:163
python.PyAthenaComps.Alg.getContext
def getContext(self)
Definition: PyAthenaComps.py:173
python.PyAthenaComps.AlgTool.detStore
def detStore(self)
Definition: PyAthenaComps.py:258
python.PyAthenaComps.Aud.sysFinalize
def sysFinalize(self)
Definition: PyAthenaComps.py:328
python.PyAthenaComps.Alg.detStore
def detStore(self)
Definition: PyAthenaComps.py:92
python.PyAthenaComps.__wk_setattr__
def __wk_setattr__(self, k, v)
Definition: PyAthenaComps.py:34
python.PyAthenaComps.Svc.reinitialize
def reinitialize(self)
Definition: PyAthenaComps.py:207
python.PyAthenaComps.Alg.name
name
Definition: PyAthenaComps.py:81
python.PyAthenaComps.Alg.finalize
def finalize(self)
Definition: PyAthenaComps.py:140
python.PyAthenaComps.AthFilterAlgorithm.sysInitialize
def sysInitialize(self)
Definition: PyAthenaComps.py:402
python.PyAthenaComps.Alg
Definition: PyAthenaComps.py:62
python.PyAthenaComps.Alg.start
def start(self)
Definition: PyAthenaComps.py:146
python.PyAthenaComps.WeakRefHolder
Definition: PyAthenaComps.py:30
python.PyAthenaComps.AlgTool._pyath_evtstore
_pyath_evtstore
init base class
Definition: PyAthenaComps.py:244
python.PyAthenaComps.Alg.initialize
def initialize(self)
Definition: PyAthenaComps.py:112
python.PyAthenaComps.Aud.after
def after(self, evt_name, comp_name)
Definition: PyAthenaComps.py:341
python.PyAthenaComps.Alg.setFilterPassed
def setFilterPassed(self, state)
Definition: PyAthenaComps.py:159
python.PyAthenaComps.Alg.stop
def stop(self)
Definition: PyAthenaComps.py:152
python.PyAthenaComps.AlgTool.__component_type__
__component_type__
Definition: PyAthenaComps.py:247
python.PyAthenaComps.Aud.before
def before(self, evt_name, comp_name)
default no-op implementation...
Definition: PyAthenaComps.py:338
python.PyAthenaComps.Aud.sysInitialize
def sysInitialize(self)
Definition: PyAthenaComps.py:316
python.PyAthenaComps.Alg.sysReinitialize
def sysReinitialize(self)
Definition: PyAthenaComps.py:115
python.PyAthenaComps.Alg._pyath_evtstore
_pyath_evtstore
init base class
Definition: PyAthenaComps.py:76
python.PyAthenaComps.AlgTool.condStore
def condStore(self)
Definition: PyAthenaComps.py:265
python.PyAthenaComps.AthFilterAlgorithm.__init__
def __init__(self, name=None, **kw)
Definition: PyAthenaComps.py:360
pool::DbPrintLvl::setLevel
void setLevel(MsgLevel l)
Definition: DbPrint.h:32
python.PyAthenaComps.Alg._ctx
_ctx
Definition: PyAthenaComps.py:79
python.PyAthenaComps.AlgTool._pyath_condstore
_pyath_condstore
Definition: PyAthenaComps.py:246
python.PyAthenaComps.Alg.sysStop
def sysStop(self)
Definition: PyAthenaComps.py:149
python.PyAthenaComps.Aud.finalize
def finalize(self)
Definition: PyAthenaComps.py:331
python.PyAthenaComps.Alg.sysStart
def sysStart(self)
Definition: PyAthenaComps.py:143
python.PyAthenaComps.AlgTool._pyath_detstore
_pyath_detstore
Definition: PyAthenaComps.py:245
python.PyAthenaComps._get_prop_value
def _get_prop_value(pycomp, propname)
helper methods ---------------------------------------------------------—
Definition: PyAthenaComps.py:48
python.PyAthenaComps.Alg._pyath_detstore
_pyath_detstore
Definition: PyAthenaComps.py:77
python.PyAthenaComps.AlgTool.evtStore
def evtStore(self)
Definition: PyAthenaComps.py:251
python.PyAthenaComps.AthFilterAlgorithm.cutID
cutID
Definition: PyAthenaComps.py:408
python.PyAthenaComps.Svc.sysInitialize
def sysInitialize(self)
Definition: PyAthenaComps.py:190
python.PyAthenaComps.Alg.reinitialize
def reinitialize(self)
Definition: PyAthenaComps.py:122
python.PyAthenaComps.Svc.__component_type__
__component_type__
init base class
Definition: PyAthenaComps.py:187
python.PyAthenaComps.Alg._pyath_condstore
_pyath_condstore
Definition: PyAthenaComps.py:78
python.PyAthenaComps.AlgTool
Definition: PyAthenaComps.py:231
python.PyAthenaComps.Alg.sysFinalize
def sysFinalize(self)
Definition: PyAthenaComps.py:137
python.PyAthenaComps.AthFilterAlgorithm._set_filter_descr
def _set_filter_descr(self, descr)
Definition: PyAthenaComps.py:377
python.PyAthenaComps.Alg.isExecuted
def isExecuted(self)
Definition: PyAthenaComps.py:170
python.PyAthenaComps.Svc.start
def start(self)
Definition: PyAthenaComps.py:219
python.PyAthenaComps.Svc.finalize
def finalize(self)
Definition: PyAthenaComps.py:213
python.PyAthenaComps.AthFilterAlgorithm.setFilterPassed
def setFilterPassed(self, state)
Definition: PyAthenaComps.py:415
Configurable
athena/gaudi ----------------------------------------------------------—
python.PyAthenaComps.AthFilterAlgorithm.setFilterDescription
def setFilterDescription(self, descr)
Definition: PyAthenaComps.py:391
python.PyAthenaComps.Svc.sysFinalize
def sysFinalize(self)
Definition: PyAthenaComps.py:210
python.PyAthenaComps.AthFilterAlgorithm.cutFlowSvc
def cutFlowSvc(self)
Definition: PyAthenaComps.py:384
python.PyAthenaComps.Aud.initialize
def initialize(self)
Definition: PyAthenaComps.py:323
python.PyAthenaComps.AlgTool.initialize
def initialize(self)
Definition: PyAthenaComps.py:278
python.PyAthenaComps.Alg.__component_type__
__component_type__
Definition: PyAthenaComps.py:80
python.PyAthenaComps.AthFilterAlgorithm.FilterDescription
FilterDescription
Definition: PyAthenaComps.py:380
python.PyAthenaComps.AlgTool.reinitialize
def reinitialize(self)
Definition: PyAthenaComps.py:290
python.PyAthenaComps.Aud.__init__
def __init__(self, name=None, **kw)
Definition: PyAthenaComps.py:309
python.PyAthenaComps.Alg.setExecuted
def setExecuted(self, state)
Definition: PyAthenaComps.py:167
python.PyAthenaComps.AthFilterAlgorithm._get_filter_descr
def _get_filter_descr(self)
Definition: PyAthenaComps.py:375
python.PyAthenaComps.AlgTool.__init__
def __init__(self, name=None, parent=None, **kw)
Definition: PyAthenaComps.py:236
python.PyAthenaComps.Svc.__init__
def __init__(self, name=None, **kw)
Definition: PyAthenaComps.py:182
python.PyAthenaComps.Alg.filterPassed
def filterPassed(self)
Definition: PyAthenaComps.py:155
python.PyAthenaComps.AlgTool.sysInitialize
def sysInitialize(self)
Definition: PyAthenaComps.py:271
python.PyAthenaComps.Svc.sysStart
def sysStart(self)
Definition: PyAthenaComps.py:216
python.PyAthenaComps.Alg.__init__
def __init__(self, name=None, **kw)
Definition: PyAthenaComps.py:71
python.PyAthenaComps.Alg.evtStore
def evtStore(self)
Definition: PyAthenaComps.py:85
pickleTool.object
object
Definition: pickleTool.py:30
python.PyAthenaComps.Svc.initialize
def initialize(self)
Definition: PyAthenaComps.py:197
python.PyAthenaComps.Svc.sysStop
def sysStop(self)
Definition: PyAthenaComps.py:222
python.PyAthenaComps.Alg.condStore
def condStore(self)
Definition: PyAthenaComps.py:99
python.PyAthenaComps.AlgTool.finalize
def finalize(self)
Definition: PyAthenaComps.py:297
python.PyAthenaComps.Svc
Definition: PyAthenaComps.py:179
error
Definition: IImpactPoint3dEstimator.h:70
python.PyAthenaComps.AthFilterAlgorithm
Definition: PyAthenaComps.py:347
python.PyAthenaComps.Alg.execute
def execute(self)
Definition: PyAthenaComps.py:134
python.PyAthenaComps.Aud
Definition: PyAthenaComps.py:305
python.PyAthenaComps.AlgTool.sysFinalize
def sysFinalize(self)
Definition: PyAthenaComps.py:294
python.PyAthenaComps.AthFilterAlgorithm._filter_descr
_filter_descr
init base class
Definition: PyAthenaComps.py:370
python.PyAthenaComps.Svc.stop
def stop(self)
Definition: PyAthenaComps.py:225
python.PyAthenaComps.Alg.sysInitialize
def sysInitialize(self)
Definition: PyAthenaComps.py:105