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