I have a code which is filling some TH1 histograms for a specific number of events by using each event’s weight. I use:
histo -> Fill (x_value, eventWeight);
Now, I am trying to fill a TTree with all these histograms as the TTree’s branches and I want to use the same weighting so that my branches will be identical with my histograms.
What I am doing is, giving the corresponding values to the variables connected to my TTree branches and then I use:
tree -> SetWeight(eventWeight);
But still my branches are differently weighted from my TH1 histograms. How exactly the SetWeight() function of TTree Class is working? And how I am supposed to weight my ttree in the same way I am weighting my histograms?
Thanks in advance,
I think here I need a bit more context about the meaning of “weighting a Tree”.
Are you trying to have branches of type histogram in your tree or to fill a tree with the entries you used to fill your histograms?
If the latter, the right way is to dedicate a branch, of type float or double, to the weight to be assigned to the entry, which can contain one or several other values which can be used to fill one or several other weighted histograms.
Thank you for your reply and sorry for the inconvenience!
Well, what I am trying to do is indeed the latter - to fill a tree with the entries I used to fill my histograms - but what I actually want to do is event by event weighting. So, I want for each event to fill the TTree branches with the weighted values ( the same job that histo -> Fill(x_value, eventWeight) is doing ).
So, an easy way of doing this is by multiplying each variable (which is assigned to a TBranch) with the event weight and then filling the tree (and so the branches) with the weighted values. But I was wondering whether there is a ‘standard’ way to do this because I have a large number of TTree branches.
thanks for the additional details. I believe the standard way is to add a weight branch with the weight. You can do the multiplication later: the information will be fully available in the tree.
Thanks again for answering, but I still have some questions.
Another detail for what I am doing is that I am running a code which is creating a rootfile with the TTree and its branches and then for plotting I am using a python script.
So, if for each event I fill an extra branch with the event weight then how will I be able, after that, to weight each value with the ‘correct’ (corresponding) weight? What I have in mind with what you are saying to me is that in my rootfile I will have an extra branch, so an extra histogram, which will have in x-axis the different values of weights and in y-axis the number of the events which have the specific weight.
But let’s say, for example, that for the 1st event I have the energy to be 200MeV with the event weight to be 5 and then for the 2nd event I have energy 500MeV with event weight 2. In my rootfile I will have a branch which has one entry for weight=5 and one entry for weight=2. Then how will I be able to get from the rootfile the information of which weight corresponds to each entry ?
in a rootfile every branch is a column: every row is an entry. In your python script you’ll do
for entry in myFile.myTree:
So one weight per entry, many values per entry with the same weight which is a property of the entry.
That was really enlightening!! Thank you very much!!