Histograms - Introduction

Histograms come in different types and are - for various reasons - not templated. There are inheritance relations between the types which may not seem obvious at first glance and which change betwee ROOT 2.23 and ROOT 2.24.


How to define histograms?
ROOT provides you with 3-d histograms as well.
TH1D *h1 = new TH1D("h1", "histogram title", 100, 0., 10.);
TH2D *h2 = new TH2D("h2", "histogram title", 100, 0., 10., 18, 0., 180.);
TH3D *h3 = new TH3D("h3", histogram title", 100, 0., 10., 18, 0., 180., 10, 0., 1.);

What Options are there for drawing?
Many of these options are the same like in PAW:
h2->Draw("text");
h2->Draw("col"), h2->Draw("colz")
;
h2->Draw("box");
h2->Draw("surf");
A complete list is available in the official documentation.

How to overlay two histograms
h1a->Draw();
h1b->Draw("same");

How to copy a histogram
TH1D hcopy(*h1); // assuming h1 is a pointer to a TH1D

How to get Poissonian errors in an efficiency plot
heff->Divide(hgood, hall, 1., 1., "b"); // assuming you have three histograms called hall, hgood, and heff

How to get a profile histogram from a 2-d histogram
Tprofile *px = h2->ProfileX("px", 0, 9); // where firstYbin = 0 and lastYbin = 9
px->Draw();

Tprofile *py = h2->ProfileY("py", 0, 9);
// where firstXbin = 0 and lastXbin = 9
py->Draw();

How to get a projection from a 2-d histogram
TH1D *px = h2->ProjectionX("px", 0, 9); // where firstYbin = 0 and lastYbin = 9
px->Draw();

TH1D *py = h2->ProjectionY("py", 0, 9);
// where firstXbin = 0 and lastXbin = 9
px->Draw();

How to fit a 2-d histogram binwise with functions and get to the result
TF1 *f1 = new TF1("f1", "gaus");
h2->FitSlicesY("f1");
h2_0->Draw(); // draw the first fit parameter (constant, in this case)
h2_1->Draw(); // draw the second fit parameter (mean, in this case)
h2_2->Draw(); // draw the third fit parameter (sigma, in this case)

Errors in histograms
By default, errors are sqrt(entries).
To get the error as sqrt(sum of weights), you'll have to
h1->Sumw2()
before filling the histogram.

How to dump the contents of a histogram to an ASCII file
h1->Print("all"); > filename.txt
Note the semicolon after the parenthesis.