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