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 def book(self, oid, obj=None, *args, **kw):
358 """book a histogram, profile or tree
359 @param oid is the unique object identifier even across streams,
361 @param obj either an already full-fledge constructed ROOT object
362 or None (then `*args` or `**kw` need to be correctly setup)
363 @param *args list of arguments to give to the constructor of the
364 ROOT object one wants to book
365 @param **kw a dictionary containing a key 'args' being the list of
366 arguments to the constructor of the ROOT objects one wants to
369 >>> th.book('/temp/1d/h1', 'TH1D', args=('h1','h1',100,0.,100.))
370 >>> th.book('/temp/1d/h2', ROOT.TH1D, args=('h2','h2',100,0.,100.))
371 >>> th.book('/temp/1d/h3', ROOT.TH1D, 'h3','h3',100,0.,100.)
372 >>> th.book('/temp/1d/h4', ROOT.TH1D('h4','h4',100,0.,100.))
373 >>> th.book('/temp/1d/h5', obj=ROOT.TH1D('h5','h5',100,0.,100.))
374 >>> th.book('/temp/1d/h6', klass='TH1D', args=('h6','h6',100,0.,100.))
376 >>> th.book('/temp/tree/t1', ROOT.TTree('t1','t1'))
377 >>> th.book('/temp/tree/t2', obj=ROOT.TTree('t2','t2'))
378 >>> th.book('/temp/tree/t3', klass='TTree', args=('t3','t3'))
380 _err =
"please provide _either_ an already constructed ROOT object or"\
381 "a class name/class type (with appropriate arguments)"
382 klass = kw.get(
'klass',
None)
383 assert not (obj
is None and klass
is None), _err
384 assert not (obj
is not None and klass
is not None), _err
386 if isinstance(obj, (str,type)):
390 if isinstance(obj, ROOT.TH1):
392 meth = self._cpp_regHist
393 elif isinstance(obj, (ROOT.TGraph,)):
394 meth = self._cpp_regGraph
395 elif isinstance(obj, (ROOT.TEfficiency,)):
396 meth = self._cpp_regEfficiency
397 elif isinstance(obj, (ROOT.TTree,)):
398 meth = self._cpp_regTree
400 raise TypeError(
"invalid type '%r'"%
type(obj))
401 if meth(oid, obj).isSuccess():
402 self._py_cache[oid]=obj
404 raise RuntimeError(
'could not book object [%r]'%obj)
407 if isinstance(klass, str):
408 klass = getattr(ROOT, klass)
410 return self.book(oid, obj=
klass(*args))
411 if kw
and 'args' in kw:
412 return self.book(oid, obj=
klass(*kw[
'args']))
413 err =
"invalid arguments: either provide a valid `*args` or "\
414 "a `**kw` containing a 'args' key"
415 raise RuntimeError(err)
416 raise RuntimeError(
"unforseen case: oid='%r' obj='%r' args='%r' "
417 "kw='%r'"%(oid,obj,args,kw))
419 ITHistSvc.book = book
421 def get(self, oid, klass=None):
422 """retrieve an already booked ROOT object.
423 If the object was booked on the C++ side, try to use the `klass` hint
424 (the usual string or type) to find the object in the correct 'folder'
425 (histograms, graphs or trees).
426 If `klass` is None, then go through all the folders iteratively (slow)
429 return self._py_cache[oid]
432 def _get_helper(klass, hsvc, meth, oid, update_cache=True):
433 makeNullPtr = ROOT.MakeNullPointer
434 o = makeNullPtr(klass)
435 if meth(oid, o).isSuccess():
437 hsvc._py_cache[oid] = o
441 if isinstance(klass, str):
442 klass = getattr(ROOT, klass)
443 if issubclass(klass, (ROOT.TH1,)):
444 return _get_helper(klass, self, self.getHist, oid)
445 if issubclass(klass, (ROOT.TGraph,)):
446 return _get_helper(klass, self, self.getGraph, oid)
447 if issubclass(klass, (ROOT.TEfficiency,)):
448 return _get_helper(klass, self, self.getEfficiency, oid)
449 if issubclass(klass, (ROOT.TTree,)):
450 return _get_helper(klass, self, self.getTree, oid)
451 raise RuntimeError(
'unsupported type [%r]'%klass)
457 oids = [n
for n
in self.getHists()
if n
not in self._py_cache.
keys()]
459 obj = _get_helper(ROOT.TH1, self, self.getHist, name,
463 klass = getattr(ROOT, obj.ClassName())
464 obj = _get_helper(klass, self, self.getHist, name)
467 oids = [n
for n
in self.getGraphs()
if n
not in self._py_cache.
keys()]
469 _get_helper(ROOT.TGraph, self, self.getGraph, name)
472 oids = [n
for n
in self.getEfficiencies()
if n
not in self._py_cache.
keys()]
474 _get_helper(ROOT.TEfficiency, self, self.getEfficiency, name)
477 oids = [n
for n
in self.getTrees()
if n
not in self._py_cache.
keys()]
479 _get_helper(ROOT.TTree, self, self.getTree, name)
482 return self._py_cache[oid]
487 def getitem(self, oid):
489 ITHistSvc.__getitem__ = getitem
492 def delitem(self, oid):
493 if isinstance(oid, str):
495 del self._py_cache[oid]
496 assert self.deReg(oid).isSuccess(), \
497 "could not remove object [%r]"%oid
499 ITHistSvc.__delitem__ = delitem
501 def setitem(self, k, v):
502 return self.book(k, obj=v)
503 ITHistSvc.__setitem__ = setitem
506 def regObject(self, regFcn, oid, oid_type=None):
507 """Helper method to register object 'oid' using 'regFcn'."""
508 if oid_type
is not None:
509 return self.book(oid,obj=oid_type)
510 if regFcn(self,oid).isSuccess():
513 err =
''.
join([
'invalid arguments oid=',
repr(oid),
' oid_type=',
515 raise ValueError(err)
517 ITHistSvc.regHist = partialmethod(regObject, ITHistSvc._cpp_regHist)
518 ITHistSvc.regTree = partialmethod(regObject, ITHistSvc._cpp_regTree)
519 ITHistSvc.regEfficiency = partialmethod(regObject, ITHistSvc._cpp_regEfficiency)
520 ITHistSvc.regGraph = partialmethod(regObject, ITHistSvc._cpp_regGraph)
523 def load(self, oid, oid_type):
524 """Helper method to load a given object `oid' from a stream, knowing
525 its type. `oid_type' is a string whose value is either:
526 - 'hist', to load any THx and TProfiles
527 - 'tree', to load TTrees
528 - 'efficiency', to load TEfficiency
529 - 'graph', to load TGraph and TGraphErrors
531 if oid_type ==
'hist':
532 return self.regHist(oid)
533 elif oid_type ==
'tree':
534 return self.regTree(oid)
535 elif oid_type ==
'efficiency':
536 return self.regEfficiency(oid)
537 elif oid_type ==
'graph':
538 return self.regGraph(oid)
540 raise ValueError(f
'oid_type (={oid_type}) MUST be one of hist, tree, efficiency, graph')
542 ITHistSvc.load = load
547 for n
in (
'__contains__',
551 'items',
'iteritems',
552 'iterkeys',
'itervalues',
555 def %s(self, *args, **kw):
556 return self._py_cache.%s(*args,**kw)
558 del %s""" % (n,n,n,n,n)
559 exec (code, globals(),locals())
563 return self
is not None
564 ITHistSvc.__bool__ = __bool__
569 assert self.deReg(obj).isSuccess(), \
570 "could not remove object [%r]"%k
576 k = self.iterkeys().
next()
577 return (k, self.pop(k))
578 ITHistSvc.popitem = popitem