rf701_efficiencyfit.C

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 "RooConstVar.h"
00020 #include "RooFormulaVar.h"
00021 #include "RooProdPdf.h"
00022 #include "RooEfficiency.h"
00023 #include "RooPolynomial.h"
00024 #include "RooCategory.h"
00025 #include "TCanvas.h"
00026 #include "TAxis.h"
00027 #include "RooPlot.h"
00028 using namespace RooFit ;
00029 
00030 
00031 void rf701_efficiencyfit()
00032 {
00033   // 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 ) 
00034   // -------------------------------------------------------------------
00035 
00036   // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
00037   RooRealVar x("x","x",-10,10) ;
00038 
00039   // Efficiency function eff(x;a,b) 
00040   RooRealVar a("a","a",0.4,0,1) ;
00041   RooRealVar b("b","b",5) ;
00042   RooRealVar c("c","c",-1,-10,10) ;
00043   RooFormulaVar effFunc("effFunc","(1-a)+a*cos((x-c)/b)",RooArgList(a,b,c,x)) ;
00044 
00045 
00046 
00047   // 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 ) 
00048   // ------------------------------------------------------------------------------------------
00049 
00050   // Acceptance state cut (1 or 0)
00051   RooCategory cut("cut","cutr") ;
00052   cut.defineType("accept",1) ;
00053   cut.defineType("reject",0) ;
00054 
00055   // Construct efficiency p.d.f eff(cut|x)
00056   RooEfficiency effPdf("effPdf","effPdf",effFunc,cut,"accept") ;
00057 
00058 
00059 
00060   // 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 
00061   // -----------------------------------------------------------------------------
00062 
00063   // Construct global shape p.d.f shape(x) and product model(x,cut) = eff(cut|x)*shape(x) 
00064   // (These are _only_ needed to generate some toy MC here to be used later)
00065   RooPolynomial shapePdf("shapePdf","shapePdf",x,RooConst(-0.095)) ;
00066   RooProdPdf model("model","model",shapePdf,Conditional(effPdf,cut)) ;
00067 
00068   // Generate some toy data from model
00069   RooDataSet* data = model.generate(RooArgSet(x,cut),10000) ;
00070 
00071 
00072 
00073   // 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 
00074   // --------------------------------------------------------------------------
00075 
00076   // Fit conditional efficiency p.d.f to data
00077   effPdf.fitTo(*data,ConditionalObservables(x)) ;
00078 
00079 
00080 
00081   // P l o t   f i t t e d ,   d a t a   e f f i c i e n c y  
00082   // --------------------------------------------------------
00083 
00084   // Plot distribution of all events and accepted fraction of events on frame
00085   RooPlot* frame1 = x.frame(Bins(20),Title("Data (all, accepted)")) ;
00086   data->plotOn(frame1) ;
00087   data->plotOn(frame1,Cut("cut==cut::accept"),MarkerColor(kRed),LineColor(kRed)) ;
00088 
00089   // Plot accept/reject efficiency on data overlay fitted efficiency curve
00090   RooPlot* frame2 = x.frame(Bins(20),Title("Fitted efficiency")) ;
00091   data->plotOn(frame2,Efficiency(cut)) ; // needs ROOT version >= 5.21
00092   effFunc.plotOn(frame2,LineColor(kRed)) ;
00093 
00094 
00095 
00096   // Draw all frames on a canvas
00097   TCanvas* ca = new TCanvas("rf701_efficiency","rf701_efficiency",800,400) ;
00098   ca->Divide(2) ;
00099   ca->cd(1) ; gPad->SetLeftMargin(0.15) ; frame1->GetYaxis()->SetTitleOffset(1.6) ; frame1->Draw() ;
00100   ca->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ;
00101   
00102  
00103 }

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