Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTestChi2_2D.hh
Go to the documentation of this file.
1 #ifndef __JTESTCHI2_2D__
2 #define __JTESTCHI2_2D__
3 
4 #include <istream>
5 #include <ostream>
8 
9 /**
10  * \author rgruiz
11  */
12 
13 /**
14  * Implementation of the Chi2 test for 2D 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 parameter slice() can have the values x, X, y or Y. The histograms are sliced along the corresponding axis, and the JChi2Test() test is applied to each slice.\n
17  * The input parameter threshold(), is used to evaluate whether the test is passed or failed for each slice.\n
18  * The evaluation is done by comparing the threshold() value with the result produced by JChi2Test(). The output of JChi2Test() is a p-value.\n
19  * The parameter threshold() should therefore be a real value between 0 and 1.
20  */
21 class JTestChi2_2D:
22  public JTestChi2_t,
23  public JTest_t
24 {
25 public:
26 
27  /**
28  * Default constructor.
29  */
31 
32  /**
33  * Read test parameters from input.
34  *
35  * \param in input stream
36  * \return input stream
37  */
38  std::istream& read(std::istream& in) override{
39  return in >> threshold >> failuresThreshold >> slice;
40  };
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  /**
65  * Applies Chi2 test for two ROOT TH2 histograms.
66  *
67  * \param o1 First histogram
68  * \param o2 Second histogram
69  */
70  void test(TObject* o1, TObject* o2) override{
71 
72  using namespace std;
73  using namespace JPP;
74 
75  if (!(dynamic_cast<TH2*>(o1) == NULL) && !(dynamic_cast<TH2*>(o2) == NULL)) {
76 
77  TH2D* h1 = dynamic_cast<TH2D*>(o1);
78  TH2D* h2 = dynamic_cast<TH2D*>(o2);
79 
80  if(slice == 'x' || slice == 'X' || slice == 'y' || slice == 'Y'){
81 
83 
84  results.push_back(r);
85 
86  } else {
87  ERROR ("Slice option should be: x, X, y or Y, and not " + slice);
88  }
89 
90  } else if (!(dynamic_cast<TH1*>(o1) == NULL) && !(dynamic_cast<TH1*>(o2) == NULL)) {
91  ERROR("For 1D histograms call JChi2_1D: " << o1->GetName() << endl);
92  }
93  };
94 
95 
96  /**
97  * Writes the test result to root file
98  * \param f A ROOT file
99  * \param path Path in root file.
100  * \param onlyFailures If true, write only failures.
101  */
102  virtual void save(TFile* f , std::string path, bool onlyFailures = false) override{
103 
104  using namespace std;
105 
106  for (vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r){
107 
108  if (onlyFailures){
109  if (!r->passed){
110  if (f -> GetDirectory(path.c_str())==0) f->mkdir(path.c_str());
111  f->cd(path.c_str());
112  r->obj->Write();
113  }
114  }else{
115  if (f -> GetDirectory(path.c_str())==0) f->mkdir(path.c_str());
116  f->cd(path.c_str());
117  r->obj->Write();
118  }
119  }
120  }
121 
122 
123  /**
124  * Clear results
125  */
126  void clear() override{
127  results.clear();
128  }
129 
130  /**
131  * Get test name.
132  */
133  std::string getName() override{
134  return name;
135  }
136 
137  /**
138  * Get parameter name.
139  */
140  std::string getParameter() override{
141  return parameter;
142  }
143 
144 private:
145  double threshold; //!< threshold p-value to decide if test is passed.
146  double failuresThreshold; //!< threshold p-value to decide if test is passed.
147  char slice; //!< axis to slice.
148  const std::string options = ""; //!< options for the ROOT chi2 test.
149  const std::string name = "Chi2_2D"; //!< test name.
150  const std::string parameter = "p-Value(chi2)"; //!< parameter name.
151 };
152 
153 #endif
double threshold
threshold p-value to decide if test is passed.
void clear() override
Clear results.
const std::string options
options for the ROOT chi2 test.
std::istream & read(std::istream &in) override
Read test parameters from input.
Definition: JTestChi2_2D.hh:38
std::vector< JTestResult > results
Definition: JTest_t.hh:251
Definition: JRoot.hh:19
do set_array DAQHEADER JPrintDAQHeader f
Definition: JTuneHV.sh:79
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
double failuresThreshold
threshold p-value to decide if test is passed.
JTestChi2_2D()
Default constructor.
Definition: JTestChi2_2D.hh:30
data_type r[M+1]
Definition: JPolint.hh:742
std::ostream & write(std::ostream &out, std::string delimiter=" ", bool onlyFailures=false) const override
Write test result to output.
Definition: JTestChi2_2D.hh:51
char slice
axis to slice.
Structure containing the result of the test.
Definition: JTest_t.hh:164
std::string getParameter() override
Get parameter name.
const std::string name
test name.
do cat driver txt<< EOFevent ev_configure{RC_EVT%< ev_configure.txt > RC_DWRT path
Implementation of the Chi2 test for 2D histograms.
Definition: JTestChi2_2D.hh:21
#define ERROR(A)
Definition: JMessage.hh:66
void test(TObject *o1, TObject *o2) override
Applies Chi2 test for two ROOT TH2 histograms.
Definition: JTestChi2_2D.hh:70
virtual void save(TFile *f, std::string path, bool onlyFailures=false) override
Writes the test result to root file.
std::string getName() override
Get test name.
const std::string parameter
parameter name.
Interface to read input and write output for TObject tests.
Definition: JTest_t.hh:246
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 typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
JTestResult JChi2TestSlice(TH2 *h1, TH2 *h2, double threshold, double failuresThreshold, std::string testName, std::string parameterName, std::string options, char slice)
Chi2 test for sliced 2D histograms.
Definition: JTestChi2_t.hh:111