ROOT logo

From $ROOTSYS/tutorials/roofit/rf205_compplot.C

//////////////////////////////////////////////////////////////////////////
//
// 'ADDITION AND CONVOLUTION' RooFit tutorial macro #205
// 
// Options for plotting components of composite p.d.f.s.
//
//
//
// 07/2008 - Wouter Verkerke 
// 
/////////////////////////////////////////////////////////////////////////

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


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

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

  // 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) ;
  
  // 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("sig1frac","fraction of component 1 in background",0.2,0.,1.) ;
  RooAddPdf bkg("bkg","Signal",RooArgList(bkg1,bkg2),sig1frac) ;
  
  // 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 e t u p   b a s i c   p l o t   w i t h   d a t a   a n d   f u l l   p d f 
  // ------------------------------------------------------------------------------
  
  // Generate a data sample of 1000 events in x from model
  RooDataSet *data = model.generate(x,1000) ;

  // Plot data and complete PDF overlaid
  RooPlot* xframe  = x.frame(Title("Component plotting of pdf=(sig1+sig2)+(bkg1+bkg2)")) ;
  data->plotOn(xframe) ;
  model.plotOn(xframe) ;

  // Clone xframe for use below
  RooPlot* xframe2 = (RooPlot*) xframe->Clone("xframe2") ;


  // M a k e   c o m p o n e n t   b y   o b j e c t   r e f e r e n c e 
  // --------------------------------------------------------------------

  // Plot single background component specified by object reference
  model.plotOn(xframe,Components(bkg),LineColor(kRed)) ;

  // Plot single background component specified by object reference
  model.plotOn(xframe,Components(bkg2),LineStyle(kDashed),LineColor(kRed)) ;

  // Plot multiple background components specified by object reference
  // Note that specified components may occur at any level in object tree
  // (e.g bkg is component of 'model' and 'sig2' is component 'sig')
  model.plotOn(xframe,Components(RooArgSet(bkg,sig2)),LineStyle(kDotted)) ;



  // M a k e   c o m p o n e n t   b y   n a m e  /   r e g e x p  
  // ------------------------------------------------------------

  // Plot single background component specified by name
  model.plotOn(xframe2,Components("bkg"),LineColor(kCyan)) ;

  // Plot multiple background components specified by name
  model.plotOn(xframe2,Components("bkg1,sig2"),LineStyle(kDotted),LineColor(kCyan)) ;

  // Plot multiple background components specified by regular expression on name
  model.plotOn(xframe2,Components("sig*"),LineStyle(kDashed),LineColor(kCyan)) ;
  
  // Plot multiple background components specified by multiple regular expressions on name
  model.plotOn(xframe2,Components("bkg1,sig*"),LineStyle(kDashed),LineColor(kYellow),Invisible()) ;
  

  // Draw the frame on the canvas
  TCanvas* c = new TCanvas("rf205_compplot","rf205_compplot",800,400) ;
  c->Divide(2) ;
  c->cd(1) ; gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.4) ; xframe->Draw() ;
  c->cd(2) ; gPad->SetLeftMargin(0.15) ; xframe2->GetYaxis()->SetTitleOffset(1.4) ; xframe2->Draw() ;


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