339 ITHistSvc = cppyy.gbl.ITHistSvc
345 return self.__py_cache
346 except AttributeError:
347 self.__py_cache =
dict()
348 return self.__py_cache
349 ITHistSvc._py_cache = _py_cache
353 for n
in (
'Hist',
'Graph',
'Efficiency',
'Tree'):
354 code =
"ITHistSvc._cpp_reg%s = ITHistSvc.reg%s" % (n,n)
355 exec (code, globals(),locals())
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 =
'_cpp_regHist'
393 elif isinstance(obj, (ROOT.TGraph,)):
394 meth =
'_cpp_regGraph'
395 elif isinstance(obj, (ROOT.TEfficiency,)):
396 meth =
'_cpp_regEfficiency'
397 elif isinstance(obj, (ROOT.TTree,)):
398 meth =
'_cpp_regTree'
400 raise TypeError(
"invalid type '%r'"%
type(obj))
401 if getattr(self,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
507 for n
in (
'Hist',
'Graph',
'Efficiency',
'Tree'):
509 def reg%s(self, oid, oid_type=None):
510 if not (oid_type is None):
511 return self.book(oid,obj=oid_type)
512 if ITHistSvc._cpp_reg%s(self,oid).isSuccess():
515 err = ''.join(['invalid arguments oid=',repr(oid),' oid_type=',
517 raise ValueError(err)
518 ITHistSvc.reg%s = reg%s
519 del reg%s""" % (n,n,n,n,n)
520 exec (code, globals(),locals())
522 def load(self, oid, oid_type):
523 """Helper method to load a given object `oid' from a stream, knowing
524 its type. `oid_type' is a string whose value is either:
525 - 'hist', to load any THx and TProfiles
526 - 'tree', to load TTrees
527 - 'efficiency', to load TEfficiency
528 - 'graph', to load TGraph and TGraphErrors
530 _allowed_values = (
'hist',
'tree',
'efficiency',
'graph')
531 if oid_type
not in _allowed_values:
533 'oid_type (=%r) MUST be one of %r'%(oid_type,
536 return getattr(self,
'reg%s'%oid_type.capitalize())(oid)
537 ITHistSvc.load = load
542 for n
in (
'__contains__',
546 'items',
'iteritems',
547 'iterkeys',
'itervalues',
551 def %s(self, *args, **kw):
552 return self._py_cache.%s(*args,**kw)
554 del %s""" % (n,n,n,n,n)
555 exec (code, globals(),locals())
558 assert self.deReg(obj).isSuccess(), \
559 "could not remove object [%r]"%k
565 k = self.iterkeys().
next()
566 return (k, self.pop(k))
567 ITHistSvc.popitem = popitem