ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ratioplot.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// Example displaying two histograms and their ratio.
4 ///
5 /// \macro_image
6 /// \macro_code
7 ///
8 /// \author Olivier Couet
9 
10 void ratioplot() {
11  // Define two gaussian histograms. Note the X and Y title are defined
12  // at booking time using the convention "Hist_title ; X_title ; Y_title"
13  TH1F *h1 = new TH1F("h1", "Two gaussian plots and their ratio;x title; h1 and h2 gaussian histograms", 100, -5, 5);
14  TH1F *h2 = new TH1F("h2", "h2", 100, -5, 5);
15  h1->FillRandom("gaus");
16  h2->FillRandom("gaus");
17 
18  // Define the Canvas
19  TCanvas *c = new TCanvas("c", "canvas", 800, 800);
20 
21  // Upper plot will be in pad1
22  TPad *pad1 = new TPad("pad1", "pad1", 0, 0.3, 1, 1.0);
23  pad1->SetBottomMargin(0); // Upper and lower plot are joined
24  pad1->SetGridx(); // Vertical grid
25  pad1->Draw(); // Draw the upper pad: pad1
26  pad1->cd(); // pad1 becomes the current pad
27  h1->SetStats(0); // No statistics on upper plot
28  h1->Draw(); // Draw h1
29  h2->Draw("same"); // Draw h2 on top of h1
30 
31  // Do not draw the Y axis label on the upper plot and redraw a small
32  // axis instead, in order to avoid the first label (0) to be clipped.
33  h1->GetYaxis()->SetLabelSize(0.);
34  TGaxis *axis = new TGaxis( -5, 20, -5, 220, 20,220,510,"");
35  axis->SetLabelFont(43); // Absolute font size in pixel (precision 3)
36  axis->SetLabelSize(15);
37  axis->Draw();
38 
39  // lower plot will be in pad
40  c->cd(); // Go back to the main canvas before defining pad2
41  TPad *pad2 = new TPad("pad2", "pad2", 0, 0.05, 1, 0.3);
42  pad2->SetTopMargin(0);
43  pad2->SetBottomMargin(0.2);
44  pad2->SetGridx(); // vertical grid
45  pad2->Draw();
46  pad2->cd(); // pad2 becomes the current pad
47 
48  // Define the ratio plot
49  TH1F *h3 = (TH1F*)h1->Clone("h3");
50  h3->SetLineColor(kBlack);
51  h3->SetMinimum(0.8); // Define Y ..
52  h3->SetMaximum(1.35); // .. range
53  h3->Sumw2();
54  h3->SetStats(0); // No statistics on lower plot
55  h3->Divide(h2);
56  h3->SetMarkerStyle(21);
57  h3->Draw("ep"); // Draw the ratio plot
58 
59  // h1 settings
60  h1->SetLineColor(kBlue+1);
61  h1->SetLineWidth(2);
62 
63  // Y axis h1 plot settings
64  h1->GetYaxis()->SetTitleSize(20);
65  h1->GetYaxis()->SetTitleFont(43);
66  h1->GetYaxis()->SetTitleOffset(1.55);
67 
68  // h2 settings
69  h2->SetLineColor(kRed);
70  h2->SetLineWidth(2);
71 
72  // Ratio plot (h3) settings
73  h3->SetTitle(""); // Remove the ratio title
74 
75  // Y axis ratio plot settings
76  h3->GetYaxis()->SetTitle("ratio h1/h2 ");
77  h3->GetYaxis()->SetNdivisions(505);
78  h3->GetYaxis()->SetTitleSize(20);
79  h3->GetYaxis()->SetTitleFont(43);
80  h3->GetYaxis()->SetTitleOffset(1.55);
81  h3->GetYaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
82  h3->GetYaxis()->SetLabelSize(15);
83 
84  // X axis ratio plot settings
85  h3->GetXaxis()->SetTitleSize(20);
86  h3->GetXaxis()->SetTitleFont(43);
87  h3->GetXaxis()->SetTitleOffset(4.);
88  h3->GetXaxis()->SetLabelFont(43); // Absolute font size in pixel (precision 3)
89  h3->GetXaxis()->SetLabelSize(15);
90 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:245
virtual void SetGridx(Int_t value=1)
Definition: TPad.h:327
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:394
Definition: Rtypes.h:61
Definition: Rtypes.h:60
virtual void SetNdivisions(Int_t n=510, Bool_t optim=kTRUE)
Set the number of divisions for this axis.
Definition: TAttAxis.cxx:212
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:395
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
virtual void SetTitleFont(Style_t font=62)
Set the title font.
Definition: TAttAxis.cxx:273
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:254
virtual void SetTopMargin(Float_t topmargin)
Set Pad top margin in fraction of the pad height.
Definition: TAttPad.cxx:127
TObject * Clone(const char *newname=0) const
Make a complete copy of the underlying object.
Definition: TH1.cxx:2565
virtual void SetLabelFont(Style_t font=62)
Set labels' font.
Definition: TAttAxis.cxx:166
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:514
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1192
void SetLabelSize(Float_t labelsize)
Definition: TGaxis.h:118
virtual Bool_t Divide(TF1 *f1, Double_t c1=1)
Performs the operation: this = this/(c1*f1) if errors are defined (see TH1::Sumw2), errors are also recalculated.
Definition: TH1.cxx:2630
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
virtual void SetBottomMargin(Float_t bottommargin)
Set Pad bottom margin in fraction of the pad height.
Definition: TAttPad.cxx:97
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3330
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
The most important graphics class in the ROOT system.
Definition: TPad.h:46
The axis painter class.
Definition: TGaxis.h:39
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
TAxis * GetYaxis()
Definition: TH1.h:320
virtual void SetLabelSize(Float_t size=0.04)
Set size of axis labels The size is expressed in per cent of the pad width.
Definition: TAttAxis.cxx:187
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title The size is expressed in per cent of the pad width.
Definition: TAttAxis.cxx:255
The Canvas class.
Definition: TCanvas.h:48
void SetLabelFont(Int_t labelfont)
Definition: TGaxis.h:116
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition: TH1.cxx:8350
Definition: Rtypes.h:61
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6268
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8320
TAxis * GetXaxis()
Definition: TH1.h:319