## From \$ROOTSYS/tutorials/roofit/rf804_mcstudy_constr.C

```/////////////////////////////////////////////////////////////////////////
//
// 'VALIDATION AND MC STUDIES' RooFit tutorial macro #804
//
// Using RooMCStudy on models with constrains
//
//
// 07/2008 - Wouter Verkerke
//
/////////////////////////////////////////////////////////////////////////

#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "RooPolynomial.h"
#include "RooProdPdf.h"
#include "RooMCStudy.h"
#include "RooPlot.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "TH1.h"
using namespace RooFit ;

void rf804_mcstudy_constr()
{
// C r e a t e   m o d e l   w i t h   p a r a m e t e r   c o n s t r a i n t
// ---------------------------------------------------------------------------

// Observable
RooRealVar x("x","x",-10,10) ;

// Signal component
RooRealVar m("m","m",0,-10,10) ;
RooRealVar s("s","s",2,0.1,10) ;
RooGaussian g("g","g",x,m,s) ;

// Background component
RooPolynomial p("p","p",x) ;

// Composite model
RooRealVar f("f","f",0.4,0.,1.) ;

// Construct constraint on parameter f
RooGaussian fconstraint("fconstraint","fconstraint",f,RooConst(0.7),RooConst(0.1)) ;

// Multiply constraint with p.d.f
RooProdPdf sumc("sumc","sum with constraint",RooArgSet(sum,fconstraint)) ;

// S e t u p   t o y   s t u d y   w i t h   m o d e l
// ---------------------------------------------------

// Perform toy study with internal constraint on f
RooMCStudy mcs(sumc,x,Constrain(f),Silence(),Binned(),FitOptions(PrintLevel(-1))) ;

// Run 500 toys of 2000 events.
// Before each toy is generated, a value for the f is sampled from the constraint pdf and
// that value is used for the generation of that toy.
mcs.generateAndFit(500,2000) ;

// Make plot of distribution of generated value of f parameter
TH1* h_f_gen = mcs.fitParDataSet().createHistogram("f_gen",-40) ;

// Make plot of distribution of fitted value of f parameter
RooPlot* frame1  = mcs.plotParam(f,Bins(40)) ;
frame1->SetTitle("Distribution of fitted f values") ;

// Make plot of pull distribution on f
RooPlot* frame2 = mcs.plotPull(f,Bins(40),FitGauss()) ;
frame1->SetTitle("Distribution of f pull values") ;

TCanvas* c = new TCanvas("rf804_mcstudy_constr","rf804_mcstudy_constr",1200,400) ;
c->Divide(3) ;
c->cd(1) ; gPad->SetLeftMargin(0.15) ; h_f_gen->GetYaxis()->SetTitleOffset(1.4) ; h_f_gen->Draw() ;
c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame1->GetYaxis()->SetTitleOffset(1.4) ; frame1->Draw() ;
c->cd(3) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ;

}

```
rf804_mcstudy_constr.C:1
rf804_mcstudy_constr.C:2
rf804_mcstudy_constr.C:3
rf804_mcstudy_constr.C:4
rf804_mcstudy_constr.C:5
rf804_mcstudy_constr.C:6
rf804_mcstudy_constr.C:7
rf804_mcstudy_constr.C:8
rf804_mcstudy_constr.C:9
rf804_mcstudy_constr.C:10
rf804_mcstudy_constr.C:11
rf804_mcstudy_constr.C:12
rf804_mcstudy_constr.C:13
rf804_mcstudy_constr.C:14
rf804_mcstudy_constr.C:15
rf804_mcstudy_constr.C:16
rf804_mcstudy_constr.C:17
rf804_mcstudy_constr.C:18
rf804_mcstudy_constr.C:19
rf804_mcstudy_constr.C:20
rf804_mcstudy_constr.C:21
rf804_mcstudy_constr.C:22
rf804_mcstudy_constr.C:23
rf804_mcstudy_constr.C:24
rf804_mcstudy_constr.C:25
rf804_mcstudy_constr.C:26
rf804_mcstudy_constr.C:27
rf804_mcstudy_constr.C:28
rf804_mcstudy_constr.C:29
rf804_mcstudy_constr.C:30
rf804_mcstudy_constr.C:31
rf804_mcstudy_constr.C:32
rf804_mcstudy_constr.C:33
rf804_mcstudy_constr.C:34
rf804_mcstudy_constr.C:35
rf804_mcstudy_constr.C:36
rf804_mcstudy_constr.C:37
rf804_mcstudy_constr.C:38
rf804_mcstudy_constr.C:39
rf804_mcstudy_constr.C:40
rf804_mcstudy_constr.C:41
rf804_mcstudy_constr.C:42
rf804_mcstudy_constr.C:43
rf804_mcstudy_constr.C:44
rf804_mcstudy_constr.C:45
rf804_mcstudy_constr.C:46
rf804_mcstudy_constr.C:47
rf804_mcstudy_constr.C:48
rf804_mcstudy_constr.C:49
rf804_mcstudy_constr.C:50
rf804_mcstudy_constr.C:51
rf804_mcstudy_constr.C:52
rf804_mcstudy_constr.C:53
rf804_mcstudy_constr.C:54
rf804_mcstudy_constr.C:55
rf804_mcstudy_constr.C:56
rf804_mcstudy_constr.C:57
rf804_mcstudy_constr.C:58
rf804_mcstudy_constr.C:59
rf804_mcstudy_constr.C:60
rf804_mcstudy_constr.C:61
rf804_mcstudy_constr.C:62
rf804_mcstudy_constr.C:63
rf804_mcstudy_constr.C:64
rf804_mcstudy_constr.C:65
rf804_mcstudy_constr.C:66
rf804_mcstudy_constr.C:67
rf804_mcstudy_constr.C:68
rf804_mcstudy_constr.C:69
rf804_mcstudy_constr.C:70
rf804_mcstudy_constr.C:71
rf804_mcstudy_constr.C:72
rf804_mcstudy_constr.C:73
rf804_mcstudy_constr.C:74
rf804_mcstudy_constr.C:75
rf804_mcstudy_constr.C:76
rf804_mcstudy_constr.C:77
rf804_mcstudy_constr.C:78
rf804_mcstudy_constr.C:79
rf804_mcstudy_constr.C:80
rf804_mcstudy_constr.C:81
rf804_mcstudy_constr.C:82
rf804_mcstudy_constr.C:83
rf804_mcstudy_constr.C:84
rf804_mcstudy_constr.C:85
rf804_mcstudy_constr.C:86
rf804_mcstudy_constr.C:87
rf804_mcstudy_constr.C:88
rf804_mcstudy_constr.C:89
rf804_mcstudy_constr.C:90