Skip to content

Commit 471c141

Browse files
authored
Merge pull request uniovi-hepex#15 from peruzzim/nested_modules
Expose values calculated by previous eventLoop modules
2 parents 9cfae3f + 7b1b9c9 commit 471c141

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

python/postprocessing/framework/eventloop.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from PhysicsTools.NanoAODTools.postprocessing.framework.datamodel import Event
2+
from PhysicsTools.NanoAODTools.postprocessing.framework.treeReaderArrayTools import clearExtraBranches
23
import sys, time
34

45
class Module:
@@ -26,6 +27,7 @@ def eventLoop(modules, inputFile, outputFile, inputTree, wrappedOutputTree, maxE
2627
for i in xrange(entries) if eventRange == None else eventRange:
2728
if maxEvents > 0 and i >= maxEvents-1: break
2829
e = Event(inputTree,i)
30+
clearExtraBranches(inputTree)
2931
doneEvents += 1
3032
ret = True
3133
for m in modules:

python/postprocessing/framework/output.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import ROOT
33
ROOT.PyConfig.IgnoreCommandLineOptions = True
44

5+
from PhysicsTools.NanoAODTools.postprocessing.framework.treeReaderArrayTools import setExtraBranch
6+
57
_rootBranchType2PythonArray = { 'b':'B', 'B':'b', 'i':'I', 'I':'i', 'F':'f', 'D':'d', 'l':'L', 'L':'l', 'O':'B' }
68

79
class OutputBranch:
@@ -30,9 +32,10 @@ def fill(self, val):
3032
for i,v in enumerate(val): self.buff[i] = v
3133

3234
class OutputTree:
33-
def __init__(self, tfile, ttree):
35+
def __init__(self, tfile, ttree, intree):
3436
self._file = tfile
3537
self._tree = ttree
38+
self._intree = intree
3639
self._branches = {}
3740
def branch(self, name, rootBranchType, n=1, lenVar=None, title=None):
3841
if (lenVar != None) and (lenVar not in self._branches) and (not self._tree.GetBranch(lenVar)):
@@ -43,7 +46,9 @@ def fillBranch(self, name, val):
4346
br = self._branches[name]
4447
if br.lenVar and (br.lenVar in self._branches):
4548
self._branches[br.lenVar].buff[0] = len(val)
49+
setExtraBranch(self._intree,br.lenVar,len(val))
4650
br.fill(val)
51+
setExtraBranch(self._intree,name,val)
4752
def tree(self):
4853
return self._tree
4954
def fill(self):
@@ -58,7 +63,7 @@ def __init__(self, inputFile, inputTree, outputFile, branchSelection = None, ful
5863
if branchSelection:
5964
branchSelection.selectBranches(inputTree)
6065
outputTree = inputTree.CopyTree('1') if fullClone else inputTree.CloneTree(0)
61-
OutputTree.__init__(self, outputFile, outputTree)
66+
OutputTree.__init__(self, outputFile, outputTree, inputTree)
6267
self._inputTree = inputTree
6368
self._otherTrees = {}
6469
self._otherObjects = {}
@@ -95,5 +100,5 @@ class FriendOutput(OutputTree):
95100
def __init__(self, inputFile, inputTree, outputFile, treeName="Friends"):
96101
outputFile.cd()
97102
outputTree = ROOT.TTree(treeName,"Friend tree for "+inputTree.GetName())
98-
OutputTree.__init__(self, outputFile, outputTree)
103+
OutputTree.__init__(self, outputFile, outputTree, inputTree)
99104

python/postprocessing/framework/treeReaderArrayTools.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def InputTree(tree,entrylist=None):
1919
tree.gotoEntry = types.MethodType(_gotoEntry, tree)
2020
tree.readAllBranches = types.MethodType(_readAllBranches, tree)
2121
tree.entries = tree._ttreereader.GetEntries(False)
22+
tree._extrabranches={}
2223
return tree
2324

2425
def getArrayReader(tree, branchName):
@@ -41,11 +42,18 @@ def getValueReader(tree, branchName):
4142
tree._ttrvs[branchName] = _makeValueReader(tree, typ, branchName)
4243
return tree._ttrvs[branchName]
4344

45+
def clearExtraBranches(tree):
46+
tree._extrabranches = {}
47+
48+
def setExtraBranch(tree,name,val):
49+
tree._extrabranches[name] = val
4450

4551
def readBranch(tree, branchName):
4652
"""Return the branch value if the branch is a value, and a TreeReaderArray if the branch is an array"""
4753
if tree._ttreereader._isClean: raise RuntimeError, "readBranch must not be called before calling gotoEntry"
48-
if branchName in tree._ttras:
54+
if branchName in tree._extrabranches:
55+
return tree._extrabranches[branchName]
56+
elif branchName in tree._ttras:
4957
return tree._ttras[branchName]
5058
elif branchName in tree._ttrvs:
5159
ret = tree._ttrvs[branchName].Get()[0]

0 commit comments

Comments
 (0)