Root Histograms

Root Histograms
Last Time and Todayy
„
Last time we started to talk about ROOT
„
Today we will start to use it, beginning with
Hisograms
‰
‰
‰
What are histograms
What types of histogram are there.
Booking, Filling and Plotting Histograms.
What is a histogram?
g
„
A histogram is a way of collecting data and
presenting it.
„
Data would be in the form of a collection of
data points.
‰
T i ll ffrom events
Typically
t iin a d
detector
t t
„
„
„
energy
position
iti
number of particles etc
What is a histogram?
g
„
Given th
Gi
these d
data
t a hi
histogram
t
shows
h
th
the
number of events that fall into certain
parameter
t ranges.
‰
‰
Each of these ranges are called bins
Typically these will be uniform
„
„
Not always
Histograms can then be used for further
analysis
l i
‰
To understand the underlying parent distribution.
Our p
primaryy example
p
„
Z0 decays into electron in Atlas.
‰
Primary
y electrons.
„
„
‰
Direct decay of the Z0
Invariant mass should be the Z0 mass.
Secondary electrons.
„
Indirect electrons from decays of other particles
‰
‰
‰
Heavy quark decay (b, c and s)
Tau decay
Mis-identified events
„
Events that aren’t actually electrons.
An example
p 1D histogram
g
hnelec
Events
ts
Number of Electrons
18000
Entries
50000
Mean
RMS
2.375
1.617
16000
14000
12000
10000
8000
6000
4000
2000
0
0
2
4
6
8
10
12
14
Number of Electrons
How to make this.
„
„
First you need to declare your histogram object.
The exact object depends on what type of variable you
will be filling the histogram with.
‰
„
„
Lets assume double precision numbers.
Use a TH1F object.
TH1F *hist = new TH1F(“hist”,”title”,nbins,xlow,xhigh);
‰
‰
‰
‰
title is the title of your histogram : charaters
nbins is an integer with the number of bins
xlow is a double precision number with the low edge of the
histogram
hi
xhigh is also double precision and has the high edge of the
histogram.
histogram
Fillingg your
y
histogram.
g
„
„
You need to loop through your tree to get the
number of electrons in each event.
Use the method Fill for TH1F (and all other
histogram types)
types).
for (Int_t indx=0;indx<inTree->GetEntries();indx++){
inTree->GetEntry(indx);
hist->Fill(ElecNum);
}
Drawingg your
y
histogram.
g
„
You can now d
Y
draw your hi
histogram
t
using
i th
the
Draw method.
„
hist->Draw();
();
„
There are a number of arguments to Draw
that can be used to improve the look of the
histogram that we can consider using
using.
‰
hist->Draw(“e1”);
Some basic options
p
„
E0 4 (I like E1)
E0-4
‰
„
SAME
‰
‰
„
Bar Chart
P
‰
„
Draw the histogram on the canvas without replacing what is
already there.
Use to plot one histogram on top of another
another.
B
‰
„
Draw error bars on the points.
Draw a marker at each point.
L, LF2
‰
Draw a line and fill an area.
Settingg your
y
titles
„
„
All histograms should have titles on the axis, and
probably a title as well.
To set the title on the axis you need to get the axis
object from the histogram and set the title.
‰
‰
‰
„
You can do this in one line
hist->GetXaxis()->SetTitle(“x axis title”);
hi t G tY i () S tTitl (“ axis
hist->GetYaxis()->SetTitle(“y
i title”);
titl ”)
The histogram title is set in the constructor
‰
‰
You can resett it with
Y
ith th
the S
SetTitle
tTitl method
th d
hist->SetTitle(“histogram title”);
Usingg a logg scale.
Often you will want
to use a log scale
‰
Particularly on the y
axis.
Events
„
hnelec
Number of Electrons
Entries
50000
Mean
RMS
2.375
1.617
104
3
10
„
This is a function of
the pad or canvas.
canvas
‰
GPad->SetLogy(1);
102
10
0
2
4
6
8
10
12
14
Number of Electrons
Gettingg data out of a histogram
g
„
A number of methods
exist to extract data
f
from
a histogram:
hi t
‰
‰
‰
‰
hist->GetEntries();
hist >GetMean()
hist->GetMean();
hist->GetRMS();
hist >GetIntegral();
hist->GetIntegral();
„
You can also look at the
stats box
‰
‰
On by default.
Turn off with:
„
‰
hist >SetStats(kFALSE);
hist->SetStats(kFALSE);
Control contents with:
„
„
„
gStyle >SetOptStat(num);
gStyle->SetOptStat(num);
num = 0001111 (say)
title, entries, mean, rms,
‰
See the manual.
Normalizingg a histogram.
g
„
Often you will want to normalize a histogram.
‰
‰
Useful when plotting
g one histogram
g
on top of
another.
Can be useful when turning
g a Monte Carlo data
set into a probability distribution.
Double t scale = norm/hist
Double_t
norm/hist->Integral();
>Integral();
hist->Scale(scale);
Two dimensional histograms.
g
Electron Energies
„
You can also make two
dimensional
hi t
histograms.
Here I plot the energy
off the
th two
t
electrons
l t
against each other,
making a cut such that
only two electrons were
seen
seen.
100
Ele
lectron 2 Ene
nergy (GeV))
„
90
80
70
60
50
40
30
20
10
0
0
10
20
30
40
50 60 70 80 90 100
Electron 1 Energy (GeV)
How to make this.
„
We need a 2D histogram object TH2F.
‰
„
Fill works as you’d
you d expect
‰
„
h2d->Fill(x,y);
Draw also
also, but you have more options
options.
‰
‰
„
TH2F *h2d = new TH2F(“h2d”,”title”,nxbins,xlow,
(
xhigh,nybins,ylow,yhigh)
h2d->Draw();
Contour, lego and surface plots are available.
Titles,, and data as before.
Contour plots
p
Electron Energies
„
One common way to
present 2D data is with a
contour plot.
Use the cont option in the
Draw method
‰
„
„
h2d >Draw(“cont”);
h2d->Draw(
cont );
Use contz to get the colour
key.
y
Try and avoid too many
bins as statistical
fl t ti
fluctuations
can play
l havoc
h
with contour plots.
100
Electron 2 Energy (Ge
E
GeV)
„
140
90
80
120
70
100
60
80
50
40
60
30
40
20
20
10
0
0
10
20
30
40
50 60 70 80 90 100
Electron 1 Energy (GeV)
0
Cleaningg up
p the data.
hmass
Entries
18314
Mean
RMS
71.73
32.14
2500
2000
1500
Electron Energies with Mass Cut
100
Electron 2 En
E
Energy (GeV))
Events
Electron Invarient Mass
90
80
70
60
50
40
1000
30
20
500
10
0
0
20
40
60
80 100 120 140 160 180 200
Electron Invarient Mass (GeV)
0
0
10
Cut on the invariant mass of the two electrons.
60GeV<M<120GeV
20
30
40
50 60 70 80 90 100
Electron 1 Energy (GeV)
Profile histograms.
g
„
„
A profile histogram is
another way of
presenting the data.
In each x bin the
data is collected.
The mean and error
in the mean of the y
variable is
calculated.
‰
This is the contents of
the bin.
Ele
lectron 2 Ene
nergy (GeV)
V)
„
Electron Energies with Mass Cut
100
80
60
40
20
0
0
20
40
60
80
100
120
Electron 1 Energy (GeV)
Creatingg a profile
p
histogram.
g
„
Use a TProfile object
‰
‰
‰
„
Fill like a regular 2d histogram
‰
„
TProfile *hprof = new TProfile(“hprof”,”title”,nxbins,
(
xlow,xhigh,ylow,yhigh)
Note that nybins
y
is g
gone from TH2D
ylow and yhigh are now cuts on y
hprof->Fill(x,y);
D
Draw
lik
like a 1D hi
histogram
‰
Errors are on by default.
Error bars in a p
profile histogram.
g
„
There are options
Th
ti
in
i the
th TProfile
TP fil constructor
t t to
t
control the error bars.
‰
„
The default sets the errors to the erorr in the mean
‰
„
RMS/√n
The spread option (option = “s”) give the spread
directly.
‰
„
TProfile *hprof
hprof = new TProfile(
TProfile(“hprof”
hprof ,”title”
title ,nxbins,
nxbins
xlow,xhigh,ylow,yhigh,option)
RMS
Which
Whi
h one you use will
ill d
depend
d on what
h t you are
trying to show.
Exercises
„
Take
T
k a look
l k att the
th macros used
d tto generate
t
the plots in this talk.
‰
„
The base was produced with the MakeClass
method of TTree which will be covered later, as
will some of the other actions
actions.
Make your own histograms
histograms, to show the
same distributions as I have for electrons.
‰
Does th
D
the iinvariant
i t mass vary ffrom particle
ti l tto
particle?