Tree ROOT file

Hi guys,
I was wondering if this piece of code is correct or if I’m missing something.
I have two root files and I retrieve an histogram from them.
Then, I want to save these two histos in a ROOT file with one tree and two branches. Each branch is an histogram.
To do this, I wrote this piece of code:
(histos are called h1 and h2)

//CREATE A TREE TO SAVE BOTH HISTOGRAMS//

//gROOT->cd();
TTree* tree = new TTree(“tree”,“final_tree”);
tree->Branch(“Beam spot_X”,&h1,“h1”);
tree->Branch(“Beam spot_Y”,&h2,“h2”);

//CREATE A ROOT FILE TO SAVE BOTH HISTOGRAMS//

TFile new_file(“/pathto/final_file.root”,“NEW”);
tree->Write();

Alternatively, do you know a more efficient way to draw the two histos one VS the other? My idea is to construct a tree and then:
“tree_name->Draw(“first branch:second branch>>histo(…)”)”).

Cheers


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi,
I am not sure to fully understand your question.
Do you want somehow create a 2D histogram (e.g. TH2D) from two 1D histograms (e.g TH1D)

If this is the case, it isn’t possible.
You could go from one 2D histogram to two 1D histograms, but not the other way.
Passing from 2D to 1D you lost informations that cannot be retrieved.
Look the post below, there is an example explaining why you could not do it

I found this from a ROOT example: tree.Branch(“hpx”,“TH1F”,&hpx,128000,0).

But, what do 128000 and 0 mean?

Hi @Dilicus

Thanks for your reply.

Basically, what I want to do is drawing one histogram on the X axis and the other one on the Y axis.

Anyway, my question is more generic.
It is: if I have two histos and I want to save them in a tree branch, how can I do?

Valeria

Hi this is a small macro to write two histograms in two different branches

void write()
{
    
    
    TFile * f= new TFile("file_histo.root","recreate");
    
    
    
    TH1D *h= new TH1D ("h","histo1",100,-10,10);
    TH1D *h2= new TH1D ("h2","histo2",100,-10,10);
    TTree *tree = new TTree("tree","histo_tree");
    
    
    tree->Branch("histo1",h);
    tree->Branch("histo2",h2);
    
    h->FillRandom("gaus",1000);
    h2->FillRandom("gaus",5000);
    tree->Fill();
    
    
    tree->Write();
    
    f->Close();
    
    
}

Hi @Dilicus ,
thanks for you reply.

In the end, I ended up with the same result with my own macro.
Anyway I ran your macro and I got this:
histos
I got the same result, but I thought it was wrong.

Is it correct?
Valeria

They are all the TH1 method.

If you open the root file and then
declare a histogram
set the branch address you can get the histogram and draw it


of course if you set a branch address also for the other histogram when you call the GetEntry methodyou retrieve also the second histogram

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