Get bin array


I would like to get the bin array from the ~.root data file.

I try…

#include "TH1.h"
#include "TAxis.h"

TH1F *hA = (TH1F *)fFileA->Get("fileA");
TAxis *xaxis = hA->GetXbins();
TH1F* hResult = new TH1F("hResult", "hResult", hA->GetNbinsX(), xaxis);

But it does not work. GetLowEdge(hA) does not work.

The bin of fileA is:

Float_t xAxis[41] = {-5.0, -4.0, -3.5, -3.0, -2.5, -2.0, -1.8, -1.6, -1.4, -1.2, -1.0,
						 -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1,  0.0,  0.1,
						  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1.0,  1.2,  1.4,
						  1.6,  1.8,  2.0,  2.5,  3.0,  3.5,  4.0,  5.0};

So, the bin width is different.
Would you tell me how to get fileA bin array?

Thank you!

TAxis::GetXbins returns a TArrayD*. Siomply chnage

TH1F* hResult = new TH1F("hResult", "hResult", hA->GetNbinsX(), xaxis); to

TH1F* hResult = new TH1F("hResult", "hResult", hA->GetNbinsX(), xaxis->GetArray());


Thank you for your answer, and I try,

hA->GetXbins() = 0
Error: illegal pointer to class object xAxis 0x0 470

Error message!
TAxis *xaxis = hA->TH1F::GetXbins();
does not point anything even though hA is
TH1F pointer.
TH1F *hA = (TH1F *)fFileA->Get(“fileA”);

In your previous reply, that means
but hA->GetXbins() does not work, why???


hA->GetXbins() = 0 What are you trying to do? This sounds like a C++ syntax error, isn’t it?

Rather than

TH1F *hA = (TH1F *)fFileA->Get("fileA"); I recommend:TH1F *hA; fFileA->GetObject("fileA",hA); if (hA==0) { cout << "Could not retrieve the histogram named 'fileA'. Either it is not on file or it is not a TH1F\n"; return; }which will properly check that ‘fileA’ exist on the file and is a TH1F (by the way is your histogram really named ‘fileA’?)

To get the axis object use TAxis *axis = hA->GetXaxis();



I wrote…

cout << "hA = " << hA << endl;
cout << "hA->GetXaxis() = " << hA->GetXaxis() << endl;
TArrayD* xaxis = hA->TH1F::GetXbins();
cout << "hA->TH1F::GetXbins() = " << xaxis << endl;

TH1F* hResult = new TH1F(“hResult”, “hResult”, hA->GetNbinsX(), xaxis->GetArray());

but, the message is…

hA = 0x8976410
hA->GetXaxis() = 0x8976450
hA->TH1F::GetXbins() = 0
Error: illegal pointer to class object xaxis 0x0 191
*** Interpreter error recovered ***

So, hA is ok, hA->GetXaxis() is also ok, but hA->GetXbins() is wrong!
( hA->GetXaxis() )->GetArray() does not work!
How do I fix it?




TArrayD* xaxis = hA->GetXaxis()->GetXbins();
It works, thank you so much!
By the way, TArrayD* is ok, but ArrayD* is not. (I think it is typo.)

Anyway, it takes lots of time only for “xbin axis”. It is really inconvnient.
I cannot find the syntax even though I am reading the User’s Guide, Tutorials,
and HOWTO’S. It is really pain.


1 Like


Did you try the Reference Guide? This is where you would find the complete list of functions.


PS. Yes ArrayD was a typo (missing the T)


I’m having trouble getting the bins from TAxis as described above; the TArrayD is always empty. See below for CINT output:

root [10] TH1F* h = new TH1F("name", "name", 100, 0, 100);
root [11] h->GetNbinsX()
(const Int_t)100
root [12] h->GetXaxis()->GetNbins()
(const Int_t)100
root [13] h->GetXaxis()->GetXbins()->GetSize()
(const Int_t)0

What is it that I am missing?


That’s a histogram with “fix bins” (it’s axis is defined by Xmin and Xmax only, no Xbins array).

In such a case could it be useful if the TAxis::GetXbins() method return a pointer to the corresponding fixed size bins array rather than 0?


In a such case there is no bin array stored so returning 0 is right. It would sound a bit weird to fill a fake array just to avoid returning 0. If you need a such functionality you can always make you own GetXbins function like:

Double *MyGetXBins(TAxis *ax);

which will return TAxis::GetXbins() if not 0 and, if 0, it will return a fake array.