# 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:** 2

<div class="post-metadata">

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

</div>

Instead of entering long lists of commands at the root prompt please make a macro. It is much more convenient.

---

<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:12pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/22 "2020-10-08T14:12:06Z")

</div>

Hi @couet do you mean i should create .mac file of it then generate its root files?

---

<div class="post-metadata">

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

</div>

You should make a `.C` file with all the commands you typed at the root prompt, Let’s say `mymacro.C` and do:

```auto
root [0] .x mymacro.C

```

---

<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 8, 2020, 2:26pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/24 "2020-10-08T14:26:41Z")

</div>

A macro is a .C or a .cxx or a .cpp (or whatever) file with the following content (which also fixes the issue you are seeing). Please note this is not tested!

```auto
void myMacro()
{
	TFile* f1 = TFile::Open("aluminium.root", "READ");
	TH1* h_first = static_cast<TH1>(f1->Get("h1"));
	h_first->SetLineWidth(2);
	h_first->SetLineColor(kBlack);
	h_first->SetMarkerColor(kBlack);
	h_first->SetMarkerStyle(2);
	
	TFile* f2 = TFile::Open("glass.root", "READ");
	TH1* h_second = static_cast<TH1>(f2->Get("h1"));
	h_second->SetLineWidth(2);
	h_second->SetLineColor(kGreen);
	h_second->SetMarkerColor(kGreen);
	h_second->SetMarkerStyle(2);
	
	TFile* f3 = TFile::Open("bakelite.root", "READ");
	TH1* h_third = static_cast<TH1>(f3->Get("h1"));
	h_third->SetLineWidth(2);
	h_third->SetLineColor(kRed);
	h_third->SetMarkerColor(kRed);
	h_third->SetMarkerStyle(2);
	
	h_first->Scale(1/h_first->Integral());
	h_second->Scale(1/h_second->Integral());
	h_third->Scale(1/h_third->Integral());
	
	h_first->Draw("hist");
	h_second->Draw("hist same");
	h_third->Draw("hist same");
	
	TLegend* legend = new TLegend(0.1,0.7,0.48,0.9);
	legend->AddEntry(h_first, "aluminium");
	legend->AddEntry(h_second, "glass");
	legend->AddEntry(h_third, "bakelite");
	legend->Draw();
	
	f1->Close();
	f2->Close();
	f3->Close();

	return;
}

```

---

<div class="post-metadata">

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

</div>

[afzaal.C](https://root-forum.cern.ch/uploads/short-url/nz7zeWyDOCe6GEBF0jNLcgcIaOm.C) (1.1 KB)

```auto
root [0] .x afhaal.C

```

or, at OS prompt:

```auto
$ root afhaal.C

```

---

<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, 3:55pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/26 "2020-10-08T15:55:27Z")

</div>

I have created its macro named afzaal.C. But when i opened it in root terminal error occurred(see screenshot).  
[Screenshot from 2020-10-08 20-51-57|690x387](https://root-forum.cern.ch/uploads/short-url/pQYkJSCbSKmGP3OKIkuuxIGj4i6.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:** [October 8, 2020, 4:39pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/27 "2020-10-08T16:39:47Z")

</div>

You should manually replace all `“` and `”` from Olivier’s file with `"`.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 9, 2020, 7:06am UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/28 "2020-10-09T07:06:14Z")

</div>

Oh yes sorry I copied/pasted from your post in which the quotes were not correct. Here is a version with the correct quotes: [afzaal.C](https://root-forum.cern.ch/uploads/short-url/26znmIKlMUdMIJsMRq6nOM3iaFQ.C) (1016 Bytes)

---

<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 9, 2020, 3:58pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/29 "2020-10-09T15:58:23Z")

</div>

Error!  
[Screenshot from 2020-10-09 20-57-15|690x387](https://root-forum.cern.ch/uploads/short-url/31YylW3phJUF80NQ6cUpdjVWOHk.png)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 12, 2020, 1:24pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/30 "2020-10-12T13:24:38Z")

</div>

Well … you should know where h1 should come form ???  
As I said I only copy/paste the bunch of lines you posted earlier to show you how to make a macro.  
I did not test it. … yes `h1` is not declared … up to you …

---

<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 12, 2020, 4:20pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/31 "2020-10-12T16:20:54Z")

</div>

How can i declare h1?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 13, 2020, 8:13am UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/32 "2020-10-13T08:13:26Z")

</div>

```auto
auto h1 = new TH1D("h1","h1",nbins,xmin,xmax);

```

---

