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