rf604_constraints.cxx

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////
00002 //
00003 // 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #604
00004 // 
00005 // Fitting with constraints
00006 //
00007 //
00008 // 07/2008 - Wouter Verkerke 
00009 //
00010 /////////////////////////////////////////////////////////////////////////
00011 
00012 #ifndef __CINT__
00013 #include "RooGlobalFunc.h"
00014 #endif
00015 #include "RooRealVar.h"
00016 #include "RooDataSet.h"
00017 #include "RooGaussian.h"
00018 #include "RooPolynomial.h"
00019 #include "RooAddPdf.h"
00020 #include "RooProdPdf.h"
00021 #include "RooFitResult.h"
00022 #include "RooPlot.h"
00023 #include "TCanvas.h"
00024 #include "TH1.h"
00025 using namespace RooFit ;
00026 
00027 
00028 class TestBasic604 : public RooFitTestUnit
00029 {
00030 public: 
00031   TestBasic604(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Auxiliary observable constraints",refFile,writeRef,verbose) {} ;
00032   Bool_t testCode() {
00033 
00034   // C r e a t e   m o d e l  a n d   d a t a s e t 
00035   // ----------------------------------------------
00036 
00037   // Construct a Gaussian p.d.f
00038   RooRealVar x("x","x",-10,10) ;
00039 
00040   RooRealVar m("m","m",0,-10,10) ;
00041   RooRealVar s("s","s",2,0.1,10) ;
00042   RooGaussian gauss("gauss","gauss(x,m,s)",x,m,s) ;
00043 
00044   // Construct a flat p.d.f (polynomial of 0th order)
00045   RooPolynomial poly("poly","poly(x)",x) ;
00046 
00047   // Construct model = f*gauss + (1-f)*poly
00048   RooRealVar f("f","f",0.5,0.,1.) ;
00049   RooAddPdf model("model","model",RooArgSet(gauss,poly),f) ;
00050 
00051   // Generate small dataset for use in fitting below
00052   RooDataSet* d = model.generate(x,50) ;
00053 
00054 
00055 
00056   // C r e a t e   c o n s t r a i n t   p d f 
00057   // -----------------------------------------
00058 
00059   // Construct Gaussian constraint p.d.f on parameter f at 0.8 with resolution of 0.1
00060   RooGaussian fconstraint("fconstraint","fconstraint",f,RooConst(0.8),RooConst(0.1)) ;
00061 
00062 
00063 
00064   // 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 
00065   // -------------------------------------------------------------------------------------
00066 
00067   // Multiply constraint term with regular p.d.f using RooProdPdf
00068   // Specify in fitTo() that internal constraints on parameter f should be used
00069 
00070   // Multiply constraint with p.d.f
00071   RooProdPdf modelc("modelc","model with constraint",RooArgSet(model,fconstraint)) ;
00072   
00073   // Fit modelc without use of constraint term
00074   RooFitResult* r1 = modelc.fitTo(*d,Save()) ;
00075 
00076   // Fit modelc with constraint term on parameter f
00077   RooFitResult* r2 = modelc.fitTo(*d,Constrain(f),Save()) ;
00078 
00079 
00080 
00081   // 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
00082   // -------------------------------------------------------------------------------------------------------
00083 
00084   // Construct another Gaussian constraint p.d.f on parameter f at 0.8 with resolution of 0.1
00085   RooGaussian fconstext("fconstext","fconstext",f,RooConst(0.2),RooConst(0.1)) ;
00086 
00087   // Fit with external constraint
00088   RooFitResult* r3 = model.fitTo(*d,ExternalConstraints(fconstext),Save()) ;
00089 
00090 
00091   regResult(r1,"rf604_r1") ;
00092   regResult(r2,"rf604_r2") ;
00093   regResult(r3,"rf604_r3") ;
00094 
00095   delete d ;
00096   
00097   return kTRUE ;
00098   }
00099 } ;

Generated on Tue Jul 5 15:07:51 2011 for ROOT_528-00b_version by  doxygen 1.5.1