ROOT  6.06/09
Reference Guide
rf604_constraints.cxx
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////
2 //
3 // 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #604
4 //
5 // Fitting with constraints
6 //
7 //
8 // 07/2008 - Wouter Verkerke
9 //
10 /////////////////////////////////////////////////////////////////////////
11 
12 #ifndef __CINT__
13 #include "RooGlobalFunc.h"
14 #endif
15 #include "RooRealVar.h"
16 #include "RooDataSet.h"
17 #include "RooGaussian.h"
18 #include "RooPolynomial.h"
19 #include "RooAddPdf.h"
20 #include "RooProdPdf.h"
21 #include "RooFitResult.h"
22 #include "RooPlot.h"
23 #include "TCanvas.h"
24 #include "TH1.h"
25 using namespace RooFit ;
26 
27 
28 class TestBasic604 : public RooFitTestUnit
29 {
30 public:
31  TestBasic604(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Auxiliary observable constraints",refFile,writeRef,verbose) {} ;
32  Bool_t testCode() {
33 
34  // C r e a t e m o d e l a n d d a t a s e t
35  // ----------------------------------------------
36 
37  // Construct a Gaussian p.d.f
38  RooRealVar x("x","x",-10,10) ;
39 
40  RooRealVar m("m","m",0,-10,10) ;
41  RooRealVar s("s","s",2,0.1,10) ;
42  RooGaussian gauss("gauss","gauss(x,m,s)",x,m,s) ;
43 
44  // Construct a flat p.d.f (polynomial of 0th order)
45  RooPolynomial poly("poly","poly(x)",x) ;
46 
47  // Construct model = f*gauss + (1-f)*poly
48  RooRealVar f("f","f",0.5,0.,1.) ;
49  RooAddPdf model("model","model",RooArgSet(gauss,poly),f) ;
50 
51  // Generate small dataset for use in fitting below
52  RooDataSet* d = model.generate(x,50) ;
53 
54 
55 
56  // C r e a t e c o n s t r a i n t p d f
57  // -----------------------------------------
58 
59  // Construct Gaussian constraint p.d.f on parameter f at 0.8 with resolution of 0.1
60  RooGaussian fconstraint("fconstraint","fconstraint",f,RooConst(0.8),RooConst(0.1)) ;
61 
62 
63 
64  // 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
65  // -------------------------------------------------------------------------------------
66 
67  // Multiply constraint term with regular p.d.f using RooProdPdf
68  // Specify in fitTo() that internal constraints on parameter f should be used
69 
70  // Multiply constraint with p.d.f
71  RooProdPdf modelc("modelc","model with constraint",RooArgSet(model,fconstraint)) ;
72 
73  // Fit modelc without use of constraint term
74  RooFitResult* r1 = modelc.fitTo(*d,Save()) ;
75 
76  // Fit modelc with constraint term on parameter f
77  RooFitResult* r2 = modelc.fitTo(*d,Constrain(f),Save()) ;
78 
79 
80 
81  // 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
82  // -------------------------------------------------------------------------------------------------------
83 
84  // Construct another Gaussian constraint p.d.f on parameter f at 0.8 with resolution of 0.1
85  RooGaussian fconstext("fconstext","fconstext",f,RooConst(0.2),RooConst(0.1)) ;
86 
87  // Fit with external constraint
88  RooFitResult* r3 = model.fitTo(*d,ExternalConstraints(fconstext),Save()) ;
89 
90 
91  regResult(r1,"rf604_r1") ;
92  regResult(r2,"rf604_r2") ;
93  regResult(r3,"rf604_r3") ;
94 
95  delete d ;
96 
97  return kTRUE ;
98  }
99 } ;
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Double_t x[n]
Definition: legend1.C:17
TMarker * m
Definition: textangle.C:8
bool verbose
double f(double x)
RooCmdArg Save(Bool_t flag=kTRUE)
RooConstVar & RooConst(Double_t val)
const Bool_t kTRUE
Definition: Rtypes.h:91
RooCmdArg ExternalConstraints(const RooArgSet &constraintPdfs)
RooCmdArg Constrain(const RooArgSet &params)