Event-by-event weighting and saving as a branch at TTree

Dear all,

I have a question about the exact mechanism (formula) of weighting at ROOT. Simillar question (TTree Weighting is different from TH1 weighting) was already raised same time ago, unfortunately I wasnt able to find an exact solution to my problem.

I have a weights for all my events and I would like to apply those weights for each branch (variable) of my TTree and then save this weighted variables as a new branch/branches. According the answer to the question mentioned above, the standard procedure is just to have separate branch with weights (which I have) and in the need of weighted data, do the multiplication of weight and variable. But result of this multiplication is absolutely different then TH1 obtained via Fill->(‘variable’, ‘weight’). Also, during the analysis process, I have to use several kinds of weights on data in particular order. For this reason, to be always able to save weighted values would be very beneficial (and time saving).

So far I am using workaround by filling TH1 with variable and weight, then via h->GetBinContent(bin) I get a weighted values which are then saved as a new branch. However, this method is quite inefficient, time consuming and mainly introducing a new bias due to binning. So, is there some more direct way how to apply event-by-event weights to the data and then save this values?

Thank you.

Best regards,

ROOT Version: 6.10/02
Platform: Ubuntu 16.04
Compiler: Not Provided

As the post you are referring to was successfully handle by @dpiparo, he may have an idea about your question. Meanwhile it my be useful to provide a small script showing where the problem exactly is.


thanks a lot for reply.

To illustrate this, I attached four histograms.
D_ETA - original TH1
weight - original weight for all events
D_ETA_weighted_TH1 - D_ETA weighted via h->Fill(“D_ETA”, “weight”)
D_ETA_weighted_multiplication - result of D_ETA after multiplication values and weights event-by-event.
As can be see, the result is absolutely different.

Code used for creating of a new branch, with the values after multiplication, is just a basic tutorial script, also attached.
Min_work_sample.c (526 Bytes)

I presume, the catch is in the multiplication of values and weights (done according to the linked post), for me such a method doesn’t give much sense.

double D_ETA;
double D_ETA_w;
double weight;

TBranch *newb = tree1->Branch("D_ETA_w",&D_ETA_w,"D_ETA_w/D"); 

tree1->SetBranchAddress("weight", &weight);
tree1->SetBranchAddress("D_ETA", &D_ETA);
Long64_t nentries = tree1->GetEntries();
 for(Long64_t i=0; i< nentries; i++){
	 D_ETA_w = D_ETA * weight;


I think what you want is some1Dhistogram->Fill(D_ETA, weight); which should be the same as tree1->Draw("D_ETA", "weight");.

This works, but I am trying to obtain ntuple which corresponds with the values at the weighted histogram, because I need to use this weighted distribution for another step.

You seem to misunderstand the concept of “weights” (the standard default “weight” is always 1.0, unless you explicitly change it when filling histograms).

Note that the “D_ETA” is the x-axis while “weight” is the y-axis so, if you multiply them you get what?

Yes, I fully agree that this doesn’t make any sense.

I just tried to follow the previous topic about similar question (TTree Weighting is different from TH1 weighting), where one of the final responses was:
" 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."

So now I am trying how it should be done.

Thanks for your help.

I think the “multiplication” statement is for cases in which you have multiple weights. You can simply multiply them like “weight1 * weight2 * weight3” and use the resulting value as the “total / global weight” for your event, when filling histograms (note: you could store these “total / global weight” values in a separate tree branch, of course).

Thank you, make sense now.

Unfortunately, this still isn’t a result what i would like to achieve.
During my analysis process I have to apply some weight to my data, and at one step I have to export my data out of ROOT ecosystem due to one tools from theoreticians and then back to ROOT/python. However, at that moment, I already need a weighted data and I still have no idea how to obtain same distribution as when one fill histogram via h->Fill(“variable”, “weight”).

Or am I missing something really basic here?

I’m afraid you will need to teach your “theoreticians” what “event weights” are (can be a very hard job, I know).

BTW. If everything you need later are histograms, you can easily save your filled (“weighted”) histograms (in a ROOT file) and then you do not need to care about your “event weights” any more.

Yes, you are right, that will be the most difficult step…

To be able to work with every event independently is preferable for my type of analysis work, so I just have to find another solution.
Anyway, thank you very much for all your comments and patience!

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