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