From $ROOTSYS/tutorials/roofit/rf802_mcstudy_addons.C

/////////////////////////////////////////////////////////////////////////
//
// 'VALIDATION AND MC STUDIES' RooFit tutorial macro #802
// 
// RooMCStudy: using separate fit and generator models, using the chi^2 calculator model 
//
// 
// 07/2008 - Wouter Verkerke 
//
/////////////////////////////////////////////////////////////////////////

#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "RooChebychev.h"
#include "RooAddPdf.h"
#include "RooMCStudy.h"
#include "RooChi2MCSModule.h"
#include "RooPlot.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "TH1.h"
#include "TDirectory.h"

using namespace RooFit ;


void rf802_mcstudy_addons()
{

  // C r e a t e   m o d e l 
  // -----------------------

  // Observables, parameters
  RooRealVar x("x","x",-10,10) ;
  x.setBins(10) ;
  RooRealVar mean("mean","mean of gaussian",0) ;
  RooRealVar sigma("sigma","width of gaussian",5,1,10) ;

  // Create Gaussian pdf
  RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma) ;  



  // 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
  // ----------------------------------------------------------------------------

  // Create study manager for binned likelihood fits of a Gaussian pdf in 10 bins
  RooMCStudy* mcs = new RooMCStudy(gauss,x,Silence(),Binned()) ;

  // Add chi^2 calculator module to mcs
  RooChi2MCSModule chi2mod ;
  mcs->addModule(chi2mod) ;

  // Generate 1000 samples of 1000 events
  mcs->generateAndFit(2000,1000) ;
  
  // Fill histograms with distributions chi2 and prob(chi2,ndf) that
  // are calculated by RooChiMCSModule
  TH1* hist_chi2 = mcs->fitParDataSet().createHistogram("chi2") ; 
  TH1* hist_prob = mcs->fitParDataSet().createHistogram("prob") ;   



  // 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 
  // ----------------------------------------------------------------------------

  // Create alternate pdf with shifted mean
  RooRealVar mean2("mean2","mean of gaussian 2",0.5) ;
  RooGaussian gauss2("gauss2","gaussian PDF2",x,mean2,sigma) ;  

  // Create study manager with separate generation and fit model. This configuration
  // is set up to generate bad fits as the fit and generator model have different means
  // and the mean parameter is not floating in the fit
  RooMCStudy* mcs2 = new RooMCStudy(gauss2,x,FitModel(gauss),Silence(),Binned()) ;

  // Add chi^2 calculator module to mcs
  RooChi2MCSModule chi2mod2 ;
  mcs2->addModule(chi2mod2) ;

  // Generate 1000 samples of 1000 events
  mcs2->generateAndFit(2000,1000) ;
  
  // Fill histograms with distributions chi2 and prob(chi2,ndf) that
  // are calculated by RooChiMCSModule
  TH1* hist2_chi2 = mcs2->fitParDataSet().createHistogram("chi2") ; 
  TH1* hist2_prob = mcs2->fitParDataSet().createHistogram("prob") ;   
  hist2_chi2->SetLineColor(kRed) ;
  hist2_prob->SetLineColor(kRed) ;

  

  TCanvas* c = new TCanvas("rf802_mcstudy_addons","rf802_mcstudy_addons",800,400) ;
  c->Divide(2) ;
  c->cd(1) ; gPad->SetLeftMargin(0.15) ; hist_chi2->GetYaxis()->SetTitleOffset(1.4) ; hist_chi2->Draw() ; hist2_chi2->Draw("esame") ;
  c->cd(2) ; gPad->SetLeftMargin(0.15) ; hist_prob->GetYaxis()->SetTitleOffset(1.4) ; hist_prob->Draw() ; hist2_prob->Draw("esame") ;


  
  // Make RooMCStudy object available on command line after
  // macro finishes
  gDirectory->Add(mcs) ;
}
 rf802_mcstudy_addons.C:1
 rf802_mcstudy_addons.C:2
 rf802_mcstudy_addons.C:3
 rf802_mcstudy_addons.C:4
 rf802_mcstudy_addons.C:5
 rf802_mcstudy_addons.C:6
 rf802_mcstudy_addons.C:7
 rf802_mcstudy_addons.C:8
 rf802_mcstudy_addons.C:9
 rf802_mcstudy_addons.C:10
 rf802_mcstudy_addons.C:11
 rf802_mcstudy_addons.C:12
 rf802_mcstudy_addons.C:13
 rf802_mcstudy_addons.C:14
 rf802_mcstudy_addons.C:15
 rf802_mcstudy_addons.C:16
 rf802_mcstudy_addons.C:17
 rf802_mcstudy_addons.C:18
 rf802_mcstudy_addons.C:19
 rf802_mcstudy_addons.C:20
 rf802_mcstudy_addons.C:21
 rf802_mcstudy_addons.C:22
 rf802_mcstudy_addons.C:23
 rf802_mcstudy_addons.C:24
 rf802_mcstudy_addons.C:25
 rf802_mcstudy_addons.C:26
 rf802_mcstudy_addons.C:27
 rf802_mcstudy_addons.C:28
 rf802_mcstudy_addons.C:29
 rf802_mcstudy_addons.C:30
 rf802_mcstudy_addons.C:31
 rf802_mcstudy_addons.C:32
 rf802_mcstudy_addons.C:33
 rf802_mcstudy_addons.C:34
 rf802_mcstudy_addons.C:35
 rf802_mcstudy_addons.C:36
 rf802_mcstudy_addons.C:37
 rf802_mcstudy_addons.C:38
 rf802_mcstudy_addons.C:39
 rf802_mcstudy_addons.C:40
 rf802_mcstudy_addons.C:41
 rf802_mcstudy_addons.C:42
 rf802_mcstudy_addons.C:43
 rf802_mcstudy_addons.C:44
 rf802_mcstudy_addons.C:45
 rf802_mcstudy_addons.C:46
 rf802_mcstudy_addons.C:47
 rf802_mcstudy_addons.C:48
 rf802_mcstudy_addons.C:49
 rf802_mcstudy_addons.C:50
 rf802_mcstudy_addons.C:51
 rf802_mcstudy_addons.C:52
 rf802_mcstudy_addons.C:53
 rf802_mcstudy_addons.C:54
 rf802_mcstudy_addons.C:55
 rf802_mcstudy_addons.C:56
 rf802_mcstudy_addons.C:57
 rf802_mcstudy_addons.C:58
 rf802_mcstudy_addons.C:59
 rf802_mcstudy_addons.C:60
 rf802_mcstudy_addons.C:61
 rf802_mcstudy_addons.C:62
 rf802_mcstudy_addons.C:63
 rf802_mcstudy_addons.C:64
 rf802_mcstudy_addons.C:65
 rf802_mcstudy_addons.C:66
 rf802_mcstudy_addons.C:67
 rf802_mcstudy_addons.C:68
 rf802_mcstudy_addons.C:69
 rf802_mcstudy_addons.C:70
 rf802_mcstudy_addons.C:71
 rf802_mcstudy_addons.C:72
 rf802_mcstudy_addons.C:73
 rf802_mcstudy_addons.C:74
 rf802_mcstudy_addons.C:75
 rf802_mcstudy_addons.C:76
 rf802_mcstudy_addons.C:77
 rf802_mcstudy_addons.C:78
 rf802_mcstudy_addons.C:79
 rf802_mcstudy_addons.C:80
 rf802_mcstudy_addons.C:81
 rf802_mcstudy_addons.C:82
 rf802_mcstudy_addons.C:83
 rf802_mcstudy_addons.C:84
 rf802_mcstudy_addons.C:85
 rf802_mcstudy_addons.C:86
 rf802_mcstudy_addons.C:87
 rf802_mcstudy_addons.C:88
 rf802_mcstudy_addons.C:89
 rf802_mcstudy_addons.C:90
 rf802_mcstudy_addons.C:91
 rf802_mcstudy_addons.C:92
 rf802_mcstudy_addons.C:93
 rf802_mcstudy_addons.C:94
 rf802_mcstudy_addons.C:95
 rf802_mcstudy_addons.C:96
 rf802_mcstudy_addons.C:97
 rf802_mcstudy_addons.C:98
 rf802_mcstudy_addons.C:99
 rf802_mcstudy_addons.C:100
 rf802_mcstudy_addons.C:101
 rf802_mcstudy_addons.C:102
 rf802_mcstudy_addons.C:103
 rf802_mcstudy_addons.C:104
 rf802_mcstudy_addons.C:105
 rf802_mcstudy_addons.C:106
 rf802_mcstudy_addons.C:107
 rf802_mcstudy_addons.C:108