Logo ROOT  
Reference Guide
rf514_RooCustomizer.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -nodraw
4 /// Using the RooCustomizer to create multiple PDFs that share a lot of properties, but have unique parameters for each category.
5 /// As an extra complication, some of the new parameters need to be functions
6 /// of a mass parameter.
7 ///
8 /// \macro_output
9 /// \macro_code
10 ///
11 /// \author Stephan Hageboeck, CERN
12 
13 
14 #include "RooRealVar.h"
15 #include "RooGaussian.h"
16 #include "RooPolynomial.h"
17 #include "RooAddPdf.h"
18 #include "RooCustomizer.h"
19 #include "RooCategory.h"
20 #include "RooFormulaVar.h"
21 #include <iostream>
22 
23 void rf514_RooCustomizer() {
24 
25  // Define a proto model that will be used as the template for each category
26  // ---------------------------------------------------------------------------
27 
28  RooRealVar E("Energy","Energy",0,3000);
29 
30  RooRealVar meanG("meanG","meanG", 100., 0., 3000.);
31  RooRealVar sigmaG("sigmaG","sigmaG", 3.);
32  RooGaussian gauss("gauss", "gauss", E, meanG, sigmaG);
33 
34  RooRealVar pol1("pol1", "Constant of the polynomial", 1, -10, 10);
35  RooPolynomial linear("linear", "linear", E, pol1);
36 
37  RooRealVar yieldSig("yieldSig", "yieldSig", 1, 0, 1.E4);
38  RooRealVar yieldBkg("yieldBkg", "yieldBkg", 1, 0, 1.E4);
39 
40  RooAddPdf model("model", "S + B model",
41  RooArgList(gauss,linear),
42  RooArgList(yieldSig, yieldBkg));
43 
44  std::cout << "The proto model before customisation:" << std::endl;
45  model.Print("T"); // "T" prints the model as a tree
46 
47 
48  // Build the categories
49  RooCategory sample("sample","sample");
50  sample["Sample1"] = 1;
51  sample["Sample2"] = 2;
52  sample["Sample3"] = 3;
53 
54 
55  // Start to customise the proto model that was defined above.
56  // ---------------------------------------------------------------------------
57 
58  // We need two sets for bookkeeping of PDF nodes:
59  RooArgSet newLeafs; // This set collects leafs that are created in the process.
60  RooArgSet allCustomiserNodes; // This set lists leafs that have been used in a replacement operation.
61 
62 
63  // 1. Each sample should have its own mean for the gaussian
64  // The customiser will make copies of `meanG` for each category.
65  // These will all appear in the set `newLeafs`, which will own the new nodes.
66  RooCustomizer cust(model, sample, newLeafs, &allCustomiserNodes);
67  cust.splitArg(meanG, sample);
68 
69 
70  // 2. Each sample should have its own signal yield, but there is an extra complication:
71  // We need the yields 1 and 2 to be a function of the variable "mass".
72  // For this, we pre-define nodes with exacly the names that the customiser would have created automatically,
73  // that is, "<nodeName>_<categoryName>", and we register them in the set of customiser nodes.
74  // The customiser will pick them up instead of creating new ones.
75  // If we don't provide one (e.g. for "yieldSig_Sample3"), it will be created automatically by cloning `yieldSig`.
76  RooRealVar mass("M", "M", 1, 0, 12000);
77  RooFormulaVar yield1("yieldSig_Sample1", "Signal yield in the first sample", "M/3.360779", mass);
78  RooFormulaVar yield2("yieldSig_Sample2", "Signal yield in the second sample", "M/2", mass);
79  allCustomiserNodes.add(yield1);
80  allCustomiserNodes.add(yield2);
81 
82  // Instruct the customiser to replace all yieldSig nodes for each sample:
83  cust.splitArg(yieldSig, sample);
84 
85 
86  // Now we can start building the PDFs for all categories:
87  auto pdf1 = cust.build("Sample1");
88  auto pdf2 = cust.build("Sample2");
89  auto pdf3 = cust.build("Sample3");
90 
91  // And we inspect the two PDFs
92  std::cout << "\nPDF 1 with a yield depending on M:" << std::endl;
93  pdf1->Print("T");
94  std::cout << "\nPDF 2 with a yield depending on M:" << std::endl;
95  pdf2->Print("T");
96  std::cout << "\nPDF 3 with a free yield:" << std::endl;
97  pdf3->Print("T");
98 
99  std::cout << "\nThe following leafs have been created automatically while customising:" << std::endl;
100  newLeafs.Print("V");
101 
102 
103  // If we needed to set reasonable values for the means of the gaussians, this could be done as follows:
104  auto& meanG1 = static_cast<RooRealVar&>(allCustomiserNodes["meanG_Sample1"]);
105  meanG1.setVal(200);
106  auto& meanG2 = static_cast<RooRealVar&>(allCustomiserNodes["meanG_Sample2"]);
107  meanG2.setVal(300);
108 
109  std::cout << "\nThe following leafs have been used while customising"
110  << "\n\t(partial overlap with the set of automatically created leaves."
111  << "\n\ta new customiser for a different PDF could reuse them if necessary.):" << std::endl;
112  allCustomiserNodes.Print("V");
113 
114 
115 }
RooFormulaVar.h
RooRealVar::setVal
virtual void setVal(Double_t value)
Set value of variable to 'value'.
Definition: RooRealVar.cxx:256
RooAddPdf
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:32
RooCustomizer
RooCustomizer is a factory class to produce clones of a prototype composite PDF object with the same ...
Definition: RooCustomizer.h:35
RooArgList
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooGaussian.h
RooGaussian
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
RooAddPdf.h
RooPolynomial.h
RooFormulaVar
A RooFormulaVar is a generic implementation of a real-valued object, which takes a RooArgList of serv...
Definition: RooFormulaVar.h:30
RooCustomizer.h
TGeant4Unit::gauss
static constexpr double gauss
Definition: TGeant4SystemOfUnits.h:263
RooPolynomial
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
RooArgSet::add
Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE) override
Add element to non-owning set.
Definition: RooArgSet.cxx:261
RooCategory.h
RooRealVar.h
RooCategory
RooCategory is an object to represent discrete states.
Definition: RooCategory.h:27
make_cnn_model.model
model
Definition: make_cnn_model.py:6
RooAbsCollection::Print
virtual void Print(Option_t *options=0) const
This method must be overridden when a class wants to print itself.
Definition: RooAbsCollection.h:200
RooRealVar
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:37
TMath::E
constexpr Double_t E()
Base of natural log:
Definition: TMath.h:96
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29