258 IIncidentSvc = cppyy.gbl.IIncidentSvc
260 IIncidentSvc._cpp_addListener = IIncidentSvc.addListener
261 def addListener (self, *args):
263 if hasattr (listener,
'_cppHandle'):
264 args = (listener._cppHandle,) + args[1:]
265 return self._cpp_addListener (*args)
266 addListener.__doc__ = IIncidentSvc._cpp_addListener.__doc__
267 IIncidentSvc.addListener = addListener
270 IIncidentSvc._cpp_removeListener = IIncidentSvc.removeListener
271 def removeListener (self, *args):
273 if hasattr (listener,
'_cppHandle'):
274 args = (listener._cppHandle,) + args[1:]
275 return self._cpp_removeListener (*args)
276 removeListener.__doc__ = IIncidentSvc._cpp_removeListener.__doc__
277 IIncidentSvc.removeListener = removeListener
338 ITHistSvc = cppyy.gbl.ITHistSvc
344 return self.__py_cache
345 except AttributeError:
346 self.__py_cache = dict()
347 return self.__py_cache
348 ITHistSvc._py_cache = _py_cache
352 ITHistSvc._cpp_regHist = ITHistSvc.regHist
353 ITHistSvc._cpp_regGraph = ITHistSvc.regGraph
354 ITHistSvc._cpp_regEfficiency = ITHistSvc.regEfficiency
355 ITHistSvc._cpp_regTree = ITHistSvc.regTree
357 _cpp_getHist = ROOT.AthenaInternal.getHist
358 _cpp_getGraph = ROOT.AthenaInternal.getGraph
359 _cpp_getEfficiency = ROOT.AthenaInternal.getEfficiency
360 _cpp_getTree = ROOT.AthenaInternal.getTree
362 def book(self, oid, obj=None, *args, **kw):
363 """book a histogram, profile or tree
364 @param oid is the unique object identifier even across streams,
366 @param obj either an already full-fledge constructed ROOT object
367 or None (then `*args` or `**kw` need to be correctly setup)
368 @param *args list of arguments to give to the constructor of the
369 ROOT object one wants to book
370 @param **kw a dictionary containing a key 'args' being the list of
371 arguments to the constructor of the ROOT objects one wants to
374 >>> th.book('/temp/1d/h1', 'TH1D', args=('h1','h1',100,0.,100.))
375 >>> th.book('/temp/1d/h2', ROOT.TH1D, args=('h2','h2',100,0.,100.))
376 >>> th.book('/temp/1d/h3', ROOT.TH1D, 'h3','h3',100,0.,100.)
377 >>> th.book('/temp/1d/h4', ROOT.TH1D('h4','h4',100,0.,100.))
378 >>> th.book('/temp/1d/h5', obj=ROOT.TH1D('h5','h5',100,0.,100.))
379 >>> th.book('/temp/1d/h6', klass='TH1D', args=('h6','h6',100,0.,100.))
381 >>> th.book('/temp/tree/t1', ROOT.TTree('t1','t1'))
382 >>> th.book('/temp/tree/t2', obj=ROOT.TTree('t2','t2'))
383 >>> th.book('/temp/tree/t3', klass='TTree', args=('t3','t3'))
385 _err =
"please provide _either_ an already constructed ROOT object or"\
386 "a class name/class type (with appropriate arguments)"
387 klass = kw.get(
'klass',
None)
388 assert not (obj
is None and klass
is None), _err
389 assert not (obj
is not None and klass
is not None), _err
391 if isinstance(obj, (str,type)):
395 if isinstance(obj, ROOT.TH1):
397 meth = self._cpp_regHist
398 elif isinstance(obj, (ROOT.TGraph,)):
399 meth = self._cpp_regGraph
400 elif isinstance(obj, (ROOT.TEfficiency,)):
401 meth = self._cpp_regEfficiency
402 elif isinstance(obj, (ROOT.TTree,)):
403 meth = self._cpp_regTree
405 raise TypeError(
"invalid type '%r'"%
type(obj))
406 if meth(oid, obj).isSuccess():
407 self._py_cache[oid]=obj
409 raise RuntimeError(
'could not book object [%r]'%obj)
412 if isinstance(klass, str):
413 klass = getattr(ROOT, klass)
415 return self.book(oid, obj=klass(*args))
416 if kw
and 'args' in kw:
417 return self.book(oid, obj=klass(*kw[
'args']))
418 err =
"invalid arguments: either provide a valid `*args` or "\
419 "a `**kw` containing a 'args' key"
420 raise RuntimeError(err)
421 raise RuntimeError(
"unforseen case: oid='%r' obj='%r' args='%r' "
422 "kw='%r'"%(oid,obj,args,kw))
424 ITHistSvc.book = book
426 def get(self, oid, klass=None):
427 """retrieve an already booked ROOT object.
428 If the object was booked on the C++ side, try to use the `klass` hint
429 (the usual string or type) to find the object in the correct 'folder'
430 (histograms, graphs or trees).
431 If `klass` is None, then go through all the folders iteratively (slow)
434 return self._py_cache[oid]
437 def _get_helper(hsvc, func, oid, update_cache=True):
438 sc, o = func(hsvc, oid)
441 hsvc._py_cache[oid] = o
445 if isinstance(klass, str):
446 klass = getattr(ROOT, klass)
447 if issubclass(klass, (ROOT.TH1,)):
448 return _get_helper(self, _cpp_getHist, oid)
449 if issubclass(klass, (ROOT.TGraph,)):
450 return _get_helper(self, _cpp_getGraph, oid)
451 if issubclass(klass, (ROOT.TEfficiency,)):
452 return _get_helper(self, _cpp_getEfficiency, oid)
453 if issubclass(klass, (ROOT.TTree,)):
454 return _get_helper(self, _cpp_getTree, oid)
455 raise RuntimeError(
'unsupported type [%r]'%klass)
461 oids = [n
for n
in self.getHists()
if n
not in self._py_cache.
keys()]
463 obj = _get_helper(self, _cpp_getHist, name, update_cache=
False)
466 klass = getattr(ROOT, obj.ClassName())
467 obj = _get_helper(self, _cpp_getHist, name)
470 oids = [n
for n
in self.getGraphs()
if n
not in self._py_cache.
keys()]
472 _get_helper(self, _cpp_getGraph, name)
475 oids = [n
for n
in self.getEfficiencies()
if n
not in self._py_cache.
keys()]
477 _get_helper(self, _cpp_getEfficiency, name)
480 oids = [n
for n
in self.getTrees()
if n
not in self._py_cache.
keys()]
482 _get_helper(self, _cpp_getTree, name)
485 return self._py_cache[oid]
490 def getitem(self, oid):
492 ITHistSvc.__getitem__ = getitem
495 def delitem(self, oid):
496 if isinstance(oid, str):
498 del self._py_cache[oid]
499 assert self.deReg(oid).isSuccess(), \
500 "could not remove object [%r]"%oid
502 ITHistSvc.__delitem__ = delitem
504 def setitem(self, k, v):
505 return self.book(k, obj=v)
506 ITHistSvc.__setitem__ = setitem
509 def regObject(self, regFcn, oid, oid_type=None):
510 """Helper method to register object 'oid' using 'regFcn'."""
511 if oid_type
is not None:
512 return self.book(oid,obj=oid_type)
513 if regFcn(self,oid).isSuccess():
516 err =
''.join([
'invalid arguments oid=',repr(oid),
' oid_type=',
518 raise ValueError(err)
520 ITHistSvc.regHist = partialmethod(regObject, ITHistSvc._cpp_regHist)
521 ITHistSvc.regTree = partialmethod(regObject, ITHistSvc._cpp_regTree)
522 ITHistSvc.regEfficiency = partialmethod(regObject, ITHistSvc._cpp_regEfficiency)
523 ITHistSvc.regGraph = partialmethod(regObject, ITHistSvc._cpp_regGraph)
526 def load(self, oid, oid_type):
527 """Helper method to load a given object `oid' from a stream, knowing
528 its type. `oid_type' is a string whose value is either:
529 - 'hist', to load any THx and TProfiles
530 - 'tree', to load TTrees
531 - 'efficiency', to load TEfficiency
532 - 'graph', to load TGraph and TGraphErrors
534 if oid_type ==
'hist':
535 return self.regHist(oid)
536 elif oid_type ==
'tree':
537 return self.regTree(oid)
538 elif oid_type ==
'efficiency':
539 return self.regEfficiency(oid)
540 elif oid_type ==
'graph':
541 return self.regGraph(oid)
543 raise ValueError(f
'oid_type (={oid_type}) MUST be one of hist, tree, efficiency, graph')
545 ITHistSvc.load = load
550 for n
in (
'__contains__',
554 'items',
'iteritems',
555 'iterkeys',
'itervalues',
558def %s(self, *args, **kw):
559 return self._py_cache.%s(*args,**kw)
561del %s""" % (n,n,n,n,n)
562 exec (code, globals(),locals())
566 return self
is not None
567 ITHistSvc.__bool__ = __bool__
572 assert self.deReg(obj).isSuccess(), \
573 "could not remove object [%r]"%k
579 k = self.iterkeys().next()
580 return (k, self.pop(k))
581 ITHistSvc.popitem = popitem