I have several runs (root files), and I need to plot some variable (for each run) and apply a cut and a weight at the same time. Each run has a different weighting factor. So I have:
TTree *t[n]; //n : number of runs
TCut icut = “a>0”;
Double_t weights[ ] = {w1, w2, …,wn};
when I try:
t[i] ->Draw(“h”,(icut)*“weights[i]”,""); // i = 0,…,n
it does not work. However, when I replace it with a number say:
t[i] ->Draw(“h”,(icut)*“0.8”,"");
it works.
Is there away to be able to read the weights from an array and apply it in each run?
The variable you are using, weights[i], is a double, but you are using it inside of a string, and so it is being interpreted literally as “weights[i]”. The best way I’ve found to use a variable inside a cut is to use the Form() function, explained in the documentation here.
So for your example you could do the following:
t[i]->Draw("h",(icut)*Form("%f",weights[i]),"");
Or to keep it all in one string, you could do
t[i]->Draw("h",Form("%f*%s",weights[i],icut),"");
Or, if your cut is constant across all your files, you could even do