The Gaudi Framework  v33r2 (a6f0ec87)
Bindings.py
Go to the documentation of this file.
1 
13 """ GaudiPython.Bindings module.
14  This module provides the basic bindings of the main Gaudi
15  components to Python. It is itself based on the ROOT cppyy
16  Python extension module.
17 """
18 from __future__ import absolute_import, print_function
19 
20 __all__ = [
21  'gbl', 'InterfaceCast', 'Interface', 'PropertyEntry', 'AppMgr',
22  'PyAlgorithm', 'CallbackStreamBuf', 'iAlgorithm', 'iDataSvc',
23  'iHistogramSvc', 'iNTupleSvc', 'iService', 'iAlgTool', 'Helper', 'SUCCESS',
24  'FAILURE', 'toArray', 'ROOT', 'makeNullPointer', 'setOwnership',
25  'getClass', 'loaddict', 'deprecation'
26 ]
27 
28 from GaudiKernel import ROOT6WorkAroundEnabled
29 
30 import os
31 import sys
32 import string
33 import warnings
34 import re
35 
36 # Workaround for ROOT-10769
37 with warnings.catch_warnings():
38  warnings.simplefilter("ignore")
39  import cppyy
40 
41 if sys.version_info >= (3, ):
42  # Python 2 compatibility
43  long = int
44 
45 if ROOT6WorkAroundEnabled('ROOT-5478'):
46  # Trigger the loading of GaudiKernelDict
47  cppyy.gbl.DataObject
48  # Trigger the loading of GaudiPythonDict
49  cppyy.gbl.Chrono
50 
51 from . import Pythonizations
52 # Import Configurable from AthenaCommon or GaudiKernel if the first is not
53 # available.
54 from GaudiKernel.Proxy.Configurable import Configurable, getNeededConfigurables
55 
56 # namespaces
57 gbl = cppyy.gbl
58 Gaudi = gbl.Gaudi
59 
60 _gaudi = None
61 
62 # ---- Useful shortcuts for classes -------------------------------------------
63 gbl.gInterpreter.Declare('#include "GaudiKernel/Property.h"')
64 Helper = gbl.GaudiPython.Helper
65 StringProperty = gbl.Gaudi.Property('std::string')
66 StringPropertyRef = gbl.Gaudi.Property('std::string&')
67 GaudiHandleProperty = gbl.GaudiHandleProperty
68 GaudiHandleArrayProperty = gbl.GaudiHandleArrayProperty
69 DataObject = gbl.DataObject
70 SUCCESS = gbl.StatusCode(gbl.StatusCode.SUCCESS, True)
71 FAILURE = gbl.StatusCode(gbl.StatusCode.FAILURE, True)
72 # Workaround for ROOT-10770
73 if hasattr(cppyy, 'nullptr'):
74  nullptr = cppyy.nullptr
75 elif hasattr(gbl, 'nullptr'):
76  nullptr = gbl.nullptr
77 else:
78  nullptr = None
79 # Helper to create a StringProperty
80 cppyy.gbl.gInterpreter.Declare('''
81 namespace GaudiPython { namespace Helpers {
82  Gaudi::Property<std::string> mkStringProperty(const std::string &name,
83  const std::string &value) {
84  return Gaudi::Property<std::string>{name, value};
85  }
86 }}''')
87 
88 # toIntArray, toShortArray, etc.
89 for l in [l for l in dir(Helper) if re.match("^to.*Array$", l)]:
90  exec("%s = Helper.%s" % (l, l))
91  __all__.append(l)
92 
93 # FIXME: (MCl) Hack to handle ROOT 5.18 and ROOT >= 5.20
94 if hasattr(Helper, "toArray"):
95  # This is not backward compatible, but allows to use the same signature
96  # with all the versions of ROOT.
97  def toArray(typ):
98  return getattr(Helper, "toArray")
99 else:
100  # forward to the actual implementation of GaudiPython::Helper::toArray<T>
101  def toArray(typ):
102  return getattr(Helper, "toArray<%s>" % typ)
103 
104 
105 # ----Convenient accessors to PyROOT functionality ----------------------------
106 # See https://sft.its.cern.ch/jira/browse/ROOT-10771
107 if hasattr(cppyy, 'libPyROOT'):
108  ROOT = cppyy.libPyROOT
109 else:
110  import ROOT
111 makeNullPointer = ROOT.MakeNullPointer
112 setOwnership = ROOT.SetOwnership
113 
114 
115 def deprecation(message):
116  warnings.warn('GaudiPython: ' + message, DeprecationWarning, stacklevel=3)
117 
118 
119 # ----InterfaceCast class -----------------------------------------------------
120 
121 
122 class InterfaceCast(object):
123  """ Helper class to obtain the adequate interface from a component
124  by using the Gaudi queryInterface() mechanism """
125 
126  def __init__(self, t):
127  if type(t) is str:
128  t = getattr(gbl, t)
129  self.type = t
130 
131  def __call__(self, obj):
132  if obj:
133  ip = makeNullPointer(self.type)
134  try:
135  if obj.queryInterface(self.type.interfaceID(), ip).isSuccess():
136  return ip
137  else:
138  print("ERROR: queryInterface failed for", obj,
139  "interface:", self.type)
140  except Exception as e:
141  print("ERROR: exception", e,
142  "caught when retrieving interface", self.type,
143  "for object", obj)
144  import traceback
145  traceback.print_stack()
146  return None
147 
148  cast = __call__
149 
150 
151 # ---Interface class (for backward compatibility)------------------------------
152 
153 
155  def __init__(self, t):
156  deprecation('Use InterfaceCast class instead')
157  InterfaceCast.__init__(self, t)
158 
159  def cast(self, obj):
160  return self(obj)
161 
162 
163 # ----load dictionary function using Gaudi function----------------------------
164 
165 
166 def loaddict(dict):
167  """ Load a LCG dictionary using various mechanisms"""
168  if Helper.loadDynamicLib(dict) == 1:
169  return
170  else:
171  try:
172  cppyy.loadDict(dict)
173  except:
174  raise ImportError('Error loading dictionary library')
175 
176 
177 # ---get a class (by loading modules if needed)--------------------------------
178 
179 
180 def getClass(name, libs=[]):
181  """
182  Function to retrieve a certain C++ class by name and to load dictionary if requested
183 
184  Usage:
185 
186  from gaudimodule import getClass
187  # one knows that class is already loaded
188  AppMgr = getClass( 'ApplicationMgr' )
189  # one knows where to look for class, if not loaded yet
190  MCParticle = getClass( 'MCParticle' , 'EventDict' )
191  # one knows where to look for class, if not loaded yet
192  Vertex = getClass( 'Vertex' , ['EventDict', 'PhysEventDict'] )
193  """
194  # see if class is already loaded
195  if hasattr(gbl, name):
196  return getattr(gbl, name)
197  # try to load dictionaries and look for the required class
198  if type(libs) is not list:
199  libs = [libs]
200  for lib in libs:
201  loaddict(lib)
202  if hasattr(gbl, name):
203  return getattr(gbl, name)
204  # return None ( or raise exception? I do not know... )
205  return None
206 
207 
208 # ----PropertyEntry class------------------------------------------------------
209 
210 
211 class PropertyEntry(object):
212  """ holds the value and the documentation string of a property """
213 
214  def __init__(self, prop):
215  self._type = type(prop).__name__
216  self.__doc__ = " --- Property type is " + self.ptype()
217 
218  if issubclass(type(prop), GaudiHandleProperty):
219  self._value = prop.value() # do nothing for ATLAS' handles
220  elif issubclass(type(prop), GaudiHandleArrayProperty):
221  self._value = prop.value() # do nothing for ATLAS' handles
222  else:
223  # for all other types try to extract the native python type
224  try:
225  self._value = eval(prop.toString(), {}, {})
226  except:
227  if hasattr(prop, 'value'):
228  self._value = prop.value()
229  else:
230  self._value = prop.toString()
231 
232  self.__doc__ += " --- Default value = " + str(self._value) + " --- "
233  if prop.documentation() != 'none':
234  self.__doc__ = prop.documentation() + '\\n' + self.__doc__
235  # keep the original property
236  self._property = prop # property itself
237 
238  def value(self):
239  return self._value
240 
241  def ptype(self):
242  return self._type
243 
244  def property(self):
245  "Return the underlying property itself "
246  return self._property
247 
248  def documentation(self):
249  return self.__doc__
250 
251  def hasDoc(self):
252  return len(self.__doc__) > 0 and self.__doc__ != 'none'
253 
254 
255 # ----iProperty class----------------------------------------------------------
256 
257 
258 class iProperty(object):
259  """ Python equivalent to the C++ Property interface """
260 
261  def __init__(self, name, ip=None):
262  if ip:
263  self.__dict__['_ip'] = InterfaceCast(gbl.IProperty)(ip)
264  else:
265  self.__dict__['_ip'] = None
266  self.__dict__['_svcloc'] = gbl.Gaudi.svcLocator()
267  optsvc = Helper.service(self._svcloc, 'JobOptionsSvc')
268  if optsvc:
269  self.__dict__['_optsvc'] = InterfaceCast(
270  gbl.IJobOptionsSvc)(optsvc)
271  else:
272  self.__dict__['_optsvc'] = None
273  self.__dict__['_name'] = name
274 
275  def getInterface(self):
276  if not self._ip:
277  self.retrieveInterface()
278  return self._ip
279 
280  def retrieveInterface(self):
281  pass
282 
283  def __call_interface_method__(self, ifname, method, *args):
284  if not getattr(self, ifname):
285  self.retrieveInterface()
286  return getattr(getattr(self, ifname), method)(*args)
287 
288  def __setattr__(self, name, value):
289  """
290  The method which is used for setting the property from the given value.
291  - In the case of the valid instance it sets the property through IProperty interface
292  - In the case of placeholder the property is added to JobOptionsCatalogue
293  """
294  if hasattr(value, 'toStringProperty'):
295  # user defined behaviour
296  value = '%s' % value.toStringProperty()
297  ip = self.getInterface()
298  if ip:
299  if not gbl.Gaudi.Utils.hasProperty(ip, name):
300  raise AttributeError('property %s does not exist' % name)
301  prop = ip.getProperty(name)
302 
303  if ROOT6WorkAroundEnabled('ROOT-7201'):
304  canSetValue = (hasattr(prop, 'value')
305  and 'const&[' not in prop.value.func_doc
306  and type(value) == type(prop.value()))
307  else:
308  canSetValue = (hasattr(prop, 'value')
309  and type(value) == type(prop.value()))
310 
311  if canSetValue:
312  if not prop.setValue(value):
313  raise AttributeError(
314  'property %s could not be set from %s' % (name, value))
315  else:
316  if tuple == type(value):
317  value = str(value)
318  elif hasattr(value, 'toString'):
319  value = value.toString()
320  elif not long == type(value):
321  value = '%s' % value
322  else:
323  value = '%d' % value
324  if ROOT6WorkAroundEnabled('ROOT-6028'):
325  sc = cppyy.gbl.GaudiPython.Helper.setPropertyFromString(
326  prop, value)
327  else:
328  sc = prop.fromString(value)
329  if sc.isFailure():
330  raise AttributeError(
331  'property %s could not be set from %s' % (name, value))
332  else:
333  if type(value) == str:
334  value = '"%s"' % value # need double quotes
335  elif type(value) == tuple:
336  value = str(value)
337  elif hasattr(value, 'toString'):
338  value = value.toString()
339  elif type(value) == long:
340  value = '%d' % value # prevent pending 'L'
341  sp = gbl.GaudiPython.Helpers.mkStringProperty(name, str(value))
342  self._optsvc.addPropertyToCatalogue(self._name, sp).ignore()
343 
344  def __getattr__(self, name):
345  """
346  The method which returns the value for the given property
347  - In the case of the valid instance it returns the valid property value through IProperty interface
348  - In the case of placeholder the property value is retrieved from JobOptionsCatalogue
349  """
350  ip = self.getInterface()
351  if ip:
352  if not gbl.Gaudi.Utils.hasProperty(ip, name):
353  raise AttributeError('property %s does not exist' % name)
354  prop = ip.getProperty(name)
355  if StringProperty == type(prop):
356  return prop.value()
357  elif StringPropertyRef == type(prop):
358  return prop.value()
359  try:
360  return eval(prop.toString(), {}, {})
361  except:
362  return prop.value()
363  else:
364  props = self._optsvc.getProperties(self._name)
365  for p in props:
366  if not p.name() == name:
367  continue
368  # from JobOptionsSvc we always have only strings
369  try:
370  return eval(p.value(), {}, {})
371  except:
372  return p.value()
373  raise AttributeError('property %s does not exist' % name)
374 
375  def properties(self):
376  dct = {}
377  props = None
378  ip = self.getInterface()
379  if ip:
380  props = ip.getProperties()
381  propsFrom = self._name # "interface"
382  else:
383  props = self._optsvc.getProperties(self._name)
384  propsFrom = "jobOptionsSvc"
385  if props:
386  for p in props:
387  try:
388  dct[p.name()] = PropertyEntry(p)
389  except (ValueError, TypeError) as e:
390  raise ValueError("gaudimodule.iProperty.properties(): %s%s processing property %s.%s = %s" % \
391  (e.__class__.__name__, e.args,
392  propsFrom, p.name(), p.value()))
393  return dct
394 
395  def name(self):
396  return self._name
397 
398 
399 # ----iService class-----------------------------------------------------------
400 
401 
403  """ Python equivalent to IProperty interface """
404 
405  def __init__(self, name, isvc=None):
406  iProperty.__init__(self, name, isvc)
407  if isvc:
408  self.__dict__['_isvc'] = InterfaceCast(gbl.IService)(isvc)
409  else:
410  self.__dict__['_isvc'] = None
411 
412  def retrieveInterface(self):
413  isvc = Helper.service(self._svcloc, self._name)
414  if isvc:
415  iService.__init__(self, self._name, isvc)
416 
417  def initialize(self):
418  return self.__call_interface_method__("_isvc", "initialize")
419 
420  def start(self):
421  return self.__call_interface_method__("_isvc", "start")
422 
423  def stop(self):
424  return self.__call_interface_method__("_isvc", "stop")
425 
426  def finalize(self):
427  return self.__call_interface_method__("_isvc", "finalize")
428 
429  def reinitialize(self):
430  return self.__call_interface_method__("_isvc", "reinitialize")
431 
432  def restart(self):
433  return self.__call_interface_method__("_isvc", "restart")
434 
435  def isValid(self):
436  if self._isvc:
437  return True
438  else:
439  return False
440 
441 
442 # ----iAlgorithm class---------------------------------------------------------
443 
444 
446  """ Python equivalent to IAlgorithm interface """
447 
448  def __init__(self, name, ialg=None):
449  iProperty.__init__(self, name, ialg)
450  if ialg:
451  self.__dict__['_ialg'] = InterfaceCast(gbl.IAlgorithm)(ialg)
452  else:
453  self.__dict__['_ialg'] = None
454 
455  def retrieveInterface(self):
456  ialg = Helper.algorithm(
457  InterfaceCast(gbl.IAlgManager)(self._svcloc), self._name)
458  if ialg:
459  iAlgorithm.__init__(self, self._name, ialg)
460 
461  def initialize(self):
462  return self.__call_interface_method__("_ialg", "initialize")
463 
464  def start(self):
465  return self.__call_interface_method__("_ialg", "start")
466 
467  def execute(self):
468  return self.__call_interface_method__("_ialg", "execute")
469 
470  def stop(self):
471  return self.__call_interface_method__("_ialg", "stop")
472 
473  def finalize(self):
474  return self.__call_interface_method__("_ialg", "finalize")
475 
476  def reinitialize(self):
477  return self.__call_interface_method__("_ialg", "reinitialize")
478 
479  def restart(self):
480  return self.__call_interface_method__("_ialg", "restart")
481 
482  def sysInitialize(self):
483  return self.__call_interface_method__("_ialg", "sysInitialize")
484 
485  def sysStart(self):
486  return self.__call_interface_method__("_ialg", "sysStart")
487 
488  def sysExecute(self):
489  return self.__call_interface_method__("_ialg", "sysExecute")
490 
491  def sysStop(self):
492  return self.__call_interface_method__("_ialg", "sysStop")
493 
494  def sysFinalize(self):
495  return self.__call_interface_method__("_ialg", "sysFinalize")
496 
497  def sysReinitialize(self):
498  return self.__call_interface_method__("_ialg", "sysReinitialize")
499 
500  def sysRestart(self):
501  return self.__call_interface_method__("_ialg", "sysRestart")
502 
503 
504 # ----iAlgTool class-----------------------------------------------------------
505 
506 
508  """ Python equivalent to IAlgTool interface (not completed yet) """
509 
510  def __init__(self, name, itool=None):
511  iProperty.__init__(self, name, itool)
512  if itool:
513  self.__dict__['_itool'] = itool
514  else:
515  self.__dict__['_itool'] = None
516  svc = Helper.service(self._svcloc, 'ToolSvc', True)
517  self.__dict__['_toolsvc'] = iToolSvc('ToolSvc', svc)
518 
519  def retrieveInterface(self):
520  itool = self._toolsvc._retrieve(self._name)
521  if itool:
522  iAlgTool.__init__(self, self._name, itool)
523 
524  def start(self):
525  return self.__call_interface_method__("_itool", "start")
526 
527  def stop(self):
528  return self.__call_interface_method__("_itool", "stop")
529 
530  def type(self):
531  return self.__call_interface_method__("_itool", "type")
532 
533  def name(self):
534  if self._itool:
535  return self._itool.name()
536  else:
537  return self._name
538 
539 
540 # ----iDataSvc class-----------------------------------------------------------
541 
542 
544  def __init__(self, name, idp):
545  iService.__init__(self, name, idp)
546  self.__dict__['_idp'] = InterfaceCast(gbl.IDataProviderSvc)(idp)
547  self.__dict__['_idm'] = InterfaceCast(gbl.IDataManagerSvc)(idp)
548 
549  def registerObject(self, path, obj):
550  if not self._idp:
551  raise AttributeError(
552  'C++ service %s does not exist' % self.__dict__['_name'])
553  return Helper.registerObject(self._idp, path, obj)
554 
555  def unregisterObject(self, path):
556  if not self._idp:
557  raise AttributeError(
558  'C++ service %s does not exist' % self.__dict__['_name'])
559  return Helper.unregisterObject(self._idp, path)
560 
561  def retrieveObject(self, path):
562  if not self._idp:
563  return None
564  return Helper.dataobject(self._idp, path)
565 
566  # get object from TES
567 
568  def findObject(self, path):
569  """
570 
571  Get the existing object in TransientStore for the given location
572 
573  - loading of object from persistency is NOT triggered
574  - 'data-on-demand' action is NOT triggered
575 
576  >>> svc = ... ## get the service
577  >>> path = ... ## get the path in Transient Store
578  >>> data = svc.findObject ( path ) ## use the method
579 
580  """
581  if not self._idp:
582  raise IndexError(
583  'C++ service %s does not exist' % self.__dict__['_name'])
584  return Helper.findobject(self._idp, path)
585 
586  # get or retrieve object, possible switch-off 'on-demand' actions
587  def getObject(self, path, *args):
588  """
589  Get object from Transient Store for the given location
590 
591  arguments :
592  - path : Location of object in Transient Store
593  - retrieve (bool) True : retrieve versus find
594  - disable on-demand (bool) False : temporary disable 'on-demand' actions
595 
596  >>> svc = ... ## get the service
597  >>> path = ... ## get the path
598 
599  >>> data = svc.getObject ( path , False ) ## find object in Transient Store
600 
601  ## find object in Transient Store
602  # load form tape or use 'on-demand' action for missing objects :
603  >>> data = svc.getObject ( path , True )
604 
605  ## find object in Transient Store
606  # load from tape or for missing objects, disable 'on-demand'-actions
607  >>> data = svc.getObject ( path , True , True )
608 
609  """
610  if not self._idp:
611  raise IndexError(
612  'C++ service %s does not exist' % self.__dict__['_name'])
613  return Helper.getobject(self._idp, path, *args)
614 
615  def __getitem__(self, path):
616  if not self._idp:
617  raise IndexError(
618  'C++ service %s does not exist' % self.__dict__['_name'])
619  return Helper.dataobject(self._idp, path)
620 
621  def __setitem__(self, path, obj):
622  if not self._idp:
623  raise IndexError(
624  'C++ service %s does not exist' % self.__dict__['_name'])
625  return self.registerObject(path, obj)
626 
627  def __delitem__(self, path):
628  if not self._idp:
629  raise IndexError(
630  'C++ service %s does not exist' % self.__dict__['_name'])
631  return self.unregisterObject(path)
632 
633  def leaves(self, node=None):
634  if not node:
635  node = self.retrieveObject('')
636  ll = gbl.std.vector('IRegistry*')()
637  if type(node) is str:
638  obj = self.retrieveObject(node)
639  else:
640  obj = node
641  if self._idm.objectLeaves(node, ll).isSuccess():
642  return ll
643 
644  def dump(self, node=None):
645  if not node:
646  root = self.retrieveObject('')
647  if root:
648  node = root.registry()
649  else:
650  return
651  print(node.identifier())
652  if node.object():
653  for l in self.leaves(node):
654  self.dump(l)
655 
656  def getList(self, node=None, lst=[], rootFID=None):
657  if not node:
658  root = self.retrieveObject('')
659  if root:
660  node = root.registry()
661  rootFID = node.address().par()
662  lst = []
663  else:
664  return
665  Helper.dataobject(self._idp, node.identifier())
666  if node.object():
667  lst.append(node.identifier())
668  for l in self.leaves(node):
669  if l.address() and l.address().par() == rootFID:
670  self.getList(l, lst, rootFID)
671  else:
672  continue
673  return lst
674 
675  def getHistoNames(self, node=None, lst=[]):
676  if not node:
677  root = self.retrieveObject('')
678  if root:
679  node = root.registry()
680  # rootFID = node.address().par()
681  lst = []
682  else:
683  return
684  Helper.dataobject(self._idp, node.identifier())
685  if node.object():
686  lst.append(node.identifier())
687  for l in self.leaves(node):
688  if l.name(): # and l.address().par() == rootFID :
689  self.getHistoNames(l, lst)
690  else:
691  continue
692  return lst
693 
694  def setRoot(self, name, obj):
695  if not self._idm:
696  raise IndexError(
697  'C++ service %s does not exist' % self.__dict__['_name'])
698  return self._idm.setRoot(name, obj)
699 
700  def clearStore(self):
701  if not self._idm:
702  raise IndexError(
703  'C++ service %s does not exist' % self.__dict__['_name'])
704  return self._idm.clearStore()
705 
706 
707 # ----iHistogramSvc class------------------------------------------------------
709  def __init__(self, name, ihs):
710  self.__dict__['_ihs'] = InterfaceCast(gbl.IHistogramSvc)(ihs)
711  iDataSvc.__init__(self, name, ihs)
712 
713  def retrieve1D(self, path):
714  return Helper.histo1D(self._ihs, path)
715 
716  def retrieve2D(self, path):
717  return Helper.histo2D(self._ihs, path)
718 
719  def retrieve3D(self, path):
720  return Helper.histo3D(self._ihs, path)
721 
722  def retrieveProfile1D(self, path):
723  return Helper.profile1D(self._ihs, path)
724 
725  def retrieveProfile2D(self, path):
726  return Helper.profile2D(self._ihs, path)
727 
728  def retrieve(self, path):
729  """
730  Retrieve AIDA histogram or AIDA profile histogram by path in Histogram Transient Store
731  >>> svc = ...
732  >>> histo = svc.retrieve ( 'path/to/my/histogram' )
733  """
734  h = self.retrieve1D(path)
735  if not h:
736  h = self.retrieve2D(path)
737  if not h:
738  h = self.retrieve3D(path)
739  if not h:
740  h = self.retrieveProfile1D(path)
741  if not h:
742  h = self.retrieveProfile2D(path)
743  return h
744 
745  def book(self, *args):
746  """
747  Book the histograms(1D,2D&3D) , see IHistogramSvc::book
748  >>> svc = ...
749  >>> histo = svc.book( .... )
750  """
751  return self._ihs.book(*args)
752 
753  def bookProf(self, *args):
754  """
755  Book the profile(1D&2D) histograms, see IHistogramSvc::bookProf
756  >>> svc = ...
757  >>> histo = svc.bookProf( .... )
758  """
759  return self._ihs.bookProf(*args)
760 
761  def __getitem__(self, path):
762  """
763  Retrieve the object from Histogram Transient Store (by path)
764  The reference to AIDA histogram is returned (if possible)
765  >>> svc = ...
766  >>> histo = svc['path/to/my/histogram']
767  """
768  h = self.retrieve(path)
769  if h:
770  return h
771  return iDataSvc.__getitem__(self, path)
772 
773  def getAsAIDA(self, path):
774  """
775  Retrieve the histogram from Histogram Transient Store (by path)
776  The reference to AIDA histogram is returned (if possible)
777  >>> svc = ...
778  >>> histo = svc.getAsAIDA ( 'path/to/my/histogram' )
779  """
780  return self.__getitem__(path)
781 
782  def getAsROOT(self, path):
783  """
784  Retrieve the histogram from Histogram Transient Store (by path)
785  The Underlying native ROOT object is returned (if possible)
786  >>> svc = ...
787  >>> histo = svc.getAsROOT ( 'path/to/my/histogram' )
788  """
789  fun = gbl.Gaudi.Utils.Aida2ROOT.aida2root
790  return fun(self.getAsAIDA(path))
791 
792 
793 # ----iNTupleSvc class---------------------------------------------------------
794 
795 
797  RowWiseTuple = 42
798  ColumnWiseTuple = 43
799 
800  def __init__(self, name, ints):
801  self.__dict__['_ints'] = InterfaceCast(gbl.INTupleSvc)(ints)
802  iDataSvc.__init__(self, name, ints)
803 
804  def book(self, *args):
805  return self._ints.book(*args)
806 
807  def defineOutput(self, files, typ="Gaudi::RootCnvSvc"):
808  """ Defines the mapping between logical names and the output file
809  Usage:
810  defineOutput({'LUN1':'MyFile1.root', 'LUN2':'Myfile2.root'}, svc='Gaudi::RootCnvSvc')
811  """
812  from . import Persistency as prs
813  helper = prs.get(typ)
814  helper.configure(AppMgr())
815  self.Output = [
816  helper.formatOutput(files[lun], lun=lun) for lun in files
817  ]
818  if AppMgr().HistogramPersistency == 'NONE':
819  AppMgr().HistogramPersistency = "ROOT"
820 
821  def __getitem__(self, path):
822  return iDataSvc.__getitem__(self, path)
823 
824 
825 # ----iToolSvc class-----------------------------------------------------------
827  def __init__(self, name, its):
828  self.__dict__['_its'] = InterfaceCast(gbl.IToolSvc)(its)
829  iService.__init__(self, name, its)
830 
831  def _retrieve(self, name, quiet=True):
832  sol = _gaudi.OutputLevel
833  if quiet:
834  self.OutputLevel = 6
835  if name.rfind('.') == -1:
836  itool = Helper.tool(self._its, '', name, nullptr, False)
837  elif name[0:8] == 'ToolSvc.':
838  itool = Helper.tool(self._its, '', name[8:], nullptr, False)
839  elif name.count('.') > 1:
840  ptool = self._retrieve(name[:name.rfind('.')])
841  itool = Helper.tool(self._its, '', name[name.rfind('.') + 1:],
842  ptool, False)
843  elif _gaudi:
844  prop = _gaudi.property(name[:name.rfind('.')])
845  itool = Helper.tool(self._its, '', name[name.rfind('.') + 1:],
846  prop._ip, False)
847  if quiet:
848  self.OutputLevel = sol
849  return itool
850 
851  def retrieve(self, name):
852  return iAlgTool(name, self._retrieve(name, quiet=False))
853 
854  def create(self, typ, name=None, parent=nullptr, interface=None):
855  if not name:
856  name = typ
857  itool = Helper.tool(self._its, typ, name, parent, True)
858  if interface:
859  return InterfaceCast(interface)(itool)
860  else:
861  return iAlgTool(name, itool)
862 
863  def release(self, itool):
864  if type(itool) is iAlgTool:
865  self._its.releaseTool(itool._itool)
866 
867 
868 # ----iJopOptSvc class---------------------------------------------------------
869 
870 
872  """
873  Python-image of C++ class IJobOptionsSvc
874  """
875 
876  # constructor
877 
878  def __init__(self, name, svc):
879  """ constructor """
880  self.__dict__['_optsvc'] = InterfaceCast(gbl.IJobOptionsSvc)(svc)
881  return iService.__init__(self, name, svc)
882 
883  def getProperties(self, component):
884  """
885  Extract *ALL* properties of the given component
886  Usage :
887  >>> jos = gaudi.optSvc()
888  >>> props = jos.getProperties( 'Name' )
889  """
890  props = self._optsvc.getProperties(component)
891  prps = {}
892  if not props:
893  return prps # RETURN
894  for p in props:
895  prop = p.name().upper()
896  try:
897  value = eval(p.value(), {}, {})
898  except:
899  value = p.value()
900  prps[prop] = value
901 
902  return prps # RETURN
903 
904  def getProperty(self, component, name):
905  """
906  Get a certain property of the certain component
907  Usage:
908  >>> jos = ...
909  >>> extServices = jos.getProperty( 'ApplicationMgr', 'ExtSvc' )
910  """
911  # get all properties of the component
912  all = self.getProperties(component)
913  return all.get(name.upper(), None) # RETURN
914 
915 
916 # ----iEventSelector class-----------------------------------------------------
917 
918 
920  def __init__(self):
921  iService.__init__(
922  self, 'EventSelector',
923  Helper.service(gbl.Gaudi.svcLocator(), 'EventSelector'))
924  self.__dict__['g'] = AppMgr()
925 
926  def open(self, stream, typ='Gaudi::RootCnvSvc', **kwargs):
927  from . import Persistency as prs
928  helper = prs.get(typ)
929  helper.configure(self.g)
930  self.Input = helper.formatInput(stream, **kwargs)
931  self.reinitialize()
932 
933  def rewind(self):
934  # It is not possible to reinitialize EventSelector only
935  self.g.service('EventLoopMgr').reinitialize()
936 
937 
938 # ----AppMgr class-------------------------------------------------------------
939 
940 
942  def __new__(cls, *args, **kwargs):
943  global _gaudi
944  if not _gaudi:
945  newobj = object.__new__(cls)
946  cls.__init__(newobj, *args, **kwargs)
947  _gaudi = newobj
948  return _gaudi
949 
950  def __reset__(self):
951  global _gaudi
952  # Stop, Finalize and Terminate the current AppMgr
953  self.exit()
954  # release interfaces
955  self._evtpro.release()
956  self._svcloc.release()
957  self._appmgr.release()
958  # Reset the C++ globals
959  gbl.Gaudi.setInstance(makeNullPointer('ISvcLocator'))
960  gbl.Gaudi.setInstance(makeNullPointer('IAppMgrUI'))
961  # Reset the Python global
962  _gaudi = None
963 
964  def __init__(self,
965  outputlevel=-1,
966  joboptions=None,
967  selfoptions={},
968  dllname=None,
969  factname=None):
970  global _gaudi
971  if _gaudi:
972  return
973  # Make sure the python stdout buffer is flushed before c++ runs
974  sys.stdout.flush()
975  # Protection against multiple calls to exit() if the finalization fails
976  self.__dict__['_exit_called'] = False
977  # keep the Gaudi namespace around (so it is still available during atexit shutdown)...
978  self.__dict__['_gaudi_ns'] = Gaudi
979  try:
980  from GaudiKernel.Proxy.Configurable import expandvars
981  except ImportError:
982  # pass-through implementation if expandvars is not defined (AthenaCommon)
983  def expandvars(data):
984  return data
985 
986  if dllname and factname:
987  self.__dict__['_appmgr'] = gbl.Gaudi.createApplicationMgr(
988  dllname, factname)
989  elif dllname:
990  self.__dict__['_appmgr'] = gbl.Gaudi.createApplicationMgr(dllname)
991  else:
992  self.__dict__['_appmgr'] = gbl.Gaudi.createApplicationMgr()
993  self.__dict__['_svcloc'] = gbl.Gaudi.svcLocator()
994  self.__dict__['_algmgr'] = InterfaceCast(gbl.IAlgManager)(self._appmgr)
995  self.__dict__['_evtpro'] = InterfaceCast(gbl.IEventProcessor)(
996  self._appmgr)
997  self.__dict__['_svcmgr'] = InterfaceCast(gbl.ISvcManager)(self._appmgr)
998  self.__dict__['pyalgorithms'] = []
999  iService.__init__(self, 'ApplicationMgr', self._appmgr)
1000  # ------python specific initialization-------------------------------------
1001  if self.FSMState(
1002  ) < Gaudi.StateMachine.CONFIGURED: # Not yet configured
1003  self.JobOptionsType = 'NONE'
1004  if joboptions:
1005  from GaudiKernel.ProcessJobOptions import importOptions
1006  importOptions(joboptions)
1007  # Ensure that the ConfigurableUser instances have been applied
1008  import GaudiKernel.Proxy.Configurable
1009  if hasattr(GaudiKernel.Proxy.Configurable,
1010  "applyConfigurableUsers"):
1011  GaudiKernel.Proxy.Configurable.applyConfigurableUsers()
1012  # This is the default and could be overridden with "selfopts"
1013  self.OutputLevel = 3
1014  try:
1015  appMgr = Configurable.allConfigurables['ApplicationMgr']
1016  selfprops = expandvars(appMgr.getValuedProperties())
1017  except KeyError:
1018  selfprops = {}
1019  for p, v in selfprops.items():
1020  setattr(self, p, v)
1021  for p, v in selfoptions.items():
1022  setattr(self, p, v)
1023  # Override job options
1024  if outputlevel != -1:
1025  self.OutputLevel = outputlevel
1026  self.configure()
1027  # ---MessageSvc------------------------------------------------------------
1028  ms = self.service('MessageSvc')
1029  if 'MessageSvc' in Configurable.allConfigurables:
1030  msprops = Configurable.allConfigurables['MessageSvc']
1031  ms = self.service('MessageSvc')
1032  if hasattr(msprops, "getValuedProperties"):
1033  msprops = expandvars(msprops.getValuedProperties())
1034  for p, v in msprops.items():
1035  setattr(ms, p, v)
1036  if outputlevel != -1:
1037  ms.OutputLevel = outputlevel
1038  # ---JobOptions------------------------------------------------------------
1039  self.__dict__['_optsvc'] = InterfaceCast(gbl.IJobOptionsSvc)(
1040  Helper.service(self._svcloc, 'JobOptionsSvc'))
1041  # ------Configurables initialization (part2)-------------------------------
1042  mkStringProperty = gbl.GaudiPython.Helpers.mkStringProperty
1043  for n in getNeededConfigurables():
1044  c = Configurable.allConfigurables[n]
1045  if n in ['ApplicationMgr', 'MessageSvc']:
1046  continue # These are already done---
1047  for p, v in c.getValuedProperties().items():
1048  v = expandvars(v)
1049  # Note: AthenaCommon.Configurable does not have Configurable.PropertyReference
1050  if hasattr(Configurable, "PropertyReference") and type(
1051  v) == Configurable.PropertyReference:
1052  # this is done in "getFullName", but the exception is ignored,
1053  # so we do it again to get it
1054  v = v.__resolve__()
1055  if type(v) == str:
1056  v = '"%s"' % v # need double quotes
1057  elif type(v) == long:
1058  v = '%d' % v # prevent pending 'L'
1059  self._optsvc.addPropertyToCatalogue(
1060  n, mkStringProperty(p, str(v)))
1061  if hasattr(Configurable, "_configurationLocked"):
1062  Configurable._configurationLocked = True
1063 
1064  self._install_exit_handlers()
1065 
1067  """Ensure that the exit method is called when exiting from Python, and
1068  try to ensure that ROOT doesn't intefere too much."""
1069  import atexit
1070  atexit.register(self.exit)
1071 
1072  try:
1073  exit_handlers = atexit._exithandlers
1074  except AttributeError:
1075  # Python 3's atext does not expose _exithandlers, so we can't do
1076  # anything more
1077  return
1078 
1079  # ---Hack to avoid bad interactions with the ROOT exit handler
1080  # Look for an exit handler installed by ROOT
1081  root_handler_installed = False
1082  for h in exit_handlers:
1083  func = h[0]
1084  if hasattr(func, "__module__") and func.__module__ == "ROOT":
1085  root_handler_installed = True
1086  break
1087 
1088  # If the handler is not yet installed, let's install our private version
1089  # that detects that the ROOT exit handler is installed and add our own
1090  # after it to ensure it is called before.
1091  if not root_handler_installed:
1092  orig_register = atexit.register
1093 
1094  def register(func, *targs, **kargs):
1095  orig_register(func, *targs, **kargs)
1096  if hasattr(func, "__module__") and func.__module__ == "ROOT":
1097  orig_register(self.exit)
1098  # we do not need to remove out handler from the list because
1099  # it can be safely called more than once
1100 
1101  register.__doc__ = (
1102  orig_register.__doc__ +
1103  "\nNote: version hacked by GaudiPython to work " +
1104  "around a problem with the ROOT exit handler")
1105  atexit.register = register
1106 
1107  def state(self):
1108  return self._isvc.FSMState()
1109 
1110  def FSMState(self):
1111  return self._isvc.FSMState()
1112 
1113  def targetFSMState(self):
1114  return self._isvc.targetFSMState()
1115 
1116  def service(self, name, interface=None):
1117  svc = Helper.service(self._svcloc, name)
1118  if interface:
1119  return InterfaceCast(interface)(svc)
1120  else:
1121  return iService(name, svc)
1122 
1123  def declSvcType(self, svcname, svctype):
1124  self._svcmgr.declareSvcType(svcname, svctype)
1125 
1126  def createSvc(self, name):
1127  return Helper.service(self._svcloc, name, True)
1128 
1129  def services(self):
1130  l = self._svcloc.getServices()
1131  return [s.name() for s in l]
1132 
1133  def algorithm(self, name, createIf=False):
1134  alg = Helper.algorithm(self._algmgr, name, createIf)
1135  if not alg:
1136  return iAlgorithm(name, alg)
1137  else:
1138  return iAlgorithm(alg.name(), alg)
1139 
1140  def algorithms(self):
1141  l = self._algmgr.getAlgorithms()
1142  return [a.name() for a in l]
1143 
1144  def tool(self, name):
1145  return iAlgTool(name)
1146 
1147  def property(self, name):
1148  if name in self.algorithms():
1149  return self.algorithm(name)
1150  elif name in self.services():
1151  return self.service(name)
1152  else:
1153  return iProperty(name)
1154 
1155  def datasvc(self, name):
1156  if self.state() == Gaudi.StateMachine.CONFIGURED:
1157  self.initialize()
1158  svc = Helper.service(self._svcloc, name)
1159  return iDataSvc(name, svc)
1160 
1161  def evtsvc(self):
1162  return self.datasvc('EventDataSvc')
1163 
1164  def detsvc(self):
1165  return self.datasvc('DetectorDataSvc')
1166 
1167  def filerecordsvc(self):
1168  return self.datasvc('FileRecordDataSvc')
1169 
1170  def evtsel(self):
1171  if self.state() == Gaudi.StateMachine.CONFIGURED:
1172  self.initialize()
1173  if not hasattr(self, '_evtsel'):
1174  self.__dict__['_evtsel'] = iEventSelector()
1175  return self._evtsel
1176 
1177  def histsvc(self, name='HistogramDataSvc'):
1178  svc = Helper.service(self._svcloc, name)
1179  return iHistogramSvc(name, svc)
1180 
1181  def ntuplesvc(self, name='NTupleSvc'):
1182  if name not in self.ExtSvc:
1183  self.ExtSvc += [name]
1184 # if self.HistogramPersistency == 'NONE' : self.HistogramPersistency = 'ROOT'
1185  svc = Helper.service(self._svcloc, name, True)
1186  return iNTupleSvc(name, svc)
1187 
1188  def partsvc(self):
1189  if self.FSMState() == Gaudi.StateMachine.CONFIGURED:
1190  self.initialize()
1191  svc = Helper.service(self._svcloc, 'ParticlePropertySvc')
1192  return InterfaceCast(gbl.IParticlePropertySvc)(svc)
1193 
1194  def toolsvc(self, name='ToolSvc'):
1195  svc = Helper.service(self._svcloc, name, True)
1196  return iToolSvc(name, svc)
1197 
1198  def optSvc(self, name='JobOptionsSvc'):
1199  svc = Helper.service(self._svcloc, name, True)
1200  return iJobOptSvc(name, svc)
1201 
1202  def readOptions(self, file):
1203  return self._optsvc.readOptions(file)
1204 
1205  def addAlgorithm(self, alg):
1206  """ Add an Algorithm to the list of Top algorithms. It can be either a instance of
1207  an Algorithm class or it name """
1208  if type(alg) is str:
1209  self.topAlg += [alg]
1210  else:
1211  self.pyalgorithms.append(alg)
1212  setOwnership(alg, 0)
1213  if self.targetFSMState() >= Gaudi.StateMachine.INITIALIZED:
1214  alg.sysInitialize()
1215  if self.targetFSMState() == Gaudi.StateMachine.RUNNING:
1216  alg.sysStart()
1217  self.topAlg += [alg.name()]
1218 
1219  def setAlgorithms(self, algs):
1220  """ Set the list of Top Algorithms.
1221  It can be an individual of a list of algorithms names or instances """
1222  if type(algs) is not list:
1223  algs = [algs]
1224  names = []
1225  for alg in algs:
1226  if type(alg) is str:
1227  names.append(alg)
1228  else:
1229  self.pyalgorithms.append(alg)
1230  if self.targetFSMState() >= Gaudi.StateMachine.INITIALIZED:
1231  alg.sysInitialize()
1232  if self.targetFSMState() == Gaudi.StateMachine.RUNNING:
1233  alg.sysStart()
1234  names.append(alg.name())
1235  self.topAlg = names
1236 
1237  def removeAlgorithm(self, alg):
1238  """ Remove an Algorithm to the list of Top algorithms. It can be either a instance of
1239  an Algorithm class or it name """
1240  tmp = self.topAlg
1241  if type(alg) is str:
1242  tmp.remove(alg)
1243  else:
1244  tmp.remove(alg.name())
1245  self.pyalgorithms.remove(alg)
1246  setOwnership(alg, 1)
1247  self.topAlg = tmp
1248 
1250  """
1251  Print the sequence of Algorithms.
1252  """
1253 
1254  def printAlgo(algName, appMgr, prefix=' '):
1255  print(prefix + algName)
1256  alg = appMgr.algorithm(algName.split("/")[-1])
1257  prop = alg.properties()
1258  if "Members" in prop:
1259  subs = prop["Members"].value()
1260  for i in subs:
1261  printAlgo(i.strip('"'), appMgr, prefix + " ")
1262 
1263  mp = self.properties()
1264  prefix = 'ApplicationMgr SUCCESS '
1265  print(
1266  prefix +
1267  "****************************** Algorithm Sequence ****************************"
1268  )
1269  for i in mp["TopAlg"].value():
1270  printAlgo(i, self, prefix)
1271  print(
1272  prefix +
1273  "******************************************************************************"
1274  )
1275 
1276  def config(self, **args):
1277  """
1278  Simple utility to perform the configuration of Gaudi application.
1279  It reads the set of input job-options files, and set few
1280  additional parameters 'options' through the usage of temporary *.opts file
1281  Usage:
1282  gaudi.config( files = [ '$GAUSSOPTS/Gauss.opts' ,
1283  '$DECFILESROOT/options/10022_010.0GeV.opts' ] ,
1284  options = [ 'EventSelector.PrintFreq = 5 ' ] )
1285  """
1286  files = args.get('files', [])
1287  for file in files:
1288  sc = self.readOptions(file)
1289  if sc.isFailure():
1290  raise RuntimeError(' Unable to read file "' + file + '" ')
1291  options = args.get('options', None)
1292  if options:
1293  import tempfile
1294  tmpfilename = tempfile.mktemp()
1295  tmpfile = open(tmpfilename, 'w')
1296  tmpfile.write('#pragma print on \n')
1297  tmpfile.write('/// File "' + tmpfilename +
1298  '" generated by GaudiPython \n\n')
1299  for opt in options:
1300  if type(options) is dict:
1301  tmpfile.write(' \t ' + opt + ' = ' + options[opt] +
1302  ' ; // added by GaudiPython \n')
1303  else:
1304  tmpfile.write(' \t ' + opt +
1305  ' ; // added by GaudiPython \n')
1306  tmpfile.write('/// End of file "' + tmpfilename +
1307  '" generated by GaudiPython \n\n')
1308  tmpfile.close()
1309  sc = self.readOptions(tmpfilename)
1310  if sc.isFailure():
1311  raise RuntimeError(' Unable to read file "' + tmpfilename +
1312  '" ')
1313  os.remove(tmpfilename)
1314  # We need to make sure that the options are taken by the ApplicationMgr
1315  # The state is already configured, so we need to do something....
1316  if self.FSMState() != Gaudi.StateMachine.OFFLINE:
1317 
1318  # get job-options-service, @see class iJobOptSvc
1319  jos = self.optSvc()
1320 
1321  # list of all libraries
1322  _dlls = jos.getProperty(self.name(), 'DLLs')
1323  # take care about libraries : APPEND if not done yet
1324  if _dlls:
1325  libs = [l for l in _dlls if not l in self.DLLs]
1326  if libs:
1327  self.DLLs += libs
1328 
1329  # all external services
1330  _svcs = jos.getProperty(self.name(), 'ExtSvc')
1331  # take care about services : APPEND if not done yet
1332  if _svcs:
1333  svcs = [s for s in _svcs if not s in self.ExtSvc]
1334  if svcs:
1335  self.ExtSvc += svcs
1336 
1337  # get all properties
1338  props = jos.getProperties(self.name())
1339  # finally treat all other properties (presumably scalar properties)
1340  for key in props:
1341  if 'DLLS' == key or 'EXTSVC' == key:
1342  continue
1343  self.__setattr__(key, props[key])
1344  return SUCCESS # RETURN
1345 
1346  def configure(self):
1347  return self._appmgr.configure()
1348 
1349  def start(self):
1350  return self._appmgr.start()
1351 
1352  def terminate(self):
1353  return self._appmgr.terminate()
1354 
1355  def run(self, n):
1356  if self.FSMState() == Gaudi.StateMachine.CONFIGURED:
1357  sc = self.initialize()
1358  if sc.isFailure() or self.ReturnCode != 0:
1359  return sc
1360  if self.FSMState() == Gaudi.StateMachine.INITIALIZED:
1361  sc = self.start()
1362  if sc.isFailure() or self.ReturnCode != 0:
1363  return sc
1364  return self._evtpro.executeRun(n)
1365 
1366  def executeEvent(self):
1367  return self._evtpro.executeEvent()
1368 
1369  def execute(self):
1370  return self._evtpro.executeEvent()
1371 
1372  def runSelectedEvents(self, pfn, events):
1373  if self.FSMState() == Gaudi.StateMachine.CONFIGURED:
1374  sc = self.initialize()
1375  if sc.isFailure():
1376  return sc
1377  if self.FSMState() == Gaudi.StateMachine.INITIALIZED:
1378  sc = self.start()
1379  if sc.isFailure():
1380  return sc
1381  # --- Access a number of services ----
1382  if not hasattr(self, '_perssvc'):
1383  self.__dict__['_perssvc'] = self.service('EventPersistencySvc',
1384  'IAddressCreator')
1385  if not hasattr(self, '_filecat'):
1386  self.__dict__['_filecat'] = self.service('FileCatalog',
1387  'Gaudi::IFileCatalog')
1388  if not hasattr(self, '_evtmgr'):
1389  self.__dict__['_evtmgr'] = self.service('EventDataSvc',
1390  'IDataManagerSvc')
1391  # --- Get FID from PFN and number of events in file
1392  if pfn.find('PFN:') == 0:
1393  pfn = pfn[4:]
1394  fid, maxevt = _getFIDandEvents(pfn)
1395  # --- Add FID into catalog if needed ---
1396  if not self._filecat.existsFID(fid):
1397  self._filecat.registerPFN(fid, pfn, '')
1398  # --- Loop over events
1399  if type(events) is not list:
1400  events = (events, )
1401  for evt in events:
1402  # --- Create POOL Address from Generic Address
1403  gadd = gbl.GenericAddress(0x02, 1, fid, '/Event', 0, evt)
1404  oadd = makeNullPointer('IOpaqueAddress')
1405  self._perssvc.createAddress(gadd.svcType(), gadd.clID(),
1406  gadd.par(), gadd.ipar(), oadd)
1407  # --- Clear TES, set root and run all algorithms
1408  self._evtmgr.clearStore()
1409  self._evtmgr.setRoot('/Event', oadd)
1410  self._evtpro.executeEvent()
1411 
1412  def exit(self):
1413  # Protection against multiple calls to exit() if the finalization fails
1414  if not self._exit_called:
1415  self.__dict__['_exit_called'] = True
1416  Gaudi = self._gaudi_ns
1417  if self.FSMState() == Gaudi.StateMachine.RUNNING:
1418  self._appmgr.stop().ignore()
1419  if self.FSMState() == Gaudi.StateMachine.INITIALIZED:
1420  self._appmgr.finalize().ignore()
1421  if self.FSMState() == Gaudi.StateMachine.CONFIGURED:
1422  self._appmgr.terminate()
1423  return SUCCESS
1424 
1425  # Custom destructor to ensure that the application is correctly finalized when exiting from python.
1426 
1427  def __del__(self):
1428  self.exit()
1429 
1430  evtSvc = evtsvc
1431  histSvc = histsvc
1432  ntupleSvc = ntuplesvc
1433  evtSel = evtsel
1434  detSvc = detsvc
1435  toolSvc = toolsvc
1436  partSvc = partsvc
1437 
1438 
1439 # -----------------------------------------------------------------------------
1440 
1441 
1443  tfile = gbl.TFile.Open(pfn)
1444  if not tfile:
1445  raise IOError('Cannot open ROOT file {0}'.format(pfn))
1446  tree = tfile.Get('##Params')
1447  tree.GetEvent(0)
1448  text = tree.db_string
1449  if 'NAME=FID' in text:
1450  fid = text[text.rfind('VALUE=') + 6:-1]
1451  nevt = tfile.Get('_Event').GetEntries()
1452  tfile.Close()
1453  return fid, nevt
1454 
1455 
1456 # -----------------------------------------------------------------------------
1457 
1458 
1460  """ Get all the properties of a component as a Python dictionary.
1461  The component is instantiated using the component library
1462  """
1463  properties = {}
1464  if name == 'GaudiCoreSvc':
1465  if Helper.loadDynamicLib(name) != 1:
1466  raise ImportError('Error loading component library ' + name)
1467  factorylist = gbl.FactoryTable.instance().getEntries()
1468  factories = _copyFactoriesFromList(factorylist)
1469  g = AppMgr(outputlevel=7)
1470  else:
1471  g = AppMgr(outputlevel=7)
1472  if Helper.loadDynamicLib(name) != 1:
1473  raise ImportError('Error loading component library ' + name)
1474  factorylist = gbl.FactoryTable.instance().getEntries()
1475  factories = _copyFactoriesFromList(factorylist)
1476  svcloc = gbl.Gaudi.svcLocator()
1477  dummysvc = gbl.Service('DummySvc', svcloc)
1478  for factory in factories:
1479  if InterfaceCast(gbl.IAlgFactory)(factory):
1480  ctype = 'Algorithm'
1481  elif InterfaceCast(gbl.ISvcFactory)(factory):
1482  ctype = 'Service'
1483  elif InterfaceCast(gbl.IToolFactory)(factory):
1484  ctype = 'AlgTool'
1485  elif factory.ident() == 'ApplicationMgr':
1486  ctype = 'ApplicationMgr'
1487  else:
1488  ctype = 'Unknown'
1489  cname = factory.ident().split()[-1]
1490  if ctype in ('Algorithm', 'Service', 'AlgTool', 'ApplicationMgr'):
1491  try:
1492  if ctype == 'AlgTool':
1493  obj = factory.instantiate(dummysvc)
1494  else:
1495  obj = factory.instantiate(svcloc)
1496  except RuntimeError as text:
1497  print('Error instantiating', cname, ' from ', name)
1498  print(text)
1499  continue
1500  prop = iProperty('dummy', obj)
1501  properties[cname] = [ctype, prop.properties()]
1502  try:
1503  obj.release()
1504  except:
1505  pass
1506  return properties
1507 
1508 
1509 def _copyFactoriesFromList(factories):
1510  result = []
1511  for i in range(factories.size()):
1512  factory = factories.front()
1513  result.append(factory)
1514  factories.pop_front()
1515  for factory in result:
1516  factories.push_back(factory)
1517  return result
1518 
1519 
1520 # ----CallbackStreamBuf--------------------------------------------------------
1521 # Used for redirecting C++ messages to python
1522 _CallbackStreamBufBase = gbl.GaudiPython.CallbackStreamBuf
1523 
1524 
1526  def __init__(self, callback):
1527  _CallbackStreamBufBase.__init__(self, self)
1528  self.callback = callback
1529 
1530  def _sync(self, string=None):
1531  if not string:
1532  return 0
1533  self.callback(string)
1534  return 0
1535 
1536 
1537 # ----PyAlgorithm--------------------------------------------------------------
1538 # Used to implement Algorithms in Python
1539 _PyAlgorithm = gbl.GaudiPython.PyAlgorithm
1540 
1541 
1543  def __init__(self, name=None):
1544  if not name:
1545  name = self.__class__.__name__
1546  _PyAlgorithm.__init__(self, self, name)
1547  self._svcloc = gbl.Gaudi.svcLocator()
1548  self._algmgr = InterfaceCast(gbl.IAlgManager)(self._svcloc)
1549  sc = self._algmgr.addAlgorithm(self)
1550  if sc.isFailure():
1551  raise RuntimeError('Unable to add Algorithm')
1552 
1553  def __del__(self):
1554  sc = self._algmgr.removeAlgorithm(self)
1555  if sc.isFailure():
1556  pass
1557 
1558  def initialize(self):
1559  return 1
1560 
1561  def start(self):
1562  return 1
1563 
1564  def execute(self):
1565  return 1
1566 
1567  def stop(self):
1568  return 1
1569 
1570  def finalize(self):
1571  return 1
1572 
1573 
1574 # ----Enable tab completion----------------------------------------------------
1575 try:
1576  import rlcompleter
1577  import readline
1578  readline.parse_and_bind("tab: complete")
1579 except:
1580  pass
def declSvcType(self, svcname, svctype)
Definition: Bindings.py:1123
def getHistoNames(self, node=None, lst=[])
Definition: Bindings.py:675
def setAlgorithms(self, algs)
Definition: Bindings.py:1219
def createSvc(self, name)
Definition: Bindings.py:1126
def histsvc(self, name='HistogramDataSvc')
Definition: Bindings.py:1177
def __init__(self, name, ints)
Definition: Bindings.py:800
_value
Definition: Bindings.py:219
def __init__(self, name, isvc=None)
Definition: Bindings.py:405
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
def runSelectedEvents(self, pfn, events)
Definition: Bindings.py:1372
def __getitem__(self, path)
Definition: Bindings.py:615
def findObject(self, path)
Definition: Bindings.py:568
def __init__(self, prop)
Definition: Bindings.py:214
def unregisterObject(self, path)
Definition: Bindings.py:555
def getClass(name, libs=[])
Definition: Bindings.py:180
def tool(self, name)
Definition: Bindings.py:1144
getNeededConfigurables
Definition: Proxy.py:31
def _install_exit_handlers(self)
Definition: Bindings.py:1066
bool PyHelper() addPropertyToCatalogue(IInterface *p, char *comp, char *name, char *value)
Definition: Bootstrap.cpp:255
def retrieveProfile1D(self, path)
Definition: Bindings.py:722
def property(self)
Definition: Bindings.py:244
def value(self)
Definition: Bindings.py:238
def getProperties(self, component)
Definition: Bindings.py:883
def hasDoc(self)
Definition: Bindings.py:251
def _getFIDandEvents(pfn)
Definition: Bindings.py:1442
def __init__(self, name, ialg=None)
Definition: Bindings.py:448
def toolsvc(self, name='ToolSvc')
Definition: Bindings.py:1194
Definition: Bindings.py:211
def __init__(self, name, ip=None)
Definition: Bindings.py:261
def ROOT6WorkAroundEnabled(id=None)
Definition: __init__.py:14
def service(self, name, interface=None)
Definition: Bindings.py:1116
_type
Definition: Bindings.py:215
def __init__(self, name, its)
Definition: Bindings.py:827
def algorithm(self, name, createIf=False)
Definition: Bindings.py:1133
__doc__
Definition: Bindings.py:216
def __init__(self, name, svc)
Definition: Bindings.py:878
def open(self, stream, typ='Gaudi::RootCnvSvc', **kwargs)
Definition: Bindings.py:926
def _sync(self, string=None)
Definition: Bindings.py:1530
def removeAlgorithm(self, alg)
Definition: Bindings.py:1237
def __init__(self, name, idp)
Definition: Bindings.py:544
def config(self, **args)
Definition: Bindings.py:1276
def __delitem__(self, path)
Definition: Bindings.py:627
def defineOutput(self, files, typ="Gaudi::RootCnvSvc")
Definition: Bindings.py:807
def addAlgorithm(self, alg)
Definition: Bindings.py:1205
def retrieveProfile2D(self, path)
Definition: Bindings.py:725
def __call_interface_method__(self, ifname, method, *args)
Definition: Bindings.py:283
def __new__(cls, *args, **kwargs)
Definition: Bindings.py:942
def getProperty(self, component, name)
Definition: Bindings.py:904
def __setitem__(self, path, obj)
Definition: Bindings.py:621
def loaddict(dict)
Definition: Bindings.py:166
def __init__(self, outputlevel=-1, joboptions=None, selfoptions={}, dllname=None, factname=None)
Definition: Bindings.py:964
def __init__(self, name, ihs)
Definition: Bindings.py:709
def release(self, itool)
Definition: Bindings.py:863
def ntuplesvc(self, name='NTupleSvc')
Definition: Bindings.py:1181
def datasvc(self, name)
Definition: Bindings.py:1155
def _retrieve(self, name, quiet=True)
Definition: Bindings.py:831
def dump(self, node=None)
Definition: Bindings.py:644
def property(self, name)
Definition: Bindings.py:1147
_property
Definition: Bindings.py:236
def create(self, typ, name=None, parent=nullptr, interface=None)
Definition: Bindings.py:854
def toArray(typ)
Definition: Bindings.py:97
def retrieve(self, name)
Definition: Bindings.py:851
def retrieveObject(self, path)
Definition: Bindings.py:561
def __getitem__(self, path)
Definition: Bindings.py:821
def _copyFactoriesFromList(factories)
Definition: Bindings.py:1509
def setRoot(self, name, obj)
Definition: Bindings.py:694
def __init__(self, name=None)
Definition: Bindings.py:1543
def __init__(self, name, itool=None)
Definition: Bindings.py:510
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
def registerObject(self, path, obj)
Definition: Bindings.py:549
def optSvc(self, name='JobOptionsSvc')
Definition: Bindings.py:1198
def ptype(self)
Definition: Bindings.py:241
return ep &&ep executeRun(maxevt).isSuccess()
def getComponentProperties(name)
Definition: Bindings.py:1459
def getObject(self, path, *args)
Definition: Bindings.py:587
def documentation(self)
Definition: Bindings.py:248
def leaves(self, node=None)
Definition: Bindings.py:633
def __getattr__(self, name)
Definition: Bindings.py:344
def getList(self, node=None, lst=[], rootFID=None)
Definition: Bindings.py:656
def readOptions(self, file)
Definition: Bindings.py:1202
def __setattr__(self, name, value)
Definition: Bindings.py:288
def deprecation(message)
Definition: Bindings.py:115