Plotting multiple histograms in same canvas

I have two root files and want to plot the histograms from each one together in the same canvas;
I used this code, but what I get is only the last plot and it is ignore the first plot completely, it didn’t give me any error messages.

TFile *f1 = TFile::Open("160primary.root");
TH1D *h1= (TH1D*)f1->Get("Position_X__cm_");
h1->Draw("hist l");
h1->SetLineWidth(3);
h1->SetLineColor(2);

TFile *f2 = TFile::Open("130primary.root");
TH1D *h2 = (TH1D*)f2->Get("Position_X__cm_");
h2->SetLineWidth(2);
h2->SetLineColor(4);
h2->Draw("hist l same");

any help will be deeply appreciated.

Welcome to the ROOT forum

Weird, that code should produce the two plots on the same canvas as you plot the 2nd one with the option “same”. The range along X and Y axis is defined by the first plot. Can you provide a code we can run ?

thanks for your help
I am trying to upload the file but it refused.

I got a message saying new users can not send links can you advise me how to upload it ?

An other way is to use THStack.

TFile *f1 = TFile::Open("160primary.root");
TH1D *h1= (TH1D*)f1->Get("Position_X__cm_");
h1->SetLineWidth(3);
h1->SetLineColor(2);

TFile *f2 = TFile::Open("130primary.root");
TH1D *h2 = (TH1D*)f2->Get("Position_X__cm_");
h2->SetLineWidth(2);
h2->SetLineColor(4);

auto hs = new THStack();
hs->Add(h1,"HIST L");
hs->Add(h2,"HIST L");
hs->Draw("NOSTACK");

thanks a lot
but it give me this message
*** Break *** segmentation violation

[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)

[] (no debug info)

[] (no debug info)

[/opt/local/libexec/root6/lib/root/libCling.6.24.06.so] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)

[/opt/local/libexec/root6/lib/root/libCling.6.24.06.so] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)

[/opt/l…

Which line gives this error ?

That’s totally un-clear… “TCanvas instead of TH1D” ?
have a look at the documentation…

thanks a lot for your help
I managed to solve the problem