Merging histograms from different root files

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)

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

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

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

root [0] .x mymacro.C

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!

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

afzaal.C (1.1 KB)

root [0] .x afhaal.C

or, at OS prompt:

$ root afhaal.C

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

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

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 (1016 Bytes)

Error!
Screenshot from 2020-10-09 20-57-15|690x387

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 …

How can i declare h1?

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

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

aluminium.root, glass.root and bakelite.root

glass.root (41.5 KB) aluminium.root (42.3 KB) bakelite.root (40.6 KB)

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

@Afzaal786, pretty much the same code (with scaling) was ready last Thursday, 12 posts ago in this very thread: link. Did you try running it? Was there any problem with it?

I modified a bit the macro afzaal.C (811 Bytes)

% root afzaal.C

gives: