@@ -359,6 +359,7 @@ void HFInvMassFitter::doFit()
359359 mTotalPdf ->plotOn (mInvMassFrame , Name (" Tot_c" ), LineColor (kBlue ));
360360 mSgnPdf ->plotOn (mInvMassFrame , Normalization (1.0 , RooAbsReal::RelativeExpected), DrawOption (" F" ), FillColor (TColor::GetColorTransparent (kBlue , 0.2 )), VLines ());
361361 mChiSquareOverNdfTotal = mInvMassFrame ->chiSquare (" Tot_c" , " data_c" ); // calculate reduced chi2 / DNF
362+
362363 // plot residual distribution
363364 mResidualFrame = mass->frame (Title (" Residual Distribution" ));
364365 RooHist* residualHistogram = mInvMassFrame ->residHist (" data_c" , " Bkg_c" );
@@ -375,6 +376,7 @@ void HFInvMassFitter::doFit()
375376 calculateSignal (mRawYield , mRawYieldErr );
376377 countSignal (mRawYieldCounted , mRawYieldCountedErr );
377378 calculateSignificance (mSignificance , mSignificanceErr );
379+ calculateFitToDataRatio ();
378380 }
379381}
380382
@@ -640,6 +642,37 @@ void HFInvMassFitter::drawResidual(TVirtualPad* pad)
640642 highlightPeakRegion (mResidualFrame );
641643}
642644
645+ // draw ratio on canvas
646+ void HFInvMassFitter::drawRatio (TVirtualPad* pad)
647+ {
648+ pad->cd ();
649+ mRatioFrame ->GetYaxis ()->SetTitle (" " );
650+ TPaveText* textInfo = new TPaveText (0.12 , 0.65 , 0.47 , .89 , " NDC" );
651+ textInfo->SetBorderSize (0 );
652+ textInfo->SetFillStyle (0 );
653+ textInfo->SetTextColor (kBlue );
654+ textInfo->AddText (Form (" S = %.0f #pm %.0f " , mRawYield , mRawYieldErr ));
655+ textInfo->AddText (Form (" S_{count} = %.0f #pm %.0f " , mRawYieldCounted , mRawYieldCountedErr ));
656+ textInfo->AddText (Form (" mean = %.3f #pm %.3f" , mRooMeanSgn ->getVal (), mRooMeanSgn ->getError ()));
657+ if (mTypeOfSgnPdf == DoubleGaus) {
658+ auto const & baseSigmaSgn = mWorkspace ->var (" sigma" );
659+ textInfo->AddText (Form (" sigma = %.3f #pm %.3f" , baseSigmaSgn->getVal (), baseSigmaSgn->getError ()));
660+ textInfo->AddText (Form (" sigma 2 = %.3f #pm %.3f" , mRooSigmaSgn ->getVal (), mRooSigmaSgn ->getError ()));
661+ } else {
662+ textInfo->AddText (Form (" sigma = %.3f #pm %.3f" , mRooSigmaSgn ->getVal (), mRooSigmaSgn ->getError ()));
663+ }
664+ mRatioFrame ->addObject (textInfo);
665+ double xMin = mRatioFrame ->GetXaxis ()->GetXmin ();
666+ double xMax = mRatioFrame ->GetXaxis ()->GetXmax ();
667+ TLine* line = new TLine (xMin, 1.0 , xMax, 1.0 );
668+ line->SetLineColor (kGray );
669+ line->SetLineStyle (2 );
670+ line->SetLineWidth (2 );
671+ mRatioFrame ->addObject (line);
672+ mRatioFrame ->Draw ();
673+ highlightPeakRegion (mRatioFrame );
674+ }
675+
643676// draw peak region with vertical lines
644677void HFInvMassFitter::highlightPeakRegion (const RooPlot* plot, Color_t color, Width_t width, Style_t style) const
645678{
@@ -820,6 +853,42 @@ void HFInvMassFitter::plotRefl(RooAbsPdf* pdf)
820853 pdf->plotOn (mInvMassFrame , Components (namesOfReflPdf.at (mTypeOfReflPdf ).c_str ()), Name (" Refl_c" ), LineColor (kGreen ));
821854}
822855
856+ // Calculate fit to data ratio
857+ void HFInvMassFitter::calculateFitToDataRatio ()
858+ {
859+ if (!mInvMassFrame )
860+ return ;
861+ // Create a new RooPlot for the ratio
862+ mRatioFrame = mWorkspace ->var (" mass" )->frame (Title (" Fit/Data Ratio" ));
863+
864+ // Get the data and fit curves from the frame
865+ auto * dataHist = dynamic_cast <RooHist*>(mInvMassFrame ->findObject (" data_c" ));
866+ auto * fitCurve = dynamic_cast <RooCurve*>(mInvMassFrame ->findObject (" Tot_c" )); // or the relevant fit curve
867+
868+ if (!dataHist || !fitCurve)
869+ return ;
870+
871+ RooHist* ratioHist = new RooHist ();
872+
873+ for (int i = 0 ; i < dataHist->GetN (); ++i) {
874+ double x, dataY, dataErr;
875+ dataHist->GetPoint (i, x, dataY);
876+ dataErr = dataHist->GetErrorY (i);
877+
878+ double fitY = fitCurve->Eval (x);
879+
880+ double ratio = dataY != 0 ? fitY / dataY : 0 ;
881+ double err = dataY != 0 ? ratio * dataErr / dataY : 0 ;
882+
883+ ratioHist->SetPoint (i, x, ratio);
884+ ratioHist->SetPointError (i, 0 , 0 , err, err);
885+ }
886+
887+ mRatioFrame ->SetMinimum (0.0 );
888+ mRatioFrame ->SetMaximum (2.0 );
889+ mRatioFrame ->addPlotable (ratioHist, " P" );
890+ }
891+
823892// Fix reflection pdf
824893void HFInvMassFitter::setReflFuncFixed ()
825894{
0 commit comments