@@ -142,6 +142,7 @@ HFInvMassFitter::HFInvMassFitter(TH1* histoToFit,
142142 mReflFrame(nullptr ),
143143 mReflOnlyFrame(nullptr ),
144144 mResidualFrame(nullptr ),
145+ mResidualHist(nullptr ),
145146 mRatioFrame(nullptr ),
146147 mResidualFrameForCalculation(nullptr ),
147148 mWorkspace(nullptr ),
@@ -315,9 +316,9 @@ void HFInvMassFitter::doFit()
315316 mChiSquareOverNdfTotal = mInvMassFrame ->chiSquare (" Tot_c" , " data_c" ); // calculate reduced chi2 / NDF
316317
317318 // plot residual distribution
318- RooHist* residualHistogram = mInvMassFrame ->residHist (" data_c" , " ReflBkg_c" );
319+ mResidualHist = mInvMassFrame ->residHist (" data_c" , " ReflBkg_c" );
319320 mResidualFrame = mass->frame (Title (" Residual Distribution" ));
320- mResidualFrame ->addPlotable (residualHistogram , " p" );
321+ mResidualFrame ->addPlotable (mResidualHist , " p" );
321322 mSgnPdf ->plotOn (mResidualFrame , Normalization (1.0 , RooAbsReal::RelativeExpected), LineColor (kBlue ));
322323 } else {
323324 mTotalPdf = new RooAddPdf (" mTotalPdf" , " background + signal pdf" , RooArgList (*bkgPdf, *sgnPdf), RooArgList (*mRooNBkg , *mRooNSgn ));
@@ -333,8 +334,8 @@ void HFInvMassFitter::doFit()
333334
334335 // plot residual distribution
335336 mResidualFrame = mass->frame (Title (" Residual Distribution" ));
336- RooHist* residualHistogram = mInvMassFrame ->residHist (" data_c" , " Bkg_c" );
337- mResidualFrame ->addPlotable (residualHistogram , " P" );
337+ mResidualHist = mInvMassFrame ->residHist (" data_c" , " Bkg_c" );
338+ mResidualFrame ->addPlotable (mResidualHist , " P" );
338339 mSgnPdf ->plotOn (mResidualFrame , Normalization (1.0 , RooAbsReal::RelativeExpected), LineColor (kBlue ));
339340 }
340341 mass->setRange (" bkgForSignificance" , mRooMeanSgn ->getVal () - mNSigmaForSgn * mRooSecSigmaSgn ->getVal (), mRooMeanSgn ->getVal () + mNSigmaForSgn * mRooSecSigmaSgn ->getVal ());
@@ -704,26 +705,30 @@ void HFInvMassFitter::drawReflection(TVirtualPad* pad)
704705// calculate signal yield via bin counting
705706void HFInvMassFitter::countSignal (double & signal, double & signalErr) const
706707{
708+ const double binWidth = mResidualHist ->GetX ()[1 ] - mResidualHist ->GetX ()[0 ];
709+ const double firstBinLowEdge = mResidualHist ->GetX ()[0 ] - binWidth / 2 ;
707710 const auto [minForSgn, maxForSgn] = getRangesOfSignal ();
708- const int binForMinSgn = mHistoInvMass ->FindBin (minForSgn);
709- const int binForMaxSgn = mHistoInvMass ->FindBin (maxForSgn);
710- const double binForMinSgnUpperEdge = mHistoInvMass ->GetBinLowEdge (binForMinSgn + 1 );
711- const double binForMaxSgnLowerEdge = mHistoInvMass ->GetBinLowEdge (binForMaxSgn);
712- const double binForMinSgnFraction = (binForMinSgnUpperEdge - minForSgn) / mHistoInvMass ->GetBinWidth (binForMinSgn);
713- const double binForMaxSgnFraction = (maxForSgn - binForMaxSgnLowerEdge) / mHistoInvMass ->GetBinWidth (binForMaxSgn);
714-
715- double sum = 0 ;
716- sum += mHistoInvMass ->GetBinContent (binForMinSgn) * binForMinSgnFraction;
711+ const int binForMinSgn = static_cast <int >((minForSgn - firstBinLowEdge) / binWidth) + 1 ;
712+ const int binForMaxSgn = static_cast <int >((maxForSgn - firstBinLowEdge) / binWidth) + 1 ;
713+ const double binForMinSgnUpperEdge = firstBinLowEdge + (binForMinSgn - 1 ) * binWidth + binWidth;
714+ const double binForMaxSgnLowerEdge = firstBinLowEdge + (binForMaxSgn - 1 ) * binWidth;
715+ const double binForMinSgnFraction = (binForMinSgnUpperEdge - minForSgn) / binWidth;
716+ const double binForMaxSgnFraction = (maxForSgn - binForMaxSgnLowerEdge) / binWidth;
717+
718+ auto square = [](double value) { return value * value; };
719+
720+ double sumValues{}, sumErrorsSquare{};
721+ sumValues += mResidualHist ->GetY ()[binForMinSgn - 1 ] * binForMinSgnFraction;
722+ sumErrorsSquare += square (mResidualHist ->GetErrorY (binForMinSgn - 1 ) * binForMinSgnFraction);
717723 for (int iBin = binForMinSgn + 1 ; iBin <= binForMaxSgn - 1 ; iBin++) {
718- sum += mHistoInvMass ->GetBinContent (iBin);
724+ sumValues += mResidualHist ->GetY ()[iBin - 1 ];
725+ sumErrorsSquare += square (mResidualHist ->GetErrorY (iBin - 1 ));
719726 }
720- sum += mHistoInvMass ->GetBinContent (binForMaxSgn) * binForMaxSgnFraction;
721-
722- double bkg{}, errBkg{};
723- calculateBackground (bkg, errBkg);
727+ sumValues += mResidualHist ->GetY ()[binForMaxSgn - 1 ] * binForMaxSgnFraction;
728+ sumErrorsSquare += square (mResidualHist ->GetErrorY (binForMaxSgn - 1 ) * binForMaxSgnFraction);
724729
725- signal = sum - bkg ;
726- signalErr = std::sqrt (sum + errBkg * errBkg); // sum error squared is equal to sum
730+ signal = sumValues ;
731+ signalErr = std::sqrt (sumErrorsSquare);
727732}
728733
729734// calculate signal yield
0 commit comments