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_code
8/// \macro_output
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;
81 std::unique_ptr<RooAbsData> d1{d.reduce({x, c})};
82 d1->Print("v");
83
84 cout << endl << ">> d2 has only column y" << endl;
85 std::unique_ptr<RooAbsData> d2{d.reduce({y})};
86 d2->Print("v");
87
88 cout << endl << ">> d3 has only the points with y>5.17" << endl;
89 std::unique_ptr<RooAbsData> d3{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 std::unique_ptr<RooAbsData> d4{d.reduce({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 static_cast<RooDataSet&>(*d1).merge(&static_cast<RooDataSet&>(*d2));
99 d1->Print("v");
100
101 // The append() function adds two datasets row-wise
102 cout << endl << ">> append data points of d3 to d1" << endl;
103 static_cast<RooDataSet&>(*d1).append(static_cast<RooDataSet&>(*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(false) << endl;
129 cout << ">> integral over histogram: " << dh.sum(true) << 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 std::unique_ptr<RooAbsData> dh2{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
@ kRed
Definition Rtypes.h:66
#define gPad
Storage_t const & get() const
Const access to the underlying stl container.
void Print(Option_t *options=nullptr) const override
This method must be overridden when a class wants to print itself.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Object to represent discrete states.
Definition RooCategory.h:28
Container class to hold N-dimensional binned data.
Definition RooDataHist.h:39
Container class to hold unbinned data.
Definition RooDataSet.h:57
bool merge(RooDataSet *data1, RooDataSet *data2=nullptr, RooDataSet *data3=nullptr, RooDataSet *data4=nullptr, RooDataSet *data5=nullptr, RooDataSet *data6=nullptr)
void append(RooDataSet &data)
Add all data points of given data set to this data set.
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:43
static RooPlot * frame(const RooAbsRealLValue &var, double xmin, double xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:237
void Print(Option_t *options=nullptr) const override
This method must be overridden when a class wants to print itself.
Definition RooPlot.h:134
TAxis * GetYaxis() const
Definition RooPlot.cxx:1276
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:649
Variable that can be changed from the outside.
Definition RooRealVar.h:37
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
The Canvas class.
Definition TCanvas.h:23
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
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
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 JSONIO.h:26
const char * Title
Definition TXMLSetup.cxx:68