ROOT logo
/////////////////////////////////////////////////////////////////////////
//
// 'ADDITION AND CONVOLUTION' RooFit tutorial macro #201
// 
// Composite p.d.f with signal and background component
//
// pdf = f_bkg * bkg(x,a0,a1) + (1-fbkg) * (f_sig1 * sig1(x,m,s1 + (1-f_sig1) * sig2(x,m,s2)))
//
//
// 07/2008 - Wouter Verkerke 
//
/////////////////////////////////////////////////////////////////////////

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


void rf201_composite()
{
  // S e t u p   c o m p o n e n t   p d f s 
  // ---------------------------------------

  // 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 sigma1("sigma1","width of gaussians",0.5) ;
  RooRealVar sigma2("sigma2","width of gaussians",1) ;

  RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;  
  RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;  
  
  // Build Chebychev polynomial p.d.f.  
  RooRealVar a0("a0","a0",0.5,0.,1.) ;
  RooRealVar a1("a1","a1",-0.2,0.,1.) ;
  RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;


  ////////////////////////////////////////////////////
  // M E T H O D   1 - T w o   R o o A d d P d f s  //
  ////////////////////////////////////////////////////


  // A d d   s i g n a l   c o m p o n e n t s 
  // ------------------------------------------

  // Sum the signal components into a composite signal p.d.f.
  RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
  RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;


  // A d d  s i g n a l   a n d   b a c k g r o u n d
  // ------------------------------------------------

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


  // S a m p l e ,   f i t   a n d   p l o t   m o d e l
  // ---------------------------------------------------

  // Generate a data sample of 1000 events in x from model
  RooDataSet *data = model.generate(x,1000) ;

  // Fit model to data
  model.fitTo(*data) ;
  
  // Plot data and PDF overlaid
  RooPlot* xframe = x.frame(Title("Example of composite pdf=(sig1+sig2)+bkg")) ;
  data->plotOn(xframe) ;
  model.plotOn(xframe) ;

  // Overlay the background component of model with a dashed line
  model.plotOn(xframe,Components(bkg),LineStyle(kDashed)) ;

  // Overlay the background+sig2 components of model with a dotted line
  model.plotOn(xframe,Components(RooArgSet(bkg,sig2)),LineStyle(kDotted)) ;

  // Print structure of composite p.d.f.
  model.Print("t") ;


  ////////////////////////////////////////////////////////////////////////////////////////////////////
  // M E T H O D   2 - O n e   R o o A d d P d f   w i t h   r e c u r s i v e   f r a c t i o n s  //
  ////////////////////////////////////////////////////////////////////////////////////////////////////

  // Construct sum of models on one go using recursive fraction interpretations
  //
  //   model2 = bkg + (sig1 + sig2)
  //
  RooAddPdf  model2("model","g1+g2+a",RooArgList(bkg,sig1,sig2),RooArgList(bkgfrac,sig1frac),kTRUE) ;    

  // NB: Each coefficient is interpreted as the fraction of the 
  // left-hand component of the i-th recursive sum, i.e.
  //
  //   sum4 = A + ( B + ( C + D)  with fraction fA, fB and fC expands to
  //
  //   sum4 = fA*A + (1-fA)*(fB*B + (1-fB)*(fC*C + (1-fC)*D))


  // P l o t   r e c u r s i v e   a d d i t i o n   m o d e l
  // ---------------------------------------------------------
  model2.plotOn(xframe,LineColor(kRed),LineStyle(kDashed)) ;
  model2.plotOn(xframe,Components(RooArgSet(bkg,sig2)),LineColor(kRed),LineStyle(kDashed)) ;
  model2.Print("t") ;

  
  // Draw the frame on the canvas
  new TCanvas("rf201_composite","rf201_composite",600,600) ;
  gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.4) ; xframe->Draw() ;


}

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