ROOT version: 6.16/00
OS: Ubuntu 16.04
Dear ROOT experts.
I want to write a general function that would calculate weighted number of events in the TTree branch that pass some arbitrary cut on values of that branch. I tried to do that utilizing the TTree Draw()
and TH1 GetSumOfWeights()
functions because it would allow me to pass the cut as a simple string. But there are some probmles
- The result for depends on the branch I use to create the TH1.
- None of the results are equal to the one I get simply looping over the tree.
Here’s my code and the file I’m using:
cutTest.root (2.2 MB)
def WeightSumAfterCutLoop(tree):
weightSum = 0.
for event in tree:
weightSum += event.weight
return weightSum
def WeightSumAfterCut(tree, cut, variable):
tree.Draw(variable + '>>h_temp', cut, 'gOff')
h_temp = ROOT.gDirectory.Get('h_temp')
weightSum = h_temp.GetSumOfWeights()
print h_temp
print 'hist range is [{}, {}]'.format(h_temp.GetXaxis().GetXmin(), h_temp.GetXaxis().GetXmax())
h_temp.Delete()
return weightSum
file = ROOT.TFile('cutTest.root')
tree = file.Get('tree')
cut = 'weight'
variableList = ['deltaEta', 'weight', 'mJJ']
for variable in variableList:
weightSum = WeightSumAfterCut(tree, cut, variable)
print weightSum
print
print 'Looping over TTree'
weightSum = WeightSumAfterCutLoop(tree)
print weightSum
And here are the results.
Name: h_temp Title: deltaEta {weight} NbinsX: 100
hist range is [-2.35, 2.35]
49926.1151428
Name: h_temp Title: weight {weight} NbinsX: 100
hist range is [0.0, 1.08]
49926.1172838
Name: h_temp Title: mJJ {weight} NbinsX: 100
hist range is [0.0, 2200.0]
49926.1153259
Looping over TTree
49926.1119852
Why does TTree.Draw()
results differ from each other and from the loop result? What would be the best way to code this kind of function if I wan to pass the cut as a string? I know this could be done in RDataFrame but my files are rather small and I want to run this function a lot, so I don’t want to waste time on RDF start-up every time I run it.
Thanks in advance.