Save Tree with weight

Dear Experts,

I have a tree with multiple branches, two of which are “tp_mass” and “genweight”.
If I have to draw “tp_mass” with “genweight” I do:


After having done this, is it possible to save the tree such that “tp_mass” now has the “genweight” weight applied to it?

Thanks in advance!

1 Like

Hi Tanvi,

you can solve this issue with TDataFrame.
In your case:

// assuming these names :)
ROOT::Experimental::TDataFrame f("TreeName", "FileName.root"); 
f.Define("weightedMass", "tp_mass * genweight").Snapshot("myNewTree", "myNewFileName");

If you also want the histogram:

ROOT::Experimental::TDataFrame f("TreeName", "FileName.root"); 
auto h = f.Histo1D(“tp_mass”,“genweight”);
f.Define("weightedMass", "tp_mass * genweight")..Snapshot("myNewTree", "myNewFileName");


1 Like

Thanks Danilo! This was extremely helpful.

Hi Danilo,

Sorry I have another question. I created the reweighted tree. However there is a difference between the two distributions.
In detail:

Case 1 : Draw a branch tp_mass with weight “genTotalweight”
gives me imageScreen Shot 2017-10-11 at 11.28.48 AM

Case 2 : From the reweighted tree I created (using the method you suggested)
On drawing the reweighted branch I get
imageScreen Shot 2017-10-11 at 11.29.55 AM

Its clear that the two distributions are not the same , whereas I think they should be.
Is there a difference between the two methods of drawing with weights?


Hi Tanvi,

I misinterpreted your first request: my bad. You cannot save the value and its weight in a single value by multiplying those :frowning:
The plot is telling us that :slight_smile:


Right, to give some background: you have values for tp_mass, say 5, 10, 7, 100. These values are stored in the branch tp_mass. Each of those entries has a different weight: 0.1, 0.2, 100, 0.01. No multiplication or addition or whatsoever can combine these, they are completely independent numbers. You will need to keep two branches, and weight each tp_mass entry by the genweight value - as you did with TTree::Draw().

1 Like

Yes :slightly_smiling_face: Thanks for the tip!

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