Merging histograms from different root files

in h1.C you should change afzaal to h1 … Well I did it for you in that file h1.C (807 Bytes)

then:

% root h1.C

will work

aluminium.root (42.3 KB) afzaal.C (1016 Bytes)

Your macro is an old version I fixed already. Use:

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

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

   auto f3 = new TFile("bakelite.root");
   TH1D* h_third = (TH1D*)f3->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","L");
   legend->AddEntry(h_second, "glass", "L");
   legend->AddEntry(h_third, "bakelite");
   legend->Draw();
}

The screenshot with errors you attached in today’s post does not match the afzaal.C you attached in the same post: the

TH1* h_first = static_cast<TH1>(f1->Get("h1"));

line is reported to be on line 3 of your file, while in reality it’s on line 23. You probably took an older version of it.

Also, why do you do

root.exe aluminium.root
root [0] .x afzaal.C

?
Why not do

root.exe
root [0] .x afzaal.C

or even much simpler

root.exe afzaal.C

?

Thanks @yus i tried with root.exe afzaal.C but the error is "error: use of undeclared identifier ‘h1’.

Looks like there is a mistake in the latest afzaal.C:
these lines

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

should really be

   h_first->Scale(1/h_first->Integral());
   h_first->Draw("hist same");

Actually, i am talking about its direct opening from root.exe afzaal.C command.