Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTestKolmogorov_2D.hh
Go to the documentation of this file.
1 #ifndef __JTESTKOLMOGOROV_2D__
2 #define __JTESTKOLMOGOROV_2D__
3 
4 #include <istream>
5 #include <ostream>
6 #include "TMath.h"
9 
10 /**
11  * \author rgruiz
12  */
13 
14 /**
15  * Implementation of the Kolmogorov test for 2D histograms.\n
16  * 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
17  * This test compares two 2D histograms. If the parameter slice() equals x, X y or Y, the histograms are sliced along the corresponding axis, and the JKolmogorovTest() test is applied to each slice.\n
18  * If slice() equals n or N, the histograms are not sliced, and JKolmogorovTest2D() is applied.\n
19  * The input parameter threshold(), is used to evaluate whether the test is passed or failed for each slice or for the full 2D distribution.\n
20  * The evaluation is done by comparing the threshold() value with the result produced by JKolmogorovTest() or JKolmogorovTest2D(), which is a p-value.\n
21  * The parameter threshold() should therefore be a real value between 0 and 1.
22  */
24  public JTestKolmogorov_t,
25  public JTest_t
26 {
27 public:
28  /**
29  * Default constructor.
30  */
32 
33  /**
34  * Read test parameters from input.
35  *
36  * \param in input stream
37  * \return input stream
38  */
39  std::istream& read(std::istream& in) override{
40  return in >> threshold;
41  };
42 
43  /**
44  * Write test result to output.
45  *
46  * \param out output stream
47  * \param delimiter field delimiter
48  * \param onlyFailures If true, write only failures.
49  * \return output stream
50  */
51  std::ostream& write(std::ostream& out, std::string delimiter = " ", bool onlyFailures = false) const override{
52  for (std::vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r) {
53  if (onlyFailures){
54  if (!r->passed)
55  out << std::scientific << std::setprecision(2) << (r->passed ? JPP::GREEN : JPP::RED) << r->print(delimiter) << std::endl;
56  }else{
57  out << std::scientific << std::setprecision(2) << (r->passed ? JPP::GREEN : JPP::RED) << r->print(delimiter) << std::endl;
58  }
59  }
60  return out;
61  };
62 
63  /**
64  * Applies Kolmogorov test for two ROOT TH2 histograms.
65  *
66  * \param o1 First histogram
67  * \param o2 Second histogram
68  */
69  void test(TObject* o1, TObject* o2) override{
70 
71  using namespace std;
72  using namespace JPP;
73 
74  if (!(dynamic_cast<TH2*>(o1) == NULL) && !(dynamic_cast<TH2*>(o2) == NULL)) {
75 
76  TH2D* h1 = dynamic_cast<TH2D*>(o1);
77  TH2D* h2 = dynamic_cast<TH2D*>(o2);
78 
80  results.push_back(r);
81 
82  }else if (!(dynamic_cast<TH1*>(o1) == NULL) && !(dynamic_cast<TH1*>(o2) == NULL)) {
83  ERROR("For 1D histograms call JTestKolmogorov_1D: " << o1->GetName() << endl);
84  }
85  };
86 
87  /**
88  * Writes the test result to root file
89  * \param f A ROOT file
90  * \param path Path in root file.
91  * \param onlyFailures If true, write only failures.
92  */
93  virtual void save(TFile* f , std::string path, bool onlyFailures = false) override{
94 
95  using namespace std;
96 
97  for (vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r){
98 
99  if (onlyFailures){
100  if (!r->passed){
101  if (f -> GetDirectory(path.c_str())==0) f->mkdir(path.c_str());
102  f->cd(path.c_str());
103  r->obj->Write();
104  }
105  }else{
106  if (f -> GetDirectory(path.c_str())==0) f->mkdir(path.c_str());
107  f->cd(path.c_str());
108  r->obj->Write();
109  }
110  }
111  }
112 
113 
114  /**
115  * Clear results
116  */
117  void clear() override{
118  results.clear();
119  }
120 
121  /**
122  * Get test name.
123  */
124  std::string getName() override{
125  return name;
126  }
127 
128  /**
129  * Get parameter name.
130  */
131  std::string getParameter() override{
132  return parameter;
133  }
134 
135 
136 private:
137  double threshold; //!< threshold p-value to decide if test is passed.
138  const std::string name = "KS_2D"; //!< test name.
139  const std::string parameter = "p-Value(KS)"; //!< parameter name.
140 };
141 
142 #endif
Implementation of the different Kolmogorov-related tests.
std::ostream & write(std::ostream &out, std::string delimiter=" ", bool onlyFailures=false) const override
Write test result to output.
double threshold
threshold p-value to decide if test is passed.
std::vector< JTestResult > results
Definition: JTest_t.hh:251
void clear() override
Clear results.
Definition: JRoot.hh:19
do set_array DAQHEADER JPrintDAQHeader f
Definition: JTuneHV.sh:74
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
data_type r[M+1]
Definition: JPolint.hh:742
virtual void save(TFile *f, std::string path, bool onlyFailures=false) override
Writes the test result to root file.
Structure containing the result of the test.
Definition: JTest_t.hh:164
do cat driver txt<< EOFevent ev_configure{RC_EVT%< ev_configure.txt > RC_DWRT path
std::istream & read(std::istream &in) override
Read test parameters from input.
Implementation of the Kolmogorov test for 2D histograms.
void test(TObject *o1, TObject *o2) override
Applies Kolmogorov test for two ROOT TH2 histograms.
#define ERROR(A)
Definition: JMessage.hh:66
const std::string parameter
parameter name.
const std::string name
test name.
JTestResult JKolmogorovTest2D(TH2 *h1, TH2 *h2, double threshold, std::string testName, std::string parameterName)
Kolmogorov test for 2D histograms.
Interface to read input and write output for TObject tests.
Definition: JTest_t.hh:246
std::string getName() override
Get test name.
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 source JAcoustics sh $DETECTOR_ID typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
JTestKolmogorov_2D()
Default constructor.
std::string getParameter() override
Get parameter name.