Logo ROOT   6.08/07
Reference Guide
rf402_datahandling.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// 'DATA AND CATEGORIES' RooFit tutorial macro #402
5 ///
6 /// Tools for manipulation of (un)binned datasets
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 /// \author 07/2008 - Wouter Verkerke
12 
13 
14 #include "RooRealVar.h"
15 #include "RooDataSet.h"
16 #include "RooDataHist.h"
17 #include "RooGaussian.h"
18 #include "RooConstVar.h"
19 #include "RooCategory.h"
20 #include "TCanvas.h"
21 #include "TAxis.h"
22 #include "RooPlot.h"
23 #include "TFile.h"
24 using namespace RooFit ;
25 
26 // WVE Add reduction by range
27 
28 
29 void rf402_datahandling()
30 {
31 
32  // Binned (RooDataHist) and unbinned datasets (RooDataSet) share
33  // many properties and inherit from a common abstract base class
34  // (RooAbsData), that provides an interface for all operations
35  // that can be performed regardless of the data format
36 
37  RooRealVar x("x","x",-10,10) ;
38  RooRealVar y("y","y", 0, 40) ;
39  RooCategory c("c","c") ;
40  c.defineType("Plus",+1) ;
41  c.defineType("Minus",-1) ;
42 
43 
44 
45  // 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
46  // --------------------------------------------------------------
47 
48  // RooDataSet is an unbinned dataset (a collection of points in N-dimensional space)
49  RooDataSet d("d","d",RooArgSet(x,y,c)) ;
50 
51  // Unlike RooAbsArgs (RooAbsPdf,RooFormulaVar,....) datasets are not attached to
52  // the variables they are constructed from. Instead they are attached to an internal
53  // clone of the supplied set of arguments
54 
55  // Fill d with dummy values
56  Int_t i ;
57  for (i=0 ; i<1000 ; i++) {
58  x = i/50 - 10 ;
59  y = sqrt(1.0*i) ;
60  c.setLabel((i%2)?"Plus":"Minus") ;
61 
62  // We must explicitly refer to x,y,c here to pass the values because
63  // d is not linked to them (as explained above)
64  d.add(RooArgSet(x,y,c)) ;
65  }
66  d.Print("v") ;
67  cout << endl ;
68 
69  // The get() function returns a pointer to the internal copy of the RooArgSet(x,y,c)
70  // supplied in the constructor
71  const RooArgSet* row = d.get() ;
72  row->Print("v") ;
73  cout << endl ;
74 
75  // Get with an argument loads a specific data point in row and returns
76  // a pointer to row argset. get() always returns the same pointer, unless
77  // an invalid row number is specified. In that case a null ptr is returned
78  d.get(900)->Print("v") ;
79  cout << endl ;
80 
81 
82 
83  // 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
84  // -------------------------------------------------------------
85 
86  // The reduce() function returns a new dataset which is a subset of the original
87  cout << endl << ">> d1 has only columns x,c" << endl ;
88  RooDataSet* d1 = (RooDataSet*) d.reduce(RooArgSet(x,c)) ;
89  d1->Print("v") ;
90 
91  cout << endl << ">> d2 has only column y" << endl ;
92  RooDataSet* d2 = (RooDataSet*) d.reduce(RooArgSet(y)) ;
93  d2->Print("v") ;
94 
95  cout << endl << ">> d3 has only the points with y>5.17" << endl ;
96  RooDataSet* d3 = (RooDataSet*) d.reduce("y>5.17") ;
97  d3->Print("v") ;
98 
99  cout << endl << ">> d4 has only columns x,c for data points with y>5.17" << endl ;
100  RooDataSet* d4 = (RooDataSet*) d.reduce(RooArgSet(x,c),"y>5.17") ;
101  d4->Print("v") ;
102 
103  // The merge() function adds two data set column-wise
104  cout << endl << ">> merge d2(y) with d1(x,c) to form d1(x,c,y)" << endl ;
105  d1->merge(d2) ;
106  d1->Print("v") ;
107 
108  // The append() function addes two datasets row-wise
109  cout << endl << ">> append data points of d3 to d1" << endl ;
110  d1->append(*d3) ;
111  d1->Print("v") ;
112 
113 
114 
115  // 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
116  // ---------------------------------------------------------
117 
118  // A binned dataset can be constructed empty, from an unbinned dataset, or
119  // from a ROOT native histogram (TH1,2,3)
120 
121  cout << ">> construct dh (binned) from d(unbinned) but only take the x and y dimensions," << endl
122  << ">> the category 'c' will be projected in the filling process" << endl ;
123 
124  // The binning of real variables (like x,y) is done using their fit range
125  // 'get/setRange()' and number of specified fit bins 'get/setBins()'.
126  // Category dimensions of binned datasets get one bin per defined category state
127  x.setBins(10) ;
128  y.setBins(10) ;
129  RooDataHist dh("dh","binned version of d",RooArgSet(x,y),d) ;
130  dh.Print("v") ;
131 
132  RooPlot* yframe = y.frame(Bins(10),Title("Operations on binned datasets")) ;
133  dh.plotOn(yframe) ; // plot projection of 2D binned data on y
134 
135  // Examine the statistics of a binned dataset
136  cout << ">> number of bins in dh : " << dh.numEntries() << endl ;
137  cout << ">> sum of weights in dh : " << dh.sum(kFALSE) << endl ;
138  cout << ">> integral over histogram: " << dh.sum(kTRUE) << endl ; // accounts for bin volume
139 
140  // Locate a bin from a set of coordinates and retrieve its properties
141  x = 0.3 ; y = 20.5 ;
142  cout << ">> retrieving the properties of the bin enclosing coordinate (x,y) = (0.3,20.5) " << endl ;
143  cout << " bin center:" << endl ;
144  dh.get(RooArgSet(x,y))->Print("v") ; // load bin center coordinates in internal buffer
145  cout << " weight = " << dh.weight() << endl ; // return weight of last loaded coordinates
146 
147  // Reduce the 2-dimensional binned dataset to a 1-dimensional binned dataset
148  //
149  // All reduce() methods are interfaced in RooAbsData. All reduction techniques
150  // demonstrated on unbinned datasets can be applied to binned datasets as well.
151  cout << ">> Creating 1-dimensional projection on y of dh for bins with x>0" << endl ;
152  RooDataHist* dh2 = (RooDataHist*) dh.reduce(y,"x>0") ;
153  dh2->Print("v") ;
154 
155  // Add dh2 to yframe and redraw
156  dh2->plotOn(yframe,LineColor(kRed),MarkerColor(kRed)) ;
157 
158 
159 
160  // S a v i n g a n d l o a d i n g f r o m f i l e
161  // -------------------------------------------------------
162 
163  // Datasets can be persisted with ROOT I/O
164  cout << endl << ">> Persisting d via ROOT I/O" << endl ;
165  TFile f("rf402_datahandling.root","RECREATE") ;
166  d.Write() ;
167  f.ls() ;
168 
169  // To read back in future session:
170  // > TFile f("rf402_datahandling.root") ;
171  // > RooDataSet* d = (RooDataSet*) f.FindObject("d") ;
172 
173 
174 
175  new TCanvas("rf402_datahandling","rf402_datahandling",600,600) ;
176  gPad->SetLeftMargin(0.15) ; yframe->GetYaxis()->SetTitleOffset(1.4) ; yframe->Draw() ;
177 
178 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:262
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1118
RooCmdArg LineColor(Color_t color)
return c
Definition: Rtypes.h:61
int Int_t
Definition: RtypesCore.h:41
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
const Bool_t kFALSE
Definition: Rtypes.h:92
RooCmdArg Title(const char *name)
double sqrt(double)
RooDataSet is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:40
Double_t x[n]
Definition: legend1.C:17
virtual void Print(Option_t *options=0) const
This method must be overridden when a class wants to print itself.
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
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
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
The Canvas class.
Definition: TCanvas.h:41
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsData.h:157
double f(double x)
void append(RooDataSet &data)
Add all data points of given data set to this data set.
Double_t y[n]
Definition: legend1.C:17
Bool_t merge(RooDataSet *data1, RooDataSet *data2=0, RooDataSet *data3=0, RooDataSet *data4=0, RooDataSet *data5=0, RooDataSet *data6=0)
RooCmdArg Bins(Int_t nbin)
#define gPad
Definition: TVirtualPad.h:289
RooCmdArg MarkerColor(Color_t color)
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual RooPlot * plotOn(RooPlot *frame, PlotOpt o) const
Back end function to plotting functionality.
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:559