ROOT logo

From $ROOTSYS/tutorials/roofit/rf604_constraints.C

/////////////////////////////////////////////////////////////////////////
//
// 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #604
// 
// Fitting with constraints
//
//
// 07/2008 - Wouter Verkerke 
//
/////////////////////////////////////////////////////////////////////////

#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "RooPolynomial.h"
#include "RooAddPdf.h"
#include "RooProdPdf.h"
#include "RooFitResult.h"
#include "RooPlot.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "TH1.h"
using namespace RooFit ;


void rf604_constraints()
{

  // C r e a t e   m o d e l  a n d   d a t a s e t 
  // ----------------------------------------------

  // Construct a Gaussian p.d.f
  RooRealVar x("x","x",-10,10) ;

  RooRealVar m("m","m",0,-10,10) ;
  RooRealVar s("s","s",2,0.1,10) ;
  RooGaussian gauss("gauss","gauss(x,m,s)",x,m,s) ;

  // Construct a flat p.d.f (polynomial of 0th order)
  RooPolynomial poly("poly","poly(x)",x) ;

  // Construct model = f*gauss + (1-f)*poly
  RooRealVar f("f","f",0.5,0.,1.) ;
  RooAddPdf model("model","model",RooArgSet(gauss,poly),f) ;

  // Generate small dataset for use in fitting below
  RooDataSet* d = model.generate(x,50) ;



  // C r e a t e   c o n s t r a i n t   p d f 
  // -----------------------------------------

  // Construct Gaussian constraint p.d.f on parameter f at 0.8 with resolution of 0.1
  RooGaussian fconstraint("fconstraint","fconstraint",f,RooConst(0.8),RooConst(0.1)) ;



  // 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 
  // -------------------------------------------------------------------------------------

  // Multiply constraint term with regular p.d.f using RooProdPdf
  // Specify in fitTo() that internal constraints on parameter f should be used

  // Multiply constraint with p.d.f
  RooProdPdf modelc("modelc","model with constraint",RooArgSet(model,fconstraint)) ;
  
  // Fit model (without use of constraint term)
  RooFitResult* r1 = model.fitTo(*d,Save()) ;

  // Fit modelc with constraint term on parameter f
  RooFitResult* r2 = modelc.fitTo(*d,Constrain(f),Save()) ;



  // 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
  // -------------------------------------------------------------------------------------------------------

  // Construct another Gaussian constraint p.d.f on parameter f at 0.8 with resolution of 0.1
  RooGaussian fconstext("fconstext","fconstext",f,RooConst(0.2),RooConst(0.1)) ;

  // Fit with external constraint
  RooFitResult* r3 = model.fitTo(*d,ExternalConstraints(fconstext),Save()) ;



  // Print the fit results
  cout << "fit result without constraint (data generated at f=0.5)" << endl ;
  r1->Print("v") ;
  cout << "fit result with internal constraint (data generated at f=0.5, constraint is f=0.8+/-0.2)" << endl ;
  r2->Print("v") ;
  cout << "fit result with (another) external constraint (data generated at f=0.5, constraint is f=0.2+/-0.1)" << endl ;
  r3->Print("v") ;
  
}
 rf604_constraints.C:1
 rf604_constraints.C:2
 rf604_constraints.C:3
 rf604_constraints.C:4
 rf604_constraints.C:5
 rf604_constraints.C:6
 rf604_constraints.C:7
 rf604_constraints.C:8
 rf604_constraints.C:9
 rf604_constraints.C:10
 rf604_constraints.C:11
 rf604_constraints.C:12
 rf604_constraints.C:13
 rf604_constraints.C:14
 rf604_constraints.C:15
 rf604_constraints.C:16
 rf604_constraints.C:17
 rf604_constraints.C:18
 rf604_constraints.C:19
 rf604_constraints.C:20
 rf604_constraints.C:21
 rf604_constraints.C:22
 rf604_constraints.C:23
 rf604_constraints.C:24
 rf604_constraints.C:25
 rf604_constraints.C:26
 rf604_constraints.C:27
 rf604_constraints.C:28
 rf604_constraints.C:29
 rf604_constraints.C:30
 rf604_constraints.C:31
 rf604_constraints.C:32
 rf604_constraints.C:33
 rf604_constraints.C:34
 rf604_constraints.C:35
 rf604_constraints.C:36
 rf604_constraints.C:37
 rf604_constraints.C:38
 rf604_constraints.C:39
 rf604_constraints.C:40
 rf604_constraints.C:41
 rf604_constraints.C:42
 rf604_constraints.C:43
 rf604_constraints.C:44
 rf604_constraints.C:45
 rf604_constraints.C:46
 rf604_constraints.C:47
 rf604_constraints.C:48
 rf604_constraints.C:49
 rf604_constraints.C:50
 rf604_constraints.C:51
 rf604_constraints.C:52
 rf604_constraints.C:53
 rf604_constraints.C:54
 rf604_constraints.C:55
 rf604_constraints.C:56
 rf604_constraints.C:57
 rf604_constraints.C:58
 rf604_constraints.C:59
 rf604_constraints.C:60
 rf604_constraints.C:61
 rf604_constraints.C:62
 rf604_constraints.C:63
 rf604_constraints.C:64
 rf604_constraints.C:65
 rf604_constraints.C:66
 rf604_constraints.C:67
 rf604_constraints.C:68
 rf604_constraints.C:69
 rf604_constraints.C:70
 rf604_constraints.C:71
 rf604_constraints.C:72
 rf604_constraints.C:73
 rf604_constraints.C:74
 rf604_constraints.C:75
 rf604_constraints.C:76
 rf604_constraints.C:77
 rf604_constraints.C:78
 rf604_constraints.C:79
 rf604_constraints.C:80
 rf604_constraints.C:81
 rf604_constraints.C:82
 rf604_constraints.C:83
 rf604_constraints.C:84
 rf604_constraints.C:85
 rf604_constraints.C:86
 rf604_constraints.C:87
 rf604_constraints.C:88
 rf604_constraints.C:89
 rf604_constraints.C:90
 rf604_constraints.C:91
 rf604_constraints.C:92
 rf604_constraints.C:93
 rf604_constraints.C:94
 rf604_constraints.C:95
 rf604_constraints.C:96
 rf604_constraints.C:97
 rf604_constraints.C:98
 rf604_constraints.C:99
 rf604_constraints.C:100