Logo ROOT   6.10/09
Reference Guide
rf102_dataimport.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// 'BASIC FUNCTIONALITY' RooFit tutorial macro #102
5 ///
6 /// Importing data from ROOT TTrees and THx histograms
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 /// \author 07/2008 - Wouter Verkerke
12 
13 
14 #include "RooRealVar.h"
15 #include "RooDataSet.h"
16 #include "RooDataHist.h"
17 #include "RooGaussian.h"
18 #include "TCanvas.h"
19 #include "RooPlot.h"
20 #include "TTree.h"
21 #include "TH1D.h"
22 #include "TRandom.h"
23 using namespace RooFit ;
24 
25 TH1* makeTH1() ;
26 TTree* makeTTree() ;
27 
28 
29 void rf102_dataimport()
30 {
31  // ---------------------------------------------------
32  // I m p o r t i n g R O O T h i s t o g r a m s
33  // ===================================================
34 
35  // 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
36  // ---------------------------------------------------------
37 
38  // Create a ROOT TH1 histogram
39  TH1* hh = makeTH1() ;
40 
41  // Declare observable x
42  RooRealVar x("x","x",-10,10) ;
43 
44  // Create a binned dataset that imports contents of TH1 and associates its contents to observable 'x'
45  RooDataHist dh("dh","dh",x,Import(*hh)) ;
46 
47 
48  // P l o t a n d f i t a R o o D a t a H i s t
49  // ---------------------------------------------------
50 
51  // Make plot of binned dataset showing Poisson error bars (RooFit default)
52  RooPlot* frame = x.frame(Title("Imported TH1 with Poisson error bars")) ;
53  dh.plotOn(frame) ;
54 
55  // Fit a Gaussian p.d.f to the data
56  RooRealVar mean("mean","mean",0,-10,10) ;
57  RooRealVar sigma("sigma","sigma",3,0.1,10) ;
58  RooGaussian gauss("gauss","gauss",x,mean,sigma) ;
59  gauss.fitTo(dh) ;
60  gauss.plotOn(frame) ;
61 
62  // 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
63  // ---------------------------------------------------------------------------------------------
64 
65  // If histogram has custom error (i.e. its contents is does not originate from a Poisson process
66  // but e.g. is a sum of weighted events) you can data with symmetric 'sum-of-weights' error instead
67  // (same error bars as shown by ROOT)
68  RooPlot* frame2 = x.frame(Title("Imported TH1 with internal errors")) ;
69  dh.plotOn(frame2,DataError(RooAbsData::SumW2)) ;
70  gauss.plotOn(frame2) ;
71 
72  // Please note that error bars shown (Poisson or SumW2) are for visualization only, the are NOT used
73  // in a maximum likelihood fit
74  //
75  // A (binned) ML fit will ALWAYS assume the Poisson error interpretation of data (the mathematical definition
76  // of likelihood does not take any external definition of errors). Data with non-unit weights can only be correctly
77  // fitted with a chi^2 fit (see rf602_chi2fit.C)
78 
79 
80  // -----------------------------------------
81  // I m p o r t i n g R O O T T T r e e s
82  // =========================================
83 
84 
85  // 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
86  // -----------------------------------------------------------
87 
88  TTree* tree = makeTTree() ;
89 
90  // Define 2nd observable y
91  RooRealVar y("y","y",-10,10) ;
92 
93  // Construct unbinned dataset importing tree branches x and y matching between branches and RooRealVars
94  // is done by name of the branch/RRV
95  //
96  // Note that ONLY entries for which x,y have values within their allowed ranges as defined in
97  // RooRealVar x and y are imported. Since the y values in the import tree are in the range [-15,15]
98  // and RRV y defines a range [-10,10] this means that the RooDataSet below will have less entries than the TTree 'tree'
99 
100  RooDataSet ds("ds","ds",RooArgSet(x,y),Import(*tree)) ;
101 
102 
103  // 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
104  // ------------------------------------------------------------------------------------
105 
106  // Print number of events in dataset
107  ds.Print() ;
108 
109  // Print unbinned dataset with default frame binning (100 bins)
110  RooPlot* frame3 = y.frame(Title("Unbinned data shown in default frame binning")) ;
111  ds.plotOn(frame3) ;
112 
113  // Print unbinned dataset with custom binning choice (20 bins)
114  RooPlot* frame4 = y.frame(Title("Unbinned data shown with custom binning")) ;
115  ds.plotOn(frame4,Binning(20)) ;
116 
117  // Draw all frames on a canvas
118  TCanvas* c = new TCanvas("rf102_dataimport","rf102_dataimport",800,800) ;
119  c->Divide(2,2) ;
120  c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.4) ; frame->Draw() ;
121  c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ;
122  c->cd(3) ; gPad->SetLeftMargin(0.15) ; frame3->GetYaxis()->SetTitleOffset(1.4) ; frame3->Draw() ;
123  c->cd(4) ; gPad->SetLeftMargin(0.15) ; frame4->GetYaxis()->SetTitleOffset(1.4) ; frame4->Draw() ;
124 
125 }
126 
127 
128 
129 
130 TH1* makeTH1()
131 {
132  // Create ROOT TH1 filled with a Gaussian distribution
133 
134  TH1D* hh = new TH1D("hh","hh",25,-10,10) ;
135  for (int i=0 ; i<100 ; i++) {
136  hh->Fill(gRandom->Gaus(0,3)) ;
137  }
138  return hh ;
139 }
140 
141 
142 TTree* makeTTree()
143 {
144  // Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y
145 
146  TTree* tree = new TTree("tree","tree") ;
147  Double_t* px = new Double_t ;
148  Double_t* py = new Double_t ;
149  tree->Branch("x",px,"x/D") ;
150  tree->Branch("y",py,"y/D") ;
151  for (int i=0 ; i<100 ; i++) {
152  *px = gRandom->Gaus(0,3) ;
153  *py = gRandom->Uniform()*30 - 15 ;
154  tree->Fill() ;
155  }
156  return tree ;
157 }
158 
159 
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:262
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3126
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1118
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
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
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
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)
The Canvas class.
Definition: TCanvas.h:31
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
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1135
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:310
#define gPad
Definition: TVirtualPad.h:284
Definition: tree.py:1
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:559
RooCmdArg Binning(const RooAbsBinning &binning)