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