rf103_interprfuncs.C

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'BASIC FUNCTIONALITY' RooFit tutorial macro #103
00004 // 
00005 // Interpreted functions and p.d.f.s
00006 //
00007 // 
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 "TCanvas.h"
00020 #include "TAxis.h"
00021 #include "RooPlot.h"
00022 #include "RooFitResult.h"
00023 #include "RooGenericPdf.h"
00024 #include "RooConstVar.h"
00025 
00026 using namespace RooFit ;
00027 
00028 
00029 void rf103_interprfuncs()
00030 {
00031   /////////////////////////////////////////////////////////
00032   // G e n e r i c   i n t e r p r e t e d   p . d . f . //
00033   /////////////////////////////////////////////////////////
00034 
00035   // Declare observable x
00036   RooRealVar x("x","x",-20,20) ;
00037 
00038   // C o n s t r u c t   g e n e r i c   p d f   f r o m   i n t e r p r e t e d   e x p r e s s i o n
00039   // -------------------------------------------------------------------------------------------------
00040 
00041   // To construct a proper p.d.f, the formula expression is explicitly normalized internally by dividing 
00042   // it by a numeric integral of the expresssion over x in the range [-20,20] 
00043   //
00044   RooRealVar alpha("alpha","alpha",5,0.1,10) ;
00045   RooGenericPdf genpdf("genpdf","genpdf","(1+0.1*abs(x)+sin(sqrt(abs(x*alpha+0.1))))",RooArgSet(x,alpha)) ;
00046 
00047 
00048   // S a m p l e ,   f i t   a n d   p l o t   g e n e r i c   p d f
00049   // ---------------------------------------------------------------
00050 
00051   // Generate a toy dataset from the interpreted p.d.f
00052   RooDataSet* data = genpdf.generate(x,10000) ;
00053 
00054   // Fit the interpreted p.d.f to the generated data
00055   genpdf.fitTo(*data) ;
00056 
00057   // Make a plot of the data and the p.d.f overlaid
00058   RooPlot* xframe = x.frame(Title("Interpreted expression pdf")) ;
00059   data->plotOn(xframe) ;
00060   genpdf.plotOn(xframe) ;  
00061 
00062 
00063   /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00064   // S t a n d a r d   p . d . f   a d j u s t   w i t h   i n t e r p r e t e d   h e l p e r   f u n c t i o n //
00065   //                                                                                                             //
00066   // Make a gauss(x,sqrt(mean2),sigma) from a standard RooGaussian                                               //
00067   //                                                                                                             //
00068   /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00069 
00070 
00071   // C o n s t r u c t   s t a n d a r d   p d f  w i t h   f o r m u l a   r e p l a c i n g   p a r a m e t e r
00072   // ------------------------------------------------------------------------------------------------------------
00073 
00074   // Construct parameter mean2 and sigma
00075   RooRealVar mean2("mean2","mean^2",10,0,200) ;
00076   RooRealVar sigma("sigma","sigma",3,0.1,10) ;
00077 
00078   // Construct interpreted function mean = sqrt(mean^2)
00079   RooFormulaVar mean("mean","mean","sqrt(mean2)",mean2) ;
00080 
00081   // Construct a gaussian g2(x,sqrt(mean2),sigma) ;
00082   RooGaussian g2("g2","h2",x,mean,sigma) ;
00083 
00084 
00085   // G e n e r a t e   t o y   d a t a 
00086   // ---------------------------------
00087 
00088   // Construct a separate gaussian g1(x,10,3) to generate a toy Gaussian dataset with mean 10 and width 3
00089   RooGaussian g1("g1","g1",x,RooConst(10),RooConst(3)) ;
00090   RooDataSet* data2 = g1.generate(x,1000) ;
00091 
00092 
00093   // F i t   a n d   p l o t   t a i l o r e d   s t a n d a r d   p d f 
00094   // -------------------------------------------------------------------
00095 
00096   // Fit g2 to data from g1
00097   RooFitResult* r = g2.fitTo(*data2,Save()) ;
00098   r->Print() ;
00099 
00100   // Plot data on frame and overlay projection of g2
00101   RooPlot* xframe2 = x.frame(Title("Tailored Gaussian pdf")) ;
00102   data2->plotOn(xframe2) ;
00103   g2.plotOn(xframe2) ;
00104  
00105 
00106   // Draw all frames on a canvas
00107   TCanvas* c = new TCanvas("rf103_interprfuncs","rf103_interprfuncs",800,400) ;
00108   c->Divide(2) ;
00109   c->cd(1) ; gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.4) ; xframe->Draw() ;
00110   c->cd(2) ; gPad->SetLeftMargin(0.15) ; xframe2->GetYaxis()->SetTitleOffset(1.4) ; xframe2->Draw() ;
00111 
00112   
00113 }

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