Named bins in histogram : how to retrieve bin content

Dear all,

I have a histogram with 2 “named” bins

TH1F h("adsf", "asdf", 2, 0, 1)
// Some kind of initialisation
h->Fill("PHYSICS", 0)
h->Fill("CALIBRATION", 0)
// (...)
// later on, fill depending on what you encounter
h->Fill("PHYSICS", 1)
// Retrieve the content of bin "PHYSICS"
h->GetBinContent(1)

First, I want to be sure that I have those two bins correctly labelled even if I fill only one of the two. The (bad) way I do it, is by filling both bins with a zero at startup.
What is the proper way of doing it ?
How can I have the plot show two bins, with proper names, even without actually filling them ?

Second, how to retrieve the bin content for, let’s say, “PHYSICS” ?
I can do

h->GetBinContent(1)

but I actually don’t know if it is 0 or 1. I would rather imagine something like :

h->GetBinContent("PHYSICS")

Unfortunately the latter doesn’t exist. What is the correct way of doing this ? How can I know that “PHYSICS” is the nth bin ?

Thanks in advance for your help,
Barth

Hi,

use h->GetBinContent(h->GetXaxis()->FindBin(“PHYSICS”))

Cheers, Axel.

Exactly what I was looking for !
Thank you !

What about the first question ?
How to make sure I always have 2 bins without filling with zero’s ?

Cheers,
Barth

[quote]How to make sure I always have 2 bins without filling with zero’s ? [/quote]You already did that with the 3rd argument of the constructor :slight_smile: You can indeed also pre-label the bins by using:h.GetXaxis()->SetBinLabel(1,"PHYSICS"); h.GetXaxis()->SetBinLabel(2,"CALIBRATION");Cheers,
Philippe