TH2D* hist = new TH2D("hist", "2D Histogram", 100, 0, 10, 50, -5, 5); int nbinsX = hist->GetNbinsX(); std::cout << "Number of X bins: " << nbinsX << std::endl;
for (int i = 1; i <= hist->GetNbinsX(); i++) { double binContent = hist->GetBinContent(i, 1); std::cout << "Bin " << i << " content: " << binContent << std::endl; }This code loops over all bins along the X-axis of the histogram and uses the method GetBinContent to retrieve the bin content. The bin content is then printed to the console. The package library for TH2D GetNbinsX is ROOT.