Ratio of histograms in two pads - x axis not alinged


_ROOT Version:6.12/04
_Platform:Fedora 27


Dear co-rooters,

I am trying to do a simple task

  1. Draw 2 histograms that I get from a root file in one pad
  2. Draw their ratio on a second pad

My code is the following

void ratioplot() {
    new TFile("flux.root");

   // Define the Canvas
   TCanvas *c = new TCanvas("c", "canvas", 800, 800);

   // Upper plot will be in pad1
   TPad *pad1 = new TPad("pad1", "pad1", 0, 0.3, 1, 1.0);
   pad1->SetBottomMargin(0); // Upper and lower plot are joined
   pad1->SetGridx();         // Vertical grid
   pad1->Draw();             // Draw the upper pad: pad1
   pad1->cd();               // pad1 becomes the current pad
   hFlux_eval_ear2_2014_2015_100bpd->Draw("histo");
   hFlux_eval_ear1_2014_100bpd->Draw("histosame");

   // Do not draw the Y axis label on the upper plot and redraw a small
   // axis instead, in order to avoid the first label (0) to be clipped.
   //hFlux_eval_ear2_2014_2015_100bpd->GetYaxis()->SetLabelSize(0.);
   TGaxis *axis = new TGaxis( -5, 20, -5, 220, 20,220,510,"");
   axis->SetLabelFont(43); // Absolute font size in pixel (precision 3)
   axis->SetLabelSize(15);
   axis->Draw();

   // lower plot will be in pad
   c->cd();          // Go back to the main canvas before defining pad2
   TPad *pad2 = new TPad("pad2", "pad2", 0, 0.05, 1, 0.3);
   pad2->SetTopMargin(0);
   pad2->SetBottomMargin(0.2);
   pad2->SetGridx(); // vertical grid
   pad2->Draw();
   pad2->cd();       // pad2 becomes the current pad

   // Define the ratio plot
   TH1F *hratio = (TH1F*)hFlux_eval_ear1_2014_100bpd->Clone("hratio");
   hratio->SetLineColor(kBlack);
   //hratio->SetMinimum(0.8);  // Define Y ..
   //hratio->SetMaximum(1.35); // .. range
   //hratio->Sumw2();
   hratio->SetStats(0);      // No statistics on lower plot
   hratio->Divide(hFlux_eval_ear2_2014_2015_100bpd);
   hratio->SetMarkerStyle(21);
   hratio->Draw("histo");       // Draw the ratio plot

   // Y axis h1 plot settings
   hFlux_eval_ear2_2014_2015_100bpd->GetYaxis()->SetTitleSize(20);
   hFlux_eval_ear2_2014_2015_100bpd->GetYaxis()->SetTitleFont(43);
   hFlux_eval_ear2_2014_2015_100bpd->GetYaxis()->SetTitleOffset(1.55);

   // h2 settings
   hFlux_eval_ear1_2014_100bpd->SetLineColor(kRed);
   hFlux_eval_ear1_2014_100bpd->SetLineWidth(2);

   // Ratio plot (hratio) settings
   hratio->SetTitle(""); // Remove the ratio title

   // Y axis ratio plot settings
   hratio->GetYaxis()->SetTitle("ratio EAR1/EAR2");
   hratio->GetYaxis()->SetNdivisions(505);
   hratio->GetYaxis()->SetTitleSize(20);
   hratio->GetYaxis()->SetTitleFont(43);
   hratio->GetYaxis()->SetTitleOffset(1.55);
   hratio->GetYaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
   hratio->GetYaxis()->SetLabelSize(15);

   // X axis ratio plot settings
   hratio->GetXaxis()->SetTitleSize(20);
   hratio->GetXaxis()->SetTitleFont(43);
   hratio->GetXaxis()->SetTitleOffset(4.);
   hratio->GetXaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
   hratio->GetXaxis()->SetLabelSize(15);
   
}

What I get though is the following

So you will notice that the x-axes are not equally aligned. Any idea on how to solve that?

Thanks in advance!

flux.root (53.1 KB)

I would suggest you look at the TRatioPlot class.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.