rf501_simultaneouspdf.cxx

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #501
00004 // 
00005 // Using simultaneous p.d.f.s to describe simultaneous fits to multiple
00006 // datasets
00007 //
00008 //
00009 //
00010 // 07/2008 - Wouter Verkerke 
00011 // 
00012 /////////////////////////////////////////////////////////////////////////
00013 
00014 #ifndef __CINT__
00015 #include "RooGlobalFunc.h"
00016 #endif
00017 #include "RooRealVar.h"
00018 #include "RooDataSet.h"
00019 #include "RooGaussian.h"
00020 #include "RooChebychev.h"
00021 #include "RooAddPdf.h"
00022 #include "RooSimultaneous.h"
00023 #include "RooCategory.h"
00024 #include "TCanvas.h"
00025 #include "RooPlot.h"
00026 using namespace RooFit ;
00027 
00028 
00029 class TestBasic501 : public RooFitTestUnit
00030 {
00031 public: 
00032   TestBasic501(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Simultaneous p.d.f. operator",refFile,writeRef,verbose) {} ;
00033   Bool_t testCode() {
00034 
00035   // C r e a t e   m o d e l   f o r   p h y s i c s   s a m p l e
00036   // -------------------------------------------------------------
00037 
00038   // Create observables
00039   RooRealVar x("x","x",-8,8) ;
00040 
00041   // Construct signal pdf
00042   RooRealVar mean("mean","mean",0,-8,8) ;
00043   RooRealVar sigma("sigma","sigma",0.3,0.1,10) ;
00044   RooGaussian gx("gx","gx",x,mean,sigma) ;
00045 
00046   // Construct background pdf
00047   RooRealVar a0("a0","a0",-0.1,-1,1) ;
00048   RooRealVar a1("a1","a1",0.004,-1,1) ;
00049   RooChebychev px("px","px",x,RooArgSet(a0,a1)) ;
00050 
00051   // Construct composite pdf
00052   RooRealVar f("f","f",0.2,0.,1.) ;
00053   RooAddPdf model("model","model",RooArgList(gx,px),f) ;
00054 
00055 
00056 
00057   // C r e a t e   m o d e l   f o r   c o n t r o l   s a m p l e
00058   // --------------------------------------------------------------
00059 
00060   // Construct signal pdf. 
00061   // NOTE that sigma is shared with the signal sample model
00062   RooRealVar mean_ctl("mean_ctl","mean_ctl",-3,-8,8) ;
00063   RooGaussian gx_ctl("gx_ctl","gx_ctl",x,mean_ctl,sigma) ;
00064 
00065   // Construct the background pdf
00066   RooRealVar a0_ctl("a0_ctl","a0_ctl",-0.1,-1,1) ;
00067   RooRealVar a1_ctl("a1_ctl","a1_ctl",0.5,-0.1,1) ;
00068   RooChebychev px_ctl("px_ctl","px_ctl",x,RooArgSet(a0_ctl,a1_ctl)) ;
00069 
00070   // Construct the composite model
00071   RooRealVar f_ctl("f_ctl","f_ctl",0.5,0.,1.) ;
00072   RooAddPdf model_ctl("model_ctl","model_ctl",RooArgList(gx_ctl,px_ctl),f_ctl) ;
00073 
00074 
00075 
00076   // G e n e r a t e   e v e n t s   f o r   b o t h   s a m p l e s 
00077   // ---------------------------------------------------------------
00078 
00079   // Generate 1000 events in x and y from model
00080   RooDataSet *data = model.generate(RooArgSet(x),100) ;
00081   RooDataSet *data_ctl = model_ctl.generate(RooArgSet(x),2000) ;
00082 
00083 
00084 
00085   // C r e a t e   i n d e x   c a t e g o r y   a n d   j o i n   s a m p l e s 
00086   // ---------------------------------------------------------------------------
00087 
00088   // Define category to distinguish physics and control samples events
00089   RooCategory sample("sample","sample") ;
00090   sample.defineType("physics") ;
00091   sample.defineType("control") ;
00092 
00093   // Construct combined dataset in (x,sample)
00094   RooDataSet combData("combData","combined data",x,Index(sample),Import("physics",*data),Import("control",*data_ctl)) ;
00095 
00096 
00097 
00098   // C o n s t r u c t   a   s i m u l t a n e o u s   p d f   i n   ( x , s a m p l e )
00099   // -----------------------------------------------------------------------------------
00100 
00101   // Construct a simultaneous pdf using category sample as index
00102   RooSimultaneous simPdf("simPdf","simultaneous pdf",sample) ;
00103 
00104   // Associate model with the physics state and model_ctl with the control state
00105   simPdf.addPdf(model,"physics") ;
00106   simPdf.addPdf(model_ctl,"control") ;
00107 
00108 
00109 
00110   // P e r f o r m   a   s i m u l t a n e o u s   f i t
00111   // ---------------------------------------------------
00112 
00113   // Perform simultaneous fit of model to data and model_ctl to data_ctl
00114   simPdf.fitTo(combData) ;
00115 
00116 
00117 
00118   // P l o t   m o d e l   s l i c e s   o n   d a t a    s l i c e s 
00119   // ----------------------------------------------------------------
00120 
00121   // Make a frame for the physics sample
00122   RooPlot* frame1 = x.frame(Bins(30),Title("Physics sample")) ;
00123 
00124   // Plot all data tagged as physics sample
00125   combData.plotOn(frame1,Cut("sample==sample::physics")) ;
00126 
00127   // Plot "physics" slice of simultaneous pdf. 
00128   // NBL You _must_ project the sample index category with data using ProjWData 
00129   // as a RooSimultaneous makes no prediction on the shape in the index category 
00130   // and can thus not be integrated
00131   simPdf.plotOn(frame1,Slice(sample,"physics"),ProjWData(sample,combData)) ;
00132   simPdf.plotOn(frame1,Slice(sample,"physics"),Components("px"),ProjWData(sample,combData),LineStyle(kDashed)) ;
00133 
00134   // The same plot for the control sample slice
00135   RooPlot* frame2 = x.frame(Bins(30),Title("Control sample")) ;
00136   combData.plotOn(frame2,Cut("sample==sample::control")) ;
00137   simPdf.plotOn(frame2,Slice(sample,"control"),ProjWData(sample,combData)) ;
00138   simPdf.plotOn(frame2,Slice(sample,"control"),Components("px_ctl"),ProjWData(sample,combData),LineStyle(kDashed)) ;
00139 
00140 
00141   regPlot(frame1,"rf501_plot1") ;
00142   regPlot(frame2,"rf501_plot2") ;
00143 
00144   delete data ;
00145   delete data_ctl ;
00146 
00147   return kTRUE ;
00148 
00149   }
00150 
00151 } ;

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