Logo ROOT   6.14/05
Reference Guide
rf205_compplot.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// 'ADDITION AND CONVOLUTION' RooFit tutorial macro #205
5 ///
6 /// Options for plotting components of composite p.d.f.s.
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 /// \author 07/2008 - Wouter Verkerke
12 
13 
14 #include "RooRealVar.h"
15 #include "RooDataSet.h"
16 #include "RooGaussian.h"
17 #include "RooAddPdf.h"
18 #include "RooChebychev.h"
19 #include "RooExponential.h"
20 #include "TCanvas.h"
21 #include "TAxis.h"
22 #include "RooPlot.h"
23 using namespace RooFit ;
24 
25 
26 void rf205_compplot()
27 {
28  // S e t u p c o m p o s i t e p d f
29  // --------------------------------------
30 
31  // Declare observable x
32  RooRealVar x("x","x",0,10) ;
33 
34  // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
35  RooRealVar mean("mean","mean of gaussians",5) ;
36  RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
37  RooRealVar sigma2("sigma2","width of gaussians",1) ;
38  RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;
39  RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;
40 
41  // Sum the signal components into a composite signal p.d.f.
42  RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
43  RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
44 
45  // Build Chebychev polynomial p.d.f.
46  RooRealVar a0("a0","a0",0.5,0.,1.) ;
47  RooRealVar a1("a1","a1",0.2,0.,1.) ;
48  RooChebychev bkg1("bkg1","Background 1",x,RooArgSet(a0,a1)) ;
49 
50  // Build exponential pdf
51  RooRealVar alpha("alpha","alpha",-1) ;
52  RooExponential bkg2("bkg2","Background 2",x,alpha) ;
53 
54  // Sum the background components into a composite background p.d.f.
55  RooRealVar bkg1frac("sig1frac","fraction of component 1 in background",0.2,0.,1.) ;
56  RooAddPdf bkg("bkg","Signal",RooArgList(bkg1,bkg2),sig1frac) ;
57 
58  // Sum the composite signal and background
59  RooRealVar bkgfrac("bkgfrac","fraction of background",0.5,0.,1.) ;
60  RooAddPdf model("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) ;
61 
62 
63 
64  // 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
65  // ------------------------------------------------------------------------------
66 
67  // Generate a data sample of 1000 events in x from model
68  RooDataSet *data = model.generate(x,1000) ;
69 
70  // Plot data and complete PDF overlaid
71  RooPlot* xframe = x.frame(Title("Component plotting of pdf=(sig1+sig2)+(bkg1+bkg2)")) ;
72  data->plotOn(xframe) ;
73  model.plotOn(xframe) ;
74 
75  // Clone xframe for use below
76  RooPlot* xframe2 = (RooPlot*) xframe->Clone("xframe2") ;
77 
78 
79  // 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
80  // --------------------------------------------------------------------
81 
82  // Plot single background component specified by object reference
83  model.plotOn(xframe,Components(bkg),LineColor(kRed)) ;
84 
85  // Plot single background component specified by object reference
86  model.plotOn(xframe,Components(bkg2),LineStyle(kDashed),LineColor(kRed)) ;
87 
88  // Plot multiple background components specified by object reference
89  // Note that specified components may occur at any level in object tree
90  // (e.g bkg is component of 'model' and 'sig2' is component 'sig')
91  model.plotOn(xframe,Components(RooArgSet(bkg,sig2)),LineStyle(kDotted)) ;
92 
93 
94 
95  // 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
96  // ------------------------------------------------------------
97 
98  // Plot single background component specified by name
99  model.plotOn(xframe2,Components("bkg"),LineColor(kCyan)) ;
100 
101  // Plot multiple background components specified by name
102  model.plotOn(xframe2,Components("bkg1,sig2"),LineStyle(kDotted),LineColor(kCyan)) ;
103 
104  // Plot multiple background components specified by regular expression on name
105  model.plotOn(xframe2,Components("sig*"),LineStyle(kDashed),LineColor(kCyan)) ;
106 
107  // Plot multiple background components specified by multiple regular expressions on name
108  model.plotOn(xframe2,Components("bkg1,sig*"),LineStyle(kDashed),LineColor(kYellow),Invisible()) ;
109 
110 
111  // Draw the frame on the canvas
112  TCanvas* c = new TCanvas("rf205_compplot","rf205_compplot",800,400) ;
113  c->Divide(2) ;
114  c->cd(1) ; gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.4) ; xframe->Draw() ;
115  c->cd(2) ; gPad->SetLeftMargin(0.15) ; xframe2->GetYaxis()->SetTitleOffset(1.4) ; xframe2->Draw() ;
116 
117 
118 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:294
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1117
virtual RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) const
Plot dataset on specified frame.
Definition: RooAbsData.cxx:568
RooCmdArg Invisible()
RooCmdArg LineColor(Color_t color)
Definition: Rtypes.h:59
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
Definition: Rtypes.h:59
RooCmdArg Title(const char *name)
Double_t x[n]
Definition: legend1.C:17
RooCmdArg LineStyle(Style_t style)
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
Exponential p.d.f.
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
The Canvas class.
Definition: TCanvas.h:31
RooCmdArg Components(const RooArgSet &compSet)
Definition: Rtypes.h:59
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TNamed.cxx:74
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1162
#define gPad
Definition: TVirtualPad.h:285
#define c(i)
Definition: RSha256.hxx:101
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:558