6 """Application manager and other global Gaudi components."""
9 from AthenaCommon
import ExitCodes
11 from AthenaCommon
import AlgSequence, Configurable, Logging
12 import GaudiCoreSvc.GaudiCoreSvcConf
as GaudiCoreSvcConf
13 import GaudiCommonSvc.GaudiCommonSvcConf
as GaudiCommonSvcConf
14 from PyUtils.Helpers
import release_metadata
18 __author__ =
'Wim Lavrijsen (WLavrijsen@lbl.gov)'
20 __all__ = [
'theApp',
'ServiceMgr',
'ToolSvc',
'theAuditorSvc',
23 'athAlgSeq',
'topSequence',
30 if not type(tool)
in (list,tuple):
34 if not self._useGlobalInstances:
39 dups = [t
for t
in tool
if t
in self.getChildren()]
41 t.setParent (self.name())
42 tool = [t
for t
in tool
if t
not in self.getChildren()]
43 if len(tool)==0:
return self
48 raise TypeError(
'"%s" is not an AlgTool' %
49 (hasattr(t,
'name')
and t.name()
or "This configurable" ) )
51 super( GaudiCoreSvcConf.ToolSvc, self ).__iadd__( tool )
55 GaudiCoreSvcConf.ToolSvc.__iadd__ = iadd
60 GaudiCoreSvcConf.ToolSvc.copyChild =
lambda self, child : child
68 return 'ServiceManager'
88 msg =
'attempt to add an %s (%s) to the ServiceManager' %\
89 (
type(service).__name__, service.name())
90 Logging.log.error( msg )
91 raise TypeError( msg )
93 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 )
304 if stream
not in self._streams.getChildren():
305 self._streams += stream
309 for o
in athOutSeq.getChildren():
310 if o.name() == stream:
312 for o
in self._streams.getChildren():
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)):
395 if self._opts.config_only
is True:
397 elif self._opts.config_only:
398 fn = self._opts.config_only
400 fn =
'TempAthenaConfig.' +
str(os.getpid()) +
'.pkl'
403 Logging.log.info(
"storing configuration in %s", fn )
404 from AthenaCommon
import ConfigurationShelve
405 ConfigurationShelve.storeJobOptionsCatalogue( fn )
406 del ConfigurationShelve
408 if self._opts.drop_reload:
411 del sys.argv[ sys.argv.index(
'--drop-and-reload' ) ]
412 for arg
in sys.argv[1:]:
413 if arg[-3:] ==
'.py':
414 del sys.argv[ sys.argv.index( arg ) ]
417 idx = sys.argv.index( arg )
418 del sys.argv[ idx : idx + 2 ]
421 if self._opts.profile_python:
422 i = sys.argv.index(
'--profile-python' )
424 from AthenaCommon.Debugging
import dumpPythonProfile
428 Logging.log.info(
'restarting athena.py from %s ... ', fn )
429 sys.argv.insert( 1, fn )
430 os.execvp( sys.argv[0], sys.argv )
434 if self._opts.profile_python:
435 from AthenaCommon.Debugging
import dumpPythonProfile
437 Logging.log.info(
"configuration complete, now exiting ... " )
446 handle.__dict__[
'CreateSvc'] = self.__dict__.
get(
'CreateSvc',
449 from GaudiPython
import AppMgr
as GaudiAppMgr
459 if hasattr(atexit,
'_exithandlers'):
460 for handler
in atexit._exithandlers[:]:
461 if hasattr(handler[0],
'__module__')
and handler[0].__module__:
462 if 'audiPython' in handler[0].__module__:
464 atexit._exithandlers.remove( handler )
467 def _setattr( self, n, v ):
473 return super( GaudiAppMgr, self ).
__setattr__( n, v )
474 GaudiAppMgr_old_setattr = GaudiAppMgr.__setattr__
475 GaudiAppMgr.__setattr__ = _setattr
478 def _getattr( self, n ):
480 return self.__dict__[n]
481 return super( GaudiAppMgr, self ).__getattr__( n )
482 GaudiAppMgr_old_getattr = GaudiAppMgr.__getattr__
483 GaudiAppMgr.__getattr__ = _getattr
498 handle.TopAlg = [ master_seq.getFullName() ]
501 streams = self.__dict__[
'_streams' ]
503 handle.OutStream =
list(map(
lambda x: x.getFullName(), streams.getChildren() ))
507 super(AppMgr,self).
__setattr__(
'Dlls', handle.Dlls)
511 _createSvc = handle.__dict__[
'CreateSvc']
512 if len(_createSvc) > 0
and \
513 _createSvc[0] != svcMgr.ToolSvc.getFullName():
514 _createSvc = [ svcMgr.ToolSvc.getFullName() ] + _createSvc
516 if 'CreateSvc' in self.__dict__:
517 del self.__dict__[
'CreateSvc']
518 handle.__dict__[
'CreateSvc'] = [ s
for s
in _createSvc ]
521 del handle.__dict__[
'CreateSvc']
522 GaudiAppMgr.__getattr__ = GaudiAppMgr_old_getattr
523 GaudiAppMgr.__setattr__ = GaudiAppMgr_old_setattr
526 Logging.log.debug(
'Updating (C++) "CreateSvc" property...' )
527 handle.CreateSvc = _createSvc
528 Logging.log.debug(
'Updating (C++) "CreateSvc" property... [ok]' )
531 if self._opts
and self._opts.config_dump_file:
532 import AthenaCommon.ConfigurationShelve
as cs
533 cs.saveToAscii(self._opts.config_dump_file)
542 from os
import environ
543 environ[
'CLING_STANDARD_PCH'] =
'none'
545 getattr(cppyy.gbl,
'vector<bool>')
546 getattr(cppyy.gbl,
'vector<float>')
547 getattr(cppyy.gbl,
'vector<unsigned short>')
548 getattr(cppyy.gbl,
'vector<short>')
549 getattr(cppyy.gbl,
'vector<unsigned long>')
550 getattr(cppyy.gbl,
'vector<ULong64_t>')
551 getattr(cppyy.gbl,
'map<string,string>')
557 from AthenaCommon.ConcurrencyFlags
import jobproperties
as jp
560 is_threaded = jp.ConcurrencyFlags.NumThreads() > 0
561 self.
getHandle()._appmgr.initialize.__release_gil__ = is_threaded
572 if self._opts
and self._opts.profile_python:
574 cProfile._athena_python_profiler.enable()
582 parts = name.split(
'/')
583 algname = len(parts) == 2
and parts[1]
or parts[0]
584 sc = self.
algorithm( algname )._ialg.reinitialize()
587 except AttributeError:
594 def run( self, nEvt = None ):
596 if self.state()
in ( AthAppMgr.State.OFFLINE,
597 AthAppMgr.State.CONFIGURED, ):
598 Logging.log.debug(
'Initializing application manager' )
601 Logging.log.debug(
'Re-initializing application manager' )
615 from AthenaCommon.Debugging
import DbgStage
616 if DbgStage.value ==
"exec":
617 from .Debugging
import hookDebugger
622 from AthenaCommon.ConcurrencyFlags
import jobproperties
as jp
625 executeRunMethod = self.
getHandle()._evtpro.executeRun
626 executeRunMethod.__release_gil__ = jp.ConcurrencyFlags.NumThreads() > 0
627 sc = executeRunMethod(nEvt)
639 sc = GaudiPython.SUCCESS
641 Logging.log.debug(
'Starting application manager' )
647 sc = GaudiPython.SUCCESS
649 Logging.log.debug(
'Stopping application manager' )
661 raise RuntimeError(
"C++ application not instantiated : Nothing to finalize !")
663 from AthenaCommon.ConcurrencyFlags
import jobproperties
as jp
664 finalizeMethod = self.
getHandle()._appmgr.finalize
665 finalizeMethod.__release_gil__ = jp.ConcurrencyFlags.NumThreads() > 0
666 sc = finalizeMethod()
678 except AttributeError:
679 raise TypeError(
'%s does not support IEventSeek' % theApp.EventLoop )
685 if not sc.isSuccess():
689 if not sc.isSuccess():
692 except AttributeError:
693 raise TypeError(
'%s does not support IEventSeek' % theApp.EventLoop )
699 if not sc.isSuccess():
702 except AttributeError:
703 raise TypeError(
'%s does not support IEventSeek' % theApp.EventLoop )
715 sc = self.
getHandle()._evtpro.nextEvent( nEvt )
727 except AttributeError:
728 raise TypeError(
'%s does not support ICollectionSize' % theApp.EventLoop )
731 """ if we have MP version of EventLoopMgr
732 replace PoolFileCatalog.xml by MP version"""
733 if (self.
EventLoop ==
'AthMpEventLoopMgr/EventLoopMgr'):
734 if os.path.exists(
"MP_PoolFileCatalog.xml"):
735 Logging.log.info (
"replacing PoolFileCataloG.xml by MP version")
737 shutil.copy2(
"MP_PoolFileCatalog.xml",
"PoolFileCatalog.xml")
742 if self.state() > AthAppMgr.State.CONFIGURED:
745 sc2 = self.
getHandle()._appmgr.terminate()
746 if ( sc1.isFailure()
or sc2.isFailure() )
and not self.
_exitstate:
752 traceback.print_exc()
754 if self._opts.profile_python:
755 from AthenaCommon.Debugging
import dumpPythonProfile
758 Logging.log.info(
'leaving with code %d: "%s"',
760 sys.exit( code
is None and self.
_exitstate or code )
765 outputLevel = Logging.AthenaLogger.mapLevelLoggingToGaudi( Logging.log.getEffectiveLevel() )
768 ServiceMgr = theApp.serviceMgr()
769 ServiceMgr += theApp.toolSvc()
770 ToolSvc = ServiceMgr.ToolSvc
773 def iadd( self, config ):
774 super( GaudiCommonSvcConf.AuditorSvc, self ).__iadd__( config )
777 if not config.getName()
in self.Auditors
and \
778 not config.getFullName()
in self.Auditors:
779 self.Auditors += [ config.getFullName() ]
783 GaudiCommonSvcConf.AuditorSvc.__iadd__ =iadd
789 c = getattr( self, attr )
792 self.Auditors.
remove( c.getFullName() )
797 self.Auditors.
remove( c.getName() )
801 except AttributeError:
804 super( GaudiCommonSvcConf.AuditorSvc, self ).__delattr__( attr )
806 GaudiCommonSvcConf.AuditorSvc.__delattr__ = _delattr
811 ServiceMgr += GaudiCommonSvcConf.AuditorSvc()
812 theAuditorSvc = ServiceMgr.AuditorSvc
838 topSequence = athAlgSeq