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