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"
24 using namespace RooFit;
25 
26 void rf604_constraints()
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());
66 
67  // Fit modelc with constraint term on parameter f
68  RooFitResult *r2 = modelc.fitTo(*d, Constrain(f), Save());
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());
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 }
m
auto * m
Definition: textangle.C:8
RooAddPdf
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:32
f
#define f(i)
Definition: RSha256.hxx:104
RooGaussian.h
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
RooAddPdf.h
TCanvas.h
RooDataSet.h
RooFit::ExternalConstraints
RooCmdArg ExternalConstraints(const RooArgSet &constraintPdfs)
Definition: RooGlobalFunc.cxx:207
RooFitResult
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
Definition: RooFitResult.h:40
RooPolynomial.h
RooProdPdf.h
TGeant4Unit::gauss
static constexpr double gauss
Definition: TGeant4SystemOfUnits.h:263
RooFit
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: RooCFunction1Binding.h:29
RooPolynomial
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
RooFit::Constrain
RooCmdArg Constrain(const RooArgSet &params)
Definition: RooGlobalFunc.cxx:203
RooPlot.h
RooRealVar.h
RooFitResult.h
RooConstVar.h
rf604_constraints
Definition: rf604_constraints.py:1
TAxis.h
RooFitResult::Print
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooFitResult.h:66
d
#define d(i)
Definition: RSha256.hxx:102
RooDataSet
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
make_cnn_model.model
model
Definition: make_cnn_model.py:6
RooRealVar
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:37
RooFit::Save
RooCmdArg Save(Bool_t flag=kTRUE)
Definition: RooGlobalFunc.cxx:190
RooProdPdf
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:37
TH1.h
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29
RooFit::RooConst
RooConstVar & RooConst(Double_t val)
Definition: RooGlobalFunc.cxx:347