Combining two histograms

Hi all,

I still learning root, so I have two histograms generated by different .C files so how can I combine them in
a single plot, for example from terminal …

Regards,
Safinaz

[quote=“Safinaz”]Hi all,

I still learning root, so I have two histograms generated by different .C files so how can I combine them in
a single plot, for example from terminal …

Regards,
Safinaz[/quote]

Get the histogram objects (or pointers to them), then make a new canvas and draw them on the same one. Assuming hist1.C and hist2.C generate TH1D histograms with the names hist1 and hist2 from different files…

.x hist1.C
auto h1 = (TH1D*)gDirectory->Get("hist1");
.x hist2.C
auto h2 = (TH1D*)gDirectory->Get("hist2");
auto c = new TCanvas("MyCanvas", "Two Histograms", 800, 800);
h1->Draw();
h2->Draw("same");

They key piece here for you is the “same” option when calling TH(1,2,3)::Draw(), which puts a histogram onto the same, currently-selected TCanvas.

Hi,

Why does it say,
Error: illegal pointer to class object h1 0x0 1101 (tmpfile):1:

when I type
h1->Draw();

I just saved my histograms in hist1.C and hist2.C .

S.

Hi,

It works now…many thanks.
S. :slight_smile:

You can also put the histograms into a THStack.

hs->Draw(“nostack”) ;

will compute for the right scale allowing to show the 2 histograms.