Logo ROOT  
Reference Guide
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_output
8 /// \macro_code
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"
22 using namespace RooFit;
23 
24 void 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", RooArgList(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, RooArgSet(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("sig1frac", "fraction of component 1 in background", 0.2, 0., 1.);
54  RooAddPdf bkg("bkg", "Signal", RooArgList(bkg1, bkg2), sig1frac);
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", RooArgList(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  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(RooArgSet(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 }
c
#define c(i)
Definition: RSha256.hxx:101
RooPlot::Draw
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
RooChebychev.h
RooAddPdf
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:32
RooChebychev
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
RooArgList
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooGaussian.h
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
RooAddPdf.h
TCanvas.h
RooDataSet.h
kCyan
@ kCyan
Definition: Rtypes.h:66
RooFit
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: RooCFunction1Binding.h:29
RooAbsData::plotOn
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
Definition: RooAbsData.cxx:547
TNamed::Clone
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TNamed.cxx:74
RooPlot.h
RooPlot::GetYaxis
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1258
RooPlot
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:44
RooFit::Invisible
RooCmdArg Invisible(bool inv=true)
Definition: RooGlobalFunc.cxx:64
RooRealVar.h
kRed
@ kRed
Definition: Rtypes.h:66
RooExponential.h
RooFit::LineColor
RooCmdArg LineColor(Color_t color)
Definition: RooGlobalFunc.cxx:57
TCanvas
The Canvas class.
Definition: TCanvas.h:23
rf205_compplot
Definition: rf205_compplot.py:1
TAxis.h
kDashed
@ kDashed
Definition: TAttLine.h:48
gPad
#define gPad
Definition: TVirtualPad.h:287
RooDataSet
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
make_cnn_model.model
model
Definition: make_cnn_model.py:6
RooFit::LineStyle
RooCmdArg LineStyle(Style_t style)
Definition: RooGlobalFunc.cxx:58
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
RooRealVar
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:37
RooFit::Components
RooCmdArg Components(const RooArgSet &compSet)
Definition: RooGlobalFunc.cxx:75
kYellow
@ kYellow
Definition: Rtypes.h:66
RooExponential
Exponential PDF.
Definition: RooExponential.h:25
RooFit::Title
RooCmdArg Title(const char *name)
Definition: RooGlobalFunc.cxx:176
kDotted
@ kDotted
Definition: TAttLine.h:48
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29