#ifndef __JCOMPAREHISTOGRAMS__JTESTPOISSONLOGLIKELIHOODRATIOBEESTONBARLOW__ #define __JCOMPAREHISTOGRAMS__JTESTPOISSONLOGLIKELIHOODRATIOBEESTONBARLOW__ #include #include #include "JLang/JException.hh" #include "JCompareHistograms/JResultTitle.hh" #include "JCompareHistograms/JTest_t.hh" #include "TH1.h" #include "TH2.h" #include "RooRealVar.h" #include "RooRealConstant.h" #include "RooParamHistFunc.h" #include "RooHistConstraint.h" #include "RooProdPdf.h" #include "RooDataSet.h" #include "RooDataHist.h" #include "RooHistFunc.h" #include "RooHistPdf.h" #include "RooRealSumPdf.h" #include "RooFitResult.h" /** * \author bjung */ namespace JCOMPAREHISTOGRAMS { /** * Implementation of the Poisson log-likelihood ratio test.\n * The first histogram is treated as an Asimov dataset corresponding to a given null hypothesis,\n * which is compared to an alternative hypothesis given by the second histogram.\n\n * * This class is derived from the abstract class JTest_t(). For a general description of the implementation of this and other tests derived from JTest_t(), see its documentation.\n */ class JTestPoissonLogLikelihoodRatioBeestonBarlow: public JTest_t { public: /** * Default constructor. */ JTestPoissonLogLikelihoodRatioBeestonBarlow() : JTest_t("Poissonian Log-Likelihood Ratio with Beeston-Barlow Correction", "NLLR") {} /** * Applies a Poissonian log-likelihood ratio test to the two given histograms.\n * The first histogram is treated as an Asimov dataset corresponding to a given null hypothesis.\n * The second histogram is treated as the expectation for the alternative hypothesis, to which the null hypothesis is compared. * * \param o1 First histogram * \param o2 Second histogram */ void test(const TObject* o1, const TObject* o2) override { using namespace std; using namespace JPP; static const double penalty = -1e9; //!< Penalty factor const TH1* h1 = dynamic_cast(o1); const TH1* h2 = dynamic_cast(o2); if (h1 == NULL || h2 == NULL) { THROW(JValueOutOfRange, "JTestPoissonLogLikelihood::test(): Could not cast given TObjects to TH1."); } if(h1->GetNbinsX() != h2->GetNbinsX() || h1->GetNbinsY() != h2->GetNbinsY() || h1->GetNbinsZ() != h2->GetNbinsZ()) { THROW(JValueOutOfRange, "JTestPoissonLogLikelihood::test(): Histograms with different binning. The objects: " << h1->GetName() << " and " << h2->GetName() << " can not be compared." << endl); } RooRealVar x("x", "x", h1->GetBinLowEdge(1), h1->GetBinLowEdge(h1->GetNbinsX()) + h1->GetBinWidth(h1->GetNbinsX())); RooDataHist dh1("dh1", h1->GetTitle(), x, h1); RooDataHist dh2("dh2", h2->GetTitle(), x, h2); #if ROOT_VERSION_CODE < ROOT_VERSION(6,32,0) RooParamHistFunc ph1("ph1", "ph1", dh1); #else RooParamHistFunc ph1("ph1", "ph1", dh1,x); #endif RooHistConstraint hc1("hc1", "hc1", ph1); RooRealSumPdf model_tmp1("model_tmp1", "model_tmp1", ph1, RooRealConstant::value(1.0)); RooProdPdf model1 ("model1", "model1", hc1, RooFit::Conditional(model_tmp1,x)); RooFitResult* result1 = (RooFitResult*) model1.fitTo(dh2, RooFit::SumW2Error(false), RooFit::PrintLevel(-1), RooFit::Verbose(false), RooFit::Save()); if (result1 == NULL) { THROW(JValueOutOfRange, "JTestPoissonLogLikelihoodRatioBeestonBarlow::test(): Unable to retrieve fit results"); } double nll2 = 0.0; // Poissonian negative log-likelihood of Asimov data-set onto itself for (int i=1 ; i <= h1->GetNbinsX() ; ++i) { for (int j=1 ; j <= h1->GetNbinsY() ; ++j) { for (int k=1 ; k <= h1->GetNbinsZ() ; ++k) { const double n = h2->GetBinContent(i,j,k); if (n > 0.0) { nll2 += -n*log(n) + log(tgamma(n+1)) + n; } else { nll2 -= penalty; } } } } const double nllratio = 2 * (result1->minNll() - nll2); const bool passed = (nllratio < threshold); const JResultTitle title(testName, resultType, passed, nllratio); TH2* h3 = result1->correlationHist(); h3->SetTitle(title.getTitle().c_str()); const JTestResult r (testName, JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())), JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())), resultType, nllratio, threshold, h3, passed); this->push_back(r); } /** * Read test parameters from input. * * \param in input stream * \return input stream */ std::istream& read(std::istream& in) override { using namespace JPP; in >> threshold; if (!(threshold > 0.0)) { THROW(JValueOutOfRange, "JTestPoissonLogLikelihoodRatioBeestonBarlow::read(): Given chi-square threshold " << threshold << " is invalid"); } return in; } private: double threshold; //!< threshold chi-square to decide if test is passed. }; } #endif