rf203_ranges.cxx

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'ADDITION AND CONVOLUTION' RooFit tutorial macro #203
00004 // 
00005 // Fitting and plotting in sub ranges
00006 //
00007 //
00008 // 07/2008 - Wouter Verkerke 
00009 //
00010 /////////////////////////////////////////////////////////////////////////
00011 
00012 #ifndef __CINT__
00013 #include "RooGlobalFunc.h"
00014 #endif
00015 #include "RooRealVar.h"
00016 #include "RooDataSet.h"
00017 #include "RooGaussian.h"
00018 #include "RooPolynomial.h"
00019 #include "RooAddPdf.h"
00020 #include "RooFitResult.h"
00021 #include "RooPlot.h"
00022 #include "TCanvas.h"
00023 #include "TH1.h"
00024 using namespace RooFit ;
00025 
00026 
00027 class TestBasic203 : public RooFitTestUnit
00028 {
00029 public: 
00030   TestBasic203(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Basic fitting and plotting in ranges",refFile,writeRef,verbose) {} ;
00031   Bool_t testCode() {
00032 
00033     // S e t u p   m o d e l 
00034     // ---------------------
00035     
00036     // Construct observables x
00037     RooRealVar x("x","x",-10,10) ;
00038     
00039     // Construct gaussx(x,mx,1)
00040     RooRealVar mx("mx","mx",0,-10,10) ;
00041     RooGaussian gx("gx","gx",x,mx,RooConst(1)) ;
00042     
00043     // Construct px = 1 (flat in x)
00044     RooPolynomial px("px","px",x) ;
00045     
00046     // Construct model = f*gx + (1-f)px
00047     RooRealVar f("f","f",0.,1.) ;
00048     RooAddPdf model("model","model",RooArgList(gx,px),f) ;
00049     
00050     // Generated 10000 events in (x,y) from p.d.f. model
00051     RooDataSet* modelData = model.generate(x,10000) ;
00052     
00053     // F i t   f u l l   r a n g e 
00054     // ---------------------------
00055     
00056     // Fit p.d.f to all data
00057     RooFitResult* r_full = model.fitTo(*modelData,Save(kTRUE)) ;
00058     
00059     
00060     // F i t   p a r t i a l   r a n g e 
00061     // ----------------------------------
00062     
00063     // Define "signal" range in x as [-3,3]
00064     x.setRange("signal",-3,3) ;  
00065     
00066     // Fit p.d.f only to data in "signal" range
00067     RooFitResult* r_sig = model.fitTo(*modelData,Save(kTRUE),Range("signal")) ;
00068     
00069     
00070     // P l o t   /   p r i n t   r e s u l t s 
00071     // ---------------------------------------
00072     
00073     // Make plot frame in x and add data and fitted model
00074     RooPlot* frame = x.frame(Title("Fitting a sub range")) ;
00075     modelData->plotOn(frame) ;
00076     model.plotOn(frame,Range("Full"),LineStyle(kDashed),LineColor(kRed)) ; // Add shape in full ranged dashed
00077     model.plotOn(frame) ; // By default only fitted range is shown
00078     
00079     regPlot(frame,"rf203_plot") ;
00080     regResult(r_full,"rf203_r_full") ;
00081     regResult(r_sig,"rf203_r_sig") ;
00082 
00083     delete modelData ;    
00084     return kTRUE;
00085   }  
00086 } ;

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