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;
}
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)
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?
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.
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