@@ -2058,14 +2058,25 @@ TH1D *TH3::DoProject1D(const char* name, const char * title, const TAxis* projX,
20582058 }
20592059 }
20602060 }
2061- Int_t ix = h1->FindBin ( projX->GetBinCenter (ixbin) );
2061+ Int_t ix = h1->FindBin (projX->GetBinCenter (ixbin));
2062+ if (ix == h1->GetNbinsX () + 1 || ix == 0 ) {
2063+ auto eps = 1e-12 ;
2064+ // if upper/lower edge is +/-infinite, the bin center is +/-infinite if the other edge is finite,
2065+ // so FindBin is in overflow/underflow
2066+ // Check close to the lower or upper edges instead of the bin center in these cases
2067+ if (std::isinf (projX->GetBinUpEdge (ixbin))) {
2068+ ix = h1->FindBin (projX->GetBinLowEdge (ixbin) + eps);
2069+ } else if (std::isinf (projX->GetBinLowEdge (ixbin))) {
2070+ ix = h1->FindBin (projX->GetBinUpEdge (ixbin) - eps);
2071+ }
2072+ }
20622073 h1->SetBinContent (ix ,cont);
20632074 if (computeErrors) h1->SetBinError (ix, TMath::Sqrt (err2) );
20642075 // sum all content
20652076 totcont += cont;
20662077
20672078 }
2068- if ( labels ) h1->GetXaxis ()->SetCanExtend (extendable);
2079+ if (labels) h1->GetXaxis ()->SetCanExtend (extendable);
20692080
20702081 // since we use a combination of fill and SetBinError we need to reset and recalculate the statistics
20712082 // for weighted histograms otherwise sumw2 will be wrong.
@@ -2267,11 +2278,32 @@ TH2D *TH3::DoProject2D(const char* name, const char * title, const TAxis* projX,
22672278
22682279 for (ixbin=0 ;ixbin<=1 +projX->GetNbins ();ixbin++) {
22692280 if ( projX->TestBit (TAxis::kAxisRange ) && ( ixbin < ixmin || ixbin > ixmax )) continue ;
2270- Int_t ix = h2->GetYaxis ()->FindBin ( projX->GetBinCenter (ixbin) );
2281+ Int_t ix = h2->GetYaxis ()->FindBin (projX->GetBinCenter (ixbin));
2282+ auto eps = 1e-12 ;
2283+ if (ix == h2->GetYaxis ()->GetNbins () + 1 || ix == 0 ) {
2284+ // if upper/lower edge is +/-infinite, the bin center is +/-infinite if the other edge is finite,
2285+ // so FindBin is in overflow/underflow
2286+ // Check close to the lower or upper edges instead of the bin center in these cases
2287+ if (std::isinf (projX->GetBinUpEdge (ixbin))) {
2288+ ix = h2->GetYaxis ()->FindBin (projX->GetBinLowEdge (ixbin) + eps);
2289+ } else if (std::isinf (projX->GetBinLowEdge (ixbin))) {
2290+ ix = h2->GetYaxis ()->FindBin (projX->GetBinUpEdge (ixbin) - eps);
2291+ }
2292+ }
22712293
22722294 for (iybin=0 ;iybin<=1 +projY->GetNbins ();iybin++) {
22732295 if ( projY->TestBit (TAxis::kAxisRange ) && ( iybin < iymin || iybin > iymax )) continue ;
22742296 Int_t iy = h2->GetXaxis ()->FindBin ( projY->GetBinCenter (iybin) );
2297+ if (iy == h2->GetXaxis ()->GetNbins () + 1 || iy == 0 ) {
2298+ // if upper/lower edge is +/-infinite, the bin center is +/-infinite if the other edge is finite,
2299+ // so FindBin is in overflow/underflow
2300+ // Check close to the lower or upper edges instead of the bin center in these cases
2301+ if (std::isinf (projY->GetBinUpEdge (iybin))) {
2302+ iy = h2->GetXaxis ()->FindBin (projY->GetBinLowEdge (iybin) + eps);
2303+ } else if (std::isinf (projY->GetBinLowEdge (iybin))) {
2304+ iy = h2->GetXaxis ()->FindBin (projY->GetBinUpEdge (iybin) - eps);
2305+ }
2306+ }
22752307
22762308 Double_t cont = 0 ;
22772309 Double_t err2 = 0 ;
0 commit comments