Logo ROOT   6.16/01
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' RooFit tutorial macro #604
5///
6/// Fitting with constraints
7///
8/// \macro_output
9/// \macro_code
10/// \author 07/2008 - Wouter Verkerke
11
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"
25using namespace RooFit ;
26
27
29{
30
31 // C r e a t e m o d e l a n d d a t a s e t
32 // ----------------------------------------------
33
34 // Construct a Gaussian p.d.f
35 RooRealVar x("x","x",-10,10) ;
36
37 RooRealVar m("m","m",0,-10,10) ;
38 RooRealVar s("s","s",2,0.1,10) ;
39 RooGaussian gauss("gauss","gauss(x,m,s)",x,m,s) ;
40
41 // Construct a flat p.d.f (polynomial of 0th order)
42 RooPolynomial poly("poly","poly(x)",x) ;
43
44 // Construct model = f*gauss + (1-f)*poly
45 RooRealVar f("f","f",0.5,0.,1.) ;
46 RooAddPdf model("model","model",RooArgSet(gauss,poly),f) ;
47
48 // Generate small dataset for use in fitting below
49 RooDataSet* d = model.generate(x,50) ;
50
51
52
53 // C r e a t e c o n s t r a i n t p d f
54 // -----------------------------------------
55
56 // Construct Gaussian constraint p.d.f on parameter f at 0.8 with resolution of 0.1
57 RooGaussian fconstraint("fconstraint","fconstraint",f,RooConst(0.8),RooConst(0.1)) ;
58
59
60
61 // 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
62 // -------------------------------------------------------------------------------------
63
64 // Multiply constraint term with regular p.d.f using RooProdPdf
65 // Specify in fitTo() that internal constraints on parameter f should be used
66
67 // Multiply constraint with p.d.f
68 RooProdPdf modelc("modelc","model with constraint",RooArgSet(model,fconstraint)) ;
69
70 // Fit model (without use of constraint term)
71 RooFitResult* r1 = model.fitTo(*d,Save()) ;
72
73 // Fit modelc with constraint term on parameter f
74 RooFitResult* r2 = modelc.fitTo(*d,Constrain(f),Save()) ;
75
76
77
78 // 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
79 // -------------------------------------------------------------------------------------------------------
80
81 // Construct another Gaussian constraint p.d.f on parameter f at 0.8 with resolution of 0.1
82 RooGaussian fconstext("fconstext","fconstext",f,RooConst(0.2),RooConst(0.1)) ;
83
84 // Fit with external constraint
85 RooFitResult* r3 = model.fitTo(*d,ExternalConstraints(fconstext),Save()) ;
86
87
88
89 // Print the fit results
90 cout << "fit result without constraint (data generated at f=0.5)" << endl ;
91 r1->Print("v") ;
92 cout << "fit result with internal constraint (data generated at f=0.5, constraint is f=0.8+/-0.2)" << endl ;
93 r2->Print("v") ;
94 cout << "fit result with (another) external constraint (data generated at f=0.5, constraint is f=0.2+/-0.1)" << endl ;
95 r3->Print("v") ;
96
97}
#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:29
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:31
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
Definition: RooFitResult.h:40
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooFitResult.h:66
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
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:31
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
Double_t x[n]
Definition: legend1.C:17
RooCmdArg Constrain(const RooArgSet &params)
RooCmdArg ExternalConstraints(const RooArgSet &constraintPdfs)
RooConstVar & RooConst(Double_t val)
RooCmdArg Save(Bool_t flag=kTRUE)
static constexpr double s
static constexpr double gauss
auto * m
Definition: textangle.C:8