Hello!
I’m trying to get data from TH2F. And at first I wanted to get number of bins but I get an error with GetNbins().
#include "TFile.h"
#include "THashList.h"
#include "TObject.h"
#include "TObjArray.h"
#include "TClonesArray.h"
#include "TH1.h"
#include "TH2.h"
#include "TH3.h"
#include "TCanvas.h"
#include "TMath.h"
const char *name;
TObject *obj = histESD->At(13);
if (!obj) continue;
if(!std::strcmp(obj->ClassName(), "TH2F"))
{
name = obj->GetName();
Int_t num = obj->GetNbins();
}
Error: Can’t call TObject::GetNbins() in current scope
What I’m doing wrong? Thanks.
Try:
Int_t num_x = ((TH2F*)obj)->GetNbinsX();
Int_t num_y = ((TH2F*)obj)->GetNbinsY();
[quote=“Pepe Le Pew”]Try:
Int_t num_x = ((TH2F*)obj)->GetNbinsX();
Int_t num_y = ((TH2F*)obj)->GetNbinsY();[/quote]
Thanks very much. It work’s.
But for example in case of TH1I histogram I’m trying to get values and my code looks like this
Int_t num_x = ((TH1I*)obj)->GetNbinsX(); //num_x == 2000
Int_t num_y = ((TH1I*)obj)->GetNbinsY(); //num_y == 1
Int_t num_z = ((TH1I*)obj)->GetNbinsZ(); //num_z == 1
Float_t *bins = new Float_t[num_x];
for (Int_t i = 0; i < num_x; i++)
{
bins[i] = ((TH1I*)obj)->GetBinContent(i);
}
But bins contains only ones and zeros. What should I do for getting of values?
bins[i] = ((TH1I*)obj)->GetBinContent(i + 1); // TH1I : histogram with one int per channel
Thank you very much for your time and effort. It’s also works.
One more question about TH2F histograms.
For getting values along X-axis/Y-axis I wrote:
Int_t num_x = ((TH2F*)obj)->GetNbinsX(); // == 400
Int_t num_y = ((TH2F*)obj)->GetNbinsY(); // == 40
Int_t num_z = ((TH2F*)obj)->GetNbinsZ(); // == 1
// For X-axis
Float_t *bins_x = new float[num_x];
for (Int_t i = 0; i < num_x; i++)
{
bins_x[i] = ((TH2F*)obj)->GetBinContent(i + 1);
}
GetBinContent returns only zeros.
GetBinCenter, GetBinLowEdge, GetBinLowEdge return Nan.
Float_t bins_xy = new Float_t[(num_x * num_y)];
for (Int_t j = 0; j < num_y; j++) {
for (Int_t i = 0; i < num_x; i++) {
bins_xy[(i + j * num_y)] = ((TH2F)obj)->GetBinContent(i + 1, j + 1);
}
}