ROOT logo

From $ROOTSYS/tutorials/hist/ratioplot.C

// Example displaying two histograms and their ratio.
// Author: Olivier Couet
{
   // Define two gaussian histograms. Note the X and Y title are defined
   // at booking time using the convention "Hist_title ; X_title ; Y_title"
   TH1F *h1 = new TH1F("h1", "Two gaussian plots and their ratio;x title; h1 and h2 gaussian histograms", 100, -5, 5);
   TH1F *h2 = new TH1F("h2", "h2", 100, -5, 5);
   h1->FillRandom("gaus");
   h2->FillRandom("gaus");

   // 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
   h1->SetStats(0);          // No statistics on upper plot
   h1->Draw();               // Draw h1
   h2->Draw("same");         // Draw h2 on top of h1

   // 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.
   h1->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 *h3 = (TH1F*)h1->Clone("h3");
   h3->SetLineColor(kBlack);
   h3->SetMinimum(0.8);  // Define Y ..
   h3->SetMaximum(1.35); // .. range
   h3->Sumw2();
   h3->SetStats(0);      // No statistics on lower plot
   h3->Divide(h2);
   h3->SetMarkerStyle(21);
   h3->Draw("ep");       // Draw the ratio plot

   // h1 settings
   h1->SetLineColor(kBlue+1);
   h1->SetLineWidth(2);

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

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

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

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

   // X axis ratio plot settings
   h3->GetXaxis()->SetTitleSize(20);
   h3->GetXaxis()->SetTitleFont(43);
   h3->GetXaxis()->SetTitleOffset(4.);
   h3->GetXaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
   h3->GetXaxis()->SetLabelSize(15);
 ratioplot.C:1
 ratioplot.C:2
 ratioplot.C:3
 ratioplot.C:4
 ratioplot.C:5
 ratioplot.C:6
 ratioplot.C:7
 ratioplot.C:8
 ratioplot.C:9
 ratioplot.C:10
 ratioplot.C:11
 ratioplot.C:12
 ratioplot.C:13
 ratioplot.C:14
 ratioplot.C:15
 ratioplot.C:16
 ratioplot.C:17
 ratioplot.C:18
 ratioplot.C:19
 ratioplot.C:20
 ratioplot.C:21
 ratioplot.C:22
 ratioplot.C:23
 ratioplot.C:24
 ratioplot.C:25
 ratioplot.C:26
 ratioplot.C:27
 ratioplot.C:28
 ratioplot.C:29
 ratioplot.C:30
 ratioplot.C:31
 ratioplot.C:32
 ratioplot.C:33
 ratioplot.C:34
 ratioplot.C:35
 ratioplot.C:36
 ratioplot.C:37
 ratioplot.C:38
 ratioplot.C:39
 ratioplot.C:40
 ratioplot.C:41
 ratioplot.C:42
 ratioplot.C:43
 ratioplot.C:44
 ratioplot.C:45
 ratioplot.C:46
 ratioplot.C:47
 ratioplot.C:48
 ratioplot.C:49
 ratioplot.C:50
 ratioplot.C:51
 ratioplot.C:52
 ratioplot.C:53
 ratioplot.C:54
 ratioplot.C:55
 ratioplot.C:56
 ratioplot.C:57
 ratioplot.C:58
 ratioplot.C:59
 ratioplot.C:60
 ratioplot.C:61
 ratioplot.C:62
 ratioplot.C:63
 ratioplot.C:64
 ratioplot.C:65
 ratioplot.C:66
 ratioplot.C:67
 ratioplot.C:68
 ratioplot.C:69
 ratioplot.C:70
 ratioplot.C:71
 ratioplot.C:72
 ratioplot.C:73
 ratioplot.C:74
 ratioplot.C:75
 ratioplot.C:76
 ratioplot.C:77
 ratioplot.C:78
 ratioplot.C:79
 ratioplot.C:80
 ratioplot.C:81
 ratioplot.C:82
 ratioplot.C:83