ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
chi2test.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_math
3 /// Example to use chi2 test for comparing two histograms
4 /// One unweighted histogram is compared with a weighted histogram.
5 /// The normalized residuals are retrieved and plotted in a simple graph.
6 /// The QQ plot of the normalized residual using the
7 /// normal distribution is also plotted.
8 ///
9 /// \macro_image
10 /// \macro_output
11 /// \macro_code
12 ///
13 /// \author Nikolai Gagunashvili, Daniel Haertl, Lorenzo Moneta
14 
15 #include "TH1.h"
16 #include "TH1D.h"
17 #include "TF1.h"
18 #include "TGraph.h"
19 #include "TGraphQQ.h"
20 #include "TCanvas.h"
21 #include "TStyle.h"
22 #include "TMath.h"
23 
24 TCanvas * chi2test(Float_t w=0)
25 {
26  // Note: The parameter w is used to produce the 2 pictures in
27  // the TH1::Chi2Test method. The 1st picture is produced with
28  // w=0 and the 2nd with w=17 (see TH1::Chi2Test() help).
29 
30  // Define Histograms.
31  const Int_t n = 20;
32 
33  TH1D *h1 = new TH1D("h1", "h1", n, 4, 16);
34  TH1D *h2 = new TH1D("h2", "h2", n, 4, 16);
35 
36  h1->SetTitle("Unweighted Histogram");
37  h2->SetTitle("Weighted Histogram");
38 
39  h1->SetBinContent(1, 0);
40  h1->SetBinContent(2, 1);
41  h1->SetBinContent(3, 0);
42  h1->SetBinContent(4, 1);
43  h1->SetBinContent(5, 1);
44  h1->SetBinContent(6, 6);
45  h1->SetBinContent(7, 7);
46  h1->SetBinContent(8, 2);
47  h1->SetBinContent(9, 22);
48  h1->SetBinContent(10, 30);
49  h1->SetBinContent(11, 27);
50  h1->SetBinContent(12, 20);
51  h1->SetBinContent(13, 13);
52  h1->SetBinContent(14, 9);
53  h1->SetBinContent(15, 9 + w);
54  h1->SetBinContent(16, 13);
55  h1->SetBinContent(17, 19);
56  h1->SetBinContent(18, 11);
57  h1->SetBinContent(19, 9);
58  h1->SetBinContent(20, 0);
59 
60  h2->SetBinContent(1, 2.20173025 );
61  h2->SetBinContent(2, 3.30143857);
62  h2->SetBinContent(3, 2.5892849);
63  h2->SetBinContent(4, 2.99990201);
64  h2->SetBinContent(5, 4.92877054);
65  h2->SetBinContent(6, 8.33036995);
66  h2->SetBinContent(7, 6.95084763);
67  h2->SetBinContent(8, 15.206357);
68  h2->SetBinContent(9, 23.9236012);
69  h2->SetBinContent(10, 44.3848114);
70  h2->SetBinContent(11, 49.4465599);
71  h2->SetBinContent(12, 25.1868858);
72  h2->SetBinContent(13, 16.3129692);
73  h2->SetBinContent(14, 13.0289612);
74  h2->SetBinContent(15, 16.7857609);
75  h2->SetBinContent(16, 22.9914703);
76  h2->SetBinContent(17, 30.5279255);
77  h2->SetBinContent(18, 12.5252123);
78  h2->SetBinContent(19, 16.4104557);
79  h2->SetBinContent(20, 7.86067867);
80  h2->SetBinError(1, 0.38974303 );
81  h2->SetBinError(2, 0.536510944);
82  h2->SetBinError(3, 0.529702604);
83  h2->SetBinError(4, 0.642001867);
84  h2->SetBinError(5, 0.969341516);
85  h2->SetBinError(6, 1.47611344);
86  h2->SetBinError(7, 1.69797957);
87  h2->SetBinError(8, 3.28577447);
88  h2->SetBinError(9, 5.40784931);
89  h2->SetBinError(10, 9.10106468);
90  h2->SetBinError(11, 9.73541737);
91  h2->SetBinError(12, 5.55019951);
92  h2->SetBinError(13, 3.57914758);
93  h2->SetBinError(14, 2.77877331);
94  h2->SetBinError(15, 3.23697519);
95  h2->SetBinError(16, 4.3608489);
96  h2->SetBinError(17, 5.77172089);
97  h2->SetBinError(18, 3.38666105);
98  h2->SetBinError(19, 2.98861837);
99  h2->SetBinError(20, 1.58402085);
100 
101  h1->SetEntries(217);
102  h2->SetEntries(500);
103 
104  //apply the chi2 test and retrieve the residuals
105  Double_t res[n], x[20];
106  h1->Chi2Test(h2,"UW P",res);
107 
108  //Graph for Residuals
109  for (Int_t i=0; i<n; i++) x[i]= 4.+i*12./20.+12./40.;
110  TGraph *resgr = new TGraph(n,x,res);
111  resgr->GetXaxis()->SetRangeUser(4,16);
112  resgr->GetYaxis()->SetRangeUser(-3.5,3.5);
113  resgr->GetYaxis()->SetTitle("Normalized Residuals");
114  resgr->SetMarkerStyle(21);
115  resgr->SetMarkerColor(2);
116  resgr->SetMarkerSize(.9);
117  resgr->SetTitle("Normalized Residuals");
118 
119  //Quantile-Quantile plot
120  TF1 *f = new TF1("f","TMath::Gaus(x,0,1)",-10,10);
121  TGraphQQ *qqplot = new TGraphQQ(n,res,f);
122  qqplot->SetMarkerStyle(20);
123  qqplot->SetMarkerColor(2);
124  qqplot->SetMarkerSize(.9);
125  qqplot->SetTitle("Q-Q plot of Normalized Residuals");
126 
127  //create Canvas
128  TCanvas *c1 = new TCanvas("c1","Chistat Plot",10,10,700,600);
129  c1->SetFillColor(33);
130  c1->Divide(2,2);
131 
132  // Draw Histogramms and Graphs
133  c1->cd(1);
134  gPad->SetFrameFillColor(21);
135  h1->SetMarkerColor(4);
136  h1->SetMarkerStyle(20);
137 
138  h1->Draw("E");
139 
140  c1->cd(2);
141  gPad->SetFrameFillColor(21);
142  h2->Draw("");
143  h2->SetMarkerColor(4);
144  h2->SetMarkerStyle(20);
145 
146  c1->cd(3);
147  gPad->SetFrameFillColor(21);
148  gPad->SetGridy();
149  resgr->Draw("APL");
150 
151  c1->cd(4);
152  gPad->SetFrameFillColor(21);
153  qqplot->Draw("AP");
154 
155  c1->cd(0);
156 
157  c1->Update();
158  return c1;
159 }
float Float_t
Definition: RtypesCore.h:53
TCanvas * c1
Definition: legend1.C:2
virtual Double_t Chi2Test(const TH1 *h2, Option_t *option="UU", Double_t *res=0) const
chi^{2} test for comparing weighted and unweighted histograms
Definition: TH1.cxx:1850
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
int Int_t
Definition: RtypesCore.h:41
virtual void SetTitle(const char *title="")
Set graph title.
Definition: TGraph.cxx:2153
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:740
This class allows to draw quantile-quantile plots.
Definition: TGraphQQ.h:28
virtual void SetRangeUser(Double_t ufirst, Double_t ulast)
Set the viewing range for the axis from ufirst to ulast (in user coordinates).
Definition: TAxis.cxx:869
virtual void SetMarkerColor(Color_t mcolor=1)
Definition: TAttMarker.h:51
virtual void SetBinError(Int_t bin, Double_t error)
see convention for numbering bins in TH1::GetBin
Definition: TH1.cxx:8528
TAxis * GetXaxis() const
Get x axis of the graph.
Definition: TGraph.cxx:1563
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
Definition: TH1.cxx:8543
tuple w
Definition: qtexample.py:51
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
virtual void SetMarkerSize(Size_t msize=1)
Definition: TAttMarker.h:54
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
TAxis * GetYaxis() const
Get y axis of the graph.
Definition: TGraph.cxx:1573
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1073
1-Dim function class
Definition: TF1.h:149
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
#define gPad
Definition: TVirtualPad.h:288
virtual void SetEntries(Double_t n)
Definition: TH1.h:382
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6268
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
const Int_t n
Definition: legend1.C:16