<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 13, 2020, 5:20pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/33 "2020-10-13T17:20:58Z")

</div>

![Screenshot from 2020-10-13 22-17-16](https://root-forum.cern.ch/uploads/default/original/3X/0/2/02b8be0fa33a945461b6ade805114707b56082aa.png)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 14, 2020, 9:17am UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/34 "2020-10-14T09:17:40Z")

</div>

Can you send me the 3 root files your macro is accessing ?

aluminium.root, glass.root and bakelite.root

---

<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 14, 2020, 4:18pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/35 "2020-10-14T16:18:55Z")

</div>

[glass.root](https://root-forum.cern.ch/uploads/short-url/5IDp7jfCGsOXwIb9Vtac0YNghDb.root) (41.5 KB) [aluminium.root](https://root-forum.cern.ch/uploads/short-url/gucDcdMTkqpfoK14XGI7AHN9fw7.root) (42.3 KB) [bakelite.root](https://root-forum.cern.ch/uploads/short-url/mUwQhi6AQ2kuXxMswPcMVkGEgcL.root) (40.6 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 14, 2020, 4:30pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/36 "2020-10-14T16:30:58Z")

</div>

```auto
void afzaal() {
   auto f1 = new TFile("aluminium.root");
   TH1D *h_first = (TH1D*)f1->Get("h1");
   h_first->SetLineWidth(2);
   h_first->SetLineColor(kBlack);
   h_first->SetMarkerColor(kBlack);
   h_first->SetMarkerStyle(2);
   h_first->Draw("hist");

   auto f2 = new TFile("glass.root");
   TH1D* h_second = (TH1D*)f1->Get("h1");
   h_second->SetLineWidth(2);
   h_second->SetLineColor(kRed);
   h_second->SetMarkerColor(kRed);
   h_second->SetMarkerStyle(2);
   h_second->Draw("hist same");

   auto f3 = new TFile("bakelite.root");
   TH1D* h_third = (TH1D*)f1->Get("h1");
   h_third->SetLineWidth(2);
   h_third->SetLineColor(kGreen);
   h_third->SetMarkerColor(kGreen);
   h_third->SetMarkerStyle(2);
   h_third->Draw("hist same");

   auto legend = new TLegend(0.1,0.7,0.48,0.9);
   legend->AddEntry(h_first, "aluminium");
   legend->AddEntry(h_second, "glass");
   legend->AddEntry(h_third, "bakelite");
   legend->Draw();

// Double_t scale=1./h1->Integral();
// h1->Scale(1/h1->Integral());
// h1->Draw("hist same");
}

```

---

<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 14, 2020, 7:22pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/37 "2020-10-14T19:22:40Z")

</div>

@Afzaal786, pretty much the same code (with scaling) was ready last Thursday, 12 posts ago in this very thread: [link](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/24). Did you try running it? Was there any problem with it?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 15, 2020, 8:41am UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/38 "2020-10-15T08:41:54Z")

</div>

I modified a bit the macro [afzaal.C](https://root-forum.cern.ch/uploads/short-url/omJ5v6p6Gp2euCxjEC0qrHyKP6d.C) (811 Bytes)

```auto
% root afzaal.C

```

gives:

 ![Screenshot 2020-10-15 at 10.41.07](https://root-forum.cern.ch/uploads/default/original/3X/9/9/99b9c238b5887dd69f2dc24f83470cd88cc0eb13.jpeg)

---

<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 15, 2020, 3:17pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/39 "2020-10-15T15:17:07Z")

</div>

Thanks @couet for sending me macro afzaal.C file that i renamed to h1.C. But there is an error in direct opening in it(see screenshot). But fine when i copy & paste its commands in root terminal. I want to draw them directly as you did.

 ![Screenshot from 2020-10-15 20-11-14](https://root-forum.cern.ch/uploads/default/original/3X/3/c/3c00803afa96d10a3daa2d0700c977a87e55ed24.png)

---

<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 15, 2020, 3:27pm UTC](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610/40 "2020-10-15T15:27:55Z")

</div>

Thanks @yus. Yes, it is pretty much the same code that you send earlier. But the problem is when i run its macro in root then it undeclare h1 character(see screenshot).However, by coping & pasting its command on root terminal made the required histograms. I wanna draw them directly from root i.e by .x afzaal.C.Thanks

 ![Screenshot from 2020-10-09 20-57-15](https://root-forum.cern.ch/uploads/default/original/3X/1/5/153fb93ecc0e638d566421ac4659379c746fb966.png)

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

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