ROOT logo
//////////////////////////////////////////////////////////////////////////
//
// 'DATA AND CATEGORIES' RooFit tutorial macro #402
// 
// Tools for manipulation of (un)binned datasets
//
//
//
// 07/2008 - Wouter Verkerke 
// 
/////////////////////////////////////////////////////////////////////////

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

// WVE Add reduction by range


void rf402_datahandling()
{

  // Binned (RooDataHist) and unbinned datasets (RooDataSet) share
  // many properties and inherit from a common abstract base class
  // (RooAbsData), that provides an interface for all operations
  // that can be performed regardless of the data format

  RooRealVar  x("x","x",-10,10) ;
  RooRealVar  y("y","y", 0, 40) ;
  RooCategory c("c","c") ;
  c.defineType("Plus",+1) ;
  c.defineType("Minus",-1) ;



  // B a s i c   O p e r a t i o n s   o n   u n b i n n e d   d a t a s e t s 
  // --------------------------------------------------------------

  // RooDataSet is an unbinned dataset (a collection of points in N-dimensional space)
  RooDataSet d("d","d",RooArgSet(x,y,c)) ;

  // Unlike RooAbsArgs (RooAbsPdf,RooFormulaVar,....) datasets are not attached to 
  // the variables they are constructed from. Instead they are attached to an internal 
  // clone of the supplied set of arguments

  // Fill d with dummy values
  Int_t i ;
  for (i=0 ; i<1000 ; i++) {
    x = i/50 - 10 ;
    y = sqrt(1.0*i) ;
    c.setLabel((i%2)?"Plus":"Minus") ;

    // We must explicitly refer to x,y,c here to pass the values because
    // d is not linked to them (as explained above)
    d.add(RooArgSet(x,y,c)) ;
  }
  d.Print("v") ;
  cout << endl ;

  // The get() function returns a pointer to the internal copy of the RooArgSet(x,y,c)
  // supplied in the constructor
  const RooArgSet* row = d.get() ;
  row->Print("v") ;
  cout << endl ;

  // Get with an argument loads a specific data point in row and returns
  // a pointer to row argset. get() always returns the same pointer, unless
  // an invalid row number is specified. In that case a null ptr is returned
  d.get(900)->Print("v") ;
  cout << endl ;



  // R e d u c i n g ,   A p p e n d i n g   a n d   M e r g i n g
  // -------------------------------------------------------------

  // The reduce() function returns a new dataset which is a subset of the original
  cout << endl << ">> d1 has only columns x,c" << endl ;
  RooDataSet* d1 = (RooDataSet*) d.reduce(RooArgSet(x,c)) ; 
  d1->Print("v") ;

  cout << endl << ">> d2 has only column y" << endl ;
  RooDataSet* d2 = (RooDataSet*) d.reduce(RooArgSet(y)) ;   
  d2->Print("v") ;

  cout << endl << ">> d3 has only the points with y>5.17" << endl ;
  RooDataSet* d3 = (RooDataSet*) d.reduce("y>5.17") ; 
  d3->Print("v") ;

  cout << endl << ">> d4 has only columns x,c for data points with y>5.17" << endl ;
  RooDataSet* d4 = (RooDataSet*) d.reduce(RooArgSet(x,c),"y>5.17") ; 
  d4->Print("v") ;

  // The merge() function adds two data set column-wise
  cout << endl << ">> merge d2(y) with d1(x,c) to form d1(x,c,y)" << endl ;
  d1->merge(d2) ; 
  d1->Print("v") ;

  // The append() function addes two datasets row-wise
  cout << endl << ">> append data points of d3 to d1" << endl ;
  d1->append(*d3) ;
  d1->Print("v") ;  



  // O p e r a t i o n s   o n   b i n n e d   d a t a s e t s 
  // ---------------------------------------------------------

  // A binned dataset can be constructed empty, from an unbinned dataset, or
  // from a ROOT native histogram (TH1,2,3)

  cout << ">> construct dh (binned) from d(unbinned) but only take the x and y dimensions," << endl 
       << ">> the category 'c' will be projected in the filling process" << endl ;
    
  // The binning of real variables (like x,y) is done using their fit range
  // 'get/setRange()' and number of specified fit bins 'get/setBins()'.
  // Category dimensions of binned datasets get one bin per defined category state
  x.setBins(10) ;
  y.setBins(10) ;
  RooDataHist dh("dh","binned version of d",RooArgSet(x,y),d) ;
  dh.Print("v") ;

  RooPlot* yframe = y.frame(Bins(10),Title("Operations on binned datasets")) ;
  dh.plotOn(yframe) ; // plot projection of 2D binned data on y

  // Examine the statistics of a binned dataset
  cout << ">> number of bins in dh   : " << dh.numEntries() << endl ;
  cout << ">> sum of weights in dh   : " << dh.sum(kFALSE)  << endl ;
  cout << ">> integral over histogram: " << dh.sum(kTRUE)   << endl ; // accounts for bin volume

  // Locate a bin from a set of coordinates and retrieve its properties
  x = 0.3 ;  y = 20.5 ;
  cout << ">> retrieving the properties of the bin enclosing coordinate (x,y) = (0.3,20.5) " << endl ;
  cout << " bin center:" << endl ;
  dh.get(RooArgSet(x,y))->Print("v") ; // load bin center coordinates in internal buffer
  cout << " weight = " << dh.weight() << endl ; // return weight of last loaded coordinates

  // Reduce the 2-dimensional binned dataset to a 1-dimensional binned dataset
  //
  // All reduce() methods are interfaced in RooAbsData. All reduction techniques
  // demonstrated on unbinned datasets can be applied to binned datasets as well.
  cout << ">> Creating 1-dimensional projection on y of dh for bins with x>0" << endl ;
  RooDataHist* dh2 = (RooDataHist*) dh.reduce(y,"x>0") ;
  dh2->Print("v") ;

  // Add dh2 to yframe and redraw
  dh2->plotOn(yframe,LineColor(kRed),MarkerColor(kRed)) ;



  // S a v i n g   a n d   l o a d i n g   f r o m   f i l e 
  // -------------------------------------------------------

  // Datasets can be persisted with ROOT I/O
  cout << endl << ">> Persisting d via ROOT I/O" << endl ;
  TFile f("rf402_datahandling.root","RECREATE") ;
  d.Write() ;
  f.ls() ;

  // To read back in future session:
  // > TFile f("rf402_datahandling.root") ;
  // > RooDataSet* d = (RooDataSet*) f.FindObject("d") ;
  


  new TCanvas("rf402_datahandling","rf402_datahandling",600,600) ;
  gPad->SetLeftMargin(0.15) ; yframe->GetYaxis()->SetTitleOffset(1.4) ; yframe->Draw() ;

}
 rf402_datahandling.C:1
 rf402_datahandling.C:2
 rf402_datahandling.C:3
 rf402_datahandling.C:4
 rf402_datahandling.C:5
 rf402_datahandling.C:6
 rf402_datahandling.C:7
 rf402_datahandling.C:8
 rf402_datahandling.C:9
 rf402_datahandling.C:10
 rf402_datahandling.C:11
 rf402_datahandling.C:12
 rf402_datahandling.C:13
 rf402_datahandling.C:14
 rf402_datahandling.C:15
 rf402_datahandling.C:16
 rf402_datahandling.C:17
 rf402_datahandling.C:18
 rf402_datahandling.C:19
 rf402_datahandling.C:20
 rf402_datahandling.C:21
 rf402_datahandling.C:22
 rf402_datahandling.C:23
 rf402_datahandling.C:24
 rf402_datahandling.C:25
 rf402_datahandling.C:26
 rf402_datahandling.C:27
 rf402_datahandling.C:28
 rf402_datahandling.C:29
 rf402_datahandling.C:30
 rf402_datahandling.C:31
 rf402_datahandling.C:32
 rf402_datahandling.C:33
 rf402_datahandling.C:34
 rf402_datahandling.C:35
 rf402_datahandling.C:36
 rf402_datahandling.C:37
 rf402_datahandling.C:38
 rf402_datahandling.C:39
 rf402_datahandling.C:40
 rf402_datahandling.C:41
 rf402_datahandling.C:42
 rf402_datahandling.C:43
 rf402_datahandling.C:44
 rf402_datahandling.C:45
 rf402_datahandling.C:46
 rf402_datahandling.C:47
 rf402_datahandling.C:48
 rf402_datahandling.C:49
 rf402_datahandling.C:50
 rf402_datahandling.C:51
 rf402_datahandling.C:52
 rf402_datahandling.C:53
 rf402_datahandling.C:54
 rf402_datahandling.C:55
 rf402_datahandling.C:56
 rf402_datahandling.C:57
 rf402_datahandling.C:58
 rf402_datahandling.C:59
 rf402_datahandling.C:60
 rf402_datahandling.C:61
 rf402_datahandling.C:62
 rf402_datahandling.C:63
 rf402_datahandling.C:64
 rf402_datahandling.C:65
 rf402_datahandling.C:66
 rf402_datahandling.C:67
 rf402_datahandling.C:68
 rf402_datahandling.C:69
 rf402_datahandling.C:70
 rf402_datahandling.C:71
 rf402_datahandling.C:72
 rf402_datahandling.C:73
 rf402_datahandling.C:74
 rf402_datahandling.C:75
 rf402_datahandling.C:76
 rf402_datahandling.C:77
 rf402_datahandling.C:78
 rf402_datahandling.C:79
 rf402_datahandling.C:80
 rf402_datahandling.C:81
 rf402_datahandling.C:82
 rf402_datahandling.C:83
 rf402_datahandling.C:84
 rf402_datahandling.C:85
 rf402_datahandling.C:86
 rf402_datahandling.C:87
 rf402_datahandling.C:88
 rf402_datahandling.C:89
 rf402_datahandling.C:90
 rf402_datahandling.C:91
 rf402_datahandling.C:92
 rf402_datahandling.C:93
 rf402_datahandling.C:94
 rf402_datahandling.C:95
 rf402_datahandling.C:96
 rf402_datahandling.C:97
 rf402_datahandling.C:98
 rf402_datahandling.C:99
 rf402_datahandling.C:100
 rf402_datahandling.C:101
 rf402_datahandling.C:102
 rf402_datahandling.C:103
 rf402_datahandling.C:104
 rf402_datahandling.C:105
 rf402_datahandling.C:106
 rf402_datahandling.C:107
 rf402_datahandling.C:108
 rf402_datahandling.C:109
 rf402_datahandling.C:110
 rf402_datahandling.C:111
 rf402_datahandling.C:112
 rf402_datahandling.C:113
 rf402_datahandling.C:114
 rf402_datahandling.C:115
 rf402_datahandling.C:116
 rf402_datahandling.C:117
 rf402_datahandling.C:118
 rf402_datahandling.C:119
 rf402_datahandling.C:120
 rf402_datahandling.C:121
 rf402_datahandling.C:122
 rf402_datahandling.C:123
 rf402_datahandling.C:124
 rf402_datahandling.C:125
 rf402_datahandling.C:126
 rf402_datahandling.C:127
 rf402_datahandling.C:128
 rf402_datahandling.C:129
 rf402_datahandling.C:130
 rf402_datahandling.C:131
 rf402_datahandling.C:132
 rf402_datahandling.C:133
 rf402_datahandling.C:134
 rf402_datahandling.C:135
 rf402_datahandling.C:136
 rf402_datahandling.C:137
 rf402_datahandling.C:138
 rf402_datahandling.C:139
 rf402_datahandling.C:140
 rf402_datahandling.C:141
 rf402_datahandling.C:142
 rf402_datahandling.C:143
 rf402_datahandling.C:144
 rf402_datahandling.C:145
 rf402_datahandling.C:146
 rf402_datahandling.C:147
 rf402_datahandling.C:148
 rf402_datahandling.C:149
 rf402_datahandling.C:150
 rf402_datahandling.C:151
 rf402_datahandling.C:152
 rf402_datahandling.C:153
 rf402_datahandling.C:154
 rf402_datahandling.C:155
 rf402_datahandling.C:156
 rf402_datahandling.C:157
 rf402_datahandling.C:158
 rf402_datahandling.C:159
 rf402_datahandling.C:160
 rf402_datahandling.C:161
 rf402_datahandling.C:162
 rf402_datahandling.C:163
 rf402_datahandling.C:164
 rf402_datahandling.C:165
 rf402_datahandling.C:166
 rf402_datahandling.C:167
 rf402_datahandling.C:168
 rf402_datahandling.C:169
 rf402_datahandling.C:170
 rf402_datahandling.C:171
 rf402_datahandling.C:172
 rf402_datahandling.C:173
 rf402_datahandling.C:174
 rf402_datahandling.C:175
 rf402_datahandling.C:176
 rf402_datahandling.C:177
 rf402_datahandling.C:178
 rf402_datahandling.C:179
 rf402_datahandling.C:180
 rf402_datahandling.C:181