# What does GetNcells exactly return?

Hi!

Try this:

``````TCanvas * asde () {
TCanvas * canvas = new TCanvas("d", "c", 600, 400);
TH2D * hist = new TH2D("a", "b", 4, 0, 10, 2, -3, 3);

cout << hist->GetNcells() << endl;

for (size_t i = 1; i <= hist->GetNcells(); i++)
hist->SetBinContent(i, i);

hist->Draw("COLZ");
return canvas;
}
``````

Why do I get “24” as result of “GetNcells()” when my TH2D is made of 2x4 = 8 bins.

“Bins” is not equivalent to “cells”? So what “cells” mean? And what’s the right method “Get…” to call in order to get 8?

In your case GetNcells returns “(1 + GetNbinsX + 1) * (1 + GetNbinsY + 1)” (where these “1s” represent the underflow and the overflow bins).

1 Like

So it counts the underflow and overflow bins too…

How do I set the bin contents correctly then?

I tried this but I do not get what I want:

``````TCanvas * asde () {
TCanvas * canvas = new TCanvas("d", "c", 600, 400);
TH2D * hist = new TH2D("a", "b", 4, 0, 10, 2, -3, 3);

cout << hist->GetNbinsX() * hist->GetNbinsY() << endl;

for (size_t i = 1; i <= hist->GetNbinsX() * hist->GetNbinsY(); i++)
hist->SetBinContent(i, i);

hist->Draw("COLZ");
return canvas;
}
``````
``````for (Int_t i = 1; i <= hist->GetNbinsX(); i++)
for (Int_t j = 1; j <= hist->GetNbinsY(); j++)
hist->SetBinContent(i, j, some_double_value);
``````

where you could also use:

``````hist->SetBinContent(hist->GetBin(i, j), some_double_value);
``````

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.