Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 pdfs.
5///
6/// \macro_image
7/// \macro_code
8/// \macro_output
9///
10/// \date July 2008
11/// \author Wouter Verkerke
12
13#include "RooRealVar.h"
14#include "RooDataSet.h"
15#include "RooGaussian.h"
16#include "RooAddPdf.h"
17#include "RooChebychev.h"
18#include "RooExponential.h"
19#include "TCanvas.h"
20#include "TAxis.h"
21#include "RooPlot.h"
22using namespace RooFit;
23
24void rf205_compplot()
25{
26 // S e t u p c o m p o s i t e p d f
27 // --------------------------------------
28
29 // Declare observable x
30 RooRealVar x("x", "x", 0, 10);
31
32 // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
33 RooRealVar mean("mean", "mean of gaussians", 5);
34 RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
35 RooRealVar sigma2("sigma2", "width of gaussians", 1);
36 RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
37 RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
38
39 // Sum the signal components into a composite signal pdf
40 RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
41 RooAddPdf sig("sig", "Signal", {sig1, sig2}, sig1frac);
42
43 // Build Chebychev polynomial pdf
44 RooRealVar a0("a0", "a0", 0.5, 0., 1.);
45 RooRealVar a1("a1", "a1", 0.2, 0., 1.);
46 RooChebychev bkg1("bkg1", "Background 1", x, {a0, a1});
47
48 // Build exponential pdf
49 RooRealVar alpha("alpha", "alpha", -1);
50 RooExponential bkg2("bkg2", "Background 2", x, alpha);
51
52 // Sum the background components into a composite background pdf
53 RooRealVar bkg1frac("bkg1frac", "fraction of component 1 in background", 0.8, 0., 1.);
54 RooAddPdf bkg("bkg", "Total background", {bkg1, bkg2}, bkg1frac);
55
56 // Sum the composite signal and background
57 RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
58 RooAddPdf model("model", "g1+g2+a", {bkg, sig}, bkgfrac);
59
60 // 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
61 // ------------------------------------------------------------------------------
62
63 // Generate a data sample of 1000 events in x from model
64 std::unique_ptr<RooDataSet> data{model.generate(x, 1000)};
65
66 // Plot data and complete PDF overlaid
67 RooPlot *xframe = x.frame(Title("Component plotting of pdf=(sig1+sig2)+(bkg1+bkg2)"));
68 data->plotOn(xframe);
69 model.plotOn(xframe);
70
71 // Clone xframe for use below
72 RooPlot *xframe2 = (RooPlot *)xframe->Clone("xframe2");
73
74 // 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
75 // --------------------------------------------------------------------
76
77 // Plot single background component specified by object reference
78 model.plotOn(xframe, Components(bkg), LineColor(kRed));
79
80 // Plot single background component specified by object reference
81 model.plotOn(xframe, Components(bkg2), LineStyle(kDashed), LineColor(kRed));
82
83 // Plot multiple background components specified by object reference
84 // Note that specified components may occur at any level in object tree
85 // (e.g bkg is component of 'model' and 'sig2' is component 'sig')
86 model.plotOn(xframe, Components(bkg, sig2), LineStyle(kDotted));
87
88 // 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
89 // ------------------------------------------------------------
90
91 // Plot single background component specified by name
92 model.plotOn(xframe2, Components("bkg"), LineColor(kCyan));
93
94 // Plot multiple background components specified by name
95 model.plotOn(xframe2, Components("bkg1,sig2"), LineStyle(kDotted), LineColor(kCyan));
96
97 // Plot multiple background components specified by regular expression on name
98 model.plotOn(xframe2, Components("sig*"), LineStyle(kDashed), LineColor(kCyan));
99
100 // Plot multiple background components specified by multiple regular expressions on name
101 model.plotOn(xframe2, Components("bkg1,sig*"), LineStyle(kDashed), LineColor(kYellow), Invisible());
102
103 // Draw the frame on the canvas
104 TCanvas *c = new TCanvas("rf205_compplot", "rf205_compplot", 800, 400);
105 c->Divide(2);
106 c->cd(1);
107 gPad->SetLeftMargin(0.15);
108 xframe->GetYaxis()->SetTitleOffset(1.4);
109 xframe->Draw();
110 c->cd(2);
111 gPad->SetLeftMargin(0.15);
112 xframe2->GetYaxis()->SetTitleOffset(1.4);
113 xframe2->Draw();
114 c->Draw();
115}
#define c(i)
Definition RSha256.hxx:101
@ kRed
Definition Rtypes.h:66
@ kCyan
Definition Rtypes.h:66
@ kYellow
Definition Rtypes.h:66
@ kDashed
Definition TAttLine.h:48
@ kDotted
Definition TAttLine.h:48
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
#define gPad
Efficient implementation of a sum of PDFs of the form.
Definition RooAddPdf.h:33
Chebychev polynomial p.d.f.
Exponential PDF.
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:43
static RooPlot * frame(const RooAbsRealLValue &var, double xmin, double xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:185
TAxis * GetYaxis() const
Definition RooPlot.cxx:1224
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:597
Variable that can be changed from the outside.
Definition RooRealVar.h:37
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
The Canvas class.
Definition TCanvas.h:23
TObject * Clone(const char *newname="") const override
Make a clone of an object using the Streamer facility.
Definition TNamed.cxx:74
RooCmdArg Components(Args_t &&... argsOrArgSet)
RooCmdArg Invisible(bool inv=true)
RooCmdArg LineColor(Color_t color)
RooCmdArg LineStyle(Style_t style)
Double_t x[n]
Definition legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:64
const char * Title
Definition TXMLSetup.cxx:68