Logo ROOT  
Reference Guide
rf604_constraints.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -nodraw
4/// Likelihood and minimization: fitting with constraints
5///
6/// \macro_output
7/// \macro_code
8/// \author 07/2008 - Wouter Verkerke
9
10#include "RooRealVar.h"
11#include "RooDataSet.h"
12#include "RooGaussian.h"
13#include "RooConstVar.h"
14#include "RooPolynomial.h"
15#include "RooAddPdf.h"
16#include "RooProdPdf.h"
17#include "RooFitResult.h"
18#include "RooPlot.h"
19#include "TCanvas.h"
20#include "TAxis.h"
21#include "TH1.h"
22using namespace RooFit;
23
25{
26
27 // C r e a t e m o d e l a n d d a t a s e t
28 // ----------------------------------------------
29
30 // Construct a Gaussian p.d.f
31 RooRealVar x("x", "x", -10, 10);
32
33 RooRealVar m("m", "m", 0, -10, 10);
34 RooRealVar s("s", "s", 2, 0.1, 10);
35 RooGaussian gauss("gauss", "gauss(x,m,s)", x, m, s);
36
37 // Construct a flat p.d.f (polynomial of 0th order)
38 RooPolynomial poly("poly", "poly(x)", x);
39
40 // Construct model = f*gauss + (1-f)*poly
41 RooRealVar f("f", "f", 0.5, 0., 1.);
42 RooAddPdf model("model", "model", RooArgSet(gauss, poly), f);
43
44 // Generate small dataset for use in fitting below
45 RooDataSet *d = model.generate(x, 50);
46
47 // C r e a t e c o n s t r a i n t p d f
48 // -----------------------------------------
49
50 // Construct Gaussian constraint p.d.f on parameter f at 0.8 with resolution of 0.1
51 RooGaussian fconstraint("fconstraint", "fconstraint", f, RooConst(0.8), RooConst(0.2));
52
53 // M E T H O D 1 - A d d i n t e r n a l c o n s t r a i n t t o m o d e l
54 // -------------------------------------------------------------------------------------
55
56 // Multiply constraint term with regular p.d.f using RooProdPdf
57 // Specify in fitTo() that internal constraints on parameter f should be used
58
59 // Multiply constraint with p.d.f
60 RooProdPdf modelc("modelc", "model with constraint", RooArgSet(model, fconstraint));
61
62 // Fit model (without use of constraint term)
63 RooFitResult *r1 = model.fitTo(*d, Save());
64
65 // Fit modelc with constraint term on parameter f
66 RooFitResult *r2 = modelc.fitTo(*d, Constrain(f), Save());
67
68 // M E T H O D 2 - S p e c i f y e x t e r n a l c o n s t r a i n t w h e n f i t t i n g
69 // -------------------------------------------------------------------------------------------------------
70
71 // Construct another Gaussian constraint p.d.f on parameter f at 0.2 with resolution of 0.1
72 RooGaussian fconstext("fconstext", "fconstext", f, RooConst(0.2), RooConst(0.1));
73
74 // Fit with external constraint
75 RooFitResult *r3 = model.fitTo(*d, ExternalConstraints(fconstext), Save());
76
77 // Print the fit results
78 cout << "fit result without constraint (data generated at f=0.5)" << endl;
79 r1->Print("v");
80 cout << "fit result with internal constraint (data generated at f=0.5, constraint is f=0.8+/-0.2)" << endl;
81 r2->Print("v");
82 cout << "fit result with (another) external constraint (data generated at f=0.5, constraint is f=0.2+/-0.1)" << endl;
83 r3->Print("v");
84}
#define d(i)
Definition: RSha256.hxx:102
#define f(i)
Definition: RSha256.hxx:104
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:31
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
Definition: RooFitResult.h:40
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooFitResult.h:66
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:31
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:35
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
RooCmdArg Constrain(const RooArgSet &params)
RooCmdArg ExternalConstraints(const RooArgSet &constraintPdfs)
RooConstVar & RooConst(Double_t val)
RooCmdArg Save(Bool_t flag=kTRUE)
static constexpr double s
static constexpr double gauss
auto * m
Definition: textangle.C:8