Merging histograms from different root files

Hi, i have three different root files but wanna draw their h1 histograms on a single pad (merge three different root files histograms). How can i do that?
Any kind of help meant me a lot.
Thanks

Hi Muhammad,

to do this you should:

  • Open your ROOT file
  • Grab your histogram
  • Draw it
  • Repeat for all ROOT files you have

Thanks @yus for your response.
I have draw h1 histogram from one root file(see attachment). But to draw same h1 histogram from other root file in the same canvas is challenging.!
Screenshot from 2020-09-27 21-45-05|690x387

If you want to use ROOT CLI, Iā€™d suggest something along these lines:

root -l 
root [0] TFile *f1 = TFile::Open("myfile01.root", "READ");
root [1] h1->SetLineColor(kBlack);
root [2] h1->Draw("hist");
root [3] TFile *f2 = TFile::Open("myfile02.root", "READ");
root [4] h1->SetLineColor(kRed);
root [5] h1->Draw("hist same");
root [6] TFile *f3 = TFile::Open("myfile03.root", "READ");
root [7] h1->SetLineColor(kBlue);
root [8] h1->Draw("hist same");
1 Like

This line opens file1.root
TFile* file1 = new TFile("file1.root", "READ");

This line grabs histogram with a name h_name from your file
TH1F* h1 = (TH1F*)file1->Get("h_name");

You should do this for all your 3 files assigning different variable names file1, file2, file3ā€¦

Then you can plot 3 histo together with

h1->Draw("histo");
h2->Draw("histo same");
h3->Draw("histo same");

Option ā€œsameā€ tells to plot on top of existing ones

1 Like

This is not necessary. Even if the histogram has the same name in all three files, itā€™s okay not to rename it as long as we draw it (i.e. we are done working with it) before jumping to the next file (see my example above).

2 Likes

Thank you @yus it really works well.
But now when i used TLegends commands to set legends for each histogram i.e which color & material represent which histogram then same color was set in legends for each histogram(see attachment). I wanna set in legends Black color for aluminium Red for glass & Green for bakelite.
Thank you for your consideration.!
Screenshot from 2020-09-29 22-41-47|690x387

In this case youā€™ll have to write a macro just as @FoxWise suggested, that is youā€™ll have to name all your histograms differently. This is pretty complicated to achieve just using the command line - there are too many lines of code.

Variables/files names are different but the histograms within them has same name (h1).

Yes, and youā€™ll have to create pointers for them with different names:

TH1* h_first = static_cast<TH1*>(file1->Get("h1"));
TH1* h_second = static_cast<TH1*>(file2->Get("h1"));
TH1* h_third = static_cast<TH1*>(file3->Get("h1"));

Thanks @yus i tried. But think i am doing mistake. Please check my program. The. output is still same(same color to all in legends)see attachment.

root [1] TFile *f1 = TFile::Open("aluminium.root", "READ");
root [2] h1->SetLineWidth(2);
root [3] h1->SetLineColor(kBlack);
root [4] h1->SetMarkerColor(kBlack);
root [5] h1->SetMarkerStyle(2);
root [6] h1->Draw("hist");
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [7] TFile *f2 = TFile::Open("glass.root", "READ");
root [8] h1->SetLineWidth(2);
root [9] h1->SetLineColor(kRed);
root [10] h1->SetMarkerColor(kRed);
root [11] h1->SetMarkerStyle(2);
root [12] h1->Draw("hist same");
root [13] TFile *f3 = TFile::Open("bakelite.root", "READ");
root [14] h1->SetLineWidth(2);
root [15] h1->SetLineColor(kGreen);
root [16] h1->SetMarkerColor(kGreen);
root [17] h1->SetMarkerStyle(2);
root [18] h1->Draw("hist same");
root [19] TH1* h_first = static_cast<TH1*>(aluminium->Get("h1"));
input_line_90:2:22: error: use of undeclared identifier 'aluminium'
 (static_cast<TH1 *>(aluminium->Get("h1")))
                     ^
Error in <HandleInterpreterException>: Error evaluating expression (static_cast<TH1 *>(aluminium->Get("h1"))).
Execution of your code was aborted.
root [20] TH1* h_first = static_cast<TH1*>(aluminium.root->Get("h1"));
ROOT_prompt_20:1:6: error: redefinition of 'h_first'
TH1* h_first = static_cast<TH1*>(aluminium.root->Get("h1"));
     ^
ROOT_prompt_19:1:6: note: previous definition is here
TH1* h_first = static_cast<TH1*>(aluminium->Get("h1"));
     ^
root [21] TH1* h_second = static_cast<TH1*>(glass.root->Get("h1"));
input_line_93:2:22: error: use of undeclared identifier 'glass'
 (static_cast<TH1 *>(glass.root->Get("h1")))
                     ^
Error in <HandleInterpreterException>: Error evaluating expression (static_cast<TH1 *>(glass.root->Get("h1"))).
Execution of your code was aborted.
root [22] TH1* h_third = static_cast<TH1*>(bakelite.root->Get("h1"));
input_line_95:2:22: error: use of undeclared identifier 'bakelite'
 (static_cast<TH1 *>(bakelite.root->Get("h1")))
                     ^
