Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTestChi2_1D.hh
Go to the documentation of this file.
1 #ifndef __JTESTCHI2_1D__
2 #define __JTESTCHI2_1D__
3 
4 #include <istream>
5 #include <ostream>
8 
9 /**
10  * \author rgruiz
11  */
12 
13 /**
14  * Implementation of the Chi2 test for 1D histograms.\n
15  * 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
16  * The input parameter threshold(), is used to evaluate whether the test is passed or failed.\n
17  * The evaluation is done by comparing the threshold() value with the result produced by JChi2Test(). The output of a Chi2 test is a p-value.\n
18  * The parameter threshold() should therefore be a real value between 0 and 1.
19  */
20 class JTestChi2_1D:
21  public JTest_t ,
22  public JTestChi2_t
23 {
24 public:
25 
26  /**
27  * Default constructor.
28  */
30 
31  /**
32  * Read test parameters from input.
33  *
34  * \param in input stream
35  * \return input stream
36  */
37  std::istream& read(std::istream& in) override{
38  return in >> threshold;
39  };
40 
41  /**
42  * Write test result to output.
43  *
44  * \param out output stream
45  * \param delimiter field delimiter
46  * \param onlyFailures If true, write only failures.
47  * \return output stream
48  */
49  std::ostream& write(std::ostream& out, std::string delimiter = " ", bool onlyFailures = false) const override{
50  for (std::vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r) {
51  if (onlyFailures){
52  if (!r->passed)
53  out << std::scientific << std::setprecision(2) << (r->passed ? JPP::GREEN : JPP::RED) << r->print(delimiter) << std::endl;
54  }else{
55  out << std::scientific << std::setprecision(2) << (r->passed ? JPP::GREEN : JPP::RED) << r->print(delimiter) << std::endl;
56  }
57  }
58  return out;
59  };
60 
61  /**
62  * Applies Chi2 test for two ROOT TH1 histograms.
63  *
64  * \param o1 First histogram
65  * \param o2 Second histogram
66  */
67  void test(TObject* o1, TObject* o2) override{
68 
69  using namespace std;
70 
71  if (!(dynamic_cast<TH2*>(o1) == NULL) || !(dynamic_cast<TH2*>(o2) == NULL)) {
72  ERROR("For 2D histograms call JTestChi2_2D: " << o1->GetName() << endl);
73 
74  } else if (!(dynamic_cast<TH1*>(o1) == NULL) && !(dynamic_cast<TH1*>(o2) == NULL)) {
75 
76  TH1D* h1 = dynamic_cast<TH1D*>(o1);
77  TH1D* h2 = dynamic_cast<TH1D*>(o2);
78 
79  if(h1 -> GetNbinsX() != h2 -> GetNbinsX())
80  ERROR("Histograms with different bining. The objects: " << h1 -> GetName() << " can not be compared." << endl);
81 
83 
84  results.push_back(r);
85  }
86  };
87 
88  /**
89  * Writes the test result to root file
90  * \param f A ROOT file
91  * \param path Path in root file.
92  * \param onlyFailures If true, write only failures.
93  */
94  virtual void save(TFile* f , std::string path, bool onlyFailures = false) override{
95 
96  using namespace std;
97 
98  for (vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r){
99 
100  if (onlyFailures){
101  if (!r->passed){
102  if (f -> GetDirectory(path.c_str())==0) f->mkdir(path.c_str());
103  f->cd(path.c_str());
104  r->obj->Write();
105  }
106  }else{
107  if (f -> GetDirectory(path.c_str())==0) f->mkdir(path.c_str());
108  f->cd(path.c_str());
109  r->obj->Write();
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 private:
136  double threshold; //!< threshold p-value to decide if test is passed.
137  const std::string options = ""; //!< options for the ROOT chi2 test.
138  const std::string name = "Chi2_1D"; //!< test name.
139  const std::string parameter = "p-Value(chi2)"; //!< test name.
140 };
141 
142 #endif
const std::string parameter
test name.
Implementation of the Chi2 test for 1D histograms.
Definition: JTestChi2_1D.hh:20
std::vector< JTestResult > results
Definition: JTest_t.hh:251
Definition: JRoot.hh:19
double threshold
threshold p-value to decide if test is passed.
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
data_type r[M+1]
Definition: JPolint.hh:742
std::istream & read(std::istream &in) override
Read test parameters from input.
Definition: JTestChi2_1D.hh:37
Structure containing the result of the test.
Definition: JTest_t.hh:164
then JPizza f
Definition: JPizza.sh:46
do cat driver txt<< EOFevent ev_configure{RC_EVT%< ev_configure.txt > RC_DWRT path
const std::string name
test name.
virtual void save(TFile *f, std::string path, bool onlyFailures=false) override
Writes the test result to root file.
Definition: JTestChi2_1D.hh:94
#define ERROR(A)
Definition: JMessage.hh:66
void test(TObject *o1, TObject *o2) override
Applies Chi2 test for two ROOT TH1 histograms.
Definition: JTestChi2_1D.hh:67
std::string getParameter() override
Get parameter name.
JTestChi2_1D()
Default constructor.
Definition: JTestChi2_1D.hh:29
std::ostream & write(std::ostream &out, std::string delimiter=" ", bool onlyFailures=false) const override
Write test result to output.
Definition: JTestChi2_1D.hh:49
Interface to read input and write output for TObject tests.
Definition: JTest_t.hh:246
const std::string options
options for the ROOT chi2 test.
Implementation of the different Chi2-related tests.
Definition: JTestChi2_t.hh:15
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 CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:38
void clear() override
Clear results.
JTestResult JChi2Test(TH1 *h1, TH1 *h2, double threshold, std::string testName, std::string parameterName, std::string options)
Chi2 test for 1D histograms.
Definition: JTestChi2_t.hh:39
std::string getName() override
Get test name.