Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTestChi2R_2D.hh
Go to the documentation of this file.
1 #ifndef __JTESTCHI2R_2D__
2 #define __JTESTCHI2R_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  */
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 >> slice;
40  };
41 
42  /**
43  * Write test result to output.
44  *
45  * \param out output stream
46  * \param delimiter field delimiter
47  * \param onlyFailures If true, write only failures.
48  * \return output stream
49  */
50  std::ostream& write(std::ostream& out, std::string delimiter = " ", bool onlyFailures = false) const override{
51  for (std::vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r) {
52  if (onlyFailures){
53  if (!r->passed)
54  out << std::scientific << std::setprecision(2) << (r->passed ? JPP::GREEN : JPP::RED) << r->print(delimiter) << std::endl;
55  }else{
56  out << std::scientific << std::setprecision(2) << (r->passed ? JPP::GREEN : JPP::RED) << r->print(delimiter) << std::endl;
57  }
58  }
59  return out;
60  };
61 
62  /**
63  * Applies Chi2 test for two ROOT TH2 histograms.
64  *
65  * \param o1 First histogram
66  * \param o2 Second histogram
67  */
68  void test(TObject* o1, TObject* o2) override{
69 
70  using namespace std;
71  using namespace JPP;
72 
73  if (!(dynamic_cast<TH2*>(o1) == NULL) && !(dynamic_cast<TH2*>(o2) == NULL)) {
74 
75  TH2D* h1 = dynamic_cast<TH2D*>(o1);
76  TH2D* h2 = dynamic_cast<TH2D*>(o2);
77 
78  if(slice == 'x' || slice == 'X' || slice == 'y' || slice == 'Y'){
79 
81 
82  results.push_back(r);
83 
84  } else {
85  ERROR ("Slice option should be: x, X, y or Y, and not " + slice);
86  }
87 
88  } else if (!(dynamic_cast<TH1*>(o1) == NULL) && !(dynamic_cast<TH1*>(o2) == NULL)) {
89  ERROR("For 1D histograms call JChi2_1D: " << o1->GetName() << endl);
90  }
91  };
92 
93  /**
94  * Writes the test result to root file
95  * \param f A ROOT file
96  * \param path Path in root file.
97  * \param onlyFailures If true, write only failures.
98  */
99  virtual void save(TFile* f , std::string path, bool onlyFailures = false) override{
100 
101  using namespace std;
102 
103  for (vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r){
104 
105  if (onlyFailures){
106  if (!r->passed){
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  }else{
112  if (f -> GetDirectory(path.c_str())==0) f->mkdir(path.c_str());
113  f->cd(path.c_str());
114  r->obj->Write();
115  }
116  }
117  }
118 
119  /**
120  * Clear results
121  */
122  void clear() override{
123  results.clear();
124  }
125 
126  /**
127  * Get test name.
128  */
129  std::string getName() override{
130  return name;
131  }
132 
133  /**
134  * Get parameter name.
135  */
136  std::string getParameter() override{
137  return parameter;
138  }
139 
140 private:
141  char slice; //!< axis to slice.
142  double threshold; //!< threshold p-value to decide if test is passed.
143  double failuresThreshold; //!< threshold p-value to decide if test is passed.
144  const std::string name = "Chi2R_2D"; //!< test name.
145  const std::string options = "CHI2/NDF"; //!< options for the ROOT chi2 test.
146  const std::string parameter = "chi2/NDF"; //!< parameter name.
147 };
148 
149 #endif
virtual void save(TFile *f, std::string path, bool onlyFailures=false) override
Writes the test result to root file.
std::ostream & write(std::ostream &out, std::string delimiter=" ", bool onlyFailures=false) const override
Write test result to output.
const std::string parameter
parameter name.
std::vector< JTestResult > results
Definition: JTest_t.hh:251
std::istream & read(std::istream &in) override
Read test parameters from input.
Definition: JRoot.hh:19
do set_array DAQHEADER JPrintDAQHeader f
Definition: JTuneHV.sh:79
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
data_type r[M+1]
Definition: JPolint.hh:742
const std::string options
options for the ROOT chi2 test.
void clear() override
Clear results.
Structure containing the result of the test.
Definition: JTest_t.hh:164
double failuresThreshold
threshold p-value to decide if test is passed.
do cat driver txt<< EOFevent ev_configure{RC_EVT%< ev_configure.txt > RC_DWRT path
#define ERROR(A)
Definition: JMessage.hh:66
JTestChi2R_2D()
Default constructor.
std::string getName() override
Get test name.
double threshold
threshold p-value to decide if test is passed.
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
std::string getParameter() override
Get parameter name.
const std::string name
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
Implementation of the Chi2 test for 2D histograms.
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
void test(TObject *o1, TObject *o2) override
Applies Chi2 test for two ROOT TH2 histograms.
char slice
axis to slice.