rf802_mcstudy_addons.cxx

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'VALIDATION AND MC STUDIES' RooFit tutorial macro #802
00004 // 
00005 // RooMCStudy: using separate fit and generator models, using the chi^2 calculator model 
00006 //
00007 // 
00008 // 07/2008 - Wouter Verkerke 
00009 //
00010 /////////////////////////////////////////////////////////////////////////
00011 
00012 #ifndef __CINT__
00013 #include "RooGlobalFunc.h"
00014 #endif
00015 #include "RooRealVar.h"
00016 #include "RooDataSet.h"
00017 #include "RooGaussian.h"
00018 #include "RooChebychev.h"
00019 #include "RooAddPdf.h"
00020 #include "RooMCStudy.h"
00021 #include "RooChi2MCSModule.h"
00022 #include "RooPlot.h"
00023 #include "TCanvas.h"
00024 #include "TH1.h"
00025 #include "TDirectory.h"
00026 
00027 using namespace RooFit ;
00028 
00029 
00030 // Elementary operations on a gaussian PDF
00031 class TestBasic802 : public RooFitTestUnit
00032 {
00033 public: 
00034   TestBasic802(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("MC Study with chi^2 calculator",refFile,writeRef,verbose) {} ;
00035   Bool_t testCode() {
00036 
00037   // C r e a t e   m o d e l 
00038   // -----------------------
00039 
00040   // Observables, parameters
00041   RooRealVar x("x","x",-10,10) ;
00042   x.setBins(10) ;
00043   RooRealVar mean("mean","mean of gaussian",0) ;
00044   RooRealVar sigma("sigma","width of gaussian",5,1,10) ;
00045 
00046   // Create Gaussian pdf
00047   RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma) ;  
00048 
00049 
00050 
00051   // C r e a t e   m a n a g e r  w i t h   c h i ^ 2   a d d - o n   m o d u l e
00052   // ----------------------------------------------------------------------------
00053 
00054   // Create study manager for binned likelihood fits of a Gaussian pdf in 10 bins
00055   RooMCStudy* mcs = new RooMCStudy(gauss,x,Silence(),Binned()) ;
00056 
00057   // Add chi^2 calculator module to mcs
00058   RooChi2MCSModule chi2mod ;
00059   mcs->addModule(chi2mod) ;
00060 
00061   // Generate 200 samples of 1000 events
00062   mcs->generateAndFit(200,1000) ;
00063   
00064   // Fill histograms with distributions chi2 and prob(chi2,ndf) that
00065   // are calculated by RooChiMCSModule
00066 
00067   RooRealVar* chi2 = (RooRealVar*) mcs->fitParDataSet().get()->find("chi2") ;
00068   RooRealVar* prob = (RooRealVar*) mcs->fitParDataSet().get()->find("prob") ;
00069 
00070   TH1* h_chi2  = new TH1F("h_chi2","",40,0,20) ;
00071   TH1* h_prob  = new TH1F("h_prob","",40,0,1) ;
00072 
00073   mcs->fitParDataSet().fillHistogram(h_chi2,*chi2) ; 
00074   mcs->fitParDataSet().fillHistogram(h_prob,*prob) ;   
00075 
00076 
00077 
00078   // C r e a t e   m a n a g e r  w i t h   s e p a r a t e   f i t   m o d e l 
00079   // ----------------------------------------------------------------------------
00080 
00081   // Create alternate pdf with shifted mean
00082   RooRealVar mean2("mean2","mean of gaussian 2",0.5) ;
00083   RooGaussian gauss2("gauss2","gaussian PDF2",x,mean2,sigma) ;  
00084 
00085   // Create study manager with separate generation and fit model. This configuration
00086   // is set up to generate bad fits as the fit and generator model have different means
00087   // and the mean parameter is not floating in the fit
00088   RooMCStudy* mcs2 = new RooMCStudy(gauss2,x,FitModel(gauss),Silence(),Binned()) ;
00089 
00090   // Add chi^2 calculator module to mcs
00091   RooChi2MCSModule chi2mod2 ;
00092   mcs2->addModule(chi2mod2) ;
00093 
00094   // Generate 200 samples of 1000 events
00095   mcs2->generateAndFit(200,1000) ;
00096   
00097   // Fill histograms with distributions chi2 and prob(chi2,ndf) that
00098   // are calculated by RooChiMCSModule
00099 
00100   TH1* h2_chi2  = new TH1F("h2_chi2","",40,0,20) ;
00101   TH1* h2_prob  = new TH1F("h2_prob","",40,0,1) ;
00102   
00103   mcs2->fitParDataSet().fillHistogram(h2_chi2,*chi2) ; 
00104   mcs2->fitParDataSet().fillHistogram(h2_prob,*prob) ;   
00105 
00106   h_chi2->SetLineColor(kRed) ;
00107   h_prob->SetLineColor(kRed) ;
00108 
00109   regTH(h_chi2,"rf802_hist_chi2") ;
00110   regTH(h2_chi2,"rf802_hist2_chi2") ;
00111   regTH(h_prob,"rf802_hist_prob") ;
00112   regTH(h2_prob,"rf802_hist2_prob") ;
00113 
00114   delete mcs ;
00115   delete mcs2 ;
00116 
00117   return kTRUE ;  
00118   }
00119 } ;

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