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///
9/// \date 07/2008
10/// \author Wouter Verkerke
11
12#include "RooRealVar.h"
13#include "RooDataSet.h"
14#include "RooGaussian.h"
15#include "RooConstVar.h"
16#include "RooPolynomial.h"
17#include "RooAddPdf.h"
18#include "RooProdPdf.h"
19#include "RooFitResult.h"
20#include "RooPlot.h"
21#include "TCanvas.h"
22#include "TAxis.h"
23#include "TH1.h"
24using namespace RooFit;
25
27{
28
29 // C r e a t e m o d e l a n d d a t a s e t
30 // ----------------------------------------------
31
32 // Construct a Gaussian pdf
33 RooRealVar x("x", "x", -10, 10);
34
35 RooRealVar m("m", "m", 0, -10, 10);
36 RooRealVar s("s", "s", 2, 0.1, 10);
37 RooGaussian gauss("gauss", "gauss(x,m,s)", x, m, s);
38
39 // Construct a flat pdf (polynomial of 0th order)
40 RooPolynomial poly("poly", "poly(x)", x);
41
42 // Construct model = f*gauss + (1-f)*poly
43 RooRealVar f("f", "f", 0.5, 0., 1.);
44 RooAddPdf model("model", "model", RooArgSet(gauss, poly), f);
45
46 // Generate small dataset for use in fitting below
47 RooDataSet *d = model.generate(x, 50);
48
49 // C r e a t e c o n s t r a i n t p d f
50 // -----------------------------------------
51
52 // Construct Gaussian constraint pdf on parameter f at 0.8 with resolution of 0.1
53 RooGaussian fconstraint("fconstraint", "fconstraint", f, RooConst(0.8), RooConst(0.2));
54
55 // 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
56 // -------------------------------------------------------------------------------------
57
58 // Multiply constraint term with regular pdf using RooProdPdf
59 // Specify in fitTo() that internal constraints on parameter f should be used
60
61 // Multiply constraint with pdf
62 RooProdPdf modelc("modelc", "model with constraint", RooArgSet(model, fconstraint));
63
64 // Fit model (without use of constraint term)
65 RooFitResult *r1 = model.fitTo(*d, Save(), PrintLevel(-1));
66
67 // Fit modelc with constraint term on parameter f
68 RooFitResult *r2 = modelc.fitTo(*d, Constrain(f), Save(), PrintLevel(-1));
69
70 // 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
71 // -------------------------------------------------------------------------------------------------------
72
73 // Construct another Gaussian constraint pdf on parameter f at 0.2 with resolution of 0.1
74 RooGaussian fconstext("fconstext", "fconstext", f, RooConst(0.2), RooConst(0.1));
75
76 // Fit with external constraint
77 RooFitResult *r3 = model.fitTo(*d, ExternalConstraints(fconstext), Save(), PrintLevel(-1));
78
79 // Print the fit results
80 cout << "fit result without constraint (data generated at f=0.5)" << endl;
81 r1->Print("v");
82 cout << "fit result with internal constraint (data generated at f=0.5, constraint is f=0.8+/-0.2)" << endl;
83 r2->Print("v");
84 cout << "fit result with (another) external constraint (data generated at f=0.5, constraint is f=0.2+/-0.1)" << endl;
85 r3->Print("v");
86}
#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:34
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:56
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:55
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
Definition: RooFitResult.h:40
void Print(Option_t *options=nullptr) const override
Print TNamed name and title.
Definition: RooFitResult.h:66
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
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:33
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:40
RooConstVar & RooConst(double val)
RooCmdArg Constrain(const RooArgSet &params)
RooCmdArg Save(bool flag=true)
RooCmdArg ExternalConstraints(const RooArgSet &constraintPdfs)
RooCmdArg PrintLevel(Int_t code)
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: Common.h:18
static constexpr double s
static constexpr double gauss
TMarker m
Definition: textangle.C:8