Getting the sum of weights in the tree after a cut

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

  1. The result for depends on the branch I use to create the TH1.
  2. 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())
	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 '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]

Name: h_temp Title: weight {weight} NbinsX: 100
hist range is [0.0, 1.08]

Name: h_temp Title: mJJ {weight} NbinsX: 100
hist range is [0.0, 2200.0]

Looping over TTree

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.

In your “${ROOTSYS}/etc/system.rootrc” file, try to set “Hist.Precision.1D: double”.

Thanks, now all of the results are the same. Do you know if it is possible to change the Draw() precision on the lxplus machines?

Create your private “${HOME}/.rootrc” file with this “magic” line (works for ROOT 6, not ROOT 5).

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.