ROOT logo

From $ROOTSYS/tutorials/roofit/rf405_realtocatfuncs.C

//////////////////////////////////////////////////////////////////////////
//
// 'DATA AND CATEGORIES' RooFit tutorial macro #405
// 
// Demonstration of real-->discrete mapping functions
//
//
//
// 07/2008 - Wouter Verkerke 
// 
/////////////////////////////////////////////////////////////////////////

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


void rf405_realtocatfuncs()
{

  // D e f i n e   p d f   i n   x ,   s a m p l e   d a t a s e t   i n   x 
  // ------------------------------------------------------------------------


  // Define a dummy PDF in x 
  RooRealVar x("x","x",0,10) ;
  RooArgusBG a("a","argus(x)",x,RooConst(10),RooConst(-1)) ;

  // Generate a dummy dataset 
  RooDataSet *data = a.generate(x,10000) ;



  // C r e a t e   a   t h r e s h o l d   r e a l - > c a t   f u n c t i o n
  // --------------------------------------------------------------------------

  // A RooThresholdCategory is a category function that maps regions in a real-valued 
  // input observable observables to state names. At construction time a 'default'
  // state name must be specified to which all values of x are mapped that are not
  // otherwise assigned
  RooThresholdCategory xRegion("xRegion","region of x",x,"Background") ;

  // Specify thresholds and state assignments one-by-one. 
  // Each statement specifies that all values _below_ the given value 
  // (and above any lower specified threshold) are mapped to the
  // category state with the given name
  //
  // Background | SideBand | Signal | SideBand | Background
  //           4.23       5.23     8.23       9.23 
  xRegion.addThreshold(4.23,"Background") ;
  xRegion.addThreshold(5.23,"SideBand") ;
  xRegion.addThreshold(8.23,"Signal") ;
  xRegion.addThreshold(9.23,"SideBand") ; 



  // U s e   t h r e s h o l d   f u n c t i o n   t o   p l o t   d a t a   r e g i o n s
  // -------------------------------------------------------------------------------------

  // Add values of threshold function to dataset so that it can be used as observable
  data->addColumn(xRegion) ;

  // Make plot of data in x
  RooPlot* xframe = x.frame(Title("Demo of threshold and binning mapping functions")) ;
  data->plotOn(xframe) ;

  // Use calculated category to select sideband data
  data->plotOn(xframe,Cut("xRegion==xRegion::SideBand"),MarkerColor(kRed),LineColor(kRed)) ;



  // C r e a t e   a   b i n n i n g    r e a l - > c a t   f u n c t i o n
  // ----------------------------------------------------------------------

  // A RooBinningCategory is a category function that maps bins of a (named) binning definition 
  // in a real-valued input observable observables to state names. The state names are automatically
  // constructed from the variable name, the binning name and the bin number. If no binning name
  // is specified the default binning is mapped

  x.setBins(10,"coarse") ;
  RooBinningCategory xBins("xBins","coarse bins in x",x,"coarse") ;



  // U s e   b i n n i n g   f u n c t i o n   f o r   t a b u l a t i o n   a n d   p l o t t i n g
  // -----------------------------------------------------------------------------------------------

  // Print table of xBins state multiplicity. Note that xBins does not need to be an observable in data
  // it can be a function of observables in data as well
  Roo1DTable* xbtable = data->table(xBins) ;
  xbtable->Print("v") ;

  // Add values of xBins function to dataset so that it can be used as observable
  RooCategory* xb = (RooCategory*) data->addColumn(xBins) ;

  // Define range "alt" as including bins 1,3,5,7,9 
  xb->setRange("alt","x_coarse_bin1,x_coarse_bin3,x_coarse_bin5,x_coarse_bin7,x_coarse_bin9") ;
  
  // Construct subset of data matching range "alt" but only for the first 5000 events and plot it on the frame
  RooDataSet* dataSel = (RooDataSet*) data->reduce(CutRange("alt"),EventRange(0,5000)) ;
  dataSel->plotOn(xframe,MarkerColor(kGreen),LineColor(kGreen)) ;



  new TCanvas("rf405_realtocatfuncs","rf405_realtocatfuncs",600,600) ;
  xframe->SetMinimum(0.01) ;
  gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.4) ; xframe->Draw() ;


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