3 ROOT.PyConfig.IgnoreCommandLineOptions =
True 6 def InputTree(tree, entrylist=ROOT.MakeNullPointer(ROOT.TEntryList)):
7 """add to the PyROOT wrapper of a TTree a TTreeReader and methods readBranch, arrayReader, valueReader""" 8 if hasattr(tree,
'_ttreereader'):
11 tree._entrylist = entrylist
12 tree._ttreereader = ROOT.TTreeReader(tree, tree._entrylist)
13 tree._ttreereader._isClean =
True 17 tree._ttreereaderversion = 1
18 tree.arrayReader = types.MethodType(getArrayReader, tree)
19 tree.valueReader = types.MethodType(getValueReader, tree)
20 tree.readBranch = types.MethodType(readBranch, tree)
21 tree.gotoEntry = types.MethodType(_gotoEntry, tree)
22 tree.readAllBranches = types.MethodType(_readAllBranches, tree)
23 tree.entries = tree._ttreereader.GetEntries(
False)
24 tree._extrabranches = {}
29 """Make a reader for branch branchName containing a variable-length value array.""" 30 if branchName
not in tree._ttras:
31 if not tree.GetBranch(branchName):
32 raise RuntimeError(
"Can't find branch '%s'" % branchName)
33 if not tree.GetBranchStatus(branchName):
34 raise RuntimeError(
"Branch %s has status=0" % branchName)
35 leaf = tree.GetBranch(branchName).GetLeaf(branchName)
36 if not bool(leaf.GetLeafCount()):
37 raise RuntimeError(
"Branch %s is not a variable-length value array" % branchName)
38 typ = leaf.GetTypeName()
40 return tree._ttras[branchName]
44 """Make a reader for branch branchName containing a single value.""" 45 if branchName
not in tree._ttrvs:
46 if not tree.GetBranch(branchName):
47 raise RuntimeError(
"Can't find branch '%s'" % branchName)
48 if not tree.GetBranchStatus(branchName):
49 raise RuntimeError(
"Branch %s has status=0" % branchName)
50 leaf = tree.GetBranch(branchName).GetLeaf(branchName)
51 if bool(leaf.GetLeafCount())
or leaf.GetLen() != 1:
52 raise RuntimeError(
"Branch %s is not a value" % branchName)
53 typ = leaf.GetTypeName()
55 return tree._ttrvs[branchName]
59 tree._extrabranches = {}
63 tree._extrabranches[name] = val
67 """Return the branch value if the branch is a value, and a TreeReaderArray if the branch is an array""" 68 if tree._ttreereader._isClean:
69 raise RuntimeError(
"readBranch must not be called before calling gotoEntry")
70 if branchName
in tree._extrabranches:
71 return tree._extrabranches[branchName]
72 elif branchName
in tree._ttras:
73 return tree._ttras[branchName]
74 elif branchName
in tree._ttrvs:
75 ret = tree._ttrvs[branchName].
Get()[0]
76 return ord(ret)
if type(ret) == str
else ret
78 branch = tree.GetBranch(branchName)
80 raise RuntimeError(
"Unknown branch %s" % branchName)
81 if not tree.GetBranchStatus(branchName):
82 raise RuntimeError(
"Branch %s has status=0" % branchName)
83 leaf = branch.GetLeaf(branchName)
84 typ = leaf.GetTypeName()
85 if leaf.GetLen() == 1
and not bool(leaf.GetLeafCount()):
88 tree.gotoEntry(tree.entry, forceCall=
True)
90 return ord(ret)
if type(ret) == str
else ret
94 tree.gotoEntry(tree.entry, forceCall=
True)
101 if not tree._ttreereader._isClean:
103 ttra = ROOT.TTreeReaderArray(typ)(tree._ttreereader, nam)
104 tree._leafTypes[nam] = typ
105 tree._ttras[nam] = ttra
106 return tree._ttras[nam]
110 if not tree._ttreereader._isClean:
112 ttrv = ROOT.TTreeReaderValue(typ)(tree._ttreereader, nam)
113 tree._leafTypes[nam] = typ
114 tree._ttrvs[nam] = ttrv
115 return tree._ttrvs[nam]
119 _ttreereader = ROOT.TTreeReader(tree, getattr(tree,
'_entrylist', ROOT.MakeNullPointer(ROOT.TEntryList)))
120 _ttreereader._isClean =
True 122 for k
in tree._ttrvs.keys():
123 _ttrvs[k] = ROOT.TTreeReaderValue(tree._leafTypes[k])(_ttreereader, k)
125 for k
in tree._ttras.keys():
126 _ttras[k] = ROOT.TTreeReaderArray(tree._leafTypes[k])(_ttreereader, k)
129 tree._ttreereader = _ttreereader
130 tree._ttreereaderversion += 1
139 return tree._entrylist.GetEntry(tree.entry)
145 tree._ttreereader._isClean =
False 146 if tree.entry != entry
or forceCall:
147 if (tree.entry == entry - 1
and entry != 0):
148 tree._ttreereader.Next()
150 tree._ttreereader.SetEntry(entry)