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