Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ratioplot.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist_legacy
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 `tutorials/hist`.
6///
7/// \macro_image
8/// \macro_code
9///
10/// \date May 2021
11/// \author Olivier Couet
12
13void ratioplot()
14{
15 // Define two gaussian histograms. Note the X and Y title are defined
16 // at booking time using the convention "Hist_title ; X_title ; Y_title"
17 TH1F *h1 = new TH1F("h1", "Two gaussian plots and their ratio;x title; h1 and h2 gaussian histograms", 100, -5, 5);
18 TH1F("h2", "h2", 100, -5, 5);
19 h1->FillRandom("gaus");
20 h2->FillRandom("gaus");
21
22 // Define the Canvas
23 TCanvas *c = new TCanvas("c", "canvas", 800, 800);
24
25 // Upper plot will be in pad1
26 TPad *pad1 = new TPad("pad1", "pad1", 0, 0.3, 1, 1.0);
27 pad1->SetBottomMargin(0); // Upper and lower plot are joined
28 pad1->SetGridx(); // Vertical grid
29 pad1->Draw(); // Draw the upper pad: pad1
30 pad1->cd(); // pad1 becomes the current pad
31 h1->SetStats(0); // No statistics on upper plot
32 h1->Draw(); // Draw h1
33 h2->Draw("same"); // Draw h2 on top of h1
34
35#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 8, 0)
36 // Avoid the first label (0) to be clipped.
37 TAxis *axis = h1->GetYaxis();
38 axis->ChangeLabel(1, -1, -1, -1, -1, -1, " ");
39 axis->SetLabelFont(43); // Absolute font size in pixel (precision 3)
40 axis->SetLabelSize(15);
41#else
42 // Do not draw the Y axis label on the upper plot and redraw a small
43 // axis instead, in order to avoid the first label (0) to be clipped.
44 h1->GetYaxis()->SetLabelSize(0.);
45 TGaxis *axis = new TGaxis(-5, 20, -5, 220, 20, 220, 510, "");
46 axis->SetLabelFont(43); // Absolute font size in pixel (precision 3)
47 axis->SetLabelSize(15);
48 axis->Draw();
49#endif
50
51 // lower plot will be in pad
52 c->cd(); // Go back to the main canvas before defining pad2
53 TPad *pad2 = new TPad("pad2", "pad2", 0, 0.05, 1, 0.3);
54 pad2->SetTopMargin(0);
55 pad2->SetBottomMargin(0.2);
56 pad2->SetGridx(); // vertical grid
57 pad2->Draw();
58 pad2->cd(); // pad2 becomes the current pad
59
60 // Define the ratio plot
61 Clone("h3");
62 kBlack);
63 h3->SetMinimum(0.8); // Define Y ..
64 h3->SetMaximum(1.35); // .. range
65 h3->Sumw2();
66 h3->SetStats(0); // No statistics on lower plot
67 h3->Divide(h2);
68 h3->SetMarkerStyle(21);
69 h3->Draw("ep"); // Draw the ratio plot
70
71 // h1 settings
72 h1->SetLineColor(kBlue + 1);
73 h1->SetLineWidth(2);
74
75 // Y axis h1 plot settings
76 h1->GetYaxis()->SetTitleSize(20);
77 h1->GetYaxis()->SetTitleFont(43);
78 h1->GetYaxis()->SetTitleOffset(1.55);
79
80 // h2 settings
81 kRed);
82 h2->SetLineWidth(2);
83
84 // Ratio plot (h3) settings
85 h3->SetTitle(""); // Remove the ratio title
86
87 // Y axis ratio plot settings
88 h3->GetYaxis()->SetTitle("ratio h1/h2 ");
89 h3->GetYaxis()->SetNdivisions(505);
90 h3->GetYaxis()->SetTitleSize(20);
91 h3->GetYaxis()->SetTitleFont(43);
92 h3->GetYaxis()->SetTitleOffset(1.55);
93 h3->GetYaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
94 h3->GetYaxis()->SetLabelSize(15);
95
96 // X axis ratio plot settings
97 h3->GetXaxis()->SetTitleSize(20);
98 h3->GetXaxis()->SetTitleFont(43);
99 h3->GetXaxis()->SetTitleOffset(1);
100 h3->GetXaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
101 h3->GetXaxis()->SetLabelSize(15);
102}
#define c(i)
Definition RSha256.hxx:101
@ kRed
Definition Rtypes.h:66
@ kBlack
Definition Rtypes.h:65
@ kBlue
Definition Rtypes.h:66
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:280
virtual void SetLabelSize(Float_t size=0.04)
Set size of axis labels.
Definition TAttAxis.cxx:185
virtual void SetTitleFont(Style_t font=62)
Set the title font.
Definition TAttAxis.cxx:309
virtual void SetLabelFont(Style_t font=62)
Set labels' font.
Definition TAttAxis.cxx:162
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title.
Definition TAttAxis.cxx:291
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:45
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:42
Class to manage histogram axis.
Definition TAxis.h:32
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, const TString &labText="")
Define new text attributes for the label number "labNum".
Definition TAxis.cxx:951
The Canvas class.
Definition TCanvas.h:23
The axis painter class.
Definition TGaxis.h:26
void SetLabelFont(Int_t labelfont)
Definition TGaxis.h:107
void SetLabelSize(Float_t labelsize)
Definition TGaxis.h:109
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:877
virtual void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng=nullptr)
Definition TH1.cxx:3500
TAxis * GetYaxis()
Definition TH1.h:572
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3038
TObject * Clone(const char *newname="") const override
Make a complete copy of the underlying object.
Definition TH1.cxx:2724
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:8986
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:293
The most important graphics class in the ROOT system.
Definition TPad.h:28
TH1F * h1
Definition legend1.C:5