TH1F SetBinContent

The question is very stupid but I am not able to set the contents for my desired bin

TH1F* meanSteps(TH2F* h2, int ch_mi, int ch_ma,int step_mi, int step_ma, int step) {
  int nch = ch_ma-ch_mi;
  float nsteps = (step_ma-step_mi)/step;

  TH1F* h1=new TH1F("h1","Mean Counts at Threshold", nch, ch_mi-0.5, ch_ma+0.5);
  for(int ichan=ch_mi; ichan<ch_ma+1; ichan++) {
    int sumCount=0;
    float meanCount=0;
    for(int istep=step_mi; istep<step_ma; istep= istep+step) {
      sumCount=h2->GetBinContent(ichan, istep);
      meanCount = (float) sumCount/nsteps;
    h1->SetBinContent(ichan,meanCount); // this part fails.
  return h1;

Can anyone tell me why SetBinContent(…) fails? My histogram has zero entries
Many thanks in advance


h1->SetBinContent( (ichan - ch_mi + 1), meanCount );
But of course, “sumCount=h2->GetBinContent(ichan, istep);” is also wrong.
Maybe something like this would do:
sumCount = h2->GetBinContent( h2->FindBin(ichan, istep) );
h1->SetBinContent( h1->FindBin(ichan), meanCount );
And in the for loop, instead of “ichan<ch_ma+1”, you should have “ichan<ch_ma”.

Actually, it seems to me that you wanted to have “int nch = ch_ma - ch_mi + 1;” (but that’s just my private opinion).

Thanks, now it works