Retrieving the XAxis Bin Centers from TH2D

I have a 2D Histogram (TH2D), its got X[1, 2, 3, 4], Y[1, 11, 22, 33] axis of different ranges. I try to do

((TAxis*)h2->GetXaxis())->GetBinCenter(binNumber)

Surprisingly, this didn’t work for XAxis, I wanted to see 1, 2, 3, 4, 5, but its blank, however I am able to see 1, 11, 22, 33 for Y Axis

Do you have a concrete example we can try?

Please find the files here :

Try with:

   double bcx = ((TAxis*)h2->GetXaxis())->GetBinCenter(ix);
   double bcy = ((TAxis*)h2->GetYaxis())->GetBinCenter(iy);

See the GetBinCenter() Documentation

Thanks a lot !
Apparently, got used to typing int and didn’t realize its of a different data type.

1 Like

Does TH2D data structure store the bin error for x, y separately, I didn’t see anything in the documentation, perhaps I missed it [GetBinErrorLow()]?

Well, I don’t know, I would have to dig in the code to be able to answer… Or maybe @moneta can give more details…

It would be great if I can retrieve the values, else I have create a ton of histograms just because I would like to have the error !

As the TH1::GetBinError() documentation says:

if the sum of squares of weights has been defined (via Sumw2),
this function returns the sqrt(sum of w2).
otherwise it returns the sqrt(contents) for this bin.

I am not sure, how I can get a h1 from h2 without Projection i.e.
h2->h1->GetBinError(xbin)
Projection projects everything. Is there a way to project only a section of x axis ?

Use Double_t TH2::GetBinError (Int_t binx, Int_t biny) See TH1::GetBinError(Int_t binx, Int_t biny)
Don’t forget that TH2 inherits from TH1

I actually tried this, it works, here’s an eg of what I am looking for :
x : [0, 1, 2] // bins
xval: [0, 3, 6] // bin values
xerr: [0, 0.001, 0.3] // bin errors

y : [0, 1, 2] // bins
yval: [0, 2, 4] // bin values
yerr: [0, 0.01, 0.2] // bin errors

when I populate TH2D, I guess I am doing (x, y), so whenever there’s an x, y, I fill it by something i.e.
(3, 5) > 1 // first time
(3, 5) > 1 // second time
and so on.

per se the data structure didn’t seem like taking both data, errors for both axes separately, all together and the error can be retrieved when there’s a coincidence i.e. error related to (3, 5) is what I get through the method you’ve given me. Right ?

Sorry, I’m not sure to fully understand what you describe… If the documentation and the examples are not clear enough, then I’ll ask @moneta to give more precise (and technical) details about TH2, errors and such…

I get only one bin error per (x, y) coordinates, right ?

Yes, and there is also GetBinErrorLow (Int_t binx, Int_t biny) and GetBinErrorUp (Int_t binx, Int_t biny)

I didn’t quite get this one, do I interpret it as
GetBinErrorLow : error along x axis
GetBinErrorUp: error along y axis ?

Well, the TH1 version of GetBinErrorLow says:

Return lower error associated to bin number bin.
The error will depend on the statistic option used will return the binContent - lower interval value

So it looks like this is the error on the bin content…

Maybe you should use TProfile2D? (just trying to guess…)

Thanks for pointing TProfile, I am looking through the documentation.
Could you let me know if it works with hadd ?

I think so (I don’t see any reason why it wouldn’t)