Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 /// \author 07/2008 - Wouter Verkerke
9 
10 #include "RooRealVar.h"
11 #include "RooDataSet.h"
12 #include "RooGaussian.h"
13 #include "RooConstVar.h"
14 #include "RooPolynomial.h"
15 #include "RooAddPdf.h"
16 #include "RooProdPdf.h"
17 #include "RooFitResult.h"
18 #include "RooPlot.h"
19 #include "TCanvas.h"
20 #include "TAxis.h"
21 #include "TH1.h"
22 using namespace RooFit;
23 
24 void rf604_constraints()
25 {
26 
27  // C r e a t e m o d e l a n d d a t a s e t
28  // ----------------------------------------------
29 
30  // Construct a Gaussian p.d.f
31  RooRealVar x("x", "x", -10, 10);
32 
33  RooRealVar m("m", "m", 0, -10, 10);
34  RooRealVar s("s", "s", 2, 0.1, 10);
35  RooGaussian gauss("gauss", "gauss(x,m,s)", x, m, s);
36 
37  // Construct a flat p.d.f (polynomial of 0th order)
38  RooPolynomial poly("poly", "poly(x)", x);
39 
40  // Construct model = f*gauss + (1-f)*poly
41  RooRealVar f("f", "f", 0.5, 0., 1.);
42  RooAddPdf model("model", "model", RooArgSet(gauss, poly), f);
43 
44  // Generate small dataset for use in fitting below
45  RooDataSet *d = model.generate(x, 50);
46 
47  // C r e a t e c o n s t r a i n t p d f
48  // -----------------------------------------
49 
50  // Construct Gaussian constraint p.d.f on parameter f at 0.8 with resolution of 0.1
51  RooGaussian fconstraint("fconstraint", "fconstraint", f, RooConst(0.8), RooConst(0.2));
52 
53  // 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
54  // -------------------------------------------------------------------------------------
55 
56  // Multiply constraint term with regular p.d.f using RooProdPdf
57  // Specify in fitTo() that internal constraints on parameter f should be used
58 
59  // Multiply constraint with p.d.f
60  RooProdPdf modelc("modelc", "model with constraint", RooArgSet(model, fconstraint));
61 
62  // Fit model (without use of constraint term)
63  RooFitResult *r1 = model.fitTo(*d, Save());
64 
65  // Fit modelc with constraint term on parameter f
66  RooFitResult *r2 = modelc.fitTo(*d, Constrain(f), Save());
67 
68  // 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
69  // -------------------------------------------------------------------------------------------------------
70 
71  // Construct another Gaussian constraint p.d.f on parameter f at 0.2 with resolution of 0.1
72  RooGaussian fconstext("fconstext", "fconstext", f, RooConst(0.2), RooConst(0.1));
73 
74  // Fit with external constraint
75  RooFitResult *r3 = model.fitTo(*d, ExternalConstraints(fconstext), Save());
76 
77  // Print the fit results
78  cout << "fit result without constraint (data generated at f=0.5)" << endl;
79  r1->Print("v");
80  cout << "fit result with internal constraint (data generated at f=0.5, constraint is f=0.8+/-0.2)" << endl;
81  r2->Print("v");
82  cout << "fit result with (another) external constraint (data generated at f=0.5, constraint is f=0.2+/-0.1)" << endl;
83  r3->Print("v");
84 }