- #1
ChrisVer
Gold Member
- 3,378
- 465
Suppose I have a histogram : [itex]h(x)[/itex]
and I want to take the different values [itex]h(x_i)[/itex] as weights which I can pass in a tree...
The code I wrote so far, which (theoriticially) would do this job for me looks like this:
my problem then is that I am not sure if my logic is plausible, I can't think of any other way to do what I am trying to.
So for example if the histo's binwidth is [itex]50 ~\text{GeV}[/itex] and the value x of the iEntry is [itex]x_{i}[/itex] and lies between [itex]250 < x_i < 300[/itex], then the value of iEntry-th value of the weight should be the value of the histogram [itex]h(x)[/itex] at the bin# 6.
Then again I am not sure how I can pass/Fill a new tree with the w's... (afterwards I'll try to make the tree friends, and so it will be equivalent to adding safely a new Branch)
and I want to take the different values [itex]h(x_i)[/itex] as weights which I can pass in a tree...
The code I wrote so far, which (theoriticially) would do this job for me looks like this:
C:
TFile* fin = new TFile("file_contains_tree_and_histo.root");
TH1F* h_x = (TH1F*) fin->Get("histo_X");
TTree* myTree = (TTree*) fin->Get("Tree");
Float_t x, w;
float binwidth= h->GetXaxis()->GetBinWidth(1);
myTree->SetBranchAddress("variable_x", &x)
Long64_t N_entries = myTree->GetEntries();
for( int iEntry=0; iEntry<N_entries ; iEntry++){
myTree->GetEntry(iEntry); //Get entry i from the existing tree
int j=0;
while(j<100){
if(x> j *binwidth && x<(j+1)*binwidth){
//checks if value x is in the range of bin j+1,
//if yes then the weight is the value of the histogram
//at that value or bin.
w= h->GetBinContent(j+1);
break; //leave while
}
j++;
}
// Here I need help to pass w in a new tree
}
my problem then is that I am not sure if my logic is plausible, I can't think of any other way to do what I am trying to.
So for example if the histo's binwidth is [itex]50 ~\text{GeV}[/itex] and the value x of the iEntry is [itex]x_{i}[/itex] and lies between [itex]250 < x_i < 300[/itex], then the value of iEntry-th value of the weight should be the value of the histogram [itex]h(x)[/itex] at the bin# 6.
Then again I am not sure how I can pass/Fill a new tree with the w's... (afterwards I'll try to make the tree friends, and so it will be equivalent to adding safely a new Branch)