# Merging histograms from different root files

**URL:** <https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610>\
**Category:** Newbie\
**Created:** [September 26, 2020, 4:27pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610 "2020-09-26T16:27:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Afzaal786](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/afzaal786/32/20527_2.png) [@Afzaal786](https://root-forum.cern.ch/u/Afzaal786)\
**Post date:** [September 26, 2020, 4:27pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/1 "2020-09-26T16:27:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [September 26, 2020, 6:32pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/2 "2020-09-26T18:32:09Z")

</div>

Hi Muhammad,

to do this you should:

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

---

<div class="post-metadata">

**Author:** ![Afzaal786](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/afzaal786/32/20527_2.png) [@Afzaal786](https://root-forum.cern.ch/u/Afzaal786)\
**Post date:** [September 27, 2020, 4:52pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/3 "2020-09-27T16:52:44Z")

</div>

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](https://root-forum.cern.ch/uploads/short-url/3Ar8mLgrZRO5tEBWzOzrZuqEDAn.png)

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [September 27, 2020, 5:17pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/4 "2020-09-27T17:17:34Z")

</div>

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

```auto
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");

```

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [September 27, 2020, 5:17pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/5 "2020-09-27T17:17:34Z")

</div>

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

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

```

Option “same” tells to plot on top of existing ones

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [September 27, 2020, 5:43pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/6 "2020-09-27T17:43:12Z")

</div>

> [@FoxWise](#):
>
> assigning different variable names file1, file2, file3…

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).

---

<div class="post-metadata">

**Author:** ![Afzaal786](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/afzaal786/32/20527_2.png) [@Afzaal786](https://root-forum.cern.ch/u/Afzaal786)\
**Post date:** [September 29, 2020, 5:51pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/7 "2020-09-29T17:51:28Z")

</div>

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](https://root-forum.cern.ch/uploads/short-url/lo6PKGqRSikMBZgkeLTGUDQn4is.png)

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [September 29, 2020, 6:07pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/8 "2020-09-29T18:07:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Afzaal786](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/afzaal786/32/20527_2.png) [@Afzaal786](https://root-forum.cern.ch/u/Afzaal786)\
**Post date:** [September 29, 2020, 6:30pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/9 "2020-09-29T18:30:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [September 29, 2020, 6:48pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/10 "2020-09-29T18:48:22Z")

</div>

Yes, and you’ll have to create pointers for them with different names:

```auto
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"));

```

---

<div class="post-metadata">

**Author:** ![Afzaal786](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/afzaal786/32/20527_2.png) [@Afzaal786](https://root-forum.cern.ch/u/Afzaal786)\
**Post date:** [October 1, 2020, 12:58pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/11 "2020-10-01T12:58:39Z")

</div>

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.

 ![Screenshot from 2020-10-01 17-56-01](https://root-forum.cern.ch/uploads/default/original/3X/8/8/882a7af273f8528d7a32e82b8046257d80702e55.png)

```auto
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();

```

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [October 1, 2020, 2:09pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/12 "2020-10-01T14:09:22Z")

</div>

Hi Muhammad,

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

> [@Afzaal786](#):
>
> ```auto
> root [19] TH1* h_first = static_cast<TH1* >(aluminium->Get("h1"));
> input_line_90:2:22: error: use of undeclared identifier ‘aluminium’
> 
> ```

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

> [@Afzaal786](#):
>
> ```auto
> root [24] legend->AddEntry(h1,"aluminium");
> root [25] legend->AddEntry(h1,"glass");
> root [26] legend->AddEntry(h1,"bakelite");
> 
> ```

You should rather replace `h1` with `h_first`, `h_second`, and `h_third`.  
Finally, don’t do

> [@Afzaal786](#):
>
> ```auto
> root [31] legend->Draw("same");
> 
> ```

– it’s redundant (and wrong) given you do

```auto
root [32] legend->Draw();

```

afterwards.

---

<div class="post-metadata">

**Author:** ![Afzaal786](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/afzaal786/32/20527_2.png) [@Afzaal786](https://root-forum.cern.ch/u/Afzaal786)\
**Post date:** [October 3, 2020, 6:26pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/13 "2020-10-03T18:26:18Z")

</div>

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”);

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [October 3, 2020, 6:56pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/14 "2020-10-03T18:56:22Z")

</div>

Hi Muhammad,

> [@Afzaal786](#):
>
> only one get normalized while other two disappeared.

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:

```auto
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");

```

---

<div class="post-metadata">

**Author:** ![Afzaal786](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/afzaal786/32/20527_2.png) [@Afzaal786](https://root-forum.cern.ch/u/Afzaal786)\
**Post date:** [October 4, 2020, 5:57pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/15 "2020-10-04T17:57:05Z")

</div>

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”);

---

<div class="post-metadata">

**Author:** ![Afzaal786](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/afzaal786/32/20527_2.png) [@Afzaal786](https://root-forum.cern.ch/u/Afzaal786)\
**Post date:** [October 4, 2020, 6:05pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/16 "2020-10-04T18:05:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [October 4, 2020, 6:19pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/17 "2020-10-04T18:19:32Z")

</div>

> [@Afzaal786](#):
>
> Actually the names of three histograms are same

Why? Didn’t you do this:

> [@yus](#):
>
> ```auto
> 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"));
> 
> ```

?

---

<div class="post-metadata">

**Author:** ![Afzaal786](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/afzaal786/32/20527_2.png) [@Afzaal786](https://root-forum.cern.ch/u/Afzaal786)\
**Post date:** [October 4, 2020, 6:39pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/18 "2020-10-04T18:39:08Z")

</div>

I wrote these commands

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [October 4, 2020, 7:00pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/19 "2020-10-04T19:00:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Afzaal786](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/afzaal786/32/20527_2.png) [@Afzaal786](https://root-forum.cern.ch/u/Afzaal786)\
**Post date:** [October 8, 2020, 2:04pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/20 "2020-10-08T14:04:26Z")

</div>

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)

 ![Screenshot from 2020-10-08 18-57-04](https://root-forum.cern.ch/uploads/default/original/3X/1/4/14b236063ec4744aa98a734d1bb3d7684f5b9877.png)

[Next page](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610.md?page=2)
