 ROOT   Reference Guide rf201_composite.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4/// Addition and convolution: composite pdf with signal and background component
5///
6/// 
7/// pdf = f_bkg * bkg(x,a0,a1) + (1-fbkg) * (f_sig1 * sig1(x,m,s1 + (1-f_sig1) * sig2(x,m,s2)))
8/// 
9///
10/// \macro_image
11/// \macro_output
12/// \macro_code
13///
14/// \date July 2008
15/// \author Wouter Verkerke
16
17#include "RooRealVar.h"
18#include "RooDataSet.h"
19#include "RooGaussian.h"
20#include "RooChebychev.h"
22#include "TCanvas.h"
23#include "TAxis.h"
24#include "RooPlot.h"
25using namespace RooFit;
26
27void rf201_composite()
28{
29 // S e t u p c o m p o n e n t p d f s
30 // ---------------------------------------
31
32 // Declare observable x
33 RooRealVar x("x", "x", 0, 10);
34
35 // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
36 RooRealVar mean("mean", "mean of gaussians", 5);
37 RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
38 RooRealVar sigma2("sigma2", "width of gaussians", 1);
39
40 RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
41 RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
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 bkg("bkg", "Background", x, RooArgSet(a0, a1));
47
48 // ---------------------------------------------
49 // M E T H O D 1 - T w o R o o A d d P d f s
50 // =============================================
51
52 // A d d s i g n a l c o m p o n e n t s
53 // ------------------------------------------
54
55 // Sum the signal components into a composite signal pdf
56 RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
57 RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
58
59 // A d d s i g n a l a n d b a c k g r o u n d
60 // ------------------------------------------------
61
62 // Sum the composite signal and background
63 RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
64 RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);
65
66 // S a m p l e , f i t a n d p l o t m o d e l
67 // ---------------------------------------------------
68
69 // Generate a data sample of 1000 events in x from model
70 RooDataSet *data = model.generate(x, 1000);
71
72 // Fit model to data
73 model.fitTo(*data);
74
75 // Plot data and PDF overlaid
76 RooPlot *xframe = x.frame(Title("Example of composite pdf=(sig1+sig2)+bkg"));
77 data->plotOn(xframe);
78 model.plotOn(xframe);
79
80 // Overlay the background component of model with a dashed line
81 model.plotOn(xframe, Components(bkg), LineStyle(kDashed));
82
83 // Overlay the background+sig2 components of model with a dotted line
84 model.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineStyle(kDotted));
85
86 // Print structure of composite pdf
87 model.Print("t");
88
89 // ---------------------------------------------------------------------------------------------
90 // M E T H O D 2 - O n e R o o A d d P d f w i t h r e c u r s i v e f r a c t i o n s
91 // =============================================================================================
92
93 // Construct sum of models on one go using recursive fraction interpretations
94 //
95 // model2 = bkg + (sig1 + sig2)
96 //
97 RooAddPdf model2("model", "g1+g2+a", RooArgList(bkg, sig1, sig2), RooArgList(bkgfrac, sig1frac), kTRUE);
98
99 // NB: Each coefficient is interpreted as the fraction of the
100 // left-hand component of the i-th recursive sum, i.e.
101 //
102 // sum4 = A + ( B + ( C + D) with fraction fA, fB and fC expands to
103 //
104 // sum4 = fA*A + (1-fA)*(fB*B + (1-fB)*(fC*C + (1-fC)*D))
105
106 // P l o t r e c u r s i v e a d d i t i o n m o d e l
107 // ---------------------------------------------------------
108 model2.plotOn(xframe, LineColor(kRed), LineStyle(kDashed));
109 model2.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineColor(kRed), LineStyle(kDashed));
110 model2.Print("t");
111
112 // Draw the frame on the canvas
113 new TCanvas("rf201_composite", "rf201_composite", 600, 600);
115 xframe->GetYaxis()->SetTitleOffset(1.4);
116 xframe->Draw();
117}
const Bool_t kTRUE
Definition: RtypesCore.h:91
@ kRed
Definition: Rtypes.h:66
@ kDashed
Definition: TAttLine.h:48
@ kDotted
Definition: TAttLine.h:48
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
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:44
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1263
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:39
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
The Canvas class.
Definition: TCanvas.h:23
RooCmdArg Components(const RooArgSet &compSet)
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...
const char * Title
Definition: TXMLSetup.cxx:68