ROOT
Version master
v6.34
v6.32
v6.30
v6.28
v6.26
v6.24
v6.22
v6.20
v6.18
v6.16
v6.14
v6.12
v6.10
v6.08
v6.06
Reference Guide
►
ROOT
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Modules
Pages
Loading...
Searching...
No Matches
rf604_constraints.py
Go to the documentation of this file.
1
## \file
2
## \ingroup tutorial_roofit_main
3
## \notebook -nodraw
4
## Likelihood and minimization: fitting with constraints
5
##
6
## \macro_code
7
## \macro_output
8
##
9
## \date February 2018
10
## \authors Clemens Lange, Wouter Verkerke (C++ version)
11
12
import
ROOT
13
14
15
# Create model and dataset
16
# ----------------------------------------------
17
18
# Construct a Gaussian pdf
19
x =
ROOT.RooRealVar
(
"x"
,
"x"
, -10, 10)
20
21
m =
ROOT.RooRealVar
(
"m"
,
"m"
, 0, -10, 10)
22
s =
ROOT.RooRealVar
(
"s"
,
"s"
, 2, 0.1, 10)
23
gauss =
ROOT.RooGaussian
(
"gauss"
,
"gauss(x,m,s)"
, x, m, s)
24
25
# Construct a flat pdf (polynomial of 0th order)
26
poly =
ROOT.RooPolynomial
(
"poly"
,
"poly(x)"
, x)
27
28
# model = f*gauss + (1-f)*poly
29
f =
ROOT.RooRealVar
(
"f"
,
"f"
, 0.5, 0.0, 1.0)
30
model =
ROOT.RooAddPdf
(
"model"
,
"model"
, [gauss, poly], [f])
31
32
# Generate small dataset for use in fitting below
33
d =
model.generate
({x}, 50)
34
35
# Create constraint pdf
36
# -----------------------------------------
37
38
# Construct Gaussian constraint pdf on parameter f at 0.8 with
39
# resolution of 0.1
40
fconstraint =
ROOT.RooGaussian
(
"fconstraint"
,
"fconstraint"
, f, 0.8, 0.1)
41
42
# Method 1 - add internal constraint to model
43
# -------------------------------------------------------------------------------------
44
45
# Multiply constraint term with regular pdf using ROOT.RooProdPdf Specify in
46
# fitTo() that internal constraints on parameter f should be used
47
48
# Multiply constraint with pdf
49
modelc =
ROOT.RooProdPdf
(
"modelc"
,
"model with constraint"
, [model, fconstraint])
50
51
# Fit model (without use of constraint term)
52
r1 =
model.fitTo
(d, Save=
True
, PrintLevel=-1)
53
54
# Fit modelc with constraint term on parameter f
55
r2 =
modelc.fitTo
(d, Constrain={f}, Save=
True
, PrintLevel=-1)
56
57
# Method 2 - specify external constraint when fitting
58
# ------------------------------------------------------------------------------------------
59
60
# Construct another Gaussian constraint pdf on parameter f at 0.8 with
61
# resolution of 0.1
62
fconstext =
ROOT.RooGaussian
(
"fconstext"
,
"fconstext"
, f, 0.2, 0.1)
63
64
# Fit with external constraint
65
r3 =
model.fitTo
(d, ExternalConstraints={fconstext}, Save=
True
, PrintLevel=-1)
66
67
# Print the fit results
68
print(
"fit result without constraint (data generated at f=0.5)"
)
69
r1.Print
(
"v"
)
70
print(
"fit result with internal constraint (data generated at f=0.5, is f=0.8+/-0.2)"
)
71
r2.Print
(
"v"
)
72
print(
"fit result with (another) external constraint (data generated at f=0.5, is f=0.2+/-0.1)"
)
73
r3.Print
(
"v"
)
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
tutorials
roofit
roofit
rf604_constraints.py
ROOT master - Reference Guide Generated on Tue Apr 8 2025 06:21:58 (GVA Time) using Doxygen 1.10.0