Jpp  19.1.0
the software that should make you happy
JTestIdentical.hh
Go to the documentation of this file.
1 #ifndef __JCOMPAREHISTOGRAMS__JTESTIDENTICAL__
2 #define __JCOMPAREHISTOGRAMS__JTESTIDENTICAL__
3 
4 #include <istream>
5 #include <ostream>
6 
7 #include "JLang/JException.hh"
8 
11 
12 #include "TH1.h"
13 
14 
15 /**
16  * \author rgruiz, bjung
17  */
18 namespace JCOMPAREHISTOGRAMS {}
19 namespace JPP { using namespace JCOMPAREHISTOGRAMS; }
20 
21 namespace JCOMPAREHISTOGRAMS {
22 
23  /**
24  * Implementation of the a test to check if two 1D histograms are the same.\n
25  * 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
26  * The input parameter `tolerance`, is used to evaluate whether the test is passed or failed.\n
27  */
28  class JTestIdentical :
29  public JTest_t
30  {
31  public:
32 
33  /**
34  * Default constructor.
35  */
37  JTest_t("Identical", "Difference")
38  {}
39 
40 
41  /**
42  * Applies test for two ROOT TH1 histograms.
43  *
44  * \param o1 First histogram
45  * \param o2 Second histogram
46  */
47  void test(const TObject* o1, const TObject* o2) override
48  {
49  using namespace std;
50  using namespace JPP;
51 
52  const TH1* h1 = dynamic_cast<const TH1*>(o1);
53  const TH1* h2 = dynamic_cast<const TH1*>(o2);
54 
55  if (h1 == NULL || h2 == NULL) {
56  THROW(JValueOutOfRange, "JTestIdentical::test(): Could not cast given TObjects to TH1.");
57  }
58 
59  if(h1->GetNbinsX() != h2->GetNbinsX() ||
60  h1->GetNbinsY() != h2->GetNbinsY() ||
61  h1->GetNbinsZ() != h2->GetNbinsZ()) {
62  THROW(JValueOutOfRange, "JTestIdentical::test(): Histograms with different bining. The objects: " <<
63  h1->GetName() << " and " << h2->GetName() << " can not be compared." << endl);
64  }
65 
66  TH1* h3 = (TH1*) h1->Clone(h1->GetName() == h2->GetName() ?
67  MAKE_CSTRING(h1->GetName() << "_" << testName) :
68  MAKE_CSTRING(h1->GetName() << "_VS_" << h2->GetName() << "_" << testName));
69 
70  h3->Add(h2,-1);
71 
72  bool passed = true;
73 
74  double maxDifference = 0.0;
75 
76  for (int i=1 ; i <= h1->GetNbinsX() && passed ; ++i){
77  for (int j=1 ; j <= h1->GetNbinsY() && passed ; ++j){
78  for (int k=1 ; k <= h1->GetNbinsZ() && passed ; ++k){
79 
80  const double d = h1->GetBinContent(i,j,k) - h2->GetBinContent(i,j,k);
81 
82  if (fabs(d) > maxDifference) { maxDifference = fabs(d); }
83  if (fabs(d) > tolerance) { passed = false; }
84  }
85  }
86  }
87 
88  const JResultTitle title(testName, resultType, passed, maxDifference);
89 
90  h3->SetTitle(title.getTitle().c_str());
91 
92  const int Ndims = h3->GetDimension();
93 
94  if (Ndims == 1) {
95  h3->GetYaxis()->SetTitle(resultType.c_str());
96  } else if (Ndims == 2) {
97  h3->GetZaxis()->SetTitle(resultType.c_str());
98  }
99 
100  const JTestResult r (testName,
101  JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())),
102  JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())),
103  resultType, maxDifference, tolerance, h3, passed);
104 
105  this->push_back(r);
106  }
107 
108 
109  /**
110  * Read test parameters from input.
111  *
112  * \param in input stream
113  * \return input stream
114  */
115  std::istream& read(std::istream& in) override
116  {
117  using namespace JPP;
118 
119  in >> tolerance;
120 
121  if (tolerance < 0.0) {
122  THROW(JValueOutOfRange, "JTestIdentical::read(): Invalid tolerance value " << tolerance);
123  }
124 
125  return in;
126  }
127 
128 
129  private:
130 
131  double tolerance; //!< tolerance value to accept the difference as acceptable.
132  };
133 }
134 
135 #endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:72
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:63
Class dedicated to standardize the title of the graphical objects produced by the JTest_t() derived c...
Definition: JResultTitle.hh:26
std::string getTitle() const
Returns a standard string to be used as title of a graphical root object.
Definition: JResultTitle.hh:57
Implementation of the a test to check if two 1D histograms are the same.
std::istream & read(std::istream &in) override
Read test parameters from input.
double tolerance
tolerance value to accept the difference as acceptable.
JTestIdentical()
Default constructor.
void test(const TObject *o1, const TObject *o2) override
Applies test for two ROOT TH1 histograms.
Interface to read input and write output for TObject tests.
Definition: JTest_t.hh:42
const std::string resultType
test result type
Definition: JTest_t.hh:181
const std::string testName
test name
Definition: JTest_t.hh:180
Auxiliary class to handle file name, ROOT directory and object name.
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:180
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
data_type r[M+1]
Definition: JPolint.hh:868
int j
Definition: JPolint.hh:792
Definition: JSTDTypes.hh:14
Structure containing the result of the test.
Definition: JTestResult.hh:30
Definition: JRoot.hh:19