rf701_efficiencyfit.cxx

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'SPECIAL PDFS' RooFit tutorial macro #701
00004 // 
00005 // Unbinned maximum likelihood fit of an efficiency eff(x) function to 
00006 // a dataset D(x,cut), where cut is a category encoding a selection, of which
00007 // the efficiency as function of x should be described by eff(x)
00008 //
00009 // 07/2008 - Wouter Verkerke 
00010 //
00011 /////////////////////////////////////////////////////////////////////////
00012 
00013 #ifndef __CINT__
00014 #include "RooGlobalFunc.h"
00015 #endif
00016 #include "RooRealVar.h"
00017 #include "RooDataSet.h"
00018 #include "RooGaussian.h"
00019 #include "RooFormulaVar.h"
00020 #include "RooProdPdf.h"
00021 #include "RooEfficiency.h"
00022 #include "RooPolynomial.h"
00023 #include "RooCategory.h"
00024 #include "TCanvas.h"
00025 #include "RooPlot.h"
00026 using namespace RooFit ;
00027 
00028 
00029 // Elementary operations on a gaussian PDF
00030 class TestBasic701 : public RooFitTestUnit
00031 {
00032 public: 
00033   TestBasic701(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Efficiency operator p.d.f. 1D",refFile,writeRef,verbose) {} ;
00034   Bool_t testCode() {
00035 
00036   // C o n s t r u c t   e f f i c i e n c y   f u n c t i o n   e ( x ) 
00037   // -------------------------------------------------------------------
00038 
00039   // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
00040   RooRealVar x("x","x",-10,10) ;
00041 
00042   // Efficiency function eff(x;a,b) 
00043   RooRealVar a("a","a",0.4,0,1) ;
00044   RooRealVar b("b","b",5) ;
00045   RooRealVar c("c","c",-1,-10,10) ;
00046   RooFormulaVar effFunc("effFunc","(1-a)+a*cos((x-c)/b)",RooArgList(a,b,c,x)) ;
00047 
00048 
00049 
00050   // C o n s t r u c t   c o n d i t i o n a l    e f f i c i e n c y   p d f   E ( c u t | x ) 
00051   // ------------------------------------------------------------------------------------------
00052 
00053   // Acceptance state cut (1 or 0)
00054   RooCategory cut("cut","cutr") ;
00055   cut.defineType("accept",1) ;
00056   cut.defineType("reject",0) ;
00057 
00058   // Construct efficiency p.d.f eff(cut|x)
00059   RooEfficiency effPdf("effPdf","effPdf",effFunc,cut,"accept") ;
00060 
00061 
00062 
00063   // G e n e r a t e   d a t a   ( x ,   c u t )   f r o m   a   t o y   m o d e l 
00064   // -----------------------------------------------------------------------------
00065 
00066   // Construct global shape p.d.f shape(x) and product model(x,cut) = eff(cut|x)*shape(x) 
00067   // (These are _only_ needed to generate some toy MC here to be used later)
00068   RooPolynomial shapePdf("shapePdf","shapePdf",x,RooConst(-0.095)) ;
00069   RooProdPdf model("model","model",shapePdf,Conditional(effPdf,cut)) ;
00070 
00071   // Generate some toy data from model
00072   RooDataSet* data = model.generate(RooArgSet(x,cut),10000) ;
00073 
00074 
00075 
00076   // F i t   c o n d i t i o n a l   e f f i c i e n c y   p d f   t o   d a t a 
00077   // --------------------------------------------------------------------------
00078 
00079   // Fit conditional efficiency p.d.f to data
00080   effPdf.fitTo(*data,ConditionalObservables(x)) ;
00081 
00082 
00083 
00084   // P l o t   f i t t e d ,   d a t a   e f f i c i e n c y  
00085   // --------------------------------------------------------
00086 
00087   // Plot distribution of all events and accepted fraction of events on frame
00088   RooPlot* frame1 = x.frame(Bins(20),Title("Data (all, accepted)")) ;
00089   data->plotOn(frame1) ;
00090   data->plotOn(frame1,Cut("cut==cut::accept"),MarkerColor(kRed),LineColor(kRed)) ;
00091 
00092   // Plot accept/reject efficiency on data overlay fitted efficiency curve
00093   RooPlot* frame2 = x.frame(Bins(20),Title("Fitted efficiency")) ;
00094   data->plotOn(frame2,Efficiency(cut)) ; // needs ROOT version >= 5.21
00095   effFunc.plotOn(frame2,LineColor(kRed)) ;
00096 
00097 
00098   regPlot(frame1,"rf701_plot1") ;
00099   regPlot(frame2,"rf701_plot2") ;
00100 
00101   
00102   delete data ;
00103 
00104   return kTRUE ;  
00105   }
00106 } ;

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