rf610_visualerror.C

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #610
00004 // 
00005 // Visualization of errors from a covariance matrix
00006 //
00007 // 
00008 //
00009 // 04/2009 - Wouter Verkerke 
00010 //
00011 /////////////////////////////////////////////////////////////////////////
00012 
00013 #ifndef __CINT__
00014 #include "RooGlobalFunc.h"
00015 #endif
00016 #include "RooRealVar.h"
00017 #include "RooDataHist.h"
00018 #include "RooGaussian.h"
00019 #include "RooConstVar.h"
00020 #include "RooAddPdf.h"
00021 #include "RooPlot.h"
00022 #include "TCanvas.h"
00023 #include "TAxis.h"
00024 #include "TAxis.h"
00025 using namespace RooFit ;
00026 
00027 
00028 void rf610_visualerror()
00029 {
00030   // S e t u p   e x a m p l e   f i t 
00031   // ---------------------------------------
00032 
00033   // Create sum of two Gaussians p.d.f. with factory
00034   RooRealVar x("x","x",-10,10) ;
00035   
00036   RooRealVar m("m","m",0,-10,10) ;
00037   RooRealVar s("s","s",2,1,50) ;
00038   RooGaussian sig("sig","sig",x,m,s) ;
00039 
00040   RooRealVar m2("m2","m2",-1,-10,10) ;
00041   RooRealVar s2("s2","s2",6,1,50) ;
00042   RooGaussian bkg("bkg","bkg",x,m2,s2) ;
00043 
00044   RooRealVar fsig("fsig","fsig",0.33,0,1) ;
00045   RooAddPdf model("model","model",RooArgList(sig,bkg),fsig) ;
00046 
00047   // Create binned dataset
00048   x.setBins(25) ;  
00049   RooAbsData* d = model.generateBinned(x,1000) ;
00050 
00051   // Perform fit and save fit result
00052   RooFitResult* r = model.fitTo(*d,Save()) ;
00053 
00054 
00055   // V i s u a l i z e   f i t   e r r o r 
00056   // -------------------------------------
00057 
00058   // Make plot frame
00059   RooPlot* frame = x.frame(Bins(40),Title("P.d.f with visualized 1-sigma error band")) ;
00060   d->plotOn(frame) ;
00061 
00062   // Visualize 1-sigma error encoded in fit result 'r' as orange band using linear error propagation
00063   // This results in an error band that is by construction symmetric
00064   //
00065   // The linear error is calculated as
00066   // error(x) = Z* F_a(x) * Corr(a,a') F_a'(x)
00067   //
00068   // where     F_a(x) = [ f(x,a+da) - f(x,a-da) ] / 2, 
00069   // 
00070   //         with f(x) = the plotted curve 
00071   //              'da' = error taken from the fit result
00072   //        Corr(a,a') = the correlation matrix from the fit result
00073   //                Z = requested significance 'Z sigma band'
00074   //
00075   // The linear method is fast (required 2*N evaluations of the curve, where N is the number of parameters), 
00076   // but may not be accurate in the presence of strong correlations (~>0.9) and at Z>2 due to linear and 
00077   // Gaussian approximations made
00078   //
00079   model.plotOn(frame,VisualizeError(*r,1),FillColor(kOrange)) ;
00080 
00081 
00082   // Calculate error using sampling method and visualize as dashed red line. 
00083   //
00084   // In this method a number of curves is calculated with variations of the parameter values, as sampled 
00085   // from a multi-variate Gaussian p.d.f. that is constructed from the fit results covariance matrix. 
00086   // The error(x) is determined by calculating a central interval that capture N% of the variations 
00087   // for each valye of x, where N% is controlled by Z (i.e. Z=1 gives N=68%). The number of sampling curves 
00088   // is chosen to be such that at least 100 curves are expected to be outside the N% interval, and is minimally 
00089   // 100 (e.g. Z=1->Ncurve=356, Z=2->Ncurve=2156)) Intervals from the sampling method can be asymmetric, 
00090   // and may perform better in the presence of strong correlations, but may take (much) longer to calculate
00091   model.plotOn(frame,VisualizeError(*r,1,kFALSE),DrawOption("L"),LineWidth(2),LineColor(kRed)) ;
00092   
00093   // Perform the same type of error visualization on the background component only.
00094   // The VisualizeError() option can generally applied to _any_ kind of plot (components, asymmetries, efficiencies etc..)
00095   model.plotOn(frame,VisualizeError(*r,1),FillColor(kOrange),Components("bkg")) ;
00096   model.plotOn(frame,VisualizeError(*r,1,kFALSE),DrawOption("L"),LineWidth(2),LineColor(kRed),Components("bkg"),LineStyle(kDashed)) ;
00097 
00098   // Overlay central value
00099   model.plotOn(frame) ;
00100   model.plotOn(frame,Components("bkg"),LineStyle(kDashed)) ;
00101   d->plotOn(frame) ;
00102   frame->SetMinimum(0) ;
00103 
00104 
00105   // V i s u a l i z e   p a r t i a l   f i t   e r r o r 
00106   // ------------------------------------------------------
00107 
00108   // Make plot frame
00109   RooPlot* frame2 = x.frame(Bins(40),Title("Visualization of 2-sigma partial error from (m,m2)")) ;
00110   
00111   // Visualize partial error. For partial error visualization the covariance matrix is first reduced as follows
00112   //        ___                   -1
00113   // Vred = V22  = V11 - V12 * V22   * V21
00114   //
00115   // Where V11,V12,V21,V22 represent a block decomposition of the covariance matrix into observables that
00116   // are propagated (labeled by index '1') and that are not propagated (labeled by index '2'), and V22bar
00117   // is the Shur complement of V22, calculated as shown above  
00118   //
00119   // (Note that Vred is _not_ a simple sub-matrix of V)
00120 
00121   // Propagate partial error due to shape parameters (m,m2) using linear and sampling method
00122   model.plotOn(frame2,VisualizeError(*r,RooArgSet(m,m2),2),FillColor(kCyan)) ;
00123   model.plotOn(frame2,Components("bkg"),VisualizeError(*r,RooArgSet(m,m2),2),FillColor(kCyan)) ;
00124   
00125   model.plotOn(frame2) ;
00126   model.plotOn(frame2,Components("bkg"),LineStyle(kDashed)) ;
00127   frame2->SetMinimum(0) ;
00128  
00129 
00130   // Make plot frame
00131   RooPlot* frame3 = x.frame(Bins(40),Title("Visualization of 2-sigma partial error from (s,s2)")) ;
00132   
00133   // Propagate partial error due to yield parameter using linear and sampling method
00134   model.plotOn(frame3,VisualizeError(*r,RooArgSet(s,s2),2),FillColor(kGreen)) ;
00135   model.plotOn(frame3,Components("bkg"),VisualizeError(*r,RooArgSet(s,s2),2),FillColor(kGreen)) ;
00136   
00137   model.plotOn(frame3) ;
00138   model.plotOn(frame3,Components("bkg"),LineStyle(kDashed)) ;
00139   frame3->SetMinimum(0) ;
00140 
00141 
00142   // Make plot frame
00143   RooPlot* frame4 = x.frame(Bins(40),Title("Visualization of 2-sigma partial error from fsig")) ;
00144   
00145   // Propagate partial error due to yield parameter using linear and sampling method
00146   model.plotOn(frame4,VisualizeError(*r,RooArgSet(fsig),2),FillColor(kMagenta)) ;
00147   model.plotOn(frame4,Components("bkg"),VisualizeError(*r,RooArgSet(fsig),2),FillColor(kMagenta)) ;
00148   
00149   model.plotOn(frame4) ;
00150   model.plotOn(frame4,Components("bkg"),LineStyle(kDashed)) ;
00151   frame4->SetMinimum(0) ;
00152 
00153 
00154   
00155   TCanvas* c = new TCanvas("rf610_visualerror","rf610_visualerror",800,800) ;
00156   c->Divide(2,2) ;
00157   c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.4)  ; frame->Draw() ;
00158   c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.6) ; frame2->Draw() ;
00159   c->cd(3) ; gPad->SetLeftMargin(0.15) ; frame3->GetYaxis()->SetTitleOffset(1.6) ; frame3->Draw() ;
00160   c->cd(4) ; gPad->SetLeftMargin(0.15) ; frame4->GetYaxis()->SetTitleOffset(1.6) ; frame4->Draw() ;
00161 }

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