ROOT logo

From $ROOTSYS/tutorials/roofit/rf207_comptools.C

/////////////////////////////////////////////////////////////////////////
//
// 'ADDITION AND CONVOLUTION' RooFit tutorial macro #207
// 
// Tools and utilities for manipulation of composite objects
//
// 
// 07/2008 - Wouter Verkerke 
//
/////////////////////////////////////////////////////////////////////////

#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooChebychev.h"
#include "RooExponential.h"
#include "RooAddPdf.h"
#include "RooPlot.h"
#include "RooCustomizer.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "TH1.h"
using namespace RooFit ;



void rf207_comptools()
{
  
  // S e t u p   c o m p o s i t e    p d f,   d a t a s e t 
  // --------------------------------------------------------

  // Declare observable x
  RooRealVar x("x","x",0,10) ;

  // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their paramaters
  RooRealVar mean("mean","mean of gaussians",5) ;
  RooRealVar sigma("sigma","width of gaussians",0.5) ;
  RooGaussian sig("sig","Signal component 1",x,mean,sigma) ;  
  
  // Build Chebychev polynomial p.d.f.  
  RooRealVar a0("a0","a0",0.5,0.,1.) ;
  RooRealVar a1("a1","a1",-0.2,0.,1.) ;
  RooChebychev bkg1("bkg1","Background 1",x,RooArgSet(a0,a1)) ;

  // Build expontential pdf
  RooRealVar alpha("alpha","alpha",-1) ;
  RooExponential bkg2("bkg2","Background 2",x,alpha) ;

  // Sum the background components into a composite background p.d.f.
  RooRealVar bkg1frac("bkg1frac","fraction of component 1 in background",0.2,0.,1.) ;
  RooAddPdf bkg("bkg","Signal",RooArgList(bkg1,bkg2),bkg1frac) ;
  
  // Sum the composite signal and background 
  RooRealVar bkgfrac("bkgfrac","fraction of background",0.5,0.,1.) ;
  RooAddPdf  model("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) ;



  // Create dummy dataset that has more observables than the above pdf
  RooRealVar y("y","y",-10,10) ;
  RooDataSet data("data","data",RooArgSet(x,y)) ;




  //////////////////////////////////////////////////////////
  // B a s i c   i n f o r m a t i o n   r e q u e s t s  //
  //////////////////////////////////////////////////////////


  // G e t   l i s t   o f   o b s e r v a b l e s
  // ---------------------------------------------

  // Get list of observables of pdf in context of a dataset
  //
  // Observables are define each context as the variables
  // shared between a model and a dataset. In this case
  // that is the variable 'x'

  RooArgSet* model_obs = model.getObservables(data) ;
  model_obs->Print("v") ;
  

  // G e t   l i s t   o f   p a r a m e t e r s
  // -------------------------------------------

  // Get list of parameters, given list of observables
  RooArgSet* model_params = model.getParameters(x) ;
  model_params->Print("v") ;

  // Get list of parameters, given a dataset
  // (Gives identical results to operation above)
  RooArgSet* model_params2 = model.getParameters(data) ;
  model_params2->Print() ;


  // G e t   l i s t   o f   c o m p o n e n t s
  // -------------------------------------------

  // Get list of component objects, including top-level node
  RooArgSet* model_comps = model.getComponents() ;
  model_comps->Print("v") ;


  /////////////////////////////////////////////////////////////////////////////////////
  // M o d i f i c a t i o n s   t o   s t r u c t u r e   o f   c o m p o s i t e s //
  /////////////////////////////////////////////////////////////////////////////////////


  // Create a second Gaussian
  RooRealVar sigma2("sigma2","width of gaussians",1) ;
  RooGaussian sig2("sig2","Signal component 1",x,mean,sigma2) ;  

  // Create a sum of the original Gaussian plus the new second Gaussian
  RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
  RooAddPdf sigsum("sigsum","sig+sig2",RooArgList(sig,sig2),sig1frac) ;
  
  // Construct a customizer utility to customize model
  RooCustomizer cust(model,"cust") ;

  // Instruct the customizer to replace node 'sig' with node 'sigsum'
  cust.replaceArg(sig,sigsum) ;

  // Build a clone of the input pdf according to the above customization
  // instructions. Each node that requires modified is clone so that the
  // original pdf remained untouched. The name of each cloned node is that
  // of the original node suffixed by the name of the customizer object  
  //
  // The returned head node own all nodes that were cloned as part of
  // the build process so when cust_clone is deleted so will all other
  // nodes that were created in the process.
  RooAbsPdf* cust_clone = (RooAbsPdf*) cust.build(kTRUE) ;
  
  // Print structure of clone of model with sig->sigsum replacement.
  cust_clone->Print("t") ;


  delete cust_clone ;

}
 rf207_comptools.C:1
 rf207_comptools.C:2
 rf207_comptools.C:3
 rf207_comptools.C:4
 rf207_comptools.C:5
 rf207_comptools.C:6
 rf207_comptools.C:7
 rf207_comptools.C:8
 rf207_comptools.C:9
 rf207_comptools.C:10
 rf207_comptools.C:11
 rf207_comptools.C:12
 rf207_comptools.C:13
 rf207_comptools.C:14
 rf207_comptools.C:15
 rf207_comptools.C:16
 rf207_comptools.C:17
 rf207_comptools.C:18
 rf207_comptools.C:19
 rf207_comptools.C:20
 rf207_comptools.C:21
 rf207_comptools.C:22
 rf207_comptools.C:23
 rf207_comptools.C:24
 rf207_comptools.C:25
 rf207_comptools.C:26
 rf207_comptools.C:27
 rf207_comptools.C:28
 rf207_comptools.C:29
 rf207_comptools.C:30
 rf207_comptools.C:31
 rf207_comptools.C:32
 rf207_comptools.C:33
 rf207_comptools.C:34
 rf207_comptools.C:35
 rf207_comptools.C:36
 rf207_comptools.C:37
 rf207_comptools.C:38
 rf207_comptools.C:39
 rf207_comptools.C:40
 rf207_comptools.C:41
 rf207_comptools.C:42
 rf207_comptools.C:43
 rf207_comptools.C:44
 rf207_comptools.C:45
 rf207_comptools.C:46
 rf207_comptools.C:47
 rf207_comptools.C:48
 rf207_comptools.C:49
 rf207_comptools.C:50
 rf207_comptools.C:51
 rf207_comptools.C:52
 rf207_comptools.C:53
 rf207_comptools.C:54
 rf207_comptools.C:55
 rf207_comptools.C:56
 rf207_comptools.C:57
 rf207_comptools.C:58
 rf207_comptools.C:59
 rf207_comptools.C:60
 rf207_comptools.C:61
 rf207_comptools.C:62
 rf207_comptools.C:63
 rf207_comptools.C:64
 rf207_comptools.C:65
 rf207_comptools.C:66
 rf207_comptools.C:67
 rf207_comptools.C:68
 rf207_comptools.C:69
 rf207_comptools.C:70
 rf207_comptools.C:71
 rf207_comptools.C:72
 rf207_comptools.C:73
 rf207_comptools.C:74
 rf207_comptools.C:75
 rf207_comptools.C:76
 rf207_comptools.C:77
 rf207_comptools.C:78
 rf207_comptools.C:79
 rf207_comptools.C:80
 rf207_comptools.C:81
 rf207_comptools.C:82
 rf207_comptools.C:83
 rf207_comptools.C:84
 rf207_comptools.C:85
 rf207_comptools.C:86
 rf207_comptools.C:87
 rf207_comptools.C:88
 rf207_comptools.C:89
 rf207_comptools.C:90
 rf207_comptools.C:91
 rf207_comptools.C:92
 rf207_comptools.C:93
 rf207_comptools.C:94
 rf207_comptools.C:95
 rf207_comptools.C:96
 rf207_comptools.C:97
 rf207_comptools.C:98
 rf207_comptools.C:99
 rf207_comptools.C:100
 rf207_comptools.C:101
 rf207_comptools.C:102
 rf207_comptools.C:103
 rf207_comptools.C:104
 rf207_comptools.C:105
 rf207_comptools.C:106
 rf207_comptools.C:107
 rf207_comptools.C:108
 rf207_comptools.C:109
 rf207_comptools.C:110
 rf207_comptools.C:111
 rf207_comptools.C:112
 rf207_comptools.C:113
 rf207_comptools.C:114
 rf207_comptools.C:115
 rf207_comptools.C:116
 rf207_comptools.C:117
 rf207_comptools.C:118
 rf207_comptools.C:119
 rf207_comptools.C:120
 rf207_comptools.C:121
 rf207_comptools.C:122
 rf207_comptools.C:123
 rf207_comptools.C:124
 rf207_comptools.C:125
 rf207_comptools.C:126
 rf207_comptools.C:127
 rf207_comptools.C:128
 rf207_comptools.C:129
 rf207_comptools.C:130
 rf207_comptools.C:131
 rf207_comptools.C:132
 rf207_comptools.C:133
 rf207_comptools.C:134
 rf207_comptools.C:135
 rf207_comptools.C:136
 rf207_comptools.C:137
 rf207_comptools.C:138
 rf207_comptools.C:139
 rf207_comptools.C:140
 rf207_comptools.C:141
 rf207_comptools.C:142
 rf207_comptools.C:143
 rf207_comptools.C:144
 rf207_comptools.C:145