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 ///
5 /// Addition and convolution: options for plotting components of composite p.d.f.s.
6 ///
7 /// \macro_image
8 /// \macro_output
9 /// \macro_code
10 ///
11 /// \date 07/2008
12 /// \author Wouter Verkerke
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 void rf205_compplot()
26 {
27  // S e t u p c o m p o s i t e p d f
28  // --------------------------------------
29 
30  // Declare observable x
31  RooRealVar x("x", "x", 0, 10);
32 
33  // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
34  RooRealVar mean("mean", "mean of gaussians", 5);
35  RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
36  RooRealVar sigma2("sigma2", "width of gaussians", 1);
37  RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
38  RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
39 
40  // Sum the signal components into a composite signal p.d.f.
41  RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
42  RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
43 
44  // Build Chebychev polynomial p.d.f.
45  RooRealVar a0("a0", "a0", 0.5, 0., 1.);
46  RooRealVar a1("a1", "a1", 0.2, 0., 1.);
47  RooChebychev bkg1("bkg1", "Background 1", x, RooArgSet(a0, a1));
48 
49  // Build exponential pdf
50  RooRealVar alpha("alpha", "alpha", -1);
51  RooExponential bkg2("bkg2", "Background 2", x, alpha);
52 
53  // Sum the background components into a composite background p.d.f.
54  RooRealVar bkg1frac("sig1frac", "fraction of component 1 in background", 0.2, 0., 1.);
55  RooAddPdf bkg("bkg", "Signal", RooArgList(bkg1, bkg2), sig1frac);
56 
57  // Sum the composite signal and background
58  RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
59  RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);
60 
61  // 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
62  // ------------------------------------------------------------------------------
63 
64  // Generate a data sample of 1000 events in x from model
65  RooDataSet *data = model.generate(x, 1000);
66 
67  // Plot data and complete PDF overlaid
68  RooPlot *xframe = x.frame(Title("Component plotting of pdf=(sig1+sig2)+(bkg1+bkg2)"));
69  data->plotOn(xframe);
70  model.plotOn(xframe);
71 
72  // Clone xframe for use below
73  RooPlot *xframe2 = (RooPlot *)xframe->Clone("xframe2");
74 
75  // 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
76  // --------------------------------------------------------------------
77 
78  // Plot single background component specified by object reference
79  model.plotOn(xframe, Components(bkg), LineColor(kRed));
80 
81  // Plot single background component specified by object reference
82  model.plotOn(xframe, Components(bkg2), LineStyle(kDashed), LineColor(kRed));
83 
84  // Plot multiple background components specified by object reference
85  // Note that specified components may occur at any level in object tree
86  // (e.g bkg is component of 'model' and 'sig2' is component 'sig')
87  model.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineStyle(kDotted));
88 
89  // 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
90  // ------------------------------------------------------------
91 
92  // Plot single background component specified by name
93  model.plotOn(xframe2, Components("bkg"), LineColor(kCyan));
94 
95  // Plot multiple background components specified by name
96  model.plotOn(xframe2, Components("bkg1,sig2"), LineStyle(kDotted), LineColor(kCyan));
97 
98  // Plot multiple background components specified by regular expression on name
99  model.plotOn(xframe2, Components("sig*"), LineStyle(kDashed), LineColor(kCyan));
100 
101  // Plot multiple background components specified by multiple regular expressions on name
102  model.plotOn(xframe2, Components("bkg1,sig*"), LineStyle(kDashed), LineColor(kYellow), Invisible());
103 
104  // Draw the frame on the canvas
105  TCanvas *c = new TCanvas("rf205_compplot", "rf205_compplot", 800, 400);
106  c->Divide(2);
107  c->cd(1);
108  gPad->SetLeftMargin(0.15);
109  xframe->GetYaxis()->SetTitleOffset(1.4);
110  xframe->Draw();
111  c->cd(2);
112  gPad->SetLeftMargin(0.15);
113  xframe2->GetYaxis()->SetTitleOffset(1.4);
114  xframe2->Draw();
115  c->Draw();
116 }
c
#define c(i)
Definition: RSha256.hxx:119
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
Definition: RooAddPdf.h:32
RooChebychev
Definition: RooChebychev.h:25
RooArgList
Definition: RooArgList.h:21
RooGaussian.h
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Definition: RooGaussian.h:25
RooAddPdf.h
TCanvas.h
RooDataSet.h
kCyan
@ kCyan
Definition: Rtypes.h:66
RooFit
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:1256
RooPlot
Definition: RooPlot.h:44
RooFit::Invisible
RooCmdArg Invisible(bool inv=true)
Definition: RooGlobalFunc.cxx:63
RooRealVar.h
kRed
@ kRed
Definition: Rtypes.h:66
RooExponential.h
RooFit::LineColor
RooCmdArg LineColor(Color_t color)
Definition: RooGlobalFunc.cxx:56
TCanvas
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
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:57
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
RooRealVar
Definition: RooRealVar.h:35
RooFit::Components
RooCmdArg Components(const RooArgSet &compSet)
Definition: RooGlobalFunc.cxx:74
kYellow
@ kYellow
Definition: Rtypes.h:66
RooExponential
Definition: RooExponential.h:25
RooFit::Title
RooCmdArg Title(const char *name)
Definition: RooGlobalFunc.cxx:173
kDotted
@ kDotted
Definition: TAttLine.h:48
RooArgSet
Definition: RooArgSet.h:28