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