rf109_chi2residpull.C

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'BASIC FUNCTIONALITY' RooFit tutorial macro #109
00004 // 
00005 // Calculating chi^2 from histograms and curves in RooPlots, 
00006 // making histogram of residual and pull distributions
00007 //
00008 //
00009 //
00010 // 07/2008 - Wouter Verkerke 
00011 // 
00012 /////////////////////////////////////////////////////////////////////////
00013 
00014 #ifndef __CINT__
00015 #include "RooGlobalFunc.h"
00016 #endif
00017 #include "RooRealVar.h"
00018 #include "RooDataSet.h"
00019 #include "RooGaussian.h"
00020 #include "RooConstVar.h"
00021 #include "TCanvas.h"
00022 #include "TAxis.h"
00023 #include "RooPlot.h"
00024 #include "RooHist.h"
00025 using namespace RooFit ;
00026 
00027 
00028 void rf109_chi2residpull()
00029 {
00030 
00031   // S e t u p   m o d e l 
00032   // ---------------------
00033 
00034   // Create observables
00035   RooRealVar x("x","x",-10,10) ;
00036 
00037   // Create Gaussian
00038   RooRealVar sigma("sigma","sigma",3,0.1,10) ;
00039   RooRealVar mean("mean","mean",0,-10,10) ;
00040   RooGaussian gauss("gauss","gauss",x,RooConst(0),sigma) ;
00041 
00042   // Generate a sample of 1000 events with sigma=3
00043   RooDataSet* data = gauss.generate(x,10000) ;
00044 
00045   // Change sigma to 3.15
00046   sigma=3.15 ;
00047 
00048 
00049   // 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
00050   // ---------------------------------------------------------------------------
00051 
00052   // Overlay projection of gauss with sigma=3.15 on data with sigma=3.0
00053   RooPlot* frame1 = x.frame(Title("Data with distorted Gaussian pdf"),Bins(40)) ;
00054   data->plotOn(frame1,DataError(RooAbsData::SumW2)) ;
00055   gauss.plotOn(frame1) ;
00056 
00057 
00058   // C a l c u l a t e   c h i ^ 2 
00059   // ------------------------------
00060 
00061   // Show the chi^2 of the curve w.r.t. the histogram
00062   // If multiple curves or datasets live in the frame you can specify
00063   // the name of the relevant curve and/or dataset in chiSquare()
00064   cout << "chi^2 = " << frame1->chiSquare() << endl ;
00065 
00066 
00067   // S h o w   r e s i d u a l   a n d   p u l l   d i s t s
00068   // -------------------------------------------------------
00069 
00070   // Construct a histogram with the residuals of the data w.r.t. the curve
00071   RooHist* hresid = frame1->residHist() ;
00072 
00073   // Construct a histogram with the pulls of the data w.r.t the curve
00074   RooHist* hpull = frame1->pullHist() ;
00075 
00076   // Create a new frame to draw the residual distribution and add the distribution to the frame
00077   RooPlot* frame2 = x.frame(Title("Residual Distribution")) ;
00078   frame2->addPlotable(hresid,"P") ;
00079 
00080   // Create a new frame to draw the pull distribution and add the distribution to the frame
00081   RooPlot* frame3 = x.frame(Title("Pull Distribution")) ;
00082   frame3->addPlotable(hpull,"P") ;
00083 
00084 
00085 
00086   TCanvas* c = new TCanvas("rf109_chi2residpull","rf109_chi2residpull",900,300) ;
00087   c->Divide(3) ;
00088   c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame1->GetYaxis()->SetTitleOffset(1.6) ; frame1->Draw() ;
00089   c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.6) ; frame2->Draw() ;
00090   c->cd(3) ; gPad->SetLeftMargin(0.15) ; frame3->GetYaxis()->SetTitleOffset(1.6) ; frame3->Draw() ;
00091   
00092 }

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