chi2test.C

Go to the documentation of this file.
00001 // Example to use chi2 test for comparing two histograms 
00002 // One unweighted histogram is compared with a weighted histogram. 
00003 // The normalized residuals are retrieved and plotted in a simple graph. 
00004 // The QQ plot of the normalized residual using the 
00005 // normal distribution is also plotted. 
00006 //
00007 //Authors: Nikolai Gagunashvili, Daniel Haertl, Lorenzo Moneta
00008 
00009 
00010 #include "TH1.h"
00011 #include "TH1D.h"
00012 #include "TF1.h"
00013 #include "TGraph.h"
00014 #include "TGraphQQ.h"
00015 #include "TCanvas.h"
00016 #include "TStyle.h"
00017 #include "TMath.h"
00018 
00019 
00020 TCanvas * chi2test(Float_t w=0)
00021 {
00022   // Note: The parameter w is used to produce the 2 pictures in
00023   // the TH1::Chi2Test method. The 1st picture is produced with 
00024   // w=0 and the 2nd with w=17 (see TH1::Chi2Test() help).
00025 
00026   // Define Histograms.
00027   const Int_t n = 20;
00028 
00029   TH1D *h1 = new TH1D("h1", "h1", n, 4, 16);
00030   TH1D *h2 = new TH1D("h2", "h2", n, 4, 16);
00031 
00032   h1->SetTitle("Unweighted Histogram");
00033   h2->SetTitle("Weighted Histogram");
00034 
00035   h1->SetBinContent(1, 0);
00036   h1->SetBinContent(2, 1);
00037   h1->SetBinContent(3, 0);
00038   h1->SetBinContent(4, 1);
00039   h1->SetBinContent(5, 1);
00040   h1->SetBinContent(6, 6);
00041   h1->SetBinContent(7, 7);
00042   h1->SetBinContent(8, 2);
00043   h1->SetBinContent(9, 22);
00044   h1->SetBinContent(10, 30);
00045   h1->SetBinContent(11, 27);
00046   h1->SetBinContent(12, 20);
00047   h1->SetBinContent(13, 13);
00048   h1->SetBinContent(14, 9);
00049   h1->SetBinContent(15, 9 + w);
00050   h1->SetBinContent(16, 13);
00051   h1->SetBinContent(17, 19);
00052   h1->SetBinContent(18, 11);
00053   h1->SetBinContent(19, 9);
00054   h1->SetBinContent(20, 0);
00055 
00056   // error is automatically the sqrt of the bin content
00057 //   h1->SetBinError(1, 0);
00058 //   h1->SetBinError(2, 1);
00059 //   h1->SetBinError(3, 0);
00060 //   h1->SetBinError(4, 1);
00061 //   h1->SetBinError(5, 1);
00062 //   h1->SetBinError(6, 2.44948983);
00063 //   h1->SetBinError(7, 2.64575124 );
00064 //   h1->SetBinError(8, 1.41421354);
00065 //   h1->SetBinError(9, 4.69041586);
00066 //   h1->SetBinError(10, 5.47722578);
00067 //   h1->SetBinError(11, 5.19615221);
00068 //   h1->SetBinError(12, 4.47213602);
00069 //   h1->SetBinError(13, 3.60555124);
00070 //   h1->SetBinError(14, 3.);
00071 //   h1->SetBinError(15, 5.38516474);
00072 //   h1->SetBinError(16, 3.60555124);
00073 //   h1->SetBinError(17, 4.35889912);
00074 //   h1->SetBinError(18, 3.31662488);
00075 //   h1->SetBinError(19, 3.);
00076 //   h1->SetBinError(20, 0);
00077 
00078   h2->SetBinContent(1, 2.20173025 );
00079   h2->SetBinContent(2, 3.30143857);
00080   h2->SetBinContent(3, 2.5892849);
00081   h2->SetBinContent(4, 2.99990201);
00082   h2->SetBinContent(5, 4.92877054);
00083   h2->SetBinContent(6, 8.33036995);
00084   h2->SetBinContent(7, 6.95084763);
00085   h2->SetBinContent(8, 15.206357);
00086   h2->SetBinContent(9, 23.9236012);
00087   h2->SetBinContent(10, 44.3848114);
00088   h2->SetBinContent(11, 49.4465599);
00089   h2->SetBinContent(12, 25.1868858);
00090   h2->SetBinContent(13, 16.3129692);
00091   h2->SetBinContent(14, 13.0289612);
00092   h2->SetBinContent(15, 16.7857609);
00093   h2->SetBinContent(16, 22.9914703);
00094   h2->SetBinContent(17, 30.5279255);
00095   h2->SetBinContent(18, 12.5252123);
00096   h2->SetBinContent(19, 16.4104557);
00097   h2->SetBinContent(20, 7.86067867);
00098   h2->SetBinError(1, 0.38974303 );
00099   h2->SetBinError(2, 0.536510944);
00100   h2->SetBinError(3, 0.529702604);
00101   h2->SetBinError(4, 0.642001867);
00102   h2->SetBinError(5, 0.969341516);
00103   h2->SetBinError(6, 1.47611344);
00104   h2->SetBinError(7, 1.69797957);
00105   h2->SetBinError(8, 3.28577447);
00106   h2->SetBinError(9, 5.40784931);
00107   h2->SetBinError(10, 9.10106468);
00108   h2->SetBinError(11, 9.73541737);
00109   h2->SetBinError(12, 5.55019951);
00110   h2->SetBinError(13, 3.57914758);
00111   h2->SetBinError(14, 2.77877331);
00112   h2->SetBinError(15, 3.23697519);
00113   h2->SetBinError(16, 4.3608489);
00114   h2->SetBinError(17, 5.77172089);
00115   h2->SetBinError(18, 3.38666105);
00116   h2->SetBinError(19, 2.98861837);
00117   h2->SetBinError(20, 1.58402085);
00118 
00119   h1->SetEntries(217);
00120   h2->SetEntries(500);
00121 
00122 //apply the chi2 test and retrieve the residuals
00123   Double_t res[n], x[20];
00124   h1->Chi2Test(h2,"UW P",res);
00125 
00126 //Graph for Residuals
00127   for (Int_t i=0; i<n; i++) x[i]= 4.+i*12./20.+12./40.;
00128   TGraph *resgr = new TGraph(n,x,res);
00129   resgr->GetXaxis()->SetRangeUser(4,16);
00130   resgr->GetYaxis()->SetRangeUser(-3.5,3.5);
00131   resgr->GetYaxis()->SetTitle("Normalized Residuals");
00132   resgr->SetMarkerStyle(21);
00133   resgr->SetMarkerColor(2);
00134   resgr->SetMarkerSize(.9);
00135   resgr->SetTitle("Normalized Residuals");
00136 
00137 //Quantile-Quantile plot
00138   TF1 *f = new TF1("f","TMath::Gaus(x,0,1)",-10,10);
00139   TGraphQQ *qqplot = new TGraphQQ(n,res,f);
00140   qqplot->SetMarkerStyle(20);
00141   qqplot->SetMarkerColor(2);
00142   qqplot->SetMarkerSize(.9);
00143   qqplot->SetTitle("Q-Q plot of Normalized Residuals");
00144 
00145 //create Canvas
00146   TCanvas *c1 = new TCanvas("c1","Chistat Plot",10,10,700,600);
00147   c1->SetFillColor(33);
00148   c1->Divide(2,2);
00149 
00150 // Draw Histogramms and Graphs
00151   c1->cd(1);
00152   gPad->SetFrameFillColor(21);
00153   h1->SetMarkerColor(4);
00154   h1->SetMarkerStyle(20);
00155 
00156   h1->Draw("E");
00157 
00158   c1->cd(2);
00159   gPad->SetFrameFillColor(21);
00160   h2->Draw("");
00161   h2->SetMarkerColor(4);
00162   h2->SetMarkerStyle(20);
00163 
00164   c1->cd(3);
00165   gPad->SetFrameFillColor(21);
00166   gPad->SetGridy();
00167   resgr->Draw("APL");
00168 
00169   c1->cd(4);
00170   gPad->SetFrameFillColor(21);
00171   qqplot->Draw("AP");
00172 
00173   c1->cd(0);
00174   
00175   c1->Update();
00176    return c1;
00177 }

Generated on Tue Jul 5 15:44:49 2011 for ROOT_528-00b_version by  doxygen 1.5.1