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 <iostream>
5 #include "JTest_t.hh"
6 #include "JTestChi2_t.hh"
7 
8 using namespace std;
9 
10 /*
11  * \author rgruiz
12  */
13 
14 /*
15  * Implementation of the Chi2 test for 2D histograms.
16  */
17 class JTestChi2_2D:
18  public JTestChi2_t,
19  public JTest_t
20 {
21 public:
22 
23  /*
24  * Default constructor.
25  */
27 
28  /*
29  * Read test parameters from input.
30  *
31  * \param in input stream
32  * \return input stream
33  */
34  std::istream& read(std::istream& in) {
35  return in >> threshold >> slice;
36  };
37 
38  /*
39  * Write test result to output.
40  *
41  * \param out output stream
42  * \return output stream
43  */
44  std::ostream& write(std::ostream& out) {
45  for (vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r) {
46  out << scientific << setprecision(2) << (r->passed ? GREEN : RED) << r->message << endl;
47  }
48  return out;
49  };
50 
51  /*
52  * Applies Chi2 test for two ROOT TH2 histograms.
53  *
54  * \param o1 First histogram
55  * \param o2 Second histogram
56  */
57  void test(TObject* o1, TObject* o2) {
58 
59  if (!(dynamic_cast<TH2*>(o1) == NULL) && !(dynamic_cast<TH2*>(o2) == NULL)) {
60 
61  TH2D* h1 = dynamic_cast<TH2D*>(o1);
62  TH2D* h2 = dynamic_cast<TH2D*>(o2);
63 
64  if(slice == 'x' || slice == 'X'){
65 
66  int nSlices1 = h1->GetNbinsX();
67  int nSlices2 = h2->GetNbinsX();
68 
69  if(nSlices1 != nSlices2)
70  ERROR("Histograms with different binning. The objects: " << o1->GetName() << " can not be compared." << endl);
71 
72  for (int i=1 ; i<=nSlices1 ; ++i){
73 
74  string name = MAKE_STRING(o1->GetName() + to_string("_") + to_string(i));
75 
76  TH1D* s1 = h1->ProjectionY (name.c_str(),i,i);
77  TH1D* s2 = h2->ProjectionY (name.c_str(),i,i);
78 
79  JTestResult r = JChi2Test(s1, s2, threshold);
80  cout << "name test " << r.obj->GetName() << endl;
81  results.push_back(r);
82  }
83  }else if (slice == 'y' || slice == 'Y'){
84 
85  int nSlices1 = h1->GetNbinsY();
86  int nSlices2 = h2->GetNbinsY();
87 
88  if(nSlices1 != nSlices2)
89  ERROR("Histograms with different binning. The objects: " << o1->GetName() << " can not be compared." << endl);
90 
91  for (int i=1 ; i<=nSlices1 ; ++i){
92 
93  string name = MAKE_STRING(o1->GetName() + to_string("_") + to_string(i));
94 
95  TH1D* s1 = h1->ProjectionX (name.c_str(),i,i);
96  TH1D* s2 = h2->ProjectionX (name.c_str(),i,i);
97 
98  JTestResult r = JChi2Test(s1, s2, threshold);
99  results.push_back(r);
100  }
101  }
102 
103  } else if (!(dynamic_cast<TH1*>(o1) == NULL) && !(dynamic_cast<TH1*>(o2) == NULL)) {
104  ERROR("For 1D histograms call JChi2_1D: " << o1->GetName() << endl);
105  }
106  };
107 
108  /*
109  * Writes the test result to root file
110  * \param f A ROOT file
111  * \param path Path in root file.
112  */
113  virtual void save(TFile* f , string path){
114 
115  for (vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r){
116 
117  if (f -> GetDirectory(path.c_str())==0) f->mkdir(path.c_str());
118  f->cd(path.c_str());
119  r->obj->Write();
120  }
121  }
122 
123  /*
124  * Clear results
125  */
126  void clear(){
127  results.clear();
128  }
129 
130 private:
131  double threshold; //!< threshold p-value to decide if test is passed.
132  char slice; //!< Axis to slice.
133 };
134 
135 #endif
double threshold
threshold p-value to decide if test is passed.
virtual void save(TFile *f, string path)
esac print_variable DETECTOR INPUT_FILE OUTPUT_FILE CDF for TYPE in
Definition: JSirene.sh:45
Definition: JRoot.hh:19
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
data_type r[M+1]
Definition: JPolint.hh:709
char slice
Axis to slice.
void test(TObject *o1, TObject *o2)
Definition: JTestChi2_2D.hh:57
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:699
then JPizza f
Definition: JPizza.sh:46
do cat driver txt<< EOFevent ev_configure{RC_EVT%< ev_configure.txt > RC_DWRT path
std::ostream & write(std::ostream &out)
Definition: JTestChi2_2D.hh:44
#define ERROR(A)
Definition: JMessage.hh:66
std::istream & read(std::istream &in)
Definition: JTestChi2_2D.hh:34
then echo n User name
Definition: JCookie.sh:45
std::string to_string(const T &value)
Convert value to string.
TObject * obj
Definition: JTest_t.hh:66