ROOT   Reference Guide
Searching...
No Matches
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_code
7/// \macro_output
8///
9/// \date 07/2008
10/// \author Wouter Verkerke
11
12#include "RooRealVar.h"
13#include "RooDataSet.h"
14#include "RooGaussian.h"
15#include "RooPolynomial.h"
17#include "RooProdPdf.h"
18#include "RooFitResult.h"
19#include "RooPlot.h"
20#include "TCanvas.h"
21#include "TAxis.h"
22#include "TH1.h"
23using namespace RooFit;
24
26{
27
28 // C r e a t e m o d e l a n d d a t a s e t
29 // ----------------------------------------------
30
31 // Construct a Gaussian pdf
32 RooRealVar x("x", "x", -10, 10);
33
34 RooRealVar m("m", "m", 0, -10, 10);
35 RooRealVar s("s", "s", 2, 0.1, 10);
36 RooGaussian gauss("gauss", "gauss(x,m,s)", x, m, s);
37
38 // Construct a flat pdf (polynomial of 0th order)
39 RooPolynomial poly("poly", "poly(x)", x);
40
41 // Construct model = f*gauss + (1-f)*poly
42 RooRealVar f("f", "f", 0.5, 0., 1.);
43 RooAddPdf model("model", "model", RooArgSet(gauss, poly), f);
44
45 // Generate small dataset for use in fitting below
46 std::unique_ptr<RooDataSet> d{model.generate(x, 50)};
47
48 // C r e a t e c o n s t r a i n t p d f
49 // -----------------------------------------
50
51 // Construct Gaussian constraint pdf on parameter f at 0.8 with resolution of 0.1
52 RooGaussian fconstraint("fconstraint", "fconstraint", f, 0.8, 0.2);
53
54 // 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
55 // -------------------------------------------------------------------------------------
56
57 // Multiply constraint term with regular pdf using RooProdPdf
58 // Specify in fitTo() that internal constraints on parameter f should be used
59
60 // Multiply constraint with pdf
61 RooProdPdf modelc("modelc", "model with constraint", RooArgSet(model, fconstraint));
62
63 // Fit model (without use of constraint term)
64 std::unique_ptr<RooFitResult> r1{model.fitTo(*d, Save(), PrintLevel(-1))};
65
66 // Fit modelc with constraint term on parameter f
67 std::unique_ptr<RooFitResult> r2{modelc.fitTo(*d, Constrain(f), Save(), PrintLevel(-1))};
68
69 // 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
70 // -------------------------------------------------------------------------------------------------------
71
72 // Construct another Gaussian constraint pdf on parameter f at 0.2 with resolution of 0.1
73 RooGaussian fconstext("fconstext", "fconstext", f, 0.2, 0.1);
74
75 // Fit with external constraint
76 std::unique_ptr<RooFitResult> r3{model.fitTo(*d, ExternalConstraints(fconstext), Save(), PrintLevel(-1))};
77
78 // Print the fit results
79 cout << "fit result without constraint (data generated at f=0.5)" << endl;
80 r1->Print("v");
81 cout << "fit result with internal constraint (data generated at f=0.5, constraint is f=0.8+/-0.2)" << endl;
82 r2->Print("v");
83 cout << "fit result with (another) external constraint (data generated at f=0.5, constraint is f=0.2+/-0.1)" << endl;
84 r3->Print("v");
85}
#define d(i)
Definition RSha256.hxx:102
#define f(i)
Definition RSha256.hxx:104
Efficient implementation of a sum of PDFs of the form.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
RooPolynomial implements a polynomial p.d.f of the form.
Efficient implementation of a product of PDFs of the form.
Definition RooProdPdf.h:33
Variable that can be changed from the outside.
Definition RooRealVar.h:37
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 JSONIO.h:26
TMarker m
Definition textangle.C:8