Error in <HandleInterpreterException>: Error evaluating expression (static_cast<TH1 *>(bakelite.root->Get("h1"))).
Execution of your code was aborted.
root [23] auto legend = new TLegend(0.1,0.7,0.48,0.9);
root [24] legend->AddEntry(h1,"aluminium");
root [25] legend->AddEntry(h1,"glass");
root [26] legend->AddEntry(h1,"bakelite");
root [27] legend->Draw();
root [28] TH1* h_first = static_cast<TH1*>(aluminium.root->Get("h1"));
ROOT_prompt_28:1:6: error: redefinition of 'h_first'
TH1* h_first = static_cast<TH1*>(aluminium.root->Get("h1"));
     ^
ROOT_prompt_19:1:6: note: previous definition is here
TH1* h_first = static_cast<TH1*>(aluminium->Get("h1"));
     ^
root [29] TH1* h_second = static_cast<TH1*>(glass.root->Get("h1"));
ROOT_prompt_29:1:6: error: redefinition of 'h_second'
TH1* h_second = static_cast<TH1*>(glass.root->Get("h1"));
     ^
ROOT_prompt_21:1:6: note: previous definition is here
TH1* h_second = static_cast<TH1*>(glass.root->Get("h1"));
     ^
root [30] TH1* h_third = static_cast<TH1*>(bakelite.root->Get("h1"));
ROOT_prompt_30:1:6: error: redefinition of 'h_third'
TH1* h_third = static_cast<TH1*>(bakelite.root->Get("h1"));
     ^
ROOT_prompt_22:1:6: note: previous definition is here
TH1* h_third = static_cast<TH1*>(bakelite.root->Get("h1"));
     ^
root [31] legend->Draw("same");
root [32] legend->Draw();

Hi Muhammad,

first, I strongly suggest to write a macro instead of putting the commands one by one into the CLI.
Second, concerning

Itā€™s not aluminium, itā€™s f1. And itā€™s not glass.root or bakelite.root, itā€™s f2 and f3, respectively.
Third, you canā€™t do

You should rather replace h1 with h_first, h_second, and h_third.
Finally, donā€™t do

ā€“ itā€™s redundant (and wrong) given you do

root [32] legend->Draw();

afterwards.

1 Like

Thank you very much @yus it has solved my problem.
One thing i wanna normalize these three histograms to unity. When i entered at the end following commands to normalize these three histograms from different root files to unity then only one get normalized while other two disappeared.
Double_t norm_factor = h3->GetEntries();
h3->Scale(1/norm_factor);
h3->Draw(ā€œhistā€);

Hi Muhammad,

yes, because you drew it with "hist" and not "hist same". Also, Iā€™d suggest to normalize by the integral instead of number of entries. In the end, you should have something like this:

h1->Scale(1/h1->Integral());
h2->Scale(1/h2->Integral());
h3->Scale(1/h3->Integral());

h1->Draw("hist");
h2->Draw("hist same");
h3->Draw("hist same");

Actually the names of three histograms are same i.e h2 so what are the commands to normalized these three h2ā€™s to unity?
I tried with
Double_t scale=1./h2->Integral();
h2->Scale(1/h2->Integral());
h2->Draw(ā€œhistā€);

But it normalized only one to unity while the other two disappeared. I also tried with
h2->Draw(ā€œhist sameā€); at the end.

Why? Didnā€™t you do this:

?

I wrote these commands

Iā€™m a bit lost now. Can you please show your code you have so far?

Yes, here it is, and attached is the output where only one histo get normalized while the other two disappeared.
root [1] TFile f1 = TFile::Open(ā€œaluminium.rootā€, ā€œREADā€);
root [2] h1->SetLineWidth(2);
root [3] h1->SetLineColor(kBlack);
root [4] h1->SetMarkerColor(kBlack);
root [5] h1->SetMarkerStyle(2);
root [6] h1->Draw(ā€œhistā€);
Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1
root [7] TFile f2 = TFile::Open(ā€œglass.rootā€, ā€œREADā€);
root [8] h1->SetLineWidth(2);
root [9] h1->SetLineColor(kRed);
root [10] h1->SetMarkerColor(kRed);
root [11] h1->SetMarkerStyle(2);
root [12] h1->Draw(ā€œhist sameā€);
root [13] TFile f3 = TFile::Open(ā€œbakelite.rootā€, ā€œREADā€);
root [14] h1->SetLineWidth(2);
root [15] h1->SetLineColor(kGreen);
root [16] h1->SetMarkerColor(kGreen);
root [17] h1->SetMarkerStyle(2);
root [18] h1->Draw(ā€œhist sameā€);
root [19] TH1
h_first = static_cast<TH1
>(f1->Get(ā€œh1ā€));
root [20] TH1
h_second = static_cast<TH1*>(f2->Get(ā€œh1ā€));
root [21] TH1* h_third = static_cast<TH1*>(f3->Get(ā€œh1ā€));
root [22] auto legend = new TLegend(0.1,0.7,0.48,0.9);
root [23] legend->AddEntry(h_first, ā€œaluminiumā€);
root [24] legend->AddEntry(h_second, ā€œglassā€);
root [25] legend->AddEntry(h_third, ā€œbakeliteā€);
root [26] legend->Draw();
root [27] Double_t scale=1./h1->Integral();
root [28] h1->Scale(1/h1->Integral());
root [29] h1->Draw(ā€œhist sameā€);
root [30] h1->Draw(ā€œhistā€);
Moreover when i drag legend box from top left to top right it also disappeared(see screenshot)