From $ROOTSYS/tutorials/roofit/rf404_categories.C

//////////////////////////////////////////////////////////////////////////
//
// 'DATA AND CATEGORIES' RooFit tutorial macro #404
// 
// Working with RooCategory objects to describe discrete variables
//
//
//
// 07/2008 - Wouter Verkerke 
// 
/////////////////////////////////////////////////////////////////////////

#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooPolynomial.h"
#include "RooCategory.h"
#include "Roo1DTable.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
using namespace RooFit ;


void rf404_categories()
{

  // 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 
  // ----------------------------------------------------------------

  // Define a category with labels only
  RooCategory tagCat("tagCat","Tagging category") ;
  tagCat.defineType("Lepton") ;
  tagCat.defineType("Kaon") ;
  tagCat.defineType("NetTagger-1") ;
  tagCat.defineType("NetTagger-2") ;
  tagCat.Print() ;



  // 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
  // ----------------------------------------------------------------------------------------

  // Define a category with explicitly numbered states
  RooCategory b0flav("b0flav","B0 flavour eigenstate") ;
  b0flav.defineType("B0",-1) ;
  b0flav.defineType("B0bar",1) ;
  b0flav.Print() ;



  // 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 
  // ----------------------------------------------------------------------------
  
  // Generate a dummy dataset 
  RooRealVar x("x","x",0,10) ;
  RooDataSet *data = RooPolynomial("p","p",x).generate(RooArgSet(x,b0flav,tagCat),10000) ;



  // 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
  // ------------------------------------------------------------------------------------------

  // Tables are equivalent of plots for categories
  Roo1DTable* btable = data->table(b0flav) ;
  btable->Print() ;
  btable->Print("v") ;

  // Create table for subset of events matching cut expression
  Roo1DTable* ttable = data->table(tagCat,"x>8.23") ;
  ttable->Print() ;
  ttable->Print("v") ;

  // Create table for all (tagCat x b0flav) state combinations
  Roo1DTable* bttable = data->table(RooArgSet(tagCat,b0flav)) ;
  bttable->Print("v") ;

  // Retrieve number of events from table
  // Number can be non-integer if source dataset has weighed events
  Double_t nb0 = btable->get("B0") ;
  cout << "Number of events with B0 flavor is " << nb0 << endl ;

  // Retrieve fraction of events with "Lepton" tag
  Double_t fracLep = ttable->getFrac("Lepton") ;
  cout << "Fraction of events tagged with Lepton tag is " << fracLep << endl ;
  


  // 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
  // ------------------------------------------------------------------------------------------------------

  // Define named range as comma separated list of labels
  tagCat.setRange("good","Lepton,Kaon") ;

  // Or add state names one by one
  tagCat.addToRange("soso","NetTagger-1") ;
  tagCat.addToRange("soso","NetTagger-2") ;
  
  // Use category range in dataset reduction specification
  RooDataSet* goodData = (RooDataSet*) data->reduce(CutRange("good")) ;
  goodData->table(tagCat)->Print("v") ;


}
 rf404_categories.C:1
 rf404_categories.C:2
 rf404_categories.C:3
 rf404_categories.C:4
 rf404_categories.C:5
 rf404_categories.C:6
 rf404_categories.C:7
 rf404_categories.C:8
 rf404_categories.C:9
 rf404_categories.C:10
 rf404_categories.C:11
 rf404_categories.C:12
 rf404_categories.C:13
 rf404_categories.C:14
 rf404_categories.C:15
 rf404_categories.C:16
 rf404_categories.C:17
 rf404_categories.C:18
 rf404_categories.C:19
 rf404_categories.C:20
 rf404_categories.C:21
 rf404_categories.C:22
 rf404_categories.C:23
 rf404_categories.C:24
 rf404_categories.C:25
 rf404_categories.C:26
 rf404_categories.C:27
 rf404_categories.C:28
 rf404_categories.C:29
 rf404_categories.C:30
 rf404_categories.C:31
 rf404_categories.C:32
 rf404_categories.C:33
 rf404_categories.C:34
 rf404_categories.C:35
 rf404_categories.C:36
 rf404_categories.C:37
 rf404_categories.C:38
 rf404_categories.C:39
 rf404_categories.C:40
 rf404_categories.C:41
 rf404_categories.C:42
 rf404_categories.C:43
 rf404_categories.C:44
 rf404_categories.C:45
 rf404_categories.C:46
 rf404_categories.C:47
 rf404_categories.C:48
 rf404_categories.C:49
 rf404_categories.C:50
 rf404_categories.C:51
 rf404_categories.C:52
 rf404_categories.C:53
 rf404_categories.C:54
 rf404_categories.C:55
 rf404_categories.C:56
 rf404_categories.C:57
 rf404_categories.C:58
 rf404_categories.C:59
 rf404_categories.C:60
 rf404_categories.C:61
 rf404_categories.C:62
 rf404_categories.C:63
 rf404_categories.C:64
 rf404_categories.C:65
 rf404_categories.C:66
 rf404_categories.C:67
 rf404_categories.C:68
 rf404_categories.C:69
 rf404_categories.C:70
 rf404_categories.C:71
 rf404_categories.C:72
 rf404_categories.C:73
 rf404_categories.C:74
 rf404_categories.C:75
 rf404_categories.C:76
 rf404_categories.C:77
 rf404_categories.C:78
 rf404_categories.C:79
 rf404_categories.C:80
 rf404_categories.C:81
 rf404_categories.C:82
 rf404_categories.C:83
 rf404_categories.C:84
 rf404_categories.C:85
 rf404_categories.C:86
 rf404_categories.C:87
 rf404_categories.C:88
 rf404_categories.C:89
 rf404_categories.C:90
 rf404_categories.C:91
 rf404_categories.C:92
 rf404_categories.C:93
 rf404_categories.C:94
 rf404_categories.C:95
 rf404_categories.C:96
 rf404_categories.C:97
 rf404_categories.C:98
 rf404_categories.C:99
 rf404_categories.C:100
 rf404_categories.C:101
 rf404_categories.C:102
 rf404_categories.C:103
 rf404_categories.C:104
 rf404_categories.C:105
 rf404_categories.C:106
 rf404_categories.C:107
 rf404_categories.C:108
 rf404_categories.C:109