Problem with creating a Tree from another+histogram

  • Thread starter ChrisVer
  • Start date
  • Tags
    Tree
In summary, when trying to associate an extra variable from a histogram to trees, you can use the GetBinConcent method to obtain the corresponding bin in the histogram and assign it as the weight for a new tree. It is important to use the correct data type when filling the tree, as it can affect the result significantly.
  • #1
ChrisVer
Gold Member
3,378
464
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:
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)
 
Technology news on Phys.org
  • #2
Where is the point of the while loop? x/binwidth, rounded down, will do the job for equal bin sizes starting at zero.
Even better, you can use ROOT to directly get the right bin:
TAxis *xaxis = h->GetXaxis();
Int_t binx = xaxis->FindBin(x);
Should also work in one line:
h->GetXaxis()->FindBin(x);

is h=h_x? More descriptive variable names would help.

You have to define a branch and add it to the tree, afterwards you can use Fill() in the loop as usual.
Example code
 
  • Like
Likes ChrisVer and Silicon Waffle
  • #3
mfb said:
is h=h_x? More descriptive variable names would help.
yes sorry... The point to what I'm trying to do is associate an extra variable I was able to determine from a histogram (QCD fake factors) to trees I obtained from running the derivations on some QCD datasets. The variable x for example is the pT, since my fake factors are given from a histogram h_FakeFactor(pT).
So the while etc I used because I wanted to tell "if the QCD object has pT=100GeV, go and take the fake factor from the histogram FF= h_FakeFactor(pT=100GeV)".
I didn't know/thought of the FindBin method, thanks.
So I guess it should be something like this:
C:
w= h -> GetBinContent(   h->GetXaxis()->FindBin(x)   )
mfb said:
You have to define a branch and add it to the tree, afterwards you can use Fill() in the loop as usual.
That's what I am trying to understand too. So for example I should have defined a new tree:
C:
TTree*  tree_friend = new TTree("T_output","weight");
TBranch* branch_weight = tree_friend->Branch("weight", &w , "w/F");

and in the end of the for loop do:
C:
branch_weight->Fill();
?
 
Last edited:
  • #4
You can make a new tree, but then you should clone the old one to keep all the other branches in. It is easier to update the existing one, see the example I linked to.
The downside of the update method: it appears as two trees in a TBrowser afterwards, for reasons I don't understand.
 
  • Like
Likes ChrisVer
  • #5
mfb said:
It is easier to update the existing one, see the example I linked to.

Yes I've seen that example of "immediately adding branches to a tree". However after some small discussion I was informed that it's quiet risky to try and overwrite my existing trees from the derivation and it can be very heavy for the available space [I think?]... So I think the safest way would be to proceed with friend trees [that's why I'm trying to build a separate 2nd tree]...Afterwards the treatment of the two trees will be identical and I'll be able to call from my original tree [the one I'm working with] the variable that I created in the other.

mfb said:
but then you should clone the old one to keep all the other branches in
I didn't understand this one?
 
  • #6
Ah, friend trees. Then you do not have to clone anything. Just create a new tree with a single branch then. Cloning would create a new tree that has all the old stuff plus your new variable. More handy than friend trees, but if you process TB of data a friend tree is probably better.
 
  • #7
I am not sure, but there seems to be a problem when I try to use the TTree::Fill() method.
C:
Double_t weight, pT;
Int_t ID;

//DEFINE THE INPUT STUFF
TFile* f_input = new TFile("sum.root");
TTree*  myTree=(TTree*) f_input->Get("T");
myTree->SetBranchAddress(  "pt",&pT);
myTree->SetBranchAddress( "ID", &ID );

TH1* h_Weights_vs_pT = (TH1*) f_input->Get("h_FakeFactor");

//MAKE THE TREE TO FILL
TFile* f_output = new TFile("weightOut.root","recreate");
TTree* myTree_friend = new TTree("weight","weight");
TBranch* myBranch = myTree_friend->Branch("weight",&weight,"weight/F");

//MAKE A HISTOGRAM TO FILL TO COMPARE
TH1F* h_test = new TH1F("h2","h2",100,0.,0.4);

for(Int_t iEntry=0; iEntry< myTree->GetEntries() ; iEntry++){
    myTree->GetEntry(iEntry);
    if(ID==0)  weight= h_Weights_vs_pT ->GetBinContent( h_Weights_vs_pT ->GetXaxis()->FindBin(pT) );
    else weight=0.0;
    h_test->Fill(FF);
    myTree_friend->Fill();
  }

h_test->Write();
f_output->Write();
f_output->Close();

The result I get is shown in the pdfs : 1 the histogram's fill h_test which looks reasonable but the tree's in 2 is very off (even checking the mean ~e+35)...
 

Attachments

  • 1.pdf
    14.1 KB · Views: 202
  • 2.pdf
    13.9 KB · Views: 179
  • #8
What is FF?

You define weight as double but fill it as float. That does not work. It is one of the rare cases where the difference really matters.
 
  • Like
Likes ChrisVer

1. How can I create a tree from another histogram?

To create a tree from another histogram, you can use a data structure called a binary tree. This tree will store the values from the histogram in a hierarchical structure, with each node having two child nodes. The root node will represent the entire histogram, and its child nodes will represent the values on the left and right side of the histogram. By traversing this tree, you can recreate the histogram.

2. What are the challenges in creating a tree from another histogram?

The main challenge in creating a tree from another histogram is determining the correct structure of the tree. The values of the histogram will need to be appropriately organized in the tree, and the tree must also be balanced to ensure efficient traversal. Additionally, the tree must be able to handle any potential outliers or irregularities in the histogram data.

3. How can I ensure the accuracy of the tree created from another histogram?

To ensure the accuracy of the tree created from another histogram, it is essential to test the tree with different types of histogram data. This will help identify any potential flaws in the tree structure and allow for adjustments to be made. Additionally, it is crucial to verify that the tree can correctly recreate the original histogram data through traversing and analyzing the tree.

4. Can the tree created from another histogram be used for data analysis?

Yes, the tree created from another histogram can be used for data analysis. The hierarchical structure of the tree allows for efficient searching and sorting of the histogram data. Additionally, by analyzing the tree, you can identify patterns and trends within the histogram data that may not be apparent from just looking at the histogram itself.

5. Are there any alternative methods to creating a tree from another histogram?

Yes, there are alternative methods to creating a tree from another histogram. One alternative is to use a data structure called a hash table, which uses key-value pairs to store and organize data. Another option is to use a graph data structure, which can also represent hierarchical relationships between data. However, a binary tree is often the most efficient and straightforward method for creating a tree from a histogram.

Similar threads

  • Programming and Computer Science
Replies
7
Views
3K
  • High Energy, Nuclear, Particle Physics
Replies
3
Views
2K
  • Atomic and Condensed Matter
Replies
3
Views
859
  • Programming and Computer Science
Replies
1
Views
5K
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Math Proof Training and Practice
6
Replies
175
Views
20K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Advanced Physics Homework Help
Replies
2
Views
2K
  • Math Proof Training and Practice
2
Replies
43
Views
9K
Back
Top