Jpp  19.1.0
the software that should make you happy
JTestKolmogorov_1D.hh
Go to the documentation of this file.
1 #ifndef __JCOMPAREHISTOGRAMS__JTESTKOLMOGOROV_1D__
2 #define __JCOMPAREHISTOGRAMS__JTESTKOLMOGOROV_1D__
3 
4 #include <istream>
5 #include <ostream>
6 
7 #include "JLang/JException.hh"
8 
11 
12 #include "TH1.h"
13 #include "TMath.h"
14 
15 
16 /**
17  * \author rgruiz, bjung
18  */
19 namespace JCOMPAREHISTOGRAMS {}
20 namespace JPP { using namespace JCOMPAREHISTOGRAMS; }
21 
22 namespace JCOMPAREHISTOGRAMS {
23 
24  /**
25  *
26  * Implementation of the Kolmogorov test for 1D histograms.\n
27  * 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
28  * This test uses the input parameter threshold() to evaluate whether the test is passed or failed.\n
29  * The evaluation is done by comparing the threshold value with the result of the JKolmogorovTest() test. This is a p-value.\n
30  * The parameter threshold should therefore be a real value between 0 and 1.
31  */
33  public JTest_t
34  {
35  public:
36 
37  /**
38  * Default constructor.
39  */
41  JTest_t("KS_1D", "p-Value(KS)")
42  {}
43 
44 
45  /**
46  * Applies Kolmogorov test for two ROOT TH1 histograms.
47  *
48  * \param o1 First histogram
49  * \param o2 Second histogram
50  */
51  void test(const TObject* o1, const TObject* o2) override
52  {
53  using namespace std;
54  using namespace JPP;
55 
56  const TH1* h1 = dynamic_cast<const TH1*>(o1);
57  const TH1* h2 = dynamic_cast<const TH1*>(o2);
58 
59  if (h1->GetDimension() != 1 || h2->GetDimension() != 1) {
60  THROW(JValueOutOfRange, "JTestKolmogorov_1D::test(): Given histograms must be 1-D.");
61  }
62 
63  const int n1 = h1 -> GetNbinsX();
64  const int n2 = h2 -> GetNbinsX();
65 
66  if(n1 != n2) {
67  THROW(JValueOutOfRange, "JTestKolmogorov_1D::test(): Histograms with different bining. The objects: " <<
68  h1->GetName() << " and " << h2->GetName() << " can not be compared." << endl);
69  }
70 
71  TH1* h3 = (TH1*) h1->Clone(h1->GetName() == h2->GetName() ?
72  MAKE_CSTRING(h1->GetName() << "_" << testName) :
73  MAKE_CSTRING(h1->GetName() << "_VS_" << h2->GetName() << "_" << testName));
74 
75  const double s1 = 1./h1->Integral();
76  const double s2 = 1./h2->Integral();
77 
78  double ew1, ew2, w1 = 0, w2 = 0;
79 
80  for (int bin = 1; bin <= n1; ++bin){
81  ew1 = h1->GetBinError(bin);
82  ew2 = h2->GetBinError(bin);
83  w1 += ew1*ew1;
84  w2 += ew2*ew2;
85  }
86 
87  bool afunc1 = false;
88  bool afunc2 = false;
89 
90  double esum1 = 0, esum2 = 0;
91 
92  if (w1 > 0) {
93  esum1 = 1./s1/s1/w1;
94  } else {
95  afunc1 = true;
96  }
97 
98  if (w2 > 0) {
99  esum2 = 1./s2/s2/w2;
100  } else {
101  afunc2 = true;
102  }
103 
104  if (afunc2 && afunc1) {
105  THROW(JValueOutOfRange, "JTestKolmogorov_1D::test(): Errors are zero for both histograms.");
106  }
107 
108  double dmax = 0, c1 = 0, c2 = 0;
109 
110  for (int bin=1 ; bin<=n1 ; ++bin) {
111 
112  c1 += s1*h1->GetBinContent(bin);
113  c2 += s2*h2->GetBinContent(bin);
114 
115  double d = TMath::Abs(c1-c2)*TMath::Sqrt(esum1*esum2/(esum1+esum2));
116  double p = TMath::KolmogorovProb(d);
117 
118  dmax = TMath::Max(dmax,TMath::Abs(c1-c2));
119 
120  h3->SetBinContent(bin,p);
121  }
122 
123  double z;
124 
125  if (afunc1) {
126  z = dmax*TMath::Sqrt(esum2);
127  } else if (afunc2) {
128  z = dmax*TMath::Sqrt(esum1);
129  } else {
130  z = dmax*TMath::Sqrt(esum1*esum2/(esum1+esum2));
131  }
132 
133  const double pValue = TMath::KolmogorovProb(z);
134 
135  const bool passed = (pValue > threshold);
136 
137  const JResultTitle title(testName, resultType, passed , pValue);
138 
139  h3->SetTitle(title.getTitle().c_str());
140  h3->GetYaxis()->SetTitle(resultType.c_str());
141 
142  const JTestResult r (testName,
143  JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())),
144  JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())),
145  resultType, pValue, threshold, h3, passed);
146 
147  this->push_back(r);
148  }
149 
150 
151  /**
152  * Read test parameters from input.
153  *
154  * \param in input stream
155  * \return input stream
156  */
157  std::istream& read(std::istream& in) override
158  {
159  using namespace JPP;
160 
161  in >> threshold;
162 
163  if (threshold < 0.0 || threshold > 1.0) {
164  THROW(JValueOutOfRange, "JTestKolmogorov_1D::read(): Invalid threshold value " << threshold);
165  }
166 
167  return in;
168  }
169 
170  private:
171 
172  double threshold; //!< threshold p-value to decide if test is passed.
173  };
174 }
175 
176 #endif
TCanvas * c1
Global variables to handle mouse events.
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 Kolmogorov test for 1D histograms.
double threshold
threshold p-value to decide if test is passed.
void test(const TObject *o1, const TObject *o2) override
Applies Kolmogorov test for two ROOT TH1 histograms.
std::istream & read(std::istream &in) override
Read test parameters from input.
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
JSqrt< JF1_t > Sqrt(const JF1_t &f1)
Square root of function.
Definition: JMathlib.hh:2136
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
data_type r[M+1]
Definition: JPolint.hh:868
Definition: JSTDTypes.hh:14
Structure containing the result of the test.
Definition: JTestResult.hh:30
Definition: JRoot.hh:19