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