Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf207_comptools.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -nodraw
4/// Addition and convolution: tools and utilities for manipulation of composite objects
5///
6/// \macro_code
7/// \macro_output
8///
9/// \date July 2008
10/// \author Wouter Verkerke
11
12#include "RooRealVar.h"
13#include "RooDataSet.h"
14#include "RooGaussian.h"
15#include "RooChebychev.h"
16#include "RooExponential.h"
17#include "RooAddPdf.h"
18#include "RooPlot.h"
19#include "RooCustomizer.h"
20#include "TCanvas.h"
21#include "TAxis.h"
22#include "TH1.h"
23using namespace RooFit;
24
25void rf207_comptools()
26{
27
28 // S e t u p c o m p o s i t e p d f, d a t a s e t
29 // --------------------------------------------------------
30
31 // Declare observable x
32 RooRealVar x("x", "x", 0, 10);
33
34 // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
35 RooRealVar mean("mean", "mean of gaussians", 5);
36 RooRealVar sigma("sigma", "width of gaussians", 0.5);
37 RooGaussian sig("sig", "Signal component 1", x, mean, sigma);
38
39 // Build Chebychev polynomial pdf
40 RooRealVar a0("a0", "a0", 0.5, 0., 1.);
41 RooRealVar a1("a1", "a1", 0.2, 0., 1.);
42 RooChebychev bkg1("bkg1", "Background 1", x, RooArgSet(a0, a1));
43
44 // Build exponential pdf
45 RooRealVar alpha("alpha", "alpha", -1);
46 RooExponential bkg2("bkg2", "Background 2", x, alpha);
47
48 // Sum the background components into a composite background pdf
49 RooRealVar bkg1frac("bkg1frac", "fraction of component 1 in background", 0.2, 0., 1.);
50 RooAddPdf bkg("bkg", "Signal", RooArgList(bkg1, bkg2), bkg1frac);
51
52 // Sum the composite signal and background
53 RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
54 RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);
55
56 // Create dummy dataset that has more observables than the above pdf
57 RooRealVar y("y", "y", -10, 10);
58 RooDataSet data("data", "data", RooArgSet(x, y));
59
60 // ---------------------------------------------------
61 // B a s i c i n f o r m a t i o n r e q u e s t s
62 // ===================================================
63
64 // G e t l i s t o f o b s e r v a b l e s
65 // ---------------------------------------------
66
67 // Get list of observables of pdf in context of a dataset
68 //
69 // Observables are define each context as the variables
70 // shared between a model and a dataset. In this case
71 // that is the variable 'x'
72
73 std::unique_ptr<RooArgSet> model_obs{model.getObservables(data)};
74 model_obs->Print("v");
75
76 // G e t l i s t o f p a r a m e t e r s
77 // -------------------------------------------
78
79 // Get list of parameters, given list of observables
80 std::unique_ptr<RooArgSet> model_params{model.getParameters(x)};
81 model_params->Print("v");
82
83 // Get list of parameters, given a dataset
84 // (Gives identical results to operation above)
85 std::unique_ptr<RooArgSet> model_params2{model.getParameters(data)};
86 model_params2->Print();
87
88 // G e t l i s t o f c o m p o n e n t s
89 // -------------------------------------------
90
91 // Get list of component objects, including top-level node
92 std::unique_ptr<RooArgSet> model_comps{model.getComponents()};
93 model_comps->Print("v");
94
95 // -------------------------------------------------------------------------------
96 // M o d i f i c a t i o n s t o s t r u c t u r e o f c o m p o s i t e s
97 // ===============================================================================
98
99 // Create a second Gaussian
100 RooRealVar sigma2("sigma2", "width of gaussians", 1);
101 RooGaussian sig2("sig2", "Signal component 1", x, mean, sigma2);
102
103 // Create a sum of the original Gaussian plus the new second Gaussian
104 RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
105 RooAddPdf sigsum("sigsum", "sig+sig2", RooArgList(sig, sig2), sig1frac);
106
107 // Construct a customizer utility to customize model
108 RooCustomizer cust(model, "cust");
109
110 // Instruct the customizer to replace node 'sig' with node 'sigsum'
111 cust.replaceArg(sig, sigsum);
112
113 // Build a clone of the input pdf according to the above customization
114 // instructions. Each node that requires modified is clone so that the
115 // original pdf remained untouched. The name of each cloned node is that
116 // of the original node suffixed by the name of the customizer object
117 //
118 // The returned head node own all nodes that were cloned as part of
119 // the build process so when cust_clone is deleted so will all other
120 // nodes that were created in the process.
121 RooAbsPdf *cust_clone = (RooAbsPdf *)cust.build(true);
122
123 // Print structure of clone of model with sig->sigsum replacement.
124 cust_clone->Print("t");
125
126 delete cust_clone;
127}
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
void Print(Option_t *options=nullptr) const override
Print the object to the defaultPrintStream().
Definition RooAbsArg.h:320
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
Efficient implementation of a sum of PDFs of the form.
Definition RooAddPdf.h:33
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Chebychev polynomial p.d.f.
RooCustomizer is a factory class to produce clones of a prototype composite PDF object with the same ...
Container class to hold unbinned data.
Definition RooDataSet.h:57
Exponential PDF.
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
Variable that can be changed from the outside.
Definition RooRealVar.h:37
const Double_t sigma
Double_t y[n]
Definition legend1.C:17
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 JSONIO.h:26