#ifndef __JCOMPAREHISTOGRAMS__JTESTIDENTICAL__ #define __JCOMPAREHISTOGRAMS__JTESTIDENTICAL__ #include #include #include "JLang/JException.hh" #include "JCompareHistograms/JTest_t.hh" #include "JCompareHistograms/JResultTitle.hh" #include "TH1.h" /** * \author rgruiz, bjung */ namespace JCOMPAREHISTOGRAMS {} namespace JPP { using namespace JCOMPAREHISTOGRAMS; } namespace JCOMPAREHISTOGRAMS { /** * Implementation of the a test to check if two 1D histograms are the same.\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 * The input parameter `tolerance`, is used to evaluate whether the test is passed or failed.\n */ class JTestIdentical : public JTest_t { public: /** * Default constructor. */ JTestIdentical() : JTest_t("Identical", "Difference") {} /** * Applies test for two ROOT TH1 histograms. * * \param o1 First histogram * \param o2 Second histogram */ void test(const TObject* o1, const TObject* o2) override { using namespace std; using namespace JPP; const TH1* h1 = dynamic_cast(o1); const TH1* h2 = dynamic_cast(o2); if (h1 == NULL || h2 == NULL) { THROW(JValueOutOfRange, "JTestIdentical::test(): Could not cast given TObjects to TH1."); } if(h1->GetNbinsX() != h2->GetNbinsX() || h1->GetNbinsY() != h2->GetNbinsY() || h1->GetNbinsZ() != h2->GetNbinsZ()) { THROW(JValueOutOfRange, "JTestIdentical::test(): Histograms with different bining. 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->Add(h2,-1); bool passed = true; double maxDifference = 0.0; for (int i=1 ; i <= h1->GetNbinsX() && passed ; ++i){ for (int j=1 ; j <= h1->GetNbinsY() && passed ; ++j){ for (int k=1 ; k <= h1->GetNbinsZ() && passed ; ++k){ const double d = h1->GetBinContent(i,j,k) - h2->GetBinContent(i,j,k); if (fabs(d) > maxDifference) { maxDifference = fabs(d); } if (fabs(d) > tolerance) { passed = false; } } } } const JResultTitle title(testName, resultType, passed, maxDifference); h3->SetTitle(title.getTitle().c_str()); const int Ndims = h3->GetDimension(); if (Ndims == 1) { h3->GetYaxis()->SetTitle(resultType.c_str()); } else if (Ndims == 2) { h3->GetZaxis()->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, maxDifference, tolerance, 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 >> tolerance; if (tolerance < 0.0) { THROW(JValueOutOfRange, "JTestIdentical::read(): Invalid tolerance value " << tolerance); } return in; } private: double tolerance; //!< tolerance value to accept the difference as acceptable. }; } #endif