Logo ROOT   6.07/09
Reference Guide
rf402_datahandling.cxx
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////
2 //
3 // 'DATA AND CATEGORIES' RooFit tutorial macro #402
4 //
5 // Tools for manipulation of (un)binned datasets
6 //
7 //
8 //
9 // 07/2008 - Wouter Verkerke
10 //
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 "RooCategory.h"
21 #include "TCanvas.h"
22 #include "RooPlot.h"
23 #include "TFile.h"
24 using namespace RooFit ;
25 
26 
27 class TestBasic402 : public RooFitTestUnit
28 {
29 public:
30  TestBasic402(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Basic operations on datasets",refFile,writeRef,verbose) {} ;
31  Bool_t testCode() {
32 
33  // Binned (RooDataHist) and unbinned datasets (RooDataSet) share
34  // many properties and inherit from a common abstract base class
35  // (RooAbsData), that provides an interface for all operations
36  // that can be performed regardless of the data format
37 
38  RooRealVar x("x","x",-10,10) ;
39  RooRealVar y("y","y", 0, 40) ;
40  RooCategory c("c","c") ;
41  c.defineType("Plus",+1) ;
42  c.defineType("Minus",-1) ;
43 
44 
45 
46  // 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
47  // --------------------------------------------------------------
48 
49  // RooDataSet is an unbinned dataset (a collection of points in N-dimensional space)
50  RooDataSet d("d","d",RooArgSet(x,y,c)) ;
51 
52  // Unlike RooAbsArgs (RooAbsPdf,RooFormulaVar,....) datasets are not attached to
53  // the variables they are constructed from. Instead they are attached to an internal
54  // clone of the supplied set of arguments
55 
56  // Fill d with dummy values
57  Int_t i ;
58  for (i=0 ; i<1000 ; i++) {
59  x = i/50 - 10 ;
60  y = sqrt(1.0*i) ;
61  c.setLabel((i%2)?"Plus":"Minus") ;
62 
63  // We must explicitly refer to x,y,c here to pass the values because
64  // d is not linked to them (as explained above)
65  d.add(RooArgSet(x,y,c)) ;
66  }
67 
68 
69  // 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
70  // -------------------------------------------------------------
71 
72  // The reduce() function returns a new dataset which is a subset of the original
73  RooDataSet* d1 = (RooDataSet*) d.reduce(RooArgSet(x,c)) ;
74  RooDataSet* d2 = (RooDataSet*) d.reduce(RooArgSet(y)) ;
75  RooDataSet* d3 = (RooDataSet*) d.reduce("y>5.17") ;
76  RooDataSet* d4 = (RooDataSet*) d.reduce(RooArgSet(x,c),"y>5.17") ;
77 
78  regValue(d3->numEntries(),"rf403_nd3") ;
79  regValue(d4->numEntries(),"rf403_nd4") ;
80 
81  // The merge() function adds two data set column-wise
82  d1->merge(d2) ;
83 
84  // The append() function addes two datasets row-wise
85  d1->append(*d3) ;
86 
87  regValue(d1->numEntries(),"rf403_nd1") ;
88 
89 
90 
91 
92  // 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
93  // ---------------------------------------------------------
94 
95  // A binned dataset can be constructed empty, from an unbinned dataset, or
96  // from a ROOT native histogram (TH1,2,3)
97 
98  // The binning of real variables (like x,y) is done using their fit range
99  // 'get/setRange()' and number of specified fit bins 'get/setBins()'.
100  // Category dimensions of binned datasets get one bin per defined category state
101  x.setBins(10) ;
102  y.setBins(10) ;
103  RooDataHist dh("dh","binned version of d",RooArgSet(x,y),d) ;
104 
105  RooPlot* yframe = y.frame(Bins(10),Title("Operations on binned datasets")) ;
106  dh.plotOn(yframe) ; // plot projection of 2D binned data on y
107 
108  // Reduce the 2-dimensional binned dataset to a 1-dimensional binned dataset
109  //
110  // All reduce() methods are interfaced in RooAbsData. All reduction techniques
111  // demonstrated on unbinned datasets can be applied to binned datasets as well.
112  RooDataHist* dh2 = (RooDataHist*) dh.reduce(y,"x>0") ;
113 
114  // Add dh2 to yframe and redraw
115  dh2->plotOn(yframe,LineColor(kRed),MarkerColor(kRed),Name("dh2")) ;
116 
117  regPlot(yframe,"rf402_plot1") ;
118 
119  delete d1 ;
120  delete d2 ;
121  delete d3 ;
122  delete d4 ;
123  delete dh2 ;
124  return kTRUE ;
125  }
126 } ;
RooCmdArg LineColor(Color_t color)
return c
Definition: Rtypes.h:61
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
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:343
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
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
RooCmdArg Name(const char *name)
virtual Int_t numEntries() const
Definition: RooAbsData.cxx:269
bool verbose
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)
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.