Jpp  18.3.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTestZero.hh
Go to the documentation of this file.
1 #ifndef __JCOMPAREHISTOGRAMS__JTESTZERO__
2 #define __JCOMPAREHISTOGRAMS__JTESTZERO__
3 
4 #include <istream>
5 #include <ostream>
6 
7 #include "Math/Math.h"
8 #include "Math/Error.h"
9 #include "Math/ProbFuncMathCore.h"
10 #include "Math/SpecFuncMathCore.h"
11 
12 #include "JLang/JException.hh"
13 
16 
17 #include "JTools/JQuantile.hh"
18 #include "JTools/JRange.hh"
19 
20 #include "TObject.h"
21 #include "TMath.h"
22 #include "TH1.h"
23 
24 
25 /**
26  * \author rgruiz, bjung
27  */
28 namespace JCOMPAREHISTOGRAMS {}
29 namespace JPP { using namespace JCOMPAREHISTOGRAMS; }
30 
31 namespace JCOMPAREHISTOGRAMS {
32 
33  /**
34  * Implementation of a bin-by-bin compatibility test for histograms with low bin contents.\n
35  * The test loops over all the bins of both histograms and performs the following operations.\n
36  * -Calculates the probability that the observed bin content for histogram A is obtained from a Poisson of parameter 1.\n
37  * -Compares the previous result with the `threshold` value given as an input parameter. The result is true if the probability is higher than the `threshold`.\n
38  * -Calculates the probability that the observed bin content for histogram B is obtained from a Poisson of parameter 1.\n
39  * -Compares the previous result with the `threshold` value given as an input parameter. The result is true if the probability is higher than the `threshold`.\n
40  * -Compares the results from both bins: If both are true, or both are false, the test is passed. If one is true and the other is false, the test is failed.\n
41  * At the end of the loop, a failure fraction is computed and compared to the `outliersThreshold` parameter. If the failure fraction is above the `threshold`, the test is failed.\n
42  * 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.
43  */
44  class JTestZero:
45  public JTest_t
46  {
47  public:
48 
49  /**
50  * Default constructor.
51  */
53  JTest_t("Zero", "failure_fraction")
54  {}
55 
56 
57  /**
58  * Bin-by-bin comparison for ROOT histograms, of compatibility with a Poisson pdf of parameter 1.
59  *
60  * \param o1 First histogram
61  * \param o2 Second histogram
62  */
63  void test(const TObject* o1, const TObject* o2) override
64  {
65  using namespace std;
66  using namespace JPP;
67 
68  const TH1* h1 = dynamic_cast<const TH1*>(o1);
69  const TH1* h2 = dynamic_cast<const TH1*>(o2);
70 
71  if (h1 == NULL || h2 == NULL) {
72  THROW(JValueOutOfRange, "JTestChi2::test(): Could not cast given TObjects to TH2.");
73  }
74 
75  const int nx1 = h1->GetNbinsX();
76  const int nx2 = h2->GetNbinsX();
77  const int ny1 = h1->GetNbinsY();
78  const int ny2 = h2->GetNbinsY();
79  const int nz1 = h1->GetNbinsZ();
80  const int nz2 = h2->GetNbinsZ();
81 
82  if(nx1 != nx2 || ny1 != ny2 || nz1 != nz2) {
83  THROW(JValueOutOfRange, "JTestZero::test(): Histograms with different binning. The objects: " <<
84  h1->GetName() << " and " << h2->GetName() << " can not be compared." << endl);
85  }
86 
87  TH1* h3 = (TH1*) h1->Clone(h1->GetName() == h2->GetName() ?
88  MAKE_CSTRING(h1->GetName() << "_" << testName) :
89  MAKE_CSTRING(h1->GetName() << "_VS_" << h2->GetName() << "_" << testName));
90 
91  h3->Reset();
92 
93  double failures = 0;
94 
95  for (int i=1 ; i<nx1+1 ; ++i) {
96  for (int j=1 ; j<ny1+1 ; ++j) {
97  for (int k=1 ; k<nz1+1 ; ++k) {
98 
99  const int Nbin = h3->GetBin(i,j,k);
100 
101  const double m = h1->GetBinContent(i,j,k);
102  const double n = h2->GetBinContent(i,j,k);
103 
104  const double p1 = 1 - ROOT::Math::poisson_cdf(m,1);
105  const double p2 = 1 - ROOT::Math::poisson_cdf(n,1);
106 
107  if ((p1 > threshold && p2 < threshold) ||
108  (p1 < threshold && p2 > threshold)) {
109 
110  failures+=1./(nx1*ny1);
111  h3->Fill(Nbin);
112  }
113  }
114  }
115  }
116 
117  const bool passed = (failures < outliersThreshold);
118 
119  const JResultTitle title(testName, resultType, passed , failures);
120 
121  h3->SetTitle(title.getTitle().c_str());
122 
123  const int Ndims = h3->GetDimension();
124 
125  if (Ndims == 1) {
126  h3->GetYaxis()->SetTitle(resultType.c_str());
127  } else if (Ndims == 2) {
128  h3->GetZaxis()->SetTitle(resultType.c_str());
129  }
130 
131  const JTestResult r(testName,
132  JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())),
133  JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())),
134  resultType, failures, threshold, h3, passed);
135 
136  this->push_back(r);
137  }
138 
139 
140  /**
141  * Read test parameters from input.
142  *
143  * \param in input stream
144  * \return input stream
145  */
146  std::istream& read(std::istream& in) override
147  {
148  using namespace JPP;
149 
150  in >> threshold >> outliersThreshold;
151 
152  if (threshold < 0.0 || threshold > 1.0) {
153  THROW(JValueOutOfRange, "JTestZero::read(): Invalid threshold value " << threshold);
154  }
155 
156  if (outliersThreshold < 0.0 || outliersThreshold > 1.0) {
157  THROW(JValueOutOfRange, "JTestZero::read(): Invalid outliersThreshold value " << outliersThreshold);
158  }
159 
160  return in;
161  }
162 
163  private:
164 
165  double threshold; //!< threshold p-value to decide if test is passed.
166  double outliersThreshold; //!< Fraction of bins allowed to fail.
167  };
168 }
169 
170 #endif
then usage $script< detector file > minrun maxrun report nIn case of failures
Exceptions.
then fatal No hydrophone data file $HYDROPHONE_TXT fi sort gr k
TPaveText * p1
Interface to read input and write output for TObject tests.
Definition: JTest_t.hh:40
std::string getTitle() const
Returns a standard string to be used as title of a graphical root object.
Definition: JResultTitle.hh:57
Class dedicated to standardize the title of the graphical objects produced by the JTest_t() derived c...
Definition: JResultTitle.hh:25
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Definition: JRoot.hh:19
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:136
Auxiliary class to handle file name, ROOT directory and object name.
data_type r[M+1]
Definition: JPolint.hh:868
void test(const TObject *o1, const TObject *o2) override
Bin-by-bin comparison for ROOT histograms, of compatibility with a Poisson pdf of parameter 1...
Definition: JTestZero.hh:63
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
double outliersThreshold
Fraction of bins allowed to fail.
Definition: JTestZero.hh:166
const int n
Definition: JPolint.hh:786
double threshold
threshold p-value to decide if test is passed.
Definition: JTestZero.hh:165
const std::string resultType
test result type
Definition: JTest_t.hh:181
const std::string testName
test name
Definition: JTest_t.hh:180
p2
Definition: module-Z:fit.sh:74
std::istream & read(std::istream &in) override
Read test parameters from input.
Definition: JTestZero.hh:146
Auxiliary class to define a range between two values.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:48
Implementation of a bin-by-bin compatibility test for histograms with low bin contents.
Definition: JTestZero.hh:44
Structure containing the result of the test.
Definition: JTestResult.hh:28
int j
Definition: JPolint.hh:792
JTestZero()
Default constructor.
Definition: JTestZero.hh:52