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"
25 using namespace RooFit;
26 
27 void rf604_constraints()
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 }
m
auto * m
Definition: textangle.C:8
RooAddPdf
Definition: RooAddPdf.h:32
f
#define f(i)
Definition: RSha256.hxx:122
RooGaussian.h
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Definition: RooGaussian.h:25
RooAddPdf.h
TCanvas.h
RooDataSet.h
RooFit::ExternalConstraints
RooCmdArg ExternalConstraints(const RooArgSet &constraintPdfs)
Definition: RooGlobalFunc.cxx:204
RooFitResult
Definition: RooFitResult.h:40
RooPolynomial.h
RooProdPdf.h
TGeant4Unit::gauss
static constexpr double gauss
Definition: TGeant4SystemOfUnits.h:269
RooFit
Definition: RooCFunction1Binding.h:29
RooPolynomial
Definition: RooPolynomial.h:28
RooFit::Constrain
RooCmdArg Constrain(const RooArgSet &params)
Definition: RooGlobalFunc.cxx:200
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:120
RooDataSet
Definition: RooDataSet.h:33
make_cnn_model.model
model
Definition: make_cnn_model.py:6
RooRealVar
Definition: RooRealVar.h:35
RooFit::Save
RooCmdArg Save(Bool_t flag=kTRUE)
Definition: RooGlobalFunc.cxx:187
RooProdPdf
Definition: RooProdPdf.h:33
TH1.h
RooArgSet
Definition: RooArgSet.h:28
RooFit::RooConst
RooConstVar & RooConst(Double_t val)
Definition: RooGlobalFunc.cxx:341