Logo ROOT   6.10/09
Reference Guide
rf102_dataimport.cxx
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////
2 //
3 // 'BASIC FUNCTIONALITY' RooFit tutorial macro #102
4 //
5 // Importing data from ROOT TTrees and THx histograms
6 //
7 //
8 //
9 // 07/2008 - Wouter Verkerke
10 //
11 /////////////////////////////////////////////////////////////////////////
12 
13 
14 #ifndef __CINT__
15 #include "RooGlobalFunc.h"
16 #endif
17 #include "RooRealVar.h"
18 #include "RooDataSet.h"
19 #include "RooGaussian.h"
20 #include "TCanvas.h"
21 #include "RooPlot.h"
22 #include "TTree.h"
23 #include "TH1D.h"
24 #include "TRandom.h"
25 using namespace RooFit ;
26 
27 
28 class TestBasic102 : public RooFitTestUnit
29 {
30 public:
31  TestBasic102(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Data import methods",refFile,writeRef,verbose) {} ;
32 
33  TH1* makeTH1()
34  {
35  // Create ROOT TH1 filled with a Gaussian distribution
36 
37  TH1D* hh = new TH1D("hh","hh",25,-10,10) ;
38  for (int i=0 ; i<100 ; i++) {
39  hh->Fill(gRandom->Gaus(0,3)) ;
40  }
41  return hh ;
42  }
43 
44 
45  TTree* makeTTree()
46  {
47  // Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y
48 
49  TTree* tree = new TTree("tree","tree") ;
50  Double_t* px = new Double_t ;
51  Double_t* py = new Double_t ;
52  tree->Branch("x",px,"x/D") ;
53  tree->Branch("y",py,"y/D") ;
54  for (int i=0 ; i<100 ; i++) {
55  *px = gRandom->Gaus(0,3) ;
56  *py = gRandom->Uniform()*30 - 15 ;
57  tree->Fill() ;
58  }
59 
60  //delete px ;
61  //delete py ;
62 
63  return tree ;
64  }
65 
66  Bool_t testCode() {
67 
68  ////////////////////////////////////////////////////////
69  // I m p o r t i n g R O O T h i s t o g r a m s //
70  ////////////////////////////////////////////////////////
71 
72  // I m p o r t T H 1 i n t o a R o o D a t a H i s t
73  // ---------------------------------------------------------
74 
75  // Create a ROOT TH1 histogram
76  TH1* hh = makeTH1() ;
77 
78  // Declare observable x
79  RooRealVar x("x","x",-10,10) ;
80 
81  // Create a binned dataset that imports contents of TH1 and associates its contents to observable 'x'
82  RooDataHist dh("dh","dh",x,Import(*hh)) ;
83 
84 
85  // P l o t a n d f i t a R o o D a t a H i s t
86  // ---------------------------------------------------
87 
88  // Make plot of binned dataset showing Poisson error bars (RooFit default)
89  RooPlot* frame = x.frame(Title("Imported TH1 with Poisson error bars")) ;
90  dh.plotOn(frame) ;
91 
92  // Fit a Gaussian p.d.f to the data
93  RooRealVar mean("mean","mean",0,-10,10) ;
94  RooRealVar sigma("sigma","sigma",3,0.1,10) ;
95  RooGaussian gauss("gauss","gauss",x,mean,sigma) ;
96  gauss.fitTo(dh) ;
97  gauss.plotOn(frame) ;
98 
99  // P l o t a n d f i t a R o o D a t a H i s t w i t h i n t e r n a l e r r o r s
100  // ---------------------------------------------------------------------------------------------
101 
102  // If histogram has custom error (i.e. its contents is does not originate from a Poisson process
103  // but e.g. is a sum of weighted events) you can data with symmetric 'sum-of-weights' error instead
104  // (same error bars as shown by ROOT)
105  RooPlot* frame2 = x.frame(Title("Imported TH1 with internal errors")) ;
106  dh.plotOn(frame2,DataError(RooAbsData::SumW2)) ;
107  gauss.plotOn(frame2) ;
108 
109  // Please note that error bars shown (Poisson or SumW2) are for visualization only, the are NOT used
110  // in a maximum likelihood fit
111  //
112  // A (binned) ML fit will ALWAYS assume the Poisson error interpretation of data (the mathematical definition
113  // of likelihood does not take any external definition of errors). Data with non-unit weights can only be correctly
114  // fitted with a chi^2 fit (see rf602_chi2fit.C)
115 
116 
117  ////////////////////////////////////////////////
118  // I m p o r t i n g R O O T T T r e e s //
119  ////////////////////////////////////////////////
120 
121 
122  // I m p o r t T T r e e i n t o a R o o D a t a S e t
123  // -----------------------------------------------------------
124 
125  TTree* tree = makeTTree() ;
126 
127  // Define 2nd observable y
128  RooRealVar y("y","y",-10,10) ;
129 
130  // Construct unbinned dataset importing tree branches x and y matching between branches and RooRealVars
131  // is done by name of the branch/RRV
132  //
133  // Note that ONLY entries for which x,y have values within their allowed ranges as defined in
134  // RooRealVar x and y are imported. Since the y values in the import tree are in the range [-15,15]
135  // and RRV y defines a range [-10,10] this means that the RooDataSet below will have less entries than the TTree 'tree'
136 
137  RooDataSet ds("ds","ds",RooArgSet(x,y),Import(*tree)) ;
138 
139 
140  // P l o t d a t a s e t w i t h m u l t i p l e b i n n i n g c h o i c e s
141  // ------------------------------------------------------------------------------------
142 
143  // Print unbinned dataset with default frame binning (100 bins)
144  RooPlot* frame3 = y.frame(Title("Unbinned data shown in default frame binning")) ;
145  ds.plotOn(frame3) ;
146 
147  // Print unbinned dataset with custom binning choice (20 bins)
148  RooPlot* frame4 = y.frame(Title("Unbinned data shown with custom binning")) ;
149  ds.plotOn(frame4,Binning(20)) ;
150 
151  // Draw all frames on a canvas
152  regPlot(frame ,"rf102_plot1") ;
153  regPlot(frame2,"rf102_plot2") ;
154  regPlot(frame3,"rf102_plot3") ;
155  regPlot(frame4,"rf102_plot4") ;
156 
157  delete hh ;
158  delete tree ;
159 
160  return kTRUE ;
161  }
162 } ;
163 
164 
165 
166 
167 
168 
169 
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3126
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:235
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
RooCmdArg Title(const char *name)
RooDataSet is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:40
Double_t x[n]
Definition: legend1.C:17
RooCmdArg DataError(Int_t)
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
const Double_t sigma
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
bool verbose
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:594
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
RooCmdArg Import(const char *state, TH1 &histo)
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
The TH1 histogram class.
Definition: TH1.h:56
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:310
Definition: tree.py:1
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooCmdArg Binning(const RooAbsBinning &binning)