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