A stupid and silly question: GetBinContent is not reading properly from h2 the contents of coordinates (i, step+1), am I using the function correctly?

TH1F* getYProfile(TH2F* h2, int step) {
  TH1F* h1=new TH1F("h1", "YProfile",h2->GetNbinsX(),-0.5,h2->GetNbinsX()-0.5);
  int nchan=h2->GetNbinsX(); 
  for (int i=1; i<nchan+1; i++) 
    h1->SetBinContent(i, h2->GetBinContent(i,step+1));

  return h1;


The way you specify the upper limit of your histogram looks a bin weird seems to me.
h2->GetNbinsX() will give you an integer corresponding to the number of bin along the
X axis o the histogram h2… is that really what you want ? is it not the upper edge of the last bin ?

I assume he wants to have “h2->GetNbinsX()” bins with bin centres at 0, 1, 2, …, (h2->GetNbinsX() - 1).

BTW. I think it would be more convenient if one used: TH1F *h1 = new TH1F("h1", "YProfile", h2->GetNbinsX(), 0.5, h2->GetNbinsX() + 0.5); Sooner or later one will try to draw it using the “Log” scale for “X”, so having all “X” axis values positive is a really good choice (actually, I have just noticed that if the scale begins at -0.5 then the “Logx” draws the histogram improperly -> “bin borders” in the canvas do not coincide with “x_bin_number -/+ 0.5”).