Calculate the average histogram of various histograms

I believe that this is an error in calculating the 2D histogram. You should loop over bin from 1 to and including hists->GetNbinsX(). This should suppress the underflows. I corrected the averaging below.

TH2F *Average(std::vector<TH1F*> hists) {
   TH2F *hAvg = new TH2F("hAvg","Avg",3000,0,3000,300,0,300);
   for (size_t i=0;i < hists.size(); ++i) {
      for (int bin = 1; bin <= hists[i]->GetNbinsX(); ++bin) {
         int resultBin = hAvg->FindBin(bin, hists[i]->GetBinContent(bin));
         int binContent = hAvg->GetBinContent(resultBin) + 1;
         hAvg->SetBinContent(resultBin,binContent);
      }
   }
   return hAvg;
}

Thank you very much for your help!

I used the new code, but it seems that the 2D histogram is not properly created. The reason is that there are y~0 values that mess up the mean value(red line)

In addition the blue line(most frequent value) doesn’t seem to have changed at all

Again I think that there is no problem with the code;there are just saturated pulses!