# rf804_mcstudy_constr¶

Validation and MC studies: using RooMCStudy on models with constrains

Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, December 06, 2022 at 12:08 PM.

In [1]:
%%cpp -d
#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;


## Create model with parameter constraint¶

Observable

In [2]:
RooRealVar x("x", "x", -10, 10);


Signal component

In [3]:
RooRealVar m("m", "m", 0, -10, 10);
RooRealVar s("s", "s", 2, 0.1, 10);
RooGaussian g("g", "g", x, m, s);


Background component

In [4]:
RooPolynomial p("p", "p", x);


Composite model

In [5]:
RooRealVar f("f", "f", 0.4, 0., 1.);
RooAddPdf sum("sum", "sum", RooArgSet(g, p), f);


Construct constraint on parameter f

In [6]:
RooGaussian fconstraint("fconstraint", "fconstraint", f, RooConst(0.7), RooConst(0.1));


Multiply constraint with pdf

In [7]:
RooProdPdf sumc("sumc", "sum with constraint", RooArgSet(sum, fconstraint));


## Setup toy study with model¶

Perform toy study with internal constraint on f

In [8]:
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.

In [9]:
mcs.generateAndFit(500, 2000);

[#0] PROGRESS:Generation -- RooMCStudy::run: sample 495
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 490
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 485
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 480
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 475
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 470
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 465
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 460
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 455
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 450
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 445
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 440
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 435
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 430
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 425
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 420
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 415
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 410
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 405
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 400
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 395
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 390
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 385
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 380
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 375
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 370
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 365
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 360
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 355
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 350
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 345
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 340
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 335
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 330
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 325
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 320
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 315
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 310
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 305
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 300
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 295
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 290
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 285
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 280
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 275
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 270
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 265
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 260
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 255
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 250
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 245
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 240
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 235
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 230
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 225
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 220
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 215
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 210
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 205
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 200
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 195
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 190
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 185
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 180
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 175
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 170
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 165
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 160
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 155
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 150
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 145
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 140
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 135
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 130
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 125
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 120
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 115
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 110
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 105
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 100
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 95
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 90
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 85
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 80
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 75
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 70
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 65
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 60
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 55
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 50
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 45
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 40
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 35
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 30
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 25
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 20
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 15
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 10
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 5
[#0] PROGRESS:Generation -- RooMCStudy::run: sample 0


Make plot of distribution of generated value of f parameter

In [10]:
TH1 *h_f_gen = mcs.fitParDataSet().createHistogram("f_gen", AutoBinning(40));


Make plot of distribution of fitted value of f parameter

In [11]:
RooPlot *frame1 = mcs.plotParam(f, Bins(40));
frame1->SetTitle("Distribution of fitted f values");


Make plot of pull distribution on f

In [12]:
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);
h_f_gen->GetYaxis()->SetTitleOffset(1.4);
h_f_gen->Draw();
c->cd(2);
frame1->GetYaxis()->SetTitleOffset(1.4);
frame1->Draw();
c->cd(3);
frame2->GetYaxis()->SetTitleOffset(1.4);
frame2->Draw();

[#1] INFO:ObjectHandling -- RooWorkspace::import() importing RooRealVar::fpull
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization

Info in <THttpEngine::Create>: Starting HTTP server on port 9229


Draw all canvases

In [13]:
%jsroot on
gROOT->GetListOfCanvases()->Draw()