ROOT
Version v6.34
master
v6.32
v6.30
v6.28
v6.26
v6.24
v6.22
v6.20
v6.18
v6.16
v6.14
v6.12
v6.10
v6.08
v6.06
Reference Guide
►
ROOT
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Modules
Pages
Loading...
Searching...
No Matches
rf102_dataimport.py
Go to the documentation of this file.
1
## \file
2
## \ingroup tutorial_roofit
3
## \notebook
4
## 'BASIC FUNCTIONALITY' RooFit tutorial macro #102
5
## Importing data from ROOT TTrees and THx histograms
6
##
7
## \macro_image
8
## \macro_code
9
## \macro_output
10
##
11
## \date February 2018
12
## \authors Clemens Lange, Wouter Verkerke (C version)
13
14
import
ROOT
15
from
array
import
array
16
17
18
def
makeTH1
():
19
20
# Create ROOT ROOT.TH1 filled with a Gaussian distribution
21
22
hh =
ROOT.TH1D
(
"hh"
,
"hh"
, 25, -10, 10)
23
for
i
in
range
(100):
24
hh.Fill
(
ROOT.gRandom.Gaus
(0, 3))
25
return
hh
26
27
28
def
makeTTree
():
29
# Create ROOT ROOT.TTree filled with a Gaussian distribution in x and a
30
# uniform distribution in y
31
32
tree =
ROOT.TTree
(
"tree"
,
"tree"
)
33
px = array(
"d"
, [0])
34
py = array(
"d"
, [0])
35
tree.Branch
(
"x"
, px,
"x/D"
)
36
tree.Branch
(
"y"
, py,
"y/D"
)
37
for
i
in
range
(100):
38
px[0] =
ROOT.gRandom.Gaus
(0, 3)
39
py[0] =
ROOT.gRandom.Uniform
() * 30 - 15
40
tree.Fill
()
41
return
tree
42
43
44
############################
45
# Importing ROOT histograms
46
############################
47
# Import ROOT TH1 into a RooDataHist
48
# ---------------------------------------------------------
49
# Create a ROOT TH1 histogram
50
hh =
makeTH1
()
51
52
# Declare observable x
53
x =
ROOT.RooRealVar
(
"x"
,
"x"
, -10, 10)
54
55
# Create a binned dataset that imports contents of ROOT.TH1 and associates
56
# its contents to observable 'x'
57
dh =
ROOT.RooDataHist
(
"dh"
,
"dh"
, [x], Import=hh)
58
59
# Plot and fit a RooDataHist
60
# ---------------------------------------------------
61
# Make plot of binned dataset showing Poisson error bars (RooFit default)
62
frame =
x.frame
(Title=
"Imported ROOT.TH1 with Poisson error bars"
)
63
dh.plotOn
(frame)
64
65
# Fit a Gaussian p.d.f to the data
66
mean =
ROOT.RooRealVar
(
"mean"
,
"mean"
, 0, -10, 10)
67
sigma =
ROOT.RooRealVar
(
"sigma"
,
"sigma"
, 3, 0.1, 10)
68
gauss =
ROOT.RooGaussian
(
"gauss"
,
"gauss"
, x, mean, sigma)
69
gauss.fitTo
(dh, PrintLevel=-1)
70
gauss.plotOn
(frame)
71
72
# Plot and fit a RooDataHist with internal errors
73
# ---------------------------------------------------------------------------------------------
74
75
# If histogram has custom error (i.e. its contents is does not originate from a Poisson process
76
# but e.g. is a sum of weighted events) you can data with symmetric 'sum-of-weights' error instead
77
# (same error bars as shown by ROOT)
78
frame2 =
x.frame
(Title=
"Imported ROOT.TH1 with internal errors"
)
79
dh.plotOn
(frame2, DataError=
"SumW2"
)
80
gauss.plotOn
(frame2)
81
82
# Please note that error bars shown (Poisson or SumW2) are for visualization only, the are NOT used
83
# in a maximum likelihood fit
84
#
85
# A (binned) ML fit will ALWAYS assume the Poisson error interpretation of data (the mathematical definition
86
# of likelihood does not take any external definition of errors). Data with non-unit weights can only be correctly
87
# fitted with a chi^2 fit (see rf602_chi2fit.py)
88
#
89
# Importing ROOT TTrees
90
# -----------------------------------------------------------
91
# Import ROOT TTree into a RooDataSet
92
93
tree =
makeTTree
()
94
95
# Define 2nd observable y
96
y =
ROOT.RooRealVar
(
"y"
,
"y"
, -10, 10)
97
98
# Construct unbinned dataset importing tree branches x and y matching between branches and ROOT.RooRealVars
99
# is done by name of the branch/RRV
100
#
101
# Note that ONLY entries for which x,y have values within their allowed ranges as defined in
102
# ROOT.RooRealVar x and y are imported. Since the y values in the import tree are in the range [-15,15]
103
# and RRV y defines a range [-10,10] this means that the ROOT.RooDataSet
104
# below will have less entries than the ROOT.TTree 'tree'
105
106
ds =
ROOT.RooDataSet
(
"ds"
,
"ds"
, {x, y}, Import=tree)
107
108
# Use ascii import/export for datasets
109
# ------------------------------------------------------------------------------------
110
111
112
def
write_dataset
(ds, filename):
113
# Write data to output stream
114
outstream =
ROOT.std.ofstream
(filename)
115
# Optionally, adjust the stream here (e.g. std::setprecision)
116
ds.write
(outstream)
117
outstream.close
()
118
119
120
write_dataset
(ds,
"rf102_testData.txt"
)
121
122
# Read data from input stream. The variables of the dataset need to be supplied
123
# to the RooDataSet::read() function.
124
print(
"\n-----------------------\nReading data from ASCII"
)
125
dataReadBack =
ROOT.RooDataSet.read
(
126
"rf102_testData.txt"
,
127
[x, y],
# variables to be read. If the file has more fields, these are ignored.
128
"D"
,
# Prints if a RooFit message stream listens for debug messages. Use Q for quiet.
129
)
130
131
dataReadBack.Print
(
"V"
)
132
133
print(
"\nOriginal data, line 20:"
)
134
ds.get
(20).
Print
(
"V"
)
135
136
print(
"\nRead-back data, line 20:"
)
137
dataReadBack.get
(20).
Print
(
"V"
)
138
139
140
# Plot data set with multiple binning choices
141
# ------------------------------------------------------------------------------------
142
# Print number of events in dataset
143
ds.Print
()
144
145
# Print unbinned dataset with default frame binning (100 bins)
146
frame3 =
y.frame
(Title=
"Unbinned data shown in default frame binning"
)
147
ds.plotOn
(frame3)
148
149
# Print unbinned dataset with custom binning choice (20 bins)
150
frame4 =
y.frame
(Title=
"Unbinned data shown with custom binning"
)
151
ds.plotOn
(frame4, Binning=20)
152
153
frame5 =
y.frame
(Title=
"Unbinned data read back from ASCII file"
)
154
ds.plotOn
(frame5, Binning=20)
155
dataReadBack.plotOn
(frame5, Binning=20, MarkerColor=
"r"
, MarkerStyle=5)
156
157
# Draw all frames on a canvas
158
c =
ROOT.TCanvas
(
"rf102_dataimport"
,
"rf102_dataimport"
, 800, 800)
159
c.Divide
(3, 2)
160
c.cd
(1)
161
ROOT.gPad.SetLeftMargin
(0.15)
162
frame.GetYaxis
().SetTitleOffset(1.4)
163
frame.Draw
()
164
c.cd
(2)
165
ROOT.gPad.SetLeftMargin
(0.15)
166
frame2.GetYaxis
().SetTitleOffset(1.4)
167
frame2.Draw
()
168
c.cd
(4)
169
ROOT.gPad.SetLeftMargin
(0.15)
170
frame3.GetYaxis
().SetTitleOffset(1.4)
171
frame3.Draw
()
172
c.cd
(5)
173
ROOT.gPad.SetLeftMargin
(0.15)
174
frame4.GetYaxis
().SetTitleOffset(1.4)
175
frame4.Draw
()
176
c.cd
(6)
177
ROOT.gPad.SetLeftMargin
(0.15)
178
frame4.GetYaxis
().SetTitleOffset(1.4)
179
frame5.Draw
()
180
181
c.SaveAs
(
"rf102_dataimport.png"
)
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
Print
void Print(GNN_Data &d, std::string txt="")
Definition
TMVA_SOFIE_GNN_Application.C:59
ROOT::Detail::TRangeCast
Definition
TCollection.h:311
tutorials
roofit
rf102_dataimport.py
ROOT tags/6-34-04 - Reference Guide Generated on Fri Mar 21 2025 04:40:18 (GVA Time) using Doxygen 1.10.0