Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf109_chi2residpull.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// Basic functionality: Calculating chi^2 from histograms and curves in RooPlots, making histogram of residual and pull
5 /// distributions
6 ///
7 /// \macro_image
8 /// \macro_output
9 /// \macro_code
10 /// \author 07/2008 - Wouter Verkerke
11 
12 #include "RooRealVar.h"
13 #include "RooDataSet.h"
14 #include "RooGaussian.h"
15 #include "RooConstVar.h"
16 #include "TCanvas.h"
17 #include "TAxis.h"
18 #include "RooPlot.h"
19 #include "RooHist.h"
20 using namespace RooFit;
21 
22 void rf109_chi2residpull()
23 {
24 
25  // S e t u p m o d e l
26  // ---------------------
27 
28  // Create observables
29  RooRealVar x("x", "x", -10, 10);
30 
31  // Create Gaussian
32  RooRealVar sigma("sigma", "sigma", 3, 0.1, 10);
33  RooRealVar mean("mean", "mean", 0, -10, 10);
34  RooGaussian gauss("gauss", "gauss", x, RooConst(0), sigma);
35 
36  // Generate a sample of 1000 events with sigma=3
37  RooDataSet *data = gauss.generate(x, 10000);
38 
39  // Change sigma to 3.15
40  sigma = 3.15;
41 
42  // P l o t d a t a a n d s l i g h t l y d i s t o r t e d m o d e l
43  // ---------------------------------------------------------------------------
44 
45  // Overlay projection of gauss with sigma=3.15 on data with sigma=3.0
46  RooPlot *frame1 = x.frame(Title("Data with distorted Gaussian pdf"), Bins(40));
47  data->plotOn(frame1, DataError(RooAbsData::SumW2));
48  gauss.plotOn(frame1);
49 
50  // C a l c u l a t e c h i ^ 2
51  // ------------------------------
52 
53  // Show the chi^2 of the curve w.r.t. the histogram
54  // If multiple curves or datasets live in the frame you can specify
55  // the name of the relevant curve and/or dataset in chiSquare()
56  cout << "chi^2 = " << frame1->chiSquare() << endl;
57 
58  // S h o w r e s i d u a l a n d p u l l d i s t s
59  // -------------------------------------------------------
60 
61  // Construct a histogram with the residuals of the data w.r.t. the curve
62  RooHist *hresid = frame1->residHist();
63 
64  // Construct a histogram with the pulls of the data w.r.t the curve
65  RooHist *hpull = frame1->pullHist();
66 
67  // Create a new frame to draw the residual distribution and add the distribution to the frame
68  RooPlot *frame2 = x.frame(Title("Residual Distribution"));
69  frame2->addPlotable(hresid, "P");
70 
71  // Create a new frame to draw the pull distribution and add the distribution to the frame
72  RooPlot *frame3 = x.frame(Title("Pull Distribution"));
73  frame3->addPlotable(hpull, "P");
74 
75  TCanvas *c = new TCanvas("rf109_chi2residpull", "rf109_chi2residpull", 900, 300);
76  c->Divide(3);
77  c->cd(1);
78  gPad->SetLeftMargin(0.15);
79  frame1->GetYaxis()->SetTitleOffset(1.6);
80  frame1->Draw();
81  c->cd(2);
82  gPad->SetLeftMargin(0.15);
83  frame2->GetYaxis()->SetTitleOffset(1.6);
84  frame2->Draw();
85  c->cd(3);
86  gPad->SetLeftMargin(0.15);
87  frame3->GetYaxis()->SetTitleOffset(1.6);
88  frame3->Draw();
89 }