ATLAS Offline Software
PyAthenaEventLoopMgr.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2 
3 
6 
7 import GaudiPython.Bindings as PyGaudi
8 
9 
10 # The following method is setup at ApplicationMgr creation time; this works
11 # because the event loop manager, which imports this module if it is the
12 # python one at initialization time, will instantiate the event selector in
13 # its base-class initialize(). Hence # the order is controlled, but all on
14 # the C++ side of things.
15 def enable_seeking(silent=False):
16  """ try to install seek-stuff on the EventSelector side.
17  if `silent` is True, only an attempt at installing the seeking is performed.
18  otherwise an exception is raised if the seeking could not be installed.
19  """
20 
21  import sys
22  from AthenaCommon.Logging import log as msg
23  if 'AthenaPoolCnvSvc.ReadAthenaPool' not in sys.modules:
24  if silent:
25  _msg = msg.debug
26  else:
27  _msg = msg.info
28  # user did not import that module so we give up
29  _msg( "Cannot enable 'seeking' b/c module "
30  "[AthenaPoolCnvSvc.ReadAthenaPool] hasn't been imported..." )
31  _msg( "Modify your jobOptions to import that module "+ \
32  "(or just ignore this message)" )
33 
34  if not silent:
35  raise RuntimeError("configuration-logic error")
36 
37  return
38 
39  from AthenaCommon.AppMgr import ServiceMgr as svcMgr
40  from AthenaCommon.Configurable import Configurable
41  collectionType = svcMgr.EventSelector.properties()["CollectionType"]
42 
43  if collectionType in ( "ImplicitROOT", Configurable.propertyNoValue, ):
44  msg.info ( "=> Seeking enabled." )
45 
46  else:
47  msg.warning( "Input seeking is not compatible with collection type of %s",
48  svcMgr.EventSelector.properties()["CollectionType"] )
49  msg.warning( "=> Seeking disabled." )
50  if not silent:
51  raise RuntimeError("could not install seeking")
52 
53  from AthenaCommon.AppMgr import theApp
54  if theApp.state() != theApp.State.OFFLINE:
55  # do not bring up the whole C++ kaboodle too early in the game
56  svcMgr.EventSelector.setup()
57  return
58 
60  from AthenaConfiguration.ComponentFactory import isComponentAccumulatorCfg
62  return
63  else:
64  return enable_seeking(silent=True)
65 
67 del _setupEvtSelForSeekOps
68 
69 
70 
72 class PyAthenaEventLoopMgr( PyGaudi.iService ):
73 
74  def __init__( self ):
75  PyGaudi.iService.__init__( self, 'PyAthenaEventLoopMgr' )
76 
77  def _installServices( self, cppself ):
78  # install the interfaces onto oneself; the order is (as per the
79  # cpp side multiple inheritence:
80  # IEventSeek
81  # IService => from here ...
82  # INamedInterface
83  # IInterface
84  # IProperty => ... up to here from the iService base class
85  # IStateful
86  # IEventProcessor
87  #
88  # The expectation from the C++ side it that an IEventSeek is received. Note
89  # that this code can not call into the application manager, as this is run
90  # during initialize, making the IService PyAthenaEventLoopMgr unavailable.
91  #
92  # Note that this is all a poor man's way of not needing to have dictionaries
93  # for all base classes of the C++ PyAthenaEventLoopMgr.
94 
95  import cppyy
96 
97  # need to set all the following through the __dict__ b/c of iPropert.__setattr__
98 
99  # the expect IEventSeek
100  self.__dict__[ '_evtSeek' ] = cppyy.bind_object( cppself, cppyy.gbl.IEventSeek )
101 
102  # the IService needed for iService._isvc and likewise iProperty._ip
103  self.__dict__[ '_isvc' ] = PyGaudi.InterfaceCast( cppyy.gbl.IService )( self._evtSeek )
104  self.__dict__[ '_ip' ] = PyGaudi.InterfaceCast( cppyy.gbl.IProperty )( self._evtSeek )
105 
106  # IStateful and IEventProcessor
107  self.__dict__[ '_state' ] = PyGaudi.InterfaceCast( cppyy.gbl.IStateful )( self._evtSeek )
108  self.__dict__[ '_evtpro' ] = PyGaudi.InterfaceCast( cppyy.gbl.IEventProcessor )( self._evtSeek )
109 
110  def __getattr__( self, attr ):
111  # note the lookup order: should be as per the C++ side
112  for obj in [ self._evtSeek, self._state, self._evtpro ]:
113  try:
114  return getattr( obj, attr )
115  except Exception:
116  pass
117 
118  # let properties be tried last
119  return super( PyAthenaEventLoopMgr, self ).__getattr__( attr )
120 
121  def executeAlgorithms( self, cppcontext ):
122 
123  from AthenaPython.PyAthena import py_svc
124  from AthenaPython.PyAthenaComps import StatusCode
125  appmgr = py_svc('ApplicationMgr',iface="IProperty")
126  algmgr = py_svc('ApplicationMgr',iface='IAlgManager')
127 
128  import cppyy
129  ctx = cppyy.bind_object(cppcontext, "EventContext")
130 
131  try:
132  for name in appmgr.getProperty("TopAlg").value():
133  ialg=algmgr.algorithm(name).get()
134  ialg.execState(ctx).reset()
135  result = ialg.sysExecute(ctx)
136  if result.isFailure():
137  from AthenaCommon.Logging import log as msg
138  msg.error( "Execution of algorithm %s failed", name )
139  return result.getCode()
140  except KeyboardInterrupt:
141  from AthenaCommon.Logging import log as msg
142  msg.critical( "event loop stopped by user interrupt" )
143  return StatusCode.Failure
144 
145  return StatusCode.Success
python.PyAthenaEventLoopMgr.PyAthenaEventLoopMgr.executeAlgorithms
def executeAlgorithms(self, cppcontext)
Definition: PyAthenaEventLoopMgr.py:121
python.PyAthenaEventLoopMgr.PyAthenaEventLoopMgr
Definition: PyAthenaEventLoopMgr.py:72
athena.value
value
Definition: athena.py:122
python.PyAthenaEventLoopMgr.PyAthenaEventLoopMgr.__init__
def __init__(self)
Definition: PyAthenaEventLoopMgr.py:74
python.PyAthenaEventLoopMgr.PyAthenaEventLoopMgr._installServices
def _installServices(self, cppself)
Definition: PyAthenaEventLoopMgr.py:77
python.PyAthenaEventLoopMgr._setupEvtSelForSeekOps
def _setupEvtSelForSeekOps()
Definition: PyAthenaEventLoopMgr.py:59
python.Bindings.py_svc
def py_svc(svcName, createIf=True, iface=None)
Definition: Control/AthenaPython/python/Bindings.py:102
athena._msg
_msg
Definition: athena.py:189
python.PyAthenaEventLoopMgr.enable_seeking
def enable_seeking(silent=False)
Definition: PyAthenaEventLoopMgr.py:15
Configurable
athena/gaudi ----------------------------------------------------------—
python.JetAnalysisCommon.isComponentAccumulatorCfg
isComponentAccumulatorCfg
Definition: JetAnalysisCommon.py:263
CxxUtils::reset
constexpr std::enable_if_t< is_bitmask_v< E >, E & > reset(E &lhs, E rhs)
Convenience function to clear bits in a class enum bitmask.
Definition: bitmask.h:243
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
python.PyAthenaEventLoopMgr.PyAthenaEventLoopMgr.__getattr__
def __getattr__(self, attr)
Definition: PyAthenaEventLoopMgr.py:110