Home | News | Documentation | Download

Filling a TH2D by global bin numbers

Dear ROOTers,

Consider a TH2D:
TH2D *trial_pads = new TH2D("Pad_Occupancy", "Pad_Occupancy", 3, 1, 3, 15, 1, 15);
which I want to fill according to its global bin number.
For example, this obviously works:
trial_pads->Fill(2, 1);
whereas this:
trial_pads->Fill(5);
doesn’t. However, it is possible to extract a global bin number from the TH2D, i.e.

for (int j=0; j<trial_pads->GetNbinsX(); j++)
{
	for (int k=0; k<trial_pads->GetNbinsY(); k++){
		int global_bin_number = trial_pads->GetBin(j,k);
		std::cout << global_bin_number << std::endl;
	}
}

and then do
trial_pads->SetBinContent(global_bin_number, 400000);

My question therefore, if the latter is possible i.e. it is allowed to set the bin content of a global bin in a TH2D, why isn’t it possible to fill that global bin? I suppose such query has been raised before. A solution to this would spare me to prepare a 2-to-1 lookup table mapping between global bin numbers to their Xbins-Ybins coordinates.

Many thanks in advance for any advice!

Roy

ROOT Version: 6.18/04
Platform: Linux
Compiler: g++

Hi Roy,

In your example

you indeed fill the bin number global_bin_number, while in your previous example,

you don’t fill bin number 5 – you fill the bin with the axis number corresponding to x=5. Given your bin widths are equal to 1, you don’t see any difference.

All in all, Fill() operates on axis numbers, while SetBinContent() operates on bin numbers.

1 Like