# 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:** 1\
**Showing post:** 24

<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;
}

```

---

_[View the full topic](https://root-forum.cern.ch/t/merging-histograms-from-different-root-files/41610)._
