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