6 """Application manager and other global Gaudi components."""
10 from AthenaCommon
import ExitCodes
12 from AthenaCommon
import AlgSequence, Configurable, Logging
13 import GaudiCoreSvc.GaudiCoreSvcConf
as GaudiCoreSvcConf
14 import GaudiCommonSvc.GaudiCommonSvcConf
as GaudiCommonSvcConf
15 from PyUtils.Helpers
import release_metadata
19 __author__ =
'Wim Lavrijsen (WLavrijsen@lbl.gov)'
21 __all__ = [
'theApp',
'ServiceMgr',
'ToolSvc',
'theAuditorSvc',
31 if not type(tool)
in (list,tuple):
35 if not self._useGlobalInstances:
40 dups = [t
for t
in tool
if t
in self.getChildren()]
42 t.setParent (self.name())
43 tool = [t
for t
in tool
if t
not in self.getChildren()]
44 if len(tool)==0:
return self
49 raise TypeError(
'"%s" is not an AlgTool' %
50 (hasattr(t,
'name')
and t.name()
or "This configurable" ) )
52 super( GaudiCoreSvcConf.ToolSvc, self ).__iadd__( tool )
56 GaudiCoreSvcConf.ToolSvc.__iadd__ = iadd
61 GaudiCoreSvcConf.ToolSvc.copyChild =
lambda self, child : child
69 return 'ServiceManager'
89 msg =
'attempt to add an %s (%s) to the ServiceManager' %\
90 (
type(service).__name__, service.name())
91 Logging.log.error( msg )
92 raise TypeError( msg )
94 super( AthServiceManager, self ).
__iadd__( service )
98 theApp.ExtSvc += [ service.getFullName() ]
103 from GaudiCoreSvc.GaudiCoreSvcConf
import ApplicationMgr
as AppMgr
106 """Python equivalent of IService::State enum (kind of silly to load a whole
107 dictionary library for just this quartet of integers
119 def __init__( self, name = "ApplicationMgr", **kw ):
121 if 'outputLevel' not in kw: kw[
'outputLevel'] = 3
122 if 'jobOptions' not in kw : kw[
'jobOptions'] =
None
125 if 'JobOptionsPath' not in kw: kw[
'JobOptionsPath'] =
""
126 if 'JobOptionsType' not in kw: kw[
'JobOptionsType'] =
"NONE"
127 if 'EventLoop' not in kw: kw[
'EventLoop']=
"AthenaEventLoopMgr"
128 if 'StatusCodeCheck' not in kw: kw[
'StatusCodeCheck'] =
False
131 kw[
'ExtSvcCreates'] =
False
133 super(AthAppMgr,self).
__init__( **kw )
134 self.__dict__[
'_cppApp' ] =
None
135 self.__dict__[
'_sequences' ] = []
137 self.__dict__[
'CreateSvc' ] = []
140 self.__dict__[
'OutStream' ] = []
141 self.__dict__[
'_exitstate' ] = ExitCodes.ALL_OK
143 self.__dict__[
'state'] =
lambda : AthAppMgr.State.OFFLINE
144 self.__dict__[
'Dlls'] = []
151 self.__dict__[
'_opts' ] =
None
155 msg = Logging.log.info
156 msg(
'using release [%(project name)s-%(release)s] [%(platform)s] [%(nightly name)s/%(nightly release)s] -- built on [%(date)s]' % d )
163 return object.__setattr__( self, name, value )
165 if hasattr(self,
'_cppApp')
and self.
_cppApp and \
166 name
in AthAppMgr.__slots__:
168 return handle.__setattr__(name, value)
169 elif name ==
"TopAlg" and value:
170 raise RuntimeError(
"Setting theApp.TopAlg is not supported. "
171 "Add the algorithm to the default AlgSequence() instead.")
174 elif name ==
"OutStream" and value:
175 raise RuntimeError(
"Setting theApp.OutStream is not supported. "
176 "Use theApp.addOutputStream instead.")
177 elif name ==
"CreateSvc":
178 self.__dict__[ name ] = value
180 return super( AppMgr, self ).
__setattr__( name, value )
184 return object.__getattribute__( self, name )
185 if hasattr(self,
'_cppApp')
and self.
_cppApp and \
186 name
in AthAppMgr.__slots__:
187 return getattr(self.
_cppApp, name)
189 if name ==
"Dlls":
return []
193 """helper method to build the top-level AthSequencer from all bits and
194 pieces : AthMasterSeq, AthAlgSeq, AthOutSeq
196 from .
import AlgSequence
as _as
197 from AthenaServices.AthenaServicesConf
import AthIncFirerAlg
as IFA
198 from GaudiCoreSvc.GaudiCoreSvcConf
import IncidentProcAlg
as IPA
201 Logging.log.debug (
"building master sequence...")
202 athMasterSeq = _as.AthSequencer (
"AthMasterSeq",Sequential =
True)
203 athBeginSeq = _as.AthSequencer (
"AthBeginSeq",Sequential=
True)
204 athCondSeq = _as.AthSequencer (
"AthCondSeq", StopOverride=
True)
205 athAlgSeq = _as.AthSequencer (
"AthAlgSeq",IgnoreFilterPassed=
True, StopOverride=
True, ProcessDynamicDataDependencies=
True, ExtraDataForDynamicConsumers=[])
206 athEndSeq = _as.AthSequencer (
"AthEndSeq",Sequential=
True)
207 athOutSeq = _as.AthSequencer (
"AthOutSeq", StopOverride=
True)
208 athAllAlgSeq = _as.AthSequencer (
"AthAllAlgSeq", StopOverride=
True)
209 athAlgEvtSeq = _as.AthSequencer (
"AthAlgEvtSeq",Sequential =
True, StopOverride=
True)
215 ifaBeg=IFA(
"BeginIncFiringAlg")
216 ifaBeg.Incidents=[
"BeginEvent"]
217 ifaBeg.FireSerial=
False
218 athBeginSeq += ifaBeg
219 ipa=IPA(
"IncidentProcAlg1")
225 ifaEnd=IFA(
"EndIncFiringAlg")
226 ifaEnd.Incidents=[
"EndEvent"]
227 ifaEnd.FireSerial=
False
229 ipa2=IPA(
"IncidentProcAlg2")
240 from AthenaCommon.ConcurrencyFlags
import jobproperties
as jp
241 if ( jp.ConcurrencyFlags.NumThreads() > 0 ) :
242 athAllAlgSeq += athAlgSeq
243 athAllAlgSeq += athCondSeq
245 athAllAlgSeq += athCondSeq
246 athAllAlgSeq += athAlgSeq
248 athAlgEvtSeq += athBeginSeq
249 athAlgEvtSeq += athAllAlgSeq
250 athAlgEvtSeq += athEndSeq
252 athMasterSeq += athAlgEvtSeq
253 athMasterSeq += athOutSeq
256 athMasterSeq += IFA(
'EndAlgorithmsFiringAlg',
257 Incidents = [
'EndAlgorithms'],
259 athMasterSeq += IPA(
'IncidentProcAlg3')
261 if Logging.log.isEnabledFor(Logging.logging.DEBUG):
262 from AthenaCommon.AlgSequence
import dumpSequence
265 Logging.log.debug (
"building master sequence... [done]")
268 if not hasattr (self,
'__master_seq_built'):
271 return _as.AthSequencer (
"AthMasterSeq")
277 return super( AppMgr, self ).
algorithm( name )
282 if not hasattr( svcMgr, name ):
283 from AthenaCommon
import CfgMgr
284 svcMgr += getattr( CfgMgr, name )()
285 return getattr( svcMgr, name )
288 if outputLevel != -1 :
289 if self.state() == AthAppMgr.State.OFFLINE :
292 if not hasattr( svcMgr,
'MessageSvc' ):
293 svcMgr += GaudiCoreSvcConf.MessageSvc()
294 svcMgr.MessageSvc.OutputLevel = outputLevel
298 if seq
not in self._sequences:
299 self._sequences.
append( seq )
302 self._sequences.
remove( seq )
306 self._streams += stream
310 for o
in athOutSeq.getChildren():
311 if o.name() == stream:
314 if o.name() == stream:
319 self._streams.
remove( stream )
323 if '_toolsvc' not in self.__dict__:
324 self.__dict__[
'_toolsvc' ] = GaudiCoreSvcConf.ToolSvc( name )
330 if '_servicemgr' not in self.__dict__:
332 return self._servicemgr
333 servicemgr = serviceMgr
337 for k
in self.getProperties().
keys():
338 if k
not in [
"Go",
"Exit",
"AuditInitialize",
"AuditFinalize" ]:
339 props[k] = self.getDefaultProperty(k)
341 props[k] = getattr(self, k)
343 props[
'CreateSvc'] = []
350 _createSvc = self.__dict__[
'CreateSvc']
353 for k,v
in selfOptions.items(): setattr(self, k, v)
356 if self._opts
and not self._opts.fromdb:
357 from AthenaCommon.ConfigurableDb
import getConfigurable
358 if not hasattr(svcMgr,
'JobOptionsSvc'):
360 if not hasattr(svcMgr,
'MessageSvc'):
363 from GaudiPython
import AppMgr
as GaudiAppMgr
364 self.
_cppApp = GaudiAppMgr( outputlevel = self.outputLevel,
366 selfoptions = selfOptions )
367 self.__dict__[
'state'] = self.
_cppApp.state
368 for k,v
in selfOptions.items():
370 self.__dict__[
'CreateSvc'] = _createSvc
378 """ retrieve a handle to the IEventSeek interface of the event loop mgr
380 import AthenaPython.PyAthena
as PyAthena
381 return PyAthena.py_svc(self.
EventLoop, iface=
'IEventSeek')
385 """ retrieve a handle to the ICollectionSize interface of the event loop mgr
387 import AthenaPython.PyAthena
as PyAthena
388 return PyAthena.py_svc(self.
EventLoop, iface=
'ICollectionSize')
391 def setup( self, recursive = False ):
392 if not recursive
and (self._opts
and (self._opts.drop_reload
or self._opts.config_only)):
394 if self._opts.config_only
is True:
396 elif self._opts.config_only:
397 fn = self._opts.config_only
399 fn =
'TempAthenaConfig.' +
str(os.getpid()) +
'.pkl'
402 Logging.log.info(
"storing configuration in %s", fn )
403 from AthenaCommon
import ConfigurationShelve
404 ConfigurationShelve.storeJobOptionsCatalogue( fn )
405 del ConfigurationShelve
407 if self._opts.drop_reload:
410 del sys.argv[ sys.argv.index(
'--drop-and-reload' ) ]
411 for arg
in sys.argv[1:]:
412 if arg[-3:] ==
'.py':
413 del sys.argv[ sys.argv.index( arg ) ]
416 idx = sys.argv.index( arg )
417 del sys.argv[ idx : idx + 2 ]
420 if self._opts.profile_python:
421 i = sys.argv.index(
'--profile-python' )
423 from AthenaCommon.Debugging
import dumpPythonProfile
427 Logging.log.info(
'restarting athena.py from %s ... ', fn )
428 sys.argv.insert( 1, fn )
429 os.execvp( sys.argv[0], sys.argv )
433 if self._opts.profile_python:
434 from AthenaCommon.Debugging
import dumpPythonProfile
436 Logging.log.info(
"configuration complete, now exiting ... " )
445 handle.__dict__[
'CreateSvc'] = self.__dict__.
get(
'CreateSvc',
448 from GaudiPython
import AppMgr
as GaudiAppMgr
458 if hasattr(atexit,
'_exithandlers'):
459 for handler
in atexit._exithandlers[:]:
460 if hasattr(handler[0],
'__module__')
and handler[0].__module__:
461 if 'audiPython' in handler[0].__module__:
463 atexit._exithandlers.remove( handler )
466 def _setattr( self, n, v ):
472 return super( GaudiAppMgr, self ).
__setattr__( n, v )
473 GaudiAppMgr_old_setattr = GaudiAppMgr.__setattr__
474 GaudiAppMgr.__setattr__ = _setattr
477 def _getattr( self, n ):
479 return self.__dict__[n]
480 return super( GaudiAppMgr, self ).__getattr__( n )
481 GaudiAppMgr_old_getattr = GaudiAppMgr.__getattr__
482 GaudiAppMgr.__getattr__ = _getattr
497 handle.TopAlg = [ master_seq.getFullName() ]
500 streams = self.__dict__[
'_streams' ]
502 handle.OutStream =
list(map(
lambda x: x.getFullName(), streams.getChildren() ))
506 super(AppMgr,self).
__setattr__(
'Dlls', handle.Dlls)
510 _createSvc = handle.__dict__[
'CreateSvc']
511 if len(_createSvc) > 0
and \
512 _createSvc[0] != svcMgr.ToolSvc.getFullName():
513 _createSvc = [ svcMgr.ToolSvc.getFullName() ] + _createSvc
515 if 'CreateSvc' in self.__dict__:
516 del self.__dict__[
'CreateSvc']
517 handle.__dict__[
'CreateSvc'] = [ s
for s
in _createSvc ]
520 del handle.__dict__[
'CreateSvc']
521 GaudiAppMgr.__getattr__ = GaudiAppMgr_old_getattr
522 GaudiAppMgr.__setattr__ = GaudiAppMgr_old_setattr
525 Logging.log.debug(
'Updating (C++) "CreateSvc" property...' )
526 handle.CreateSvc = _createSvc
527 Logging.log.debug(
'Updating (C++) "CreateSvc" property... [ok]' )
530 if self._opts
and self._opts.config_dump_file:
531 import AthenaCommon.ConfigurationShelve
as cs
532 cs.saveToAscii(self._opts.config_dump_file)
541 from os
import environ
542 environ[
'CLING_STANDARD_PCH'] =
'none'
544 getattr(cppyy.gbl,
'vector<bool>')
545 getattr(cppyy.gbl,
'vector<float>')
546 getattr(cppyy.gbl,
'vector<unsigned short>')
547 getattr(cppyy.gbl,
'vector<short>')
548 getattr(cppyy.gbl,
'vector<unsigned long>')
549 getattr(cppyy.gbl,
'vector<ULong64_t>')
550 getattr(cppyy.gbl,
'map<string,string>')
556 from AthenaCommon.ConcurrencyFlags
import jobproperties
as jp
559 is_threaded = jp.ConcurrencyFlags.NumThreads() > 0
560 self.
getHandle()._appmgr.initialize.__release_gil__ = is_threaded
571 if self._opts
and self._opts.profile_python:
573 cProfile._athena_python_profiler.enable()
581 parts = name.split(
'/')
582 algname = len(parts) == 2
and parts[1]
or parts[0]
583 sc = self.
algorithm( algname )._ialg.reinitialize()
586 except AttributeError:
593 def run( self, nEvt = None ):
595 if self.state()
in ( AthAppMgr.State.OFFLINE,
596 AthAppMgr.State.CONFIGURED, ):
597 Logging.log.debug(
'Initializing application manager' )
600 Logging.log.debug(
'Re-initializing application manager' )
614 from AthenaCommon.Debugging
import DbgStage
615 if DbgStage.value ==
"exec":
616 from .Debugging
import hookDebugger
621 from AthenaCommon.ConcurrencyFlags
import jobproperties
as jp
624 executeRunMethod = self.
getHandle()._evtpro.executeRun
625 executeRunMethod.__release_gil__ = jp.ConcurrencyFlags.NumThreads() > 0
626 sc = executeRunMethod(nEvt)
638 sc = GaudiPython.SUCCESS
640 Logging.log.debug(
'Starting application manager' )
646 sc = GaudiPython.SUCCESS
648 Logging.log.debug(
'Stopping application manager' )
660 raise RuntimeError(
"C++ application not instantiated : Nothing to finalize !")
662 from AthenaCommon.ConcurrencyFlags
import jobproperties
as jp
663 finalizeMethod = self.
getHandle()._appmgr.finalize
664 finalizeMethod.__release_gil__ = jp.ConcurrencyFlags.NumThreads() > 0
665 sc = finalizeMethod()
677 except AttributeError:
678 raise TypeError(
'%s does not support IEventSeek' % theApp.EventLoop )
684 if not sc.isSuccess():
688 if not sc.isSuccess():
691 except AttributeError:
692 raise TypeError(
'%s does not support IEventSeek' % theApp.EventLoop )
698 if not sc.isSuccess():
701 except AttributeError:
702 raise TypeError(
'%s does not support IEventSeek' % theApp.EventLoop )
714 sc = self.
getHandle()._evtpro.nextEvent( nEvt )
726 except AttributeError:
727 raise TypeError(
'%s does not support ICollectionSize' % theApp.EventLoop )
730 """ if we have MP version of EventLoopMgr
731 replace PoolFileCatalog.xml by MP version"""
732 if (self.
EventLoop ==
'AthMpEventLoopMgr/EventLoopMgr'):
733 if os.path.exists(
"MP_PoolFileCatalog.xml"):
734 Logging.log.info (
"replacing PoolFileCataloG.xml by MP version")
736 shutil.copy2(
"MP_PoolFileCatalog.xml",
"PoolFileCatalog.xml")
739 """Terminate AppMgr and exit python"""
741 if self.state() > AthAppMgr.State.CONFIGURED:
744 sc2 = self.
getHandle()._appmgr.terminate()
745 if ( sc1.isFailure()
or sc2.isFailure() )
and not self.
_exitstate:
751 traceback.print_exc()
753 if self._opts.profile_python:
754 from AthenaCommon.Debugging
import dumpPythonProfile
765 if not os.environ.get(
'PYTHONINSPECT',
''):
766 Logging.log.info(
'leaving with code %d: "%s"', code, ExitCodes.what( code ) )
772 outputLevel = Logging.AthenaLogger.mapLevelLoggingToGaudi( Logging.log.getEffectiveLevel() )
775 ServiceMgr = theApp.serviceMgr()
776 ServiceMgr += theApp.toolSvc()
777 ToolSvc = ServiceMgr.ToolSvc
780 def iadd( self, config ):
781 super( GaudiCommonSvcConf.AuditorSvc, self ).__iadd__( config )
784 if not config.getName()
in self.Auditors
and \
785 not config.getFullName()
in self.Auditors:
786 self.Auditors += [ config.getFullName() ]
790 GaudiCommonSvcConf.AuditorSvc.__iadd__ =iadd
796 c = getattr( self, attr )
799 self.Auditors.
remove( c.getFullName() )
804 self.Auditors.
remove( c.getName() )
808 except AttributeError:
811 super( GaudiCommonSvcConf.AuditorSvc, self ).__delattr__( attr )
813 GaudiCommonSvcConf.AuditorSvc.__delattr__ = _delattr
818 ServiceMgr += GaudiCommonSvcConf.AuditorSvc()
819 theAuditorSvc = ServiceMgr.AuditorSvc