CMS 3D CMS Logo

treeReaderArrayTools.py
Go to the documentation of this file.
1 import types
2 import ROOT
3 ROOT.PyConfig.IgnoreCommandLineOptions = True
4 
5 
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'):
9  return tree # don't initialize twice
10  tree.entry = -1
11  tree._entrylist = entrylist
12  tree._ttreereader = ROOT.TTreeReader(tree, tree._entrylist)
13  tree._ttreereader._isClean = True
14  tree._ttrvs = {}
15  tree._ttras = {}
16  tree._leafTypes = {}
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 = {}
25  return tree
26 
27 
28 def getArrayReader(tree, branchName):
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()
39  tree._ttras[branchName] = _makeArrayReader(tree, typ, branchName)
40  return tree._ttras[branchName]
41 
42 
43 def getValueReader(tree, 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()
54  tree._ttrvs[branchName] = _makeValueReader(tree, typ, branchName)
55  return tree._ttrvs[branchName]
56 
57 
59  tree._extrabranches = {}
60 
61 
62 def setExtraBranch(tree, name, val):
63  tree._extrabranches[name] = val
64 
65 
66 def readBranch(tree, branchName):
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
77  else:
78  branch = tree.GetBranch(branchName)
79  if not branch:
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()):
86  _vr = _makeValueReader(tree, typ, branchName)
87  # force calling SetEntry as a new ValueReader was created
88  tree.gotoEntry(tree.entry, forceCall=True)
89  ret = _vr.Get()[0]
90  return ord(ret) if type(ret) == str else ret
91  else:
92  _ar = _makeArrayReader(tree, typ, branchName)
93  # force calling SetEntry as a new ArrayReader was created
94  tree.gotoEntry(tree.entry, forceCall=True)
95  return _ar
96 
97 
98 
99 
100 def _makeArrayReader(tree, typ, nam):
101  if not tree._ttreereader._isClean:
102  _remakeAllReaders(tree)
103  ttra = ROOT.TTreeReaderArray(typ)(tree._ttreereader, nam)
104  tree._leafTypes[nam] = typ
105  tree._ttras[nam] = ttra
106  return tree._ttras[nam]
107 
108 
109 def _makeValueReader(tree, typ, nam):
110  if not tree._ttreereader._isClean:
111  _remakeAllReaders(tree)
112  ttrv = ROOT.TTreeReaderValue(typ)(tree._ttreereader, nam)
113  tree._leafTypes[nam] = typ
114  tree._ttrvs[nam] = ttrv
115  return tree._ttrvs[nam]
116 
117 
119  _ttreereader = ROOT.TTreeReader(tree, getattr(tree, '_entrylist', ROOT.MakeNullPointer(ROOT.TEntryList)))
120  _ttreereader._isClean = True
121  _ttrvs = {}
122  for k in tree._ttrvs.keys():
123  _ttrvs[k] = ROOT.TTreeReaderValue(tree._leafTypes[k])(_ttreereader, k)
124  _ttras = {}
125  for k in tree._ttras.keys():
126  _ttras[k] = ROOT.TTreeReaderArray(tree._leafTypes[k])(_ttreereader, k)
127  tree._ttrvs = _ttrvs
128  tree._ttras = _ttras
129  tree._ttreereader = _ttreereader
130  tree._ttreereaderversion += 1
131 
132 
134  tree.GetEntry(_currentTreeEntry(tree))
135 
136 
138  if tree._entrylist:
139  return tree._entrylist.GetEntry(tree.entry)
140  else:
141  return tree.entry
142 
143 
144 def _gotoEntry(tree, entry, forceCall=False):
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()
149  else:
150  tree._ttreereader.SetEntry(entry)
151  tree.entry = entry
def getArrayReader(tree, branchName)
def _makeArrayReader(tree, typ, nam)
PRIVATE IMPLEMENTATION PART #######.
def readBranch(tree, branchName)
def getValueReader(tree, branchName)
def _makeValueReader(tree, typ, nam)
def _gotoEntry(tree, entry, forceCall=False)
def InputTree(tree, entrylist=ROOT.MakeNullPointer(ROOT.TEntryList))
T * Get(Args... args)
Definition: Trend.h:122
def setExtraBranch(tree, name, val)