Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTestKolmogorovSlice.hh
Go to the documentation of this file.
1 #ifndef __JTESTKOLMOGOROV_SLICE__
2 #define __JTESTKOLMOGOROV_SLICE__
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 >> slice >> failuresThreshold;
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 
79  if(slice == 'x' || slice == 'X' || slice == 'y' || slice == 'Y'){
80 
82 
83  results.push_back(r);
84 
85  } else {
86  ERROR ("Slice option should be: x, X, y or Y, and not " + slice);
87  }
88 
89  }else if (!(dynamic_cast<TH1*>(o1) == NULL) && !(dynamic_cast<TH1*>(o2) == NULL)) {
90  ERROR("For 1D histograms call JTestKolmogorov_1D: " << o1->GetName() << endl);
91  }
92  };
93 
94  /**
95  * Writes the test result to root file
96  * \param f A ROOT file
97  * \param path Path in root file.
98  * \param onlyFailures If true, write only failures.
99  */
100  virtual void save(TFile* f , std::string path, bool onlyFailures = false) override{
101 
102  using namespace std;
103 
104  for (vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r){
105 
106  if (onlyFailures){
107  if (!r->passed){
108  if (f -> GetDirectory(path.c_str())==0) f->mkdir(path.c_str());
109  f->cd(path.c_str());
110  r->obj->Write();
111  }
112  }else{
113  if (f -> GetDirectory(path.c_str())==0) f->mkdir(path.c_str());
114  f->cd(path.c_str());
115  r->obj->Write();
116  }
117  }
118  }
119 
120  /**
121  * Clear results
122  */
123  void clear() override{
124  results.clear();
125  }
126 
127  /**
128  * Get test name.
129  */
130  std::string getName() override{
131  return name;
132  }
133 
134  /**
135  * Get parameter name.
136  */
137  std::string getParameter() override{
138  return parameter;
139  }
140 
141 
142 private:
143  double threshold; //!< threshold p-value to decide if test is passed.
144  double failuresThreshold; //!< threshold p-value to decide if test is passed.
145  char slice; //!< axis to slice. x or X for x-axis, y or Y for y-axis, n or N for None.
146  const std::string name = "KS_Slice"; //!< test name.
147  const std::string parameter = "p-Value(KS)"; //!< parameter name.
148 };
149 
150 #endif
const std::string parameter
parameter name.
Implementation of the different Kolmogorov-related tests.
char slice
axis to slice. x or X for x-axis, y or Y for y-axis, n or N for None.
const std::string name
test name.
double failuresThreshold
threshold p-value to decide if test is passed.
std::vector< JTestResult > results
Definition: JTest_t.hh:251
virtual void save(TFile *f, std::string path, bool onlyFailures=false) override
Writes the test result to root file.
Implementation of the Kolmogorov test for 2D histograms.
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
JTestResult JKolmogorovTestSlice(TH2 *h1, TH2 *h2, double threshold, double failuresThreshold, std::string testName, std::string parameterName, char slice)
Kolmogorov test for sliced 2D histograms.
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
double threshold
threshold p-value to decide if test is passed.
#define ERROR(A)
Definition: JMessage.hh:66
void clear() override
Clear results.
void test(TObject *o1, TObject *o2) override
Applies Kolmogorov test for two ROOT TH2 histograms.
std::string getParameter() override
Get parameter name.
Interface to read input and write output for TObject tests.
Definition: JTest_t.hh:246
std::ostream & write(std::ostream &out, std::string delimiter=" ", bool onlyFailures=false) const override
Write test result to output.
std::istream & read(std::istream &in) override
Read test parameters from input.
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
std::string getName() override
Get test name.
JTestKolmogorovSlice()
Default constructor.