Logo ROOT   6.08/07
Reference Guide
rf404_categories.cxx
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////
2 //
3 // 'DATA AND CATEGORIES' RooFit tutorial macro #404
4 //
5 // Working with RooCategory objects to describe discrete variables
6 //
7 //
8 //
9 // 07/2008 - Wouter Verkerke
10 //
11 /////////////////////////////////////////////////////////////////////////
12 
13 #ifndef __CINT__
14 #include "RooGlobalFunc.h"
15 #endif
16 #include "RooRealVar.h"
17 #include "RooDataSet.h"
18 #include "RooPolynomial.h"
19 #include "RooCategory.h"
20 #include "Roo1DTable.h"
21 #include "RooGaussian.h"
22 #include "TCanvas.h"
23 #include "RooPlot.h"
24 using namespace RooFit ;
25 
26 
27 class TestBasic404 : public RooFitTestUnit
28 {
29 public:
30  TestBasic404(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Categories basic functionality",refFile,writeRef,verbose) {} ;
31  Bool_t testCode() {
32 
33  // 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
34  // ----------------------------------------------------------------
35 
36  // Define a category with labels only
37  RooCategory tagCat("tagCat","Tagging category") ;
38  tagCat.defineType("Lepton") ;
39  tagCat.defineType("Kaon") ;
40  tagCat.defineType("NetTagger-1") ;
41  tagCat.defineType("NetTagger-2") ;
42 
43 
44 
45  // 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 e c e s
46  // ----------------------------------------------------------------------------------------
47 
48  // Define a category with explicitly numbered states
49  RooCategory b0flav("b0flav","B0 flavour eigenstate") ;
50  b0flav.defineType("B0",-1) ;
51  b0flav.defineType("B0bar",1) ;
52 
53 
54 
55  // 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
56  // ----------------------------------------------------------------------------
57 
58  // Generate a dummy dataset
59  RooRealVar x("x","x",0,10) ;
60  RooDataSet *data = RooPolynomial("p","p",x).generate(RooArgSet(x,b0flav,tagCat),10000) ;
61 
62 
63 
64  // 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
65  // ------------------------------------------------------------------------------------------
66 
67  // Tables are equivalent of plots for categories
68  Roo1DTable* btable = data->table(b0flav) ;
69 
70  // Create table for subset of events matching cut expression
71  Roo1DTable* ttable = data->table(tagCat,"x>8.23") ;
72 
73  // Create table for all (tagCat x b0flav) state combinations
74  Roo1DTable* bttable = data->table(RooArgSet(tagCat,b0flav)) ;
75 
76  // Retrieve number of events from table
77  // Number can be non-integer if source dataset has weighed events
78  Double_t nb0 = btable->get("B0") ;
79  regValue(nb0,"rf404_nb0") ;
80 
81  // Retrieve fraction of events with "Lepton" tag
82  Double_t fracLep = ttable->getFrac("Lepton") ;
83  regValue(fracLep,"rf404_fracLep") ;
84 
85 
86  // 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
87  // ------------------------------------------------------------------------------------------------------
88 
89  // Define named range as comma separated list of labels
90  tagCat.setRange("good","Lepton,Kaon") ;
91 
92  // Or add state names one by one
93  tagCat.addToRange("soso","NetTagger-1") ;
94  tagCat.addToRange("soso","NetTagger-2") ;
95 
96  // Use category range in dataset reduction specification
97  RooDataSet* goodData = (RooDataSet*) data->reduce(CutRange("good")) ;
98  Roo1DTable* gtable = goodData->table(tagCat) ;
99 
100 
101  regTable(btable,"rf404_btable") ;
102  regTable(ttable,"rf404_ttable") ;
103  regTable(bttable,"rf404_bttable") ;
104  regTable(gtable,"rf404_gtable") ;
105 
106 
107  delete goodData ;
108  delete data ;
109  return kTRUE ;
110 
111  }
112 
113 } ;
Double_t get(const char *label, Bool_t silent=kFALSE) const
Return the table entry named 'label'.
Definition: Roo1DTable.cxx:244
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
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:343
Double_t x[n]
Definition: legend1.C:17
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
bool verbose
RooCmdArg CutRange(const char *rangeName)
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
RooCategory represents a fundamental (non-derived) discrete value object.
Definition: RooCategory.h:25
double Double_t
Definition: RtypesCore.h:55
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())
Generate a new dataset containing the specified variables with events sampled from our distribution...
Definition: RooAbsPdf.cxx:1702
RooPolynomial implements a polynomial p.d.f of the form By default coefficient a_0 is chosen to be 1...
Definition: RooPolynomial.h:28
const Bool_t kTRUE
Definition: Rtypes.h:91
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:301
Roo1DTable implements a one-dimensional table.
Definition: Roo1DTable.h:25
virtual Roo1DTable * table(const RooArgSet &catSet, const char *cuts="", const char *opts="") const
Construct table for product of categories in catSet.
Definition: RooAbsData.cxx:752