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