Weights, entries, Draw() and Fill()

Hi, here’s what I was trying to do:
[li] Loop over the names of many root files, all of similar format .root, and extract and for each.[/li]
[li] Open each file, and extract the number of entries in a tree.[/li]
[li] Make a TH2 plot with and on the axes, and the corresponding number of entries as bin content.[/li][/ol]

I’ve managed to do that, and here’s a simplified core version of the code I have:

id_dict = {}
#some code fills id_dict, such that id_dict[<file_identifier>]=[<id1>,<id2>]
selection = "(eT_miss > 500)"		#Some cut on a variable
histo = ROOT.TH2D("histo","My Histogram",10,400,900,12,0,300)

for file in id_dict:		#Loop over the files
	rootfile = ROOT.TFile(file, "read")		#Open the file
	tree = rootfile.Get("MyTree")		#Open the tree
	id_dict[file].append(int(MyTree.GetEntries(selection)))#Store the number of entries
	for i in range(0, id_dict[file][2]):		#Loop length = number of entries
		histo.Fill(id_dict[file][0], id_dict[file][1])			#Fill the histogram
histo->Draw()		#ta-da!

What I want to do is add weights. As far as I know, I can’t just pass weights as an argument in “MyTree.GetEntries()”, like I would do with “Draw()”. The weights I want to add exists as branches in MyTree - let’s call them “Weight1” and “Weight2”.

Ideally, I’d like to fill my histogram with the number of events, for each file, that pass the selection “selection”, and weighted by “Weight1*Weight2”. Is there a tree-level way of doing this, without having to loop over all the events in the tree?


The selection parameter in TTree::Draw is a weight in fact.


Hi couet :slight_smile: Yes, I realize that, but if possible I’d rather avoid plotting a histogram for every file I open. Is there a way of getting the weighted number of entries from an ntuple without drawing anything?

ntuple->Draw(“px”,“10”, “goff”)

1 Like

My problem is that ntuple->Draw("px","10","goff") and ntuple->Draw("px","","goff") return the same value. Only if I apply a cut such as ntuple->Draw("px","px>100","goff") do I get a smaller number of entries; and I get the same number with ntuple->Draw("px","(px>100)*k","goff") for any k!=0.

The weight (W) does not change the number of entries. It is like the weight in TH1::Fill(): A weighted entry will increment the filled bin with W instead of one, but it still count as one entry.

1 Like