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