Logo ROOT   6.10/09
Reference Guide
rf109_chi2residpull.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// 'BASIC FUNCTIONALITY' RooFit tutorial macro #109
5 ///
6 /// Calculating chi^2 from histograms and curves in RooPlots,
7 /// making histogram of residual and pull distributions
8 ///
9 /// \macro_image
10 /// \macro_output
11 /// \macro_code
12 /// \author 07/2008 - Wouter Verkerke
13 
14 #include "RooRealVar.h"
15 #include "RooDataSet.h"
16 #include "RooGaussian.h"
17 #include "RooConstVar.h"
18 #include "TCanvas.h"
19 #include "TAxis.h"
20 #include "RooPlot.h"
21 #include "RooHist.h"
22 using namespace RooFit ;
23 
24 
25 void rf109_chi2residpull()
26 {
27 
28  // S e t u p m o d e l
29  // ---------------------
30 
31  // Create observables
32  RooRealVar x("x","x",-10,10) ;
33 
34  // Create Gaussian
35  RooRealVar sigma("sigma","sigma",3,0.1,10) ;
36  RooRealVar mean("mean","mean",0,-10,10) ;
37  RooGaussian gauss("gauss","gauss",x,RooConst(0),sigma) ;
38 
39  // Generate a sample of 1000 events with sigma=3
40  RooDataSet* data = gauss.generate(x,10000) ;
41 
42  // Change sigma to 3.15
43  sigma=3.15 ;
44 
45 
46  // 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
47  // ---------------------------------------------------------------------------
48 
49  // Overlay projection of gauss with sigma=3.15 on data with sigma=3.0
50  RooPlot* frame1 = x.frame(Title("Data with distorted Gaussian pdf"),Bins(40)) ;
51  data->plotOn(frame1,DataError(RooAbsData::SumW2)) ;
52  gauss.plotOn(frame1) ;
53 
54 
55  // C a l c u l a t e c h i ^ 2
56  // ------------------------------
57 
58  // Show the chi^2 of the curve w.r.t. the histogram
59  // If multiple curves or datasets live in the frame you can specify
60  // the name of the relevant curve and/or dataset in chiSquare()
61  cout << "chi^2 = " << frame1->chiSquare() << endl ;
62 
63 
64  // S h o w r e s i d u a l a n d p u l l d i s t s
65  // -------------------------------------------------------
66 
67  // Construct a histogram with the residuals of the data w.r.t. the curve
68  RooHist* hresid = frame1->residHist() ;
69 
70  // Construct a histogram with the pulls of the data w.r.t the curve
71  RooHist* hpull = frame1->pullHist() ;
72 
73  // Create a new frame to draw the residual distribution and add the distribution to the frame
74  RooPlot* frame2 = x.frame(Title("Residual Distribution")) ;
75  frame2->addPlotable(hresid,"P") ;
76 
77  // Create a new frame to draw the pull distribution and add the distribution to the frame
78  RooPlot* frame3 = x.frame(Title("Pull Distribution")) ;
79  frame3->addPlotable(hpull,"P") ;
80 
81 
82 
83  TCanvas* c = new TCanvas("rf109_chi2residpull","rf109_chi2residpull",900,300) ;
84  c->Divide(3) ;
85  c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame1->GetYaxis()->SetTitleOffset(1.6) ; frame1->Draw() ;
86  c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.6) ; frame2->Draw() ;
87  c->cd(3) ; gPad->SetLeftMargin(0.15) ; frame3->GetYaxis()->SetTitleOffset(1.6) ; frame3->Draw() ;
88 
89 }
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:262
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1118
virtual RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) const
Plot dataset on specified frame.
Definition: RooAbsData.cxx:552
Double_t chiSquare(int nFitParam=0) const
Definition: RooPlot.h:166
void addPlotable(RooPlotable *plotable, Option_t *drawOptions="", Bool_t invisible=kFALSE, Bool_t refreshNorm=kFALSE)
Add the specified plotable object to our plot.
Definition: RooPlot.cxx:447
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
RooCmdArg Title(const char *name)
A RooHist is a graphical representation of binned data based on the TGraphAsymmErrors class...
Definition: RooHist.h:26
Double_t x[n]
Definition: legend1.C:17
RooCmdArg DataError(Int_t)
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
const Double_t sigma
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
The Canvas class.
Definition: TCanvas.h:31
RooHist * residHist(const char *histname=0, const char *pdfname=0, bool normalize=false, bool useAverage=kFALSE) const
Return a RooHist containing the residuals of histogram &#39;histname&#39; with respect to curve &#39;curvename&#39;...
Definition: RooPlot.cxx:998
RooConstVar & RooConst(Double_t val)
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:1135
RooCmdArg Bins(Int_t nbin)
#define gPad
Definition: TVirtualPad.h:284
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:559
RooHist * pullHist(const char *histname=0, const char *pdfname=0, bool useAverage=false) const
Definition: RooPlot.h:170