#ifndef __JCOMPAREHISTOGRAMS__JTESTIDENTICAL_T__ #define __JCOMPAREHISTOGRAMS__JTESTIDENTICAL_T__ #include #include #include "JCompareHistograms/JTest_t.hh" #include "JCompareHistograms/JResultTitle.hh" /** * \author rgruiz */ namespace JCOMPAREHISTOGRAMS { using JGIZMO::JRootObjectID; /** * Implementation test that verify if two histograms are the same. */ class JTestIdentical_t { public: /** * Default constructor. */ JTestIdentical_t(){} /** * Bin-by-Bin test of two histograms, that verifies whether the histograms are the same.\n * For each bin, the test calculates the difference, and compares it with a tolerance passed as an argument.\n * The test is passed if the difference is smaller than the tolerance for every bin.\n * The template parameter can be TH1X, TH2X or TH3X. * * \param h1 First object * \param h2 Second object * \param tolerance tolerance value for the bin-by-bin differences * \param parameterName Name of the parameter used to test the histograms * \param testName Name of the test used to compare the histograms * * \return Test result. */ template JTestResult JIdenticalTest(T* h1, T* h2, double tolerance, std::string testName, std::string parameterName) { using namespace std; using namespace JPP; if(h1 -> GetNbinsX() != h2 -> GetNbinsX() || h1 -> GetNbinsY() != h2 -> GetNbinsY() || h1 -> GetNbinsZ() != h2 -> GetNbinsZ()) ERROR("Histograms with different bining. The objects: " << h1 -> GetName() << " can not be compared." << endl); T* h3 = (T*)h1->Clone(h1->GetName()==h2->GetName() ? MAKE_CSTRING(to_string(h1->GetName())) : MAKE_CSTRING(to_string(h1->GetName()) + "_VS_" + to_string(h2->GetName()))); h3->Add(h2,-1); bool passed; double maxDifference = 0.0; for (int i=0 ; i < h1->GetNbinsX() ; ++i){ for (int j=0 ; j< h1->GetNbinsY() ; ++j){ for (int k=0 ; k< h1->GetNbinsZ() ; ++k){ double d = h1->GetBinContent(i+1,j+1,k+1) - h2->GetBinContent(i+1,j+1,k+1); if (fabs(d) > maxDifference) maxDifference = fabs(d); if (fabs(d) > tolerance) passed = false; } } } JResultTitle title(testName, parameterName, passed , maxDifference); h3->SetTitle(title.getTitle().c_str()); JTestResult r (testName, JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())), JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())), parameterName, maxDifference, tolerance, h3, passed); return r; }; }; } #endif