Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf205_compplot.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js -nodraw
4 /// Addition and convolution: options for plotting components of composite p.d.f.s.
5 ///
6 /// \macro_image
7 /// \macro_output
8 /// \macro_code
9 /// \author 07/2008 - Wouter Verkerke
10 
11 #include "RooRealVar.h"
12 #include "RooDataSet.h"
13 #include "RooGaussian.h"
14 #include "RooAddPdf.h"
15 #include "RooChebychev.h"
16 #include "RooExponential.h"
17 #include "TCanvas.h"
18 #include "TAxis.h"
19 #include "RooPlot.h"
20 using namespace RooFit;
21 
22 void rf205_compplot()
23 {
24  // S e t u p c o m p o s i t e p d f
25  // --------------------------------------
26 
27  // Declare observable x
28  RooRealVar x("x", "x", 0, 10);
29 
30  // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
31  RooRealVar mean("mean", "mean of gaussians", 5);
32  RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
33  RooRealVar sigma2("sigma2", "width of gaussians", 1);
34  RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
35  RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
36 
37  // Sum the signal components into a composite signal p.d.f.
38  RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
39  RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
40 
41  // Build Chebychev polynomial p.d.f.
42  RooRealVar a0("a0", "a0", 0.5, 0., 1.);
43  RooRealVar a1("a1", "a1", 0.2, 0., 1.);
44  RooChebychev bkg1("bkg1", "Background 1", x, RooArgSet(a0, a1));
45 
46  // Build exponential pdf
47  RooRealVar alpha("alpha", "alpha", -1);
48  RooExponential bkg2("bkg2", "Background 2", x, alpha);
49 
50  // Sum the background components into a composite background p.d.f.
51  RooRealVar bkg1frac("sig1frac", "fraction of component 1 in background", 0.2, 0., 1.);
52  RooAddPdf bkg("bkg", "Signal", RooArgList(bkg1, bkg2), sig1frac);
53 
54  // Sum the composite signal and background
55  RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
56  RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);
57 
58  // 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
59  // ------------------------------------------------------------------------------
60 
61  // Generate a data sample of 1000 events in x from model
62  RooDataSet *data = model.generate(x, 1000);
63 
64  // Plot data and complete PDF overlaid
65  RooPlot *xframe = x.frame(Title("Component plotting of pdf=(sig1+sig2)+(bkg1+bkg2)"));
66  data->plotOn(xframe);
67  model.plotOn(xframe);
68 
69  // Clone xframe for use below
70  RooPlot *xframe2 = (RooPlot *)xframe->Clone("xframe2");
71 
72  // 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
73  // --------------------------------------------------------------------
74 
75  // Plot single background component specified by object reference
76  model.plotOn(xframe, Components(bkg), LineColor(kRed));
77 
78  // Plot single background component specified by object reference
79  model.plotOn(xframe, Components(bkg2), LineStyle(kDashed), LineColor(kRed));
80 
81  // Plot multiple background components specified by object reference
82  // Note that specified components may occur at any level in object tree
83  // (e.g bkg is component of 'model' and 'sig2' is component 'sig')
84  model.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineStyle(kDotted));
85 
86  // 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
87  // ------------------------------------------------------------
88 
89  // Plot single background component specified by name
90  model.plotOn(xframe2, Components("bkg"), LineColor(kCyan));
91 
92  // Plot multiple background components specified by name
93  model.plotOn(xframe2, Components("bkg1,sig2"), LineStyle(kDotted), LineColor(kCyan));
94 
95  // Plot multiple background components specified by regular expression on name
96  model.plotOn(xframe2, Components("sig*"), LineStyle(kDashed), LineColor(kCyan));
97 
98  // Plot multiple background components specified by multiple regular expressions on name
99  model.plotOn(xframe2, Components("bkg1,sig*"), LineStyle(kDashed), LineColor(kYellow), Invisible());
100 
101  // Draw the frame on the canvas
102  TCanvas *c = new TCanvas("rf205_compplot", "rf205_compplot", 800, 400);
103  c->Divide(2);
104  c->cd(1);
105  gPad->SetLeftMargin(0.15);
106  xframe->GetYaxis()->SetTitleOffset(1.4);
107  xframe->Draw();
108  c->cd(2);
109  gPad->SetLeftMargin(0.15);
110  xframe2->GetYaxis()->SetTitleOffset(1.4);
111  xframe2->Draw();
112  c->Draw();
113 }