Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf404_categories.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -nodraw
4/// Data and categories: working with RooCategory objects to describe discrete variables
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 "RooPolynomial.h"
15#include "RooCategory.h"
16#include "Roo1DTable.h"
17#include "RooGaussian.h"
18#include "TCanvas.h"
19#include "TAxis.h"
20#include "RooPlot.h"
21#include <iostream>
22using namespace RooFit;
23
25{
26
27 // C o n s t r u c t a c a t e g o r y w i t h l a b e l s
28 // ----------------------------------------------------------------
29
30 // Define a category with labels only
31 RooCategory tagCat("tagCat", "Tagging category");
32 tagCat.defineType("Lepton");
33 tagCat.defineType("Kaon");
34 tagCat.defineType("NetTagger-1");
35 tagCat.defineType("NetTagger-2");
36 tagCat.Print();
37
38 // C o n s t r u c t a c a t e g o r y w i t h l a b e l s a n d i n d i c e s
39 // ----------------------------------------------------------------------------------------
40
41 // Define a category with explicitly numbered states
42 RooCategory b0flav("b0flav", "B0 flavour eigenstate");
43 b0flav["B0"] = -1;
44 b0flav["B0bar"] = 1;
45 // Print it in "verbose" mode to see all states.
46 b0flav.Print("V");
47
48
49 // Alternatively, define many states at once. The function takes
50 // a map with std::string --> index mapping.
51 RooCategory largeCat("largeCat", "A category with many states");
52 largeCat.defineTypes({
53 {"A", 0}, {"b", 2}, {"c", 8}, {"dee", 4},
54 {"F", 133}, {"g", 15}, {"H", -20}
55 });
56
57
58 // I t e r a t e, q u e r y a n d s e t s t a t e s
59 // --------------------------------------------------------
60
61 // One can iterate through the {index,name} pair of category objects
62 std::cout << "\nThis is the for loop over states of 'largeCat':";
63 for (const auto& idxAndName : largeCat)
64 std::cout << "\n\t" << idxAndName.first << "\t" << idxAndName.second;
65 std::cout << '\n' << std::endl;
66
67 // To ask whether a state is valid use:
68 std::cout << "Has label 'A': " << largeCat.hasLabel("A");
69 std::cout << "\nHas index '-20': " << largeCat.hasIndex(-20);
70
71 // To retrieve names or state numbers:
72 std::cout << "\nLabel corresponding to '2' is " << largeCat.lookupName(2);
73 std::cout << "\nIndex corresponding to 'A' is " << largeCat.lookupIndex("A");
74
75 // To get the current state:
76 std::cout << "\nCurrent index is " << largeCat.getCurrentIndex();
77 std::cout << "\nCurrent label is " << largeCat.getCurrentLabel();
78 std::cout << std::endl;
79
80 // To set the state, use one of the two:
81 largeCat.setIndex(8);
82 largeCat.setLabel("c");
83
84
85
86 // G e n e r a t e d u m m y d a t a f o r t a b u l a t i o n d e m o
87 // ----------------------------------------------------------------------------
88
89 // Generate a dummy dataset
90 RooRealVar x("x", "x", 0, 10);
91 std::unique_ptr<RooDataSet> data{RooPolynomial("p", "p", x).generate({x, b0flav, tagCat}, 10000)};
92
93
94 // P r i n t t a b l e s o f c a t e g o r y c o n t e n t s o f d a t a s e t s
95 // ------------------------------------------------------------------------------------------
96
97 // Tables are equivalent of plots for categories
98 Roo1DTable *btable = data->table(b0flav);
99 btable->Print();
100 btable->Print("v");
101
102 // Create table for subset of events matching cut expression
103 Roo1DTable *ttable = data->table(tagCat, "x>8.23");
104 ttable->Print();
105 ttable->Print("v");
106
107 // Create table for all (tagCat x b0flav) state combinations
108 Roo1DTable *bttable = data->table(RooArgSet(tagCat, b0flav));
109 bttable->Print("v");
110
111 // Retrieve number of events from table
112 // Number can be non-integer if source dataset has weighed events
113 double nb0 = btable->get("B0");
114 std::cout << "Number of events with B0 flavor is " << nb0 << std::endl;
115
116 // Retrieve fraction of events with "Lepton" tag
117 double fracLep = ttable->getFrac("Lepton");
118 std::cout << "Fraction of events tagged with Lepton tag is " << fracLep << std::endl;
119
120 // D e f i n i n g r a n g e s f o r p l o t t i n g , f i t t i n g o n c a t e g o r i e s
121 // ------------------------------------------------------------------------------------------------------
122
123 // Define named range as comma separated list of labels
124 tagCat.setRange("good", "Lepton,Kaon");
125
126 // Or add state names one by one
127 tagCat.addToRange("soso", "NetTagger-1");
128 tagCat.addToRange("soso", "NetTagger-2");
129
130 // Use category range in dataset reduction specification
131 std::unique_ptr<RooAbsData> goodData{data->reduce(CutRange("good"))};
132 static_cast<RooDataSet&>(*goodData).table(tagCat)->Print("v");
133}
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
One-dimensional table.
Definition Roo1DTable.h:23
double get(const char *label, bool silent=false) const
Return the table entry named 'label'.
double getFrac(const char *label, bool silent=false) const
Return the fraction of entries in the table contained in the slot named 'label'.
void Print(Option_t *options=nullptr) const override
This method must be overridden when a class wants to print itself.
Definition Roo1DTable.h:50
virtual Roo1DTable * table(const RooArgSet &catSet, const char *cuts="", const char *opts="") const
Construct table for product of categories in catSet.
RooFit::OwningPtr< RooDataSet > generate(const RooArgSet &whatVars, Int_t nEvents, const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={})
See RooAbsPdf::generate(const RooArgSet&,const RooCmdArg&,const RooCmdArg&,const RooCmdArg&,...
Definition RooAbsPdf.h:57
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Object to represent discrete states.
Definition RooCategory.h:28
Container class to hold unbinned data.
Definition RooDataSet.h:57
RooPolynomial implements a polynomial p.d.f of the form.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
RooCmdArg CutRange(const char *rangeName)
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