Overlaying 2 histograms with different x-axis

Hi,

I want to overlay two histograms containing the distribution of two samples each. I have based myself on the tutorial transpad.C, however the tutorial only accounts for different y-axis, not a different x-axis.
This is the code I’m currently using:

TCanvas *c1 = new TCanvas("c1","transparent pad",200,10,700,500); TPad *pad1 = new TPad("pad1","",0,0,1,1); TPad *pad2 = new TPad("pad2","",0,0,1,1); pad2->SetFillStyle(4000); //will be transparent pad1->Draw(); pad1->cd(); pad1->SetLogy(); EnWeightMc->Draw("H"); EnMc->Draw("][HSAME"); pad1->Update(); c1->cd(); Double_t xmin=0; Double_t xmax=1; Double_t dx = (xmax-xmin)/0.8; Double_t ymax=1*(HadMc->GetMaximum()); Double_t ymin=10; Double_t dy = (ymax-ymin)/0.8; pad2->Range(xmin-0.1*dx,ymin-0.1*dy,xmax+0.1*dx,ymax+0.1*dy); pad2->Draw(); pad2->cd(); pad2->SetLogy(); HadWeightMc->Draw("][HSAME"); HadMc->Draw("][HSAME"); pad2->Update(); TGaxis *axis = new TGaxis(xmax,ymin,xmax,ymax,ymin,ymax,510,"-G="); axis->Draw();

The resulting plot is shown as an attachment. the x-axis shown is the one of pad1. The x-axis of the second pad goes from 0 to 1 and I don’t need it to be shown.

I don’t quite understand where the blue triangle underneath the plot and the red line on the right come from and how to remove them? I’m guessing it has to do with the pad sizes not matching up, but I can’t seem to fix it. Any help would be appreciated! :slight_smile:

regards myrti


Can you post a running version of you macro ?
you sent only a subset

Hi,

Here is a minimal working example:

[code] void test2()
{
TH1F *h1 = new TH1F(“h1”,“h1”,100,0,30000);
TH1F *h2 = new TH1F(“h2”,“h2”,1000,0,1);
TF1 *fa = new TF1(“fa”,“x”,0,30000);
TF1 *fb = new TF1(“fb”,“gaus(0)”,0,1);
fb->SetParameters(100,0.5,10);
h2->FillRandom(“fb”,100000);
h1->FillRandom(“fa”,100000);
h1->SetLineColor(kBlue);
h2->SetLineColor(kRed);

h1->SetStats(0);
h2->SetStats(0);
TCanvas c1 = new TCanvas(“c1”,“transparent pad”,200,10,700,500);
TPad pad1 = new TPad(“pad1”,"",0,0,1,1);
TPad pad2 = new TPad(“pad2”,"",0,0,1,1);
pad2->SetFillStyle(4000); //will be transparent
pad1->Draw();
pad1->cd();
pad1->SetLogy();
h1->Draw(“H”);
pad1->Update();
c1->cd();
Double_t xmin=0;
Double_t xmax=1;
Double_t dx = (xmax-xmin)/0.8;
Double_t ymax=1
(h2->GetMaximum());
Double_t ymin=0;
Double_t dy = (ymax-ymin)/0.8;
pad2->Range(xmin-0.1
dx,ymin-0.1
dy,xmax+0.1dx,ymax+0.1dy);
pad2->Draw();
pad2->cd();
//pad2->SetLogy();
h2->Draw("][HSAME");
pad2->Update();
TGaxis *axis = new TGaxis(xmax,ymin,xmax,ymax,ymin,ymax,510,"-L=");
axis->Draw();
}[/code]

I had to modify the code, as I’m reading the values shown in the histograms from data and I can’t upload the data itself due to size constraints.
I have attached the screenshot correpsonding to the issue again, the triangle is still present, but red this time.

Thanks for looking into this :slight_smile:

regards myrti


1 Like

With your macro on Mac using ROOT 5.34/04 I do not see the problem

Hi,

I upgraded to 5.34 and no longer have that triangle either. :slight_smile: Therefore the problem is solved for me. Thanks!

I’m not sure if and how I should mark this thread as solved?

regards myrti

For those discovering this down the road who want to align their pads, here is the general case for overlaying two pads containing different histograms with the same y axis but different x axes:

x1l, yl, x1h, yh = array('d', [0]), array('d', [0]), array('d', [0]), array('d', [0])
pad1.GetRange(x1l, yl, x1h, yh)
pad2.Range(
    ((x2max - x2min) * x1l[0] + x2min * x1max - x2max * x1min) / (x1max - x1min),
    yl[0],
    ((x2max - x2min) * x1h[0] + x2min * x1max - x2max * x1min) / (x1max - x1min),
    yh[0]
)

ximin and ximax represent the limits of the drawn x-axes for each histogram. The calculations for the pad’s minimum and maximum are necessary to translate from the known ranges of x1 and x2 to the edges of the pad, regardless of margin settings, etc., without relying on the default ROOT values, as the previous example does.