Logo ROOT  
Reference Guide
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 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,8,0)
34  // Avoid the first label (0) to be clipped.
35  TAxis *axis = h1->GetYaxis();
36  axis->ChangeLabel(1, -1, -1, -1, -1, -1, " ");
37  axis->SetLabelFont(43); // Absolute font size in pixel (precision 3)
38  axis->SetLabelSize(15);
39 #else
40  // Do not draw the Y axis label on the upper plot and redraw a small
41  // axis instead, in order to avoid the first label (0) to be clipped.
42  h1->GetYaxis()->SetLabelSize(0.);
43  TGaxis *axis = new TGaxis( -5, 20, -5, 220, 20,220,510,"");
44  axis->SetLabelFont(43); // Absolute font size in pixel (precision 3)
45  axis->SetLabelSize(15);
46  axis->Draw();
47 #endif
48 
49  // lower plot will be in pad
50  c->cd(); // Go back to the main canvas before defining pad2
51  TPad *pad2 = new TPad("pad2", "pad2", 0, 0.05, 1, 0.3);
52  pad2->SetTopMargin(0);
53  pad2->SetBottomMargin(0.2);
54  pad2->SetGridx(); // vertical grid
55  pad2->Draw();
56  pad2->cd(); // pad2 becomes the current pad
57 
58  // Define the ratio plot
59  TH1F *h3 = (TH1F*)h1->Clone("h3");
60  h3->SetLineColor(kBlack);
61  h3->SetMinimum(0.8); // Define Y ..
62  h3->SetMaximum(1.35); // .. range
63  h3->Sumw2();
64  h3->SetStats(0); // No statistics on lower plot
65  h3->Divide(h2);
66  h3->SetMarkerStyle(21);
67  h3->Draw("ep"); // Draw the ratio plot
68 
69  // h1 settings
70  h1->SetLineColor(kBlue+1);
71  h1->SetLineWidth(2);
72 
73  // Y axis h1 plot settings
74  h1->GetYaxis()->SetTitleSize(20);
75  h1->GetYaxis()->SetTitleFont(43);
76  h1->GetYaxis()->SetTitleOffset(1.55);
77 
78  // h2 settings
79  h2->SetLineColor(kRed);
80  h2->SetLineWidth(2);
81 
82  // Ratio plot (h3) settings
83  h3->SetTitle(""); // Remove the ratio title
84 
85  // Y axis ratio plot settings
86  h3->GetYaxis()->SetTitle("ratio h1/h2 ");
87  h3->GetYaxis()->SetNdivisions(505);
88  h3->GetYaxis()->SetTitleSize(20);
89  h3->GetYaxis()->SetTitleFont(43);
90  h3->GetYaxis()->SetTitleOffset(1.55);
91  h3->GetYaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
92  h3->GetYaxis()->SetLabelSize(15);
93 
94  // X axis ratio plot settings
95  h3->GetXaxis()->SetTitleSize(20);
96  h3->GetXaxis()->SetTitleFont(43);
97  h3->GetXaxis()->SetTitleOffset(4.);
98  h3->GetXaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
99  h3->GetXaxis()->SetLabelSize(15);
100 }
c
#define c(i)
Definition: RSha256.hxx:101
TAxis
Class to manage histogram axis.
Definition: TAxis.h:30
TAttPad::SetTopMargin
virtual void SetTopMargin(Float_t topmargin)
Set Pad top margin in fraction of the pad height.
Definition: TAttPad.cxx:129
TPad
The most important graphics class in the ROOT system.
Definition: TPad.h:26
TH1::SetMinimum
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:397
TH1::Divide
virtual Bool_t Divide(TF1 *f1, Double_t c1=1)
Performs the operation: this = this/(c1*f1) if errors are defined (see TH1::Sumw2),...
Definition: TH1.cxx:2805
TH1::FillRandom
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3502
TAttLine::SetLineColor
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
h1
TH1F * h1
Definition: legend1.C:5
TAttAxis::SetTitleSize
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title.
Definition: TAttAxis.cxx:303
TH1::SetTitle
virtual void SetTitle(const char *title)
See GetStatOverflows for more information.
Definition: TH1.cxx:6564
TH1::Clone
TObject * Clone(const char *newname=0) const
Make a complete copy of the underlying object.
Definition: TH1.cxx:2717
kBlack
@ kBlack
Definition: Rtypes.h:65
TAttAxis::SetLabelSize
virtual void SetLabelSize(Float_t size=0.04)
Set size of axis labels.
Definition: TAttAxis.cxx:203
TPad::SetGridx
virtual void SetGridx(Int_t value=1)
Definition: TPad.h:328
TAttPad::SetBottomMargin
virtual void SetBottomMargin(Float_t bottommargin)
Set Pad bottom margin in fraction of the pad height.
Definition: TAttPad.cxx:99
TH1::GetYaxis
TAxis * GetYaxis()
Definition: TH1.h:319
TAttAxis::SetTitleFont
virtual void SetTitleFont(Style_t font=62)
Set the title font.
Definition: TAttAxis.cxx:321
TAxis::ChangeLabel
void ChangeLabel(Int_t labNum=0, Double_t labAngle=-1., Double_t labSize=-1., Int_t labAlign=-1, Int_t labColor=-1, Int_t labFont=-1, TString labText="")
Define new text attributes for the label number "labNum".
Definition: TAxis.cxx:873
TH1::SetMaximum
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:396
TGaxis::SetLabelFont
void SetLabelFont(Int_t labelfont)
Definition: TGaxis.h:105
TGaxis::SetLabelSize
void SetLabelSize(Float_t labelsize)
Definition: TGaxis.h:107
kRed
@ kRed
Definition: Rtypes.h:66
TNamed::SetTitle
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
TH1::SetStats
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8665
TAttLine::SetLineWidth
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
TObject::Draw
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:197
TCanvas
The Canvas class.
Definition: TCanvas.h:23
TGaxis
The axis painter class.
Definition: TGaxis.h:23
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:573
TPad::cd
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:594
TH1::Sumw2
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition: TH1.cxx:8695
kBlue
@ kBlue
Definition: Rtypes.h:66
TAttAxis::SetNdivisions
virtual void SetNdivisions(Int_t n=510, Bool_t optim=kTRUE)
Set the number of divisions for this axis.
Definition: TAttAxis.cxx:228
TAttMarker::SetMarkerStyle
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
TH1::GetXaxis
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:318
TAttAxis::SetLabelFont
virtual void SetLabelFont(Style_t font=62)
Set labels' font.
Definition: TAttAxis.cxx:182
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:3050
TPad::Draw
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1285