rf609_xychi2fit.C

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #609
00004 // 
00005 // Setting up a chi^2 fit to an unbinned dataset with X,Y,err(Y)
00006 // values (and optionally err(X) values)
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 "RooPolyVar.h"
00020 #include "RooConstVar.h"
00021 #include "RooChi2Var.h"
00022 #include "RooMinuit.h"
00023 #include "TCanvas.h"
00024 #include "TAxis.h"
00025 #include "RooPlot.h"
00026 #include "TRandom.h"
00027 
00028 using namespace RooFit ;
00029 
00030 
00031 void rf609_xychi2fit()
00032 {
00033   // C r e a t e   d a t a s e t   w i t h   X   a n d   Y   v a l u e s
00034   // -------------------------------------------------------------------
00035 
00036   // Make weighted XY dataset with asymmetric errors stored
00037   // The StoreError() argument is essential as it makes
00038   // the dataset store the error in addition to the values
00039   // of the observables. If errors on one or more observables
00040   // are asymmetric, one can store the asymmetric error
00041   // using the StoreAsymError() argument
00042 
00043   RooRealVar x("x","x",-11,11) ;
00044   RooRealVar y("y","y",-10,200) ;
00045   RooDataSet dxy("dxy","dxy",RooArgSet(x,y),StoreError(RooArgSet(x,y))) ;
00046 
00047   // Fill an example dataset with X,err(X),Y,err(Y) values
00048   for (int i=0 ; i<=10 ; i++) {
00049 
00050     // Set X value and error
00051     x = -10 + 2*i;
00052     x.setError( i<5 ? 0.5/1. : 1.0/1. ) ;
00053     
00054     // Set Y value and error 
00055     y = x.getVal() * x.getVal() + 4*fabs(gRandom->Gaus()) ;
00056     y.setError(sqrt(y.getVal())) ;
00057 
00058     dxy.add(RooArgSet(x,y)) ;    
00059   }
00060 
00061 
00062 
00063   // P e r f o r m   c h i 2   f i t   t o   X + / - d x   a n d   Y + / - d Y   v a l u e s
00064   // ---------------------------------------------------------------------------------------
00065 
00066   // Make fit function
00067   RooRealVar a("a","a",0.0,-10,10) ;
00068   RooRealVar b("b","b",0.0,-100,100) ;
00069   RooPolyVar f("f","f",x,RooArgList(b,a,RooConst(1))) ;
00070 
00071   // Plot dataset in X-Y interpretation
00072   RooPlot* frame = x.frame(Title("Chi^2 fit of function set of (X#pmdX,Y#pmdY) values")) ;
00073   dxy.plotOnXY(frame,YVar(y)) ;
00074 
00075   // Fit chi^2 using X and Y errors 
00076   f.chi2FitTo(dxy,YVar(y)) ;
00077 
00078   // Overlay fitted function
00079   f.plotOn(frame) ;
00080   
00081   // Alternative: fit chi^2 integrating f(x) over ranges defined by X errors, rather
00082   // than taking point at center of bin
00083   f.chi2FitTo(dxy,YVar(y),Integrate(kTRUE)) ;
00084 
00085   // Overlay alternate fit result
00086   f.plotOn(frame,LineStyle(kDashed),LineColor(kRed)) ;  
00087 
00088 
00089   // Draw the plot on a canvas
00090   new TCanvas("rf609_xychi2fit","rf609_xychi2fit",600,600) ;
00091   gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.4) ; frame->Draw() ;
00092   
00093 
00094 }

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