Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
ratioplotOld.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// Example displaying two histograms and their ratio. This macro does not use the
4 /// class TRatioPlot. For ROOT version >= 6.08 TRatioPlot should be used. See
5 /// the other ratio plots examples in this folder.
6 ///
7 /// \macro_image
8 /// \macro_code
9 ///
10 /// \author Olivier Couet
11 
12 void ratioplotOld( ) {
13  // Define two gaussian histograms. Note the X and Y title are defined
14  // at booking time using the convention "Hist_title ; X_title ; Y_title"
15  TH1F *h1 = new TH1F("h1", "Two gaussian plots and their ratio;x title; h1 and h2 gaussian histograms", 100, -5, 5);
16  TH1F *h2 = new TH1F("h2", "h2", 100, -5, 5);
17  h1->FillRandom("gaus");
18  h2->FillRandom("gaus");
19 
20  // Define the Canvas
21  TCanvas *c = new TCanvas("c", "canvas", 800, 800);
22 
23  // Upper plot will be in pad1
24  TPad *pad1 = new TPad("pad1", "pad1", 0, 0.3, 1, 1.0);
25  pad1->SetBottomMargin(0); // Upper and lower plot are joined
26  pad1->SetGridx(); // Vertical grid
27  pad1->Draw(); // Draw the upper pad: pad1
28  pad1->cd(); // pad1 becomes the current pad
29  h1->SetStats(0); // No statistics on upper plot
30  h1->Draw(); // Draw h1
31  h2->Draw("same"); // Draw h2 on top of h1
32 
33  // Do not draw the Y axis label on the upper plot and redraw a small
34  // axis instead, in order to avoid the first label (0) to be clipped.
35  h1->GetYaxis()->SetLabelSize(0.);
36  TGaxis *axis = new TGaxis( -5, 20, -5, 220, 20,220,510,"");
37  axis->SetLabelFont(43); // Absolute font size in pixel (precision 3)
38  axis->SetLabelSize(15);
39  axis->Draw();
40 
41  // lower plot will be in pad
42  c->cd(); // Go back to the main canvas before defining pad2
43  TPad *pad2 = new TPad("pad2", "pad2", 0, 0.05, 1, 0.3);
44  pad2->SetTopMargin(0);
45  pad2->SetBottomMargin(0.2);
46  pad2->SetGridx(); // vertical grid
47  pad2->Draw();
48  pad2->cd(); // pad2 becomes the current pad
49 
50  // Define the ratio plot
51  TH1F *h3 = (TH1F*)h1->Clone("h3");
52  h3->SetLineColor(kBlack);
53  h3->SetMinimum(0.8); // Define Y ..
54  h3->SetMaximum(1.35); // .. range
55  h3->Sumw2();
56  h3->SetStats(0); // No statistics on lower plot
57  h3->Divide(h2);
58  h3->SetMarkerStyle(21);
59  h3->Draw("ep"); // Draw the ratio plot
60 
61  // h1 settings
62  h1->SetLineColor(kBlue+1);
63  h1->SetLineWidth(2);
64 
65  // Y axis h1 plot settings
66  h1->GetYaxis()->SetTitleSize(20);
67  h1->GetYaxis()->SetTitleFont(43);
68  h1->GetYaxis()->SetTitleOffset(1.55);
69 
70  // h2 settings
71  h2->SetLineColor(kRed);
72  h2->SetLineWidth(2);
73 
74  // Ratio plot (h3) settings
75  h3->SetTitle(""); // Remove the ratio title
76 
77  // Y axis ratio plot settings
78  h3->GetYaxis()->SetTitle("ratio h1/h2 ");
79  h3->GetYaxis()->SetNdivisions(505);
80  h3->GetYaxis()->SetTitleSize(20);
81  h3->GetYaxis()->SetTitleFont(43);
82  h3->GetYaxis()->SetTitleOffset(1.55);
83  h3->GetYaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
84  h3->GetYaxis()->SetLabelSize(15);
85 
86  // X axis ratio plot settings
87  h3->GetXaxis()->SetTitleSize(20);
88  h3->GetXaxis()->SetTitleFont(43);
89  h3->GetXaxis()->SetTitleOffset(4.);
90  h3->GetXaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
91  h3->GetXaxis()->SetLabelSize(15);
92 }