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 )
97 theApp.ExtSvc += [ service.getFullName() ]
102 from GaudiCoreSvc.GaudiCoreSvcConf
import ApplicationMgr
as AppMgr
105 """Python equivalent of IService::State enum (kind of silly to load a whole
106 dictionary library for just this quartet of integers
118 def __init__( self, name = "ApplicationMgr", **kw ):
120 if 'outputLevel' not in kw: kw[
'outputLevel'] = 3
121 if 'jobOptions' not in kw : kw[
'jobOptions'] =
None
124 if 'JobOptionsPath' not in kw: kw[
'JobOptionsPath'] =
""
125 if 'JobOptionsType' not in kw: kw[
'JobOptionsType'] =
"NONE"
126 if 'EventLoop' not in kw: kw[
'EventLoop']=
"AthenaEventLoopMgr"
127 if 'StatusCodeCheck' not in kw: kw[
'StatusCodeCheck'] =
False
130 kw[
'ExtSvcCreates'] =
False
132 super(AthAppMgr,self).
__init__( **kw )
133 self.__dict__[
'_cppApp' ] =
None
134 self.__dict__[
'_sequences' ] = []
136 self.__dict__[
'CreateSvc' ] = []
139 self.__dict__[
'OutStream' ] = []
140 self.__dict__[
'_exitstate' ] = ExitCodes.ALL_OK
142 self.__dict__[
'state'] =
lambda : AthAppMgr.State.OFFLINE
143 self.__dict__[
'Dlls'] = []
150 self.__dict__[
'_opts' ] =
None
154 msg = Logging.log.info
155 msg(
'using release [%(project name)s-%(release)s] [%(platform)s] [%(nightly name)s/%(nightly release)s] -- built on [%(date)s]' % d )
162 return object.__setattr__( self, name, value )
164 if hasattr(self,
'_cppApp')
and self.
_cppApp and \
165 name
in AthAppMgr.__slots__:
167 return handle.__setattr__(name, value)
168 elif name ==
"TopAlg" and value:
169 raise RuntimeError(
"Setting theApp.TopAlg is not supported. "
170 "Add the algorithm to the default AlgSequence() instead.")
173 elif name ==
"OutStream" and value:
174 raise RuntimeError(
"Setting theApp.OutStream is not supported. "
175 "Use theApp.addOutputStream instead.")
176 elif name ==
"CreateSvc":
177 self.__dict__[ name ] = value
179 return super( AppMgr, self ).
__setattr__( name, value )
183 return object.__getattribute__( self, name )
184 if hasattr(self,
'_cppApp')
and self.
_cppApp and \
185 name
in AthAppMgr.__slots__:
186 return getattr(self.
_cppApp, name)
188 if name ==
"Dlls":
return []
192 """helper method to build the top-level AthSequencer from all bits and
193 pieces : AthMasterSeq, AthAlgSeq, AthOutSeq
195 from .
import AlgSequence
as _as
196 from AthenaServices.AthenaServicesConf
import AthIncFirerAlg
as IFA
197 from GaudiCoreSvc.GaudiCoreSvcConf
import IncidentProcAlg
as IPA
200 Logging.log.debug (
"building master sequence...")
201 athMasterSeq = _as.AthSequencer (
"AthMasterSeq",Sequential =
True)
202 athBeginSeq = _as.AthSequencer (
"AthBeginSeq",Sequential=
True)
203 athCondSeq = _as.AthSequencer (
"AthCondSeq", StopOverride=
True)
204 athAlgSeq = _as.AthSequencer (
"AthAlgSeq",IgnoreFilterPassed=
True, StopOverride=
True, ProcessDynamicDataDependencies=
True, ExtraDataForDynamicConsumers=[])
205 athEndSeq = _as.AthSequencer (
"AthEndSeq",Sequential=
True)
206 athOutSeq = _as.AthSequencer (
"AthOutSeq", StopOverride=
True)
207 athAllAlgSeq = _as.AthSequencer (
"AthAllAlgSeq", StopOverride=
True)
208 athAlgEvtSeq = _as.AthSequencer (
"AthAlgEvtSeq",Sequential =
True, StopOverride=
True)
214 ifaBeg=IFA(
"BeginIncFiringAlg")
215 ifaBeg.Incidents=[
"BeginEvent"]
216 ifaBeg.FireSerial=
False
217 athBeginSeq += ifaBeg
218 ipa=IPA(
"IncidentProcAlg1")
224 ifaEnd=IFA(
"EndIncFiringAlg")
225 ifaEnd.Incidents=[
"EndEvent"]
226 ifaEnd.FireSerial=
False
228 ipa2=IPA(
"IncidentProcAlg2")
239 from AthenaCommon.ConcurrencyFlags
import jobproperties
as jp
240 if ( jp.ConcurrencyFlags.NumThreads() > 0 ) :
241 athAllAlgSeq += athAlgSeq
242 athAllAlgSeq += athCondSeq
244 athAllAlgSeq += athCondSeq
245 athAllAlgSeq += athAlgSeq
247 athAlgEvtSeq += athBeginSeq
248 athAlgEvtSeq += athAllAlgSeq
249 athAlgEvtSeq += athEndSeq
251 athMasterSeq += athAlgEvtSeq
252 athMasterSeq += athOutSeq
255 athMasterSeq += IFA(
'EndAlgorithmsFiringAlg',
256 Incidents = [
'EndAlgorithms'],
258 athMasterSeq += IPA(
'IncidentProcAlg3')
260 if Logging.log.isEnabledFor(Logging.logging.DEBUG):
261 from AthenaCommon.AlgSequence
import dumpSequence
264 Logging.log.debug (
"building master sequence... [done]")
267 if not hasattr (self,
'__master_seq_built'):
270 return _as.AthSequencer (
"AthMasterSeq")
276 return super( AppMgr, self ).
algorithm( name )
281 if not hasattr( svcMgr, name ):
282 from AthenaCommon
import CfgMgr
283 svcMgr += getattr( CfgMgr, name )()
284 return getattr( svcMgr, name )
287 if outputLevel != -1 :
288 if self.state() == AthAppMgr.State.OFFLINE :
291 if not hasattr( svcMgr,
'MessageSvc' ):
292 svcMgr += GaudiCoreSvcConf.MessageSvc()
293 svcMgr.MessageSvc.OutputLevel = outputLevel
297 if seq
not in self._sequences:
298 self._sequences.
append( seq )
301 self._sequences.
remove( seq )
305 self._streams += stream
309 for o
in athOutSeq.getChildren():
310 if o.name() == stream:
313 if o.name() == stream:
318 self._streams.
remove( stream )
322 if '_toolsvc' not in self.__dict__:
323 self.__dict__[
'_toolsvc' ] = GaudiCoreSvcConf.ToolSvc( name )
329 if '_servicemgr' not in self.__dict__:
331 return self._servicemgr
332 servicemgr = serviceMgr
336 for k
in self.getProperties().
keys():
337 if k
not in [
"Go",
"Exit",
"AuditInitialize",
"AuditFinalize" ]:
338 props[k] = self.getDefaultProperty(k)
340 props[k] = getattr(self, k)
342 props[
'CreateSvc'] = []
349 _createSvc = self.__dict__[
'CreateSvc']
352 for k,v
in selfOptions.items(): setattr(self, k, v)
355 if self._opts
and not self._opts.fromdb:
356 from AthenaCommon.ConfigurableDb
import getConfigurable
357 if not hasattr(svcMgr,
'JobOptionsSvc'):
359 if not hasattr(svcMgr,
'MessageSvc'):
362 from GaudiPython
import AppMgr
as GaudiAppMgr
363 self.
_cppApp = GaudiAppMgr( outputlevel = self.outputLevel,
365 selfoptions = selfOptions )
366 self.__dict__[
'state'] = self.
_cppApp.state
367 for k,v
in selfOptions.items():
369 self.__dict__[
'CreateSvc'] = _createSvc
377 """ retrieve a handle to the IEventSeek interface of the event loop mgr
379 import AthenaPython.PyAthena
as PyAthena
380 return PyAthena.py_svc(self.
EventLoop, iface=
'IEventSeek')
384 """ retrieve a handle to the ICollectionSize interface of the event loop mgr
386 import AthenaPython.PyAthena
as PyAthena
387 return PyAthena.py_svc(self.
EventLoop, iface=
'ICollectionSize')
390 def setup( self, recursive = False ):
391 if not recursive
and (self._opts
and (self._opts.drop_reload
or self._opts.config_only)):
393 if self._opts.config_only
is True:
395 elif self._opts.config_only:
396 fn = self._opts.config_only
398 fn =
'TempAthenaConfig.' +
str(os.getpid()) +
'.pkl'
401 Logging.log.info(
"storing configuration in %s", fn )
402 from AthenaCommon
import ConfigurationShelve
403 ConfigurationShelve.storeJobOptionsCatalogue( fn )
404 del ConfigurationShelve
406 if self._opts.drop_reload:
409 del sys.argv[ sys.argv.index(
'--drop-and-reload' ) ]
410 for arg
in sys.argv[1:]:
411 if arg[-3:] ==
'.py':
412 del sys.argv[ sys.argv.index( arg ) ]
415 idx = sys.argv.index( arg )
416 del sys.argv[ idx : idx + 2 ]
419 if self._opts.profile_python:
420 i = sys.argv.index(
'--profile-python' )
422 from AthenaCommon.Debugging
import dumpPythonProfile
426 Logging.log.info(
'restarting athena.py from %s ... ', fn )
427 sys.argv.insert( 1, fn )
428 os.execvp( sys.argv[0], sys.argv )
432 if self._opts.profile_python:
433 from AthenaCommon.Debugging
import dumpPythonProfile
435 Logging.log.info(
"configuration complete, now exiting ... " )
444 handle.__dict__[
'CreateSvc'] = self.__dict__.
get(
'CreateSvc',
447 from GaudiPython
import AppMgr
as GaudiAppMgr
457 if hasattr(atexit,
'_exithandlers'):
458 for handler
in atexit._exithandlers[:]:
459 if hasattr(handler[0],
'__module__')
and handler[0].__module__:
460 if 'audiPython' in handler[0].__module__:
462 atexit._exithandlers.remove( handler )
465 def _setattr( self, n, v ):
471 return super( GaudiAppMgr, self ).
__setattr__( n, v )
472 GaudiAppMgr_old_setattr = GaudiAppMgr.__setattr__
473 GaudiAppMgr.__setattr__ = _setattr
476 def _getattr( self, n ):
478 return self.__dict__[n]
479 return super( GaudiAppMgr, self ).__getattr__( n )
480 GaudiAppMgr_old_getattr = GaudiAppMgr.__getattr__
481 GaudiAppMgr.__getattr__ = _getattr
496 handle.TopAlg = [ master_seq.getFullName() ]
499 streams = self.__dict__[
'_streams' ]
501 handle.OutStream =
list(map(
lambda x: x.getFullName(), streams.getChildren() ))
505 super(AppMgr,self).
__setattr__(
'Dlls', handle.Dlls)
509 _createSvc = handle.__dict__[
'CreateSvc']
510 if len(_createSvc) > 0
and \
511 _createSvc[0] != svcMgr.ToolSvc.getFullName():
512 _createSvc = [ svcMgr.ToolSvc.getFullName() ] + _createSvc
514 if 'CreateSvc' in self.__dict__:
515 del self.__dict__[
'CreateSvc']
516 handle.__dict__[
'CreateSvc'] = [ s
for s
in _createSvc ]
519 del handle.__dict__[
'CreateSvc']
520 GaudiAppMgr.__getattr__ = GaudiAppMgr_old_getattr
521 GaudiAppMgr.__setattr__ = GaudiAppMgr_old_setattr
524 Logging.log.debug(
'Updating (C++) "CreateSvc" property...' )
525 handle.CreateSvc = _createSvc
526 Logging.log.debug(
'Updating (C++) "CreateSvc" property... [ok]' )
529 if self._opts
and self._opts.config_dump_file:
530 import AthenaCommon.ConfigurationShelve
as cs
531 cs.saveToAscii(self._opts.config_dump_file)
540 from os
import environ
541 environ[
'CLING_STANDARD_PCH'] =
'none'
543 getattr(cppyy.gbl,
'vector<bool>')
544 getattr(cppyy.gbl,
'vector<float>')
545 getattr(cppyy.gbl,
'vector<unsigned short>')
546 getattr(cppyy.gbl,
'vector<short>')
547 getattr(cppyy.gbl,
'vector<unsigned long>')
548 getattr(cppyy.gbl,
'vector<ULong64_t>')
549 getattr(cppyy.gbl,
'map<string,string>')
555 from AthenaCommon.ConcurrencyFlags
import jobproperties
as jp
558 is_threaded = jp.ConcurrencyFlags.NumThreads() > 0
559 self.
getHandle()._appmgr.initialize.__release_gil__ = is_threaded
570 if self._opts
and self._opts.profile_python:
572 cProfile._athena_python_profiler.enable()
580 parts = name.split(
'/')
581 algname = len(parts) == 2
and parts[1]
or parts[0]
582 sc = self.
algorithm( algname )._ialg.reinitialize()
585 except AttributeError:
592 def run( self, nEvt = None ):
594 if self.state()
in ( AthAppMgr.State.OFFLINE,
595 AthAppMgr.State.CONFIGURED, ):
596 Logging.log.debug(
'Initializing application manager' )
599 Logging.log.debug(
'Re-initializing application manager' )
613 from AthenaCommon.Debugging
import DbgStage
614 if DbgStage.value ==
"exec":
615 from .Debugging
import hookDebugger
620 from AthenaCommon.ConcurrencyFlags
import jobproperties
as jp
623 executeRunMethod = self.
getHandle()._evtpro.executeRun
624 executeRunMethod.__release_gil__ = jp.ConcurrencyFlags.NumThreads() > 0
625 sc = executeRunMethod(nEvt)
637 sc = GaudiPython.SUCCESS
639 Logging.log.debug(
'Starting application manager' )
645 sc = GaudiPython.SUCCESS
647 Logging.log.debug(
'Stopping application manager' )
659 raise RuntimeError(
"C++ application not instantiated : Nothing to finalize !")
661 from AthenaCommon.ConcurrencyFlags
import jobproperties
as jp
662 finalizeMethod = self.
getHandle()._appmgr.finalize
663 finalizeMethod.__release_gil__ = jp.ConcurrencyFlags.NumThreads() > 0
664 sc = finalizeMethod()
676 except AttributeError:
677 raise TypeError(
'%s does not support IEventSeek' % theApp.EventLoop )
683 if not sc.isSuccess():
687 if not sc.isSuccess():
690 except AttributeError:
691 raise TypeError(
'%s does not support IEventSeek' % theApp.EventLoop )
697 if not sc.isSuccess():
700 except AttributeError:
701 raise TypeError(
'%s does not support IEventSeek' % theApp.EventLoop )
713 sc = self.
getHandle()._evtpro.nextEvent( nEvt )
725 except AttributeError:
726 raise TypeError(
'%s does not support ICollectionSize' % theApp.EventLoop )
729 """ if we have MP version of EventLoopMgr
730 replace PoolFileCatalog.xml by MP version"""
731 if (self.
EventLoop ==
'AthMpEventLoopMgr/EventLoopMgr'):
732 if os.path.exists(
"MP_PoolFileCatalog.xml"):
733 Logging.log.info (
"replacing PoolFileCataloG.xml by MP version")
735 shutil.copy2(
"MP_PoolFileCatalog.xml",
"PoolFileCatalog.xml")
738 """Terminate AppMgr and exit python"""
740 if self.state() > AthAppMgr.State.CONFIGURED:
743 sc2 = self.
getHandle()._appmgr.terminate()
744 if ( sc1.isFailure()
or sc2.isFailure() )
and not self.
_exitstate:
750 traceback.print_exc()
752 if self._opts.profile_python:
753 from AthenaCommon.Debugging
import dumpPythonProfile
764 if not os.environ.get(
'PYTHONINSPECT',
''):
765 Logging.log.info(
'leaving with code %d: "%s"', code, ExitCodes.what( code ) )
771 outputLevel = Logging.AthenaLogger.mapLevelLoggingToGaudi( Logging.log.getEffectiveLevel() )
774 ServiceMgr = theApp.serviceMgr()
775 ServiceMgr += theApp.toolSvc()
776 ToolSvc = ServiceMgr.ToolSvc
779 def iadd( self, config ):
780 super( GaudiCommonSvcConf.AuditorSvc, self ).__iadd__( config )
783 if not config.getName()
in self.Auditors
and \
784 not config.getFullName()
in self.Auditors:
785 self.Auditors += [ config.getFullName() ]
789 GaudiCommonSvcConf.AuditorSvc.__iadd__ =iadd
795 c = getattr( self, attr )
798 self.Auditors.
remove( c.getFullName() )
803 self.Auditors.
remove( c.getName() )
807 except AttributeError:
810 super( GaudiCommonSvcConf.AuditorSvc, self ).__delattr__( attr )
812 GaudiCommonSvcConf.AuditorSvc.__delattr__ = _delattr
817 ServiceMgr += GaudiCommonSvcConf.AuditorSvc()
818 theAuditorSvc = ServiceMgr.AuditorSvc