#ifndef __JCOMPAREHISTOGRAMS__JTESTPOISSONLOGLIKELIHOODRATIO__ #define __JCOMPAREHISTOGRAMS__JTESTPOISSONLOGLIKELIHOODRATIO__ #include #include #include "JLang/JException.hh" #include "JCompareHistograms/JResultTitle.hh" #include "JCompareHistograms/JTest_t.hh" #include "TH1.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 JTestPoissonLogLikelihoodRatio: public JTest_t { public: /** * Default constructor. */ JTestPoissonLogLikelihoodRatio() : JTest_t("Poissonian Log-Likelihood Ratio", "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); } TH1* h3 = (TH1*) h1->Clone(h1->GetName() == h2->GetName() ? MAKE_CSTRING(h1->GetName() << "_" << testName) : MAKE_CSTRING(h1->GetName() << "_VS_" << h2->GetName() << "_" << testName)); h3->Reset(); double LnLratio = 0.0; for (int i=1 ; i <= h1->GetNbinsX() ; ++i){ for (int j=1 ; j <= h1->GetNbinsY() ; ++j){ for (int k=1 ; k <= h1->GetNbinsZ() ; ++k){ double contribution; const double n0 = h1->GetBinContent(i,j,k); const double n1 = h2->GetBinContent(i,j,k); if (n0 > 0.0 && n1 > 0.0) { contribution = 2 * (n0 - n1 - n0 * log(n0/n1)); } else if (n0 > 0.0 || n1 > 0.0) { contribution = penalty; } else { contribution = 0.0; } h3->SetBinContent(i,j,k, contribution); LnLratio -= contribution; } } } const bool passed = (LnLratio < threshold); const JResultTitle title(testName, resultType, passed, LnLratio); h3->SetTitle(title.getTitle().c_str()); h3->GetYaxis()->SetTitle(resultType.c_str()); const JTestResult r (testName, JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())), JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())), resultType, LnLratio, 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, "JTestPoissonLogLikelihoodRatio::read(): Given chi-square threshold " << threshold << " is invalid"); } return in; } private: double threshold; //!< threshold chi-square to decide if test is passed. }; } #endif