Bottom TPad Xaxis goes too low

I have 2 TPads that I’m drawing on a TCanvas, but I can’t get the margin parameters right to show the Xaxis Title. I’ve tried loads of different combinations but the logic is escaping me. Please can someone help?

void MWE(){

  TFile* fData = TFile::Open("~/Downloads/Run660_AnalysisOutputFile.root");
  TFile* fMC   = TFile::Open("~/Downloads/MC_Run660_AnalysisOutputFile.root");
  //  TFile* f = TFile::Open("ComparisonPlotsMCData.root","recreate");

  TH1D* hNPoTData = (TH1D*)fData->Get("NPoTAnalysis/NPoTPhys");
  TH1D* hNPoTMC   = (TH1D*)fMC->Get("NPoTAnalysis/NPoT");

  //Question for Mauro: what's this factor of 1.13 doing?
  Double_t  NPoTMC   = (Double_t)hNPoTMC->GetEntries()*hNPoTMC->GetMean();//*1.13;
  Double_t  NPoTData = (Double_t)hNPoTData->GetEntries()*hNPoTData->GetMean(); 

  Double_t scalef = NPoTMC/NPoTData;
  TString name;

  TH1D* hData; 
  TH1D* hMC;

  TH1D* hScaleData; 
  TH1D* hScaleMC;
  TH1D* hRatio;
  
  TCanvas *cData;
  TCanvas *cMC;
  TCanvas *csCompare;
  TCanvas *cRatio;
  TCanvas *cFinal;

  TPad* pad1;
  TPad* pad2;

  TString XTitle;
  TString RatioTitle;
  TString MCTitle;

  Bool_t DrawIntermediate = 0;
  
  hData = (TH1D*)fData->Get("PVetoClusters/hChPVetoCluster");
  hMC   = (TH1D*)fMC->Get("PVetoClusters/hChPVetoCluster");
  name.Form("PVetoOccupancy");
  
  hData->SetStats(0);
  hMC->SetStats(0);
  
  hScaleMC=(TH1D*)hMC->Clone();
  hScaleMC->SetLineColor(kRed);
  hScaleData=(TH1D*)hData->Clone();
  hScaleData->Scale(scalef);

  double ymaxData = hScaleData->GetMaximum();
  double ymaxMC = hScaleMC->GetMaximum();
  
  double xmin = hMC->GetXaxis()->GetXmin();
  double xmax = hMC->GetXaxis()->GetXmax();
  double ymin = hMC->GetYaxis()->GetXmin();
  
  double ymax;
  if(ymaxMC>ymaxData) ymax = ymaxMC;
  else ymax = ymaxData;
  
  hScaleMC->SetMaximum(1.1*ymax); //Sets yaxis maximum
  MCTitle = hScaleMC->GetTitle();
  XTitle = ";"+MCTitle+";";
  hScaleMC->SetTitle(XTitle);
    
  hRatio = (TH1D*)hMC->Clone();
  int nbinx = hRatio->GetNbinsX();
  
  double ratio, erratio;
  for (int ii=1; ii<nbinx+1; ++ii){
    ratio =0.;
    erratio =0.;
    if (fabs(hMC->GetBinContent(ii))>1e-10) {
      ratio = hScaleData->GetBinContent(ii)/hScaleMC->GetBinContent(ii);
      erratio = sqrt(hScaleData->GetBinContent(ii)+hScaleMC->GetBinContent(ii))/hScaleMC->GetBinContent(ii);
    }
    hRatio->SetBinContent(ii, ratio); 
    hRatio->SetBinError(ii, erratio); 
  }

  RatioTitle = ";"+MCTitle+";Data/MC";
  hRatio->SetTitle(RatioTitle);
    
  cFinal = new TCanvas(name+"Final",name+"Final",900,700);
  pad1 = new TPad(name+"up",name+"up",0,0.3,1,1);
  pad1->Draw();                   // Draw the upper pad: pad1
  pad1->cd();                     // pad1 becomes the current pad
  pad1->SetBottomMargin(0.075);
  pad1->SetTopMargin(0.1);
  
  hScaleMC->Draw();
  hScaleData->Draw("sameEP");
  
  cFinal->cd();          // Go back to the main canvas before defining pad2
  pad2 = new TPad(name+"down",name+"down",0,0,1,0.34);
  pad2->SetTopMargin(0.03);
  pad2->SetBottomMargin(4);
  pad2->SetGridy(); // horizontal grid
  pad2->Draw();
  pad2->cd();       // pad2 becomes the current pad
  
  hRatio->Draw("sameEP");

  hRatio->GetXaxis()->SetTitleSize(20);
  hRatio->GetXaxis()->SetTitleFont(43);
  hRatio->GetXaxis()->SetTitleOffset(1.1);
  hRatio->GetXaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
  hRatio->GetXaxis()->SetLabelSize(20);
  
  hScaleMC->GetXaxis()->SetTitleSize(0);
  hScaleMC->GetXaxis()->SetTitleFont(43);
  hScaleMC->GetXaxis()->SetTitleOffset(1.1);
  hScaleMC->GetXaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
  hScaleMC->GetXaxis()->SetLabelSize(0);
  
  hScaleMC->GetYaxis()->SetTitleSize(20);
  hScaleMC->GetYaxis()->SetTitleFont(43);
  hScaleMC->GetYaxis()->SetTitleOffset(1.1);
  hScaleMC->GetYaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
  hScaleMC->GetYaxis()->SetLabelSize(20);
  
  hRatio->GetYaxis()->SetTitleSize(20);
  hRatio->GetYaxis()->SetTitleFont(43);
  hRatio->GetYaxis()->SetTitleOffset(1.1);
  hRatio->GetYaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
  hRatio->GetYaxis()->SetLabelSize(20);
  
  cFinal->SetBottomMargin(0.1);
  cFinal->SetBorderMode(1);
}

1 Like

Try: pad2->SetBottomMargin(0.4); // 0. <= margin < 1.

1 Like

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