rf801_mcstudy.C

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'VALIDATION AND MC STUDIES' RooFit tutorial macro #801
00004 // 
00005 // A Toy Monte Carlo study that perform cycles of
00006 // event generation and fittting
00007 //
00008 // 
00009 /////////////////////////////////////////////////////////////////////////
00010 
00011 #ifndef __CINT__
00012 #include "RooGlobalFunc.h"
00013 #endif
00014 #include "RooRealVar.h"
00015 #include "RooDataSet.h"
00016 #include "RooGaussian.h"
00017 #include "RooConstVar.h"
00018 #include "RooChebychev.h"
00019 #include "RooAddPdf.h"
00020 #include "RooMCStudy.h"
00021 #include "RooPlot.h"
00022 #include "TCanvas.h"
00023 #include "TAxis.h"
00024 #include "TH2.h"
00025 #include "RooFitResult.h"
00026 #include "TStyle.h"
00027 #include "TDirectory.h"
00028 
00029 using namespace RooFit ;
00030 
00031 
00032 void rf801_mcstudy()
00033 {
00034   // C r e a t e   m o d e l
00035   // -----------------------
00036 
00037   // Declare observable x
00038   RooRealVar x("x","x",0,10) ;
00039   x.setBins(40) ;
00040 
00041   // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
00042   RooRealVar mean("mean","mean of gaussians",5,0,10) ;
00043   RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
00044   RooRealVar sigma2("sigma2","width of gaussians",1) ;
00045 
00046   RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
00047   RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
00048   
00049   // Build Chebychev polynomial p.d.f.  
00050   RooRealVar a0("a0","a0",0.5,0.,1.) ;
00051   RooRealVar a1("a1","a1",-0.2,-1,1.) ;
00052   RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
00053 
00054   // Sum the signal components into a composite signal p.d.f.
00055   RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
00056   RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
00057 
00058   // Sum the composite signal and background 
00059   RooRealVar nbkg("nbkg","number of background events,",150,0,1000) ;
00060   RooRealVar nsig("nsig","number of signal events",150,0,1000) ;
00061   RooAddPdf  model("model","g1+g2+a",RooArgList(bkg,sig),RooArgList(nbkg,nsig)) ;
00062 
00063 
00064 
00065   // C r e a t e   m a n a g e r
00066   // ---------------------------
00067 
00068   // Instantiate RooMCStudy manager on model with x as observable and given choice of fit options
00069   //
00070   // The Silence() option kills all messages below the PROGRESS level, leaving only a single message
00071   // per sample executed, and any error message that occur during fitting
00072   //
00073   // The Extended() option has two effects: 
00074   //    1) The extended ML term is included in the likelihood and 
00075   //    2) A poisson fluctuation is introduced on the number of generated events 
00076   //
00077   // The FitOptions() given here are passed to the fitting stage of each toy experiment.
00078   // If Save() is specified, the fit result of each experiment is saved by the manager  
00079   //
00080   // A Binned() option is added in this example to bin the data between generation and fitting
00081   // to speed up the study at the expemse of some precision
00082 
00083   RooMCStudy* mcstudy = new RooMCStudy(model,x,Binned(kTRUE),Silence(),Extended(),
00084                                        FitOptions(Save(kTRUE),PrintEvalErrors(0))) ;
00085   
00086 
00087   // G e n e r a t e   a n d   f i t   e v e n t s
00088   // ---------------------------------------------
00089 
00090   // Generate and fit 1000 samples of Poisson(nExpected) events
00091   mcstudy->generateAndFit(1000) ;
00092 
00093 
00094 
00095   // E x p l o r e   r e s u l t s   o f   s t u d y 
00096   // ------------------------------------------------
00097 
00098   // Make plots of the distributions of mean, the error on mean and the pull of mean
00099   RooPlot* frame1 = mcstudy->plotParam(mean,Bins(40)) ;
00100   RooPlot* frame2 = mcstudy->plotError(mean,Bins(40)) ;
00101   RooPlot* frame3 = mcstudy->plotPull(mean,Bins(40),FitGauss(kTRUE)) ;
00102 
00103   // Plot distribution of minimized likelihood
00104   RooPlot* frame4 = mcstudy->plotNLL(Bins(40)) ;
00105 
00106   // Make some histograms from the parameter dataset
00107   TH1* hh_cor_a0_s1f = mcstudy->fitParDataSet().createHistogram("hh",a1,YVar(sig1frac)) ;
00108   TH1* hh_cor_a0_a1  = mcstudy->fitParDataSet().createHistogram("hh",a0,YVar(a1)) ;
00109 
00110   // Access some of the saved fit results from individual toys
00111   TH2* corrHist000 = mcstudy->fitResult(0)->correlationHist("c000") ;
00112   TH2* corrHist127 = mcstudy->fitResult(127)->correlationHist("c127") ;
00113   TH2* corrHist953 = mcstudy->fitResult(953)->correlationHist("c953") ;
00114 
00115 
00116 
00117   // Draw all plots on a canvas
00118   gStyle->SetPalette(1) ;
00119   gStyle->SetOptStat(0) ;
00120   TCanvas* c = new TCanvas("rf801_mcstudy","rf801_mcstudy",900,900) ;
00121   c->Divide(3,3) ;
00122   c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame1->GetYaxis()->SetTitleOffset(1.4) ; frame1->Draw() ;
00123   c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ;
00124   c->cd(3) ; gPad->SetLeftMargin(0.15) ; frame3->GetYaxis()->SetTitleOffset(1.4) ; frame3->Draw() ;
00125   c->cd(4) ; gPad->SetLeftMargin(0.15) ; frame4->GetYaxis()->SetTitleOffset(1.4) ; frame4->Draw() ;
00126   c->cd(5) ; gPad->SetLeftMargin(0.15) ; hh_cor_a0_s1f->GetYaxis()->SetTitleOffset(1.4) ; hh_cor_a0_s1f->Draw("box") ;
00127   c->cd(6) ; gPad->SetLeftMargin(0.15) ; hh_cor_a0_a1->GetYaxis()->SetTitleOffset(1.4) ; hh_cor_a0_a1->Draw("box") ;
00128   c->cd(7) ; gPad->SetLeftMargin(0.15) ; corrHist000->GetYaxis()->SetTitleOffset(1.4) ; corrHist000->Draw("colz") ;
00129   c->cd(8) ; gPad->SetLeftMargin(0.15) ; corrHist127->GetYaxis()->SetTitleOffset(1.4) ; corrHist127->Draw("colz") ;
00130   c->cd(9) ; gPad->SetLeftMargin(0.15) ; corrHist953->GetYaxis()->SetTitleOffset(1.4) ; corrHist953->Draw("colz") ;
00131 
00132   // Make RooMCStudy object available on command line after
00133   // macro finishes
00134   gDirectory->Add(mcstudy) ;
00135 }

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