Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Public Attributes | Private Attributes | List of all members
JTestChi2_2D Class Reference

Implementation of the Chi2 test for 2D histograms. More...

#include <JTestChi2_2D.hh>

Inheritance diagram for JTestChi2_2D:
JTestChi2_t JTest_t

Public Member Functions

 JTestChi2_2D ()
 Default constructor. More...
 
std::istream & read (std::istream &in) override
 Read test parameters from input. More...
 
std::ostream & write (std::ostream &out, std::string delimiter=" ", bool onlyFailures=false) const override
 Write test result to output. More...
 
void test (TObject *o1, TObject *o2) override
 Applies Chi2 test for two ROOT TH2 histograms. More...
 
virtual void save (TFile *f, std::string path, bool onlyFailures=false) override
 Writes the test result to root file. More...
 
void clear () override
 Clear results. More...
 
std::string getName () override
 Get test name. More...
 
std::string getParameter () override
 Get parameter name. More...
 
JTestResult JChi2Test (TH1 *h1, TH1 *h2, double threshold, std::string testName, std::string parameterName, std::string options)
 Chi2 test for 1D histograms. More...
 
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. More...
 
JTestResult JChi2TestBin_2D (TH2 *h1, TH2 *h2, double outliersThreshold, double chi2Threshold, std::string testName, std::string parameterName)
 Bin-by-Bin Chi2 comparison of 2D histograms. More...
 

Public Attributes

std::vector< JTestResultresults
 

Private Attributes

double threshold
 threshold p-value to decide if test is passed. More...
 
double failuresThreshold
 threshold p-value to decide if test is passed. More...
 
char slice
 axis to slice. More...
 
const std::string options = ""
 options for the ROOT chi2 test. More...
 
const std::string name = "Chi2_2D"
 test name. More...
 
const std::string parameter = "p-Value(chi2)"
 parameter name. More...
 

Detailed Description

Implementation of the Chi2 test for 2D histograms.

Author
rgruiz
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.
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.
The input parameter threshold(), is used to evaluate whether the test is passed or failed for each slice.
The evaluation is done by comparing the threshold() value with the result produced by JChi2Test(). The output of JChi2Test() is a p-value.
The parameter threshold() should therefore be a real value between 0 and 1.

Definition at line 21 of file JTestChi2_2D.hh.

Constructor & Destructor Documentation

JTestChi2_2D::JTestChi2_2D ( )
inline

Default constructor.

Definition at line 30 of file JTestChi2_2D.hh.

30 {}

Member Function Documentation

std::istream& JTestChi2_2D::read ( std::istream &  in)
inlineoverridevirtual

Read test parameters from input.

Parameters
ininput stream
Returns
input stream

Implements JTest_t.

Definition at line 38 of file JTestChi2_2D.hh.

38  {
39  return in >> threshold >> failuresThreshold >> slice;
40  };
double threshold
threshold p-value to decide if test is passed.
double failuresThreshold
threshold p-value to decide if test is passed.
char slice
axis to slice.
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
std::ostream& JTestChi2_2D::write ( std::ostream &  out,
std::string  delimiter = " ",
bool  onlyFailures = false 
) const
inlineoverridevirtual

Write test result to output.

Parameters
outoutput stream
delimiterfield delimiter
onlyFailuresIf true, write only failures.
Returns
output stream

Implements JTest_t.

Definition at line 51 of file JTestChi2_2D.hh.

51  {
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  };
std::vector< JTestResult > results
Definition: JTest_t.hh:251
data_type r[M+1]
Definition: JPolint.hh:742
void JTestChi2_2D::test ( TObject o1,
TObject o2 
)
inlineoverridevirtual

Applies Chi2 test for two ROOT TH2 histograms.

Parameters
o1First histogram
o2Second histogram

Implements JTest_t.

Definition at line 70 of file JTestChi2_2D.hh.

70  {
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  };
double threshold
threshold p-value to decide if test is passed.
const std::string options
options for the ROOT chi2 test.
std::vector< JTestResult > results
Definition: JTest_t.hh:251
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
double failuresThreshold
threshold p-value to decide if test is passed.
data_type r[M+1]
Definition: JPolint.hh:742
char slice
axis to slice.
Structure containing the result of the test.
Definition: JTest_t.hh:164
const std::string name
test name.
#define ERROR(A)
Definition: JMessage.hh:66
const std::string parameter
parameter name.
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
virtual void JTestChi2_2D::save ( TFile *  f,
std::string  path,
bool  onlyFailures = false 
)
inlineoverridevirtual

Writes the test result to root file.

Parameters
fA ROOT file
pathPath in root file.
onlyFailuresIf true, write only failures.

Implements JTest_t.

Definition at line 102 of file JTestChi2_2D.hh.

102  {
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  }
std::vector< JTestResult > results
Definition: JTest_t.hh:251
do set_array DAQHEADER JPrintDAQHeader f
Definition: JTuneHV.sh:79
data_type r[M+1]
Definition: JPolint.hh:742
do cat driver txt<< EOFevent ev_configure{RC_EVT%< ev_configure.txt > RC_DWRT path
void JTestChi2_2D::clear ( )
inlineoverridevirtual

Clear results.

Implements JTest_t.

Definition at line 126 of file JTestChi2_2D.hh.

126  {
127  results.clear();
128  }
std::vector< JTestResult > results
Definition: JTest_t.hh:251
std::string JTestChi2_2D::getName ( )
inlineoverridevirtual

Get test name.

Implements JTest_t.

Definition at line 133 of file JTestChi2_2D.hh.

133  {
134  return name;
135  }
const std::string name
test name.
std::string JTestChi2_2D::getParameter ( )
inlineoverridevirtual

Get parameter name.

Implements JTest_t.

Definition at line 140 of file JTestChi2_2D.hh.

140  {
141  return parameter;
142  }
const std::string parameter
parameter name.
JTestResult JTestChi2_t::JChi2Test ( TH1 *  h1,
TH1 *  h2,
double  threshold,
std::string  testName,
std::string  parameterName,
std::string  options 
)
inlineinherited

Chi2 test for 1D histograms.


The input parameter threshold, is used to evaluate whether the test is passed or failed.
The evaluation is done by comparing the threshold value with the result of the Chi2 test. The output of a Chi2 test is a p-value.
The parameter threshold should therefore be a real value between 0 and 1.

Parameters
h1First histogram
h2Second histogram
thresholdThreshold value for the test result
parameterNameName of the parameter used to test the histograms
testNameName of the test used to compare the histograms
optionsROOT options for the test.
Returns
Test result.

Definition at line 39 of file JTestChi2_t.hh.

39  {
40 
41  using namespace std;
42  using namespace JPP;
43 
44  if(h1 -> GetNbinsX() != h2 -> GetNbinsX())
45  ERROR("Histograms with different bining. The objects: " << h1 -> GetName() << " can not be compared." << endl);
46 
47  double chi2 = h1 -> Chi2Test (h2 , options.c_str());
48 
49  double M = h1->Integral();
50  double N = h2->Integral();
51 
52  TH2D* h3 = (TH2D*)h1->Clone(h1->GetName()==h2->GetName() ?
53  MAKE_CSTRING(to_string(h1->GetName())) :
54  MAKE_CSTRING(to_string(h1->GetName()) + "_VS_" + to_string(h2->GetName())));
55 
56  h3->Reset();
57 
58  for (int i=1 ; i < h1->GetNbinsX() ; ++i){
59 
60  double m = h1->GetBinContent(i);
61  double n = h2->GetBinContent(i);
62  if(n!=0 && m!=0){
63 
64  double c = (M*n - N*m)/sqrt((n+m)*(N*M));
65  h3->SetBinContent(i,c);
66  }
67  }
68 
69  bool passed;
70 
71  if (options.find("CHI2") != std::string::npos) {
72  (chi2 > threshold ? passed = false : passed = true);
73  }else{
74  (chi2 < threshold ? passed = false : passed = true);
75  }
76 
77  JResultTitle title(testName, parameterName, passed , chi2);
78 
79  h3->SetTitle(title.getTitle().c_str());
80 
81  JTestResult r (testName,
82  string (h1->GetDirectory()->GetPath()).append(h1->GetName()),
83  string (h2->GetDirectory()->GetPath()).append(h2->GetName()),
84  h1->GetDirectory()->GetFile()->GetName(),
85  h2->GetDirectory()->GetFile()->GetName(),
86  parameterName, chi2, threshold, h3, passed);
87 
88  return r;
89 
90  };
do $JPP JMEstimator M
Definition: JMEstimator.sh:37
Class dedicated to standardize the title of the graphical objects produced by the JTest_t() derived c...
Definition: JTest_t.hh:22
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:151
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
data_type r[M+1]
Definition: JPolint.hh:742
Structure containing the result of the test.
Definition: JTest_t.hh:164
then print u2 $script< option > print u2 Possible options
#define ERROR(A)
Definition: JMessage.hh:66
alias put_queue eval echo n
Definition: qlib.csh:19
std::string to_string(const T &value)
Convert value to string.
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37
JTestResult JTestChi2_t::JChi2TestSlice ( TH2 *  h1,
TH2 *  h2,
double  threshold,
double  failuresThreshold,
std::string  testName,
std::string  parameterName,
std::string  options,
char  slice 
)
inlineinherited

Chi2 test for sliced 2D histograms.


The histograms are sliced along the axis specified by the slice parameter. A slice per bin is made.
For each of the slices, the input parameter threshold, is used to evaluate whether the test is passed or failed.
The evaluation is done by comparing the threshold value with the result of the Chi2 test. The output of a Chi2 test is a p-value.
The parameter threshold should therefore be a real value between 0 and 1.
The fraction of failed tests is compared to the input parameter failuresThreshold. If this fraction is larger than failuresThreshold, the test fails.

Parameters
h1First histogram
h2Second histogram
thresholdThreshold value for the test result
failuresThresholdThreshold value for the fraction of failed tests.
parameterNameName of the parameter used to test the histograms
testNameName of the test used to compare the histograms
optionsROOT options for the test.
sliceThe axis along which the histogram is sliced.
Returns
Test result.

Definition at line 111 of file JTestChi2_t.hh.

111  {
112 
113  using namespace std;
114  using namespace JPP;
115 
116  int nFailures = 0;
117 
118  JTestResult r;
119 
120  if(slice == 'x' || slice == 'X'){
121 
122  int nSlices1 = h1->GetNbinsX();
123  int nSlices2 = h2->GetNbinsX();
124 
125  TH1* h3 = h1->ProjectionX(h1->GetName()==h2->GetName() ?
126  MAKE_CSTRING(to_string(h1->GetName())) :
127  MAKE_CSTRING(to_string(h1->GetName()) + "_VS_" + to_string(h2->GetName())));
128 
129  if(nSlices1 != nSlices2)
130  ERROR("Histograms with different binning. The objects: " << h1->GetName() << " and " << h2->GetName() << " can not be compared." << endl);
131 
132  for (int i=1 ; i<=nSlices1 ; ++i){
133 
134  std::string sliceName = MAKE_STRING(h1->GetName() + to_string("_") + to_string(i));
135 
136  TH1D* s1 = h1->ProjectionY (sliceName.c_str(),i,i);
137  TH1D* s2 = h2->ProjectionY (sliceName.c_str(),i,i);
138 
139  double chi2 = s1 -> Chi2Test (s2 , options.c_str());
140 
141  bool passed;
142 
143  if (options.find("CHI2") != std::string::npos) {
144  (chi2 > threshold ? passed = false : passed = true);
145  }else{
146  (chi2 < threshold ? passed = false : passed = true);
147  }
148 
149  if (!passed) nFailures++;
150 
151  h3->SetBinContent(i,chi2);
152 
153  }
154 
155  bool passed;
156 
157  (nFailures/nSlices1 > failuresThreshold ? passed = false : passed = true);
158 
159  JResultTitle title(testName, parameterName, passed , nFailures);
160 
161  h3->SetTitle(title.getTitle().c_str());
162 
163  r = JTestResult (testName,
164  string (h1->GetDirectory()->GetPath()).append(h1->GetName()),
165  string (h2->GetDirectory()->GetPath()).append(h2->GetName()),
166  h1->GetDirectory()->GetFile()->GetName(),
167  h2->GetDirectory()->GetFile()->GetName(),
168  parameterName, nFailures, failuresThreshold, h3, passed);
169 
170  }else if (slice == 'y' || slice == 'Y'){
171 
172  int nSlices1 = h1->GetNbinsY();
173  int nSlices2 = h2->GetNbinsY();
174 
175  TH1* h3 = h1->ProjectionY(h1->GetName()==h2->GetName() ?
176  MAKE_CSTRING(to_string(h1->GetName())) :
177  MAKE_CSTRING(to_string(h1->GetName()) + "_VS_" + to_string(h2->GetName())));
178 
179  if(nSlices1 != nSlices2)
180  ERROR("Histograms with different binning. The objects: " << h1->GetName() << " can not be compared." << endl);
181 
182  for (int i=1 ; i<=nSlices1 ; ++i){
183 
184  std::string sliceName = MAKE_STRING(h1->GetName() + to_string("_") + to_string(i));
185 
186  TH1D* s1 = h1->ProjectionX (sliceName.c_str(),i,i);
187  TH1D* s2 = h2->ProjectionX (sliceName.c_str(),i,i);
188 
189  double chi2 = s1 -> Chi2Test (s2 , options.c_str());
190 
191  bool passed;
192 
193  if (options.find("CHI2") != std::string::npos) {
194  (chi2 > threshold ? passed = false : passed = true);
195  }else{
196  (chi2 < threshold ? passed = false : passed = true);
197  }
198 
199  if (!passed) nFailures++;
200 
201  h3->SetBinContent(i,chi2);
202  }
203 
204  bool passed;
205 
206  (nFailures/nSlices1 > failuresThreshold ? passed = false : passed = true);
207 
208  JResultTitle title(testName, parameterName, passed , nFailures);
209 
210  h3->SetTitle(title.getTitle().c_str());
211 
212  r = JTestResult (testName,
213  string (h1->GetDirectory()->GetPath()).append(h1->GetName()),
214  string (h2->GetDirectory()->GetPath()).append(h2->GetName()),
215  h1->GetDirectory()->GetFile()->GetName(),
216  h2->GetDirectory()->GetFile()->GetName(),
217  parameterName, nFailures, failuresThreshold, h3, passed);
218 
219  }
220 
221  return r;
222  };
Class dedicated to standardize the title of the graphical objects produced by the JTest_t() derived c...
Definition: JTest_t.hh:22
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:151
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
data_type r[M+1]
Definition: JPolint.hh:742
Structure containing the result of the test.
Definition: JTest_t.hh:164
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
then print u2 $script< option > print u2 Possible options
#define ERROR(A)
Definition: JMessage.hh:66
std::string to_string(const T &value)
Convert value to string.
JTestResult JTestChi2_t::JChi2TestBin_2D ( TH2 *  h1,
TH2 *  h2,
double  outliersThreshold,
double  chi2Threshold,
std::string  testName,
std::string  parameterName 
)
inlineinherited

Bin-by-Bin Chi2 comparison of 2D histograms.


The Chi distance between h1 and h2 is calculated for each bin, and compared to the chi2Threshold() parameter.
If the calculated Chi distance is above this threshold, the test is passed for that bin.
If the fraction of failures is above the input parameter outliersThreshold(), the test is failed.

Parameters
h1First object
h2Second object
outliersThresholdfraction of bins allowed to fail the test
chi2Thresholdp-value
parameterNameName of the parameter used to test the histograms
testNameName of the test used to compare the histograms
Returns
Test result.

Definition at line 240 of file JTestChi2_t.hh.

240  {
241 
242  using namespace std;
243  using namespace JPP;
244 
245  int nx1 = h1->GetNbinsX();
246  int nx2 = h2->GetNbinsX();
247  int ny1 = h1->GetNbinsY();
248  int ny2 = h2->GetNbinsY();
249 
250  double M = h1->Integral();
251  double N = h2->Integral();
252 
253  if(nx1 != nx2 || ny1 != ny2 || M == 0 || N == 0)
254  ERROR("Histograms with different binning. The objects: " << h1->GetName() << " can not be compared." << endl);
255 
256  TH2D* h3 = (TH2D*)h1->Clone(h1->GetName()==h2->GetName() ?
257  MAKE_CSTRING(to_string(h1->GetName())) :
258  MAKE_CSTRING(to_string(h1->GetName()) + "_VS_" + to_string(h2->GetName())));
259 
260  h3->Reset();
261 
262  double outliers = 0;
263 
264  for (int i=1 ; i<nx1 ; ++i){
265  for (int j=1 ; j<ny1 ; ++j){
266 
267  double m = h1 -> GetBinContent(i,j);
268  double n = h2 -> GetBinContent(i,j);
269  double chi2 = (n-m*N/M)/sqrt(m*N/M);
270  (fabs(chi2) > chi2Threshold ? outliers+=1./(nx1*ny1) : outliers+=0 );
271  h3->SetBinContent(i,j,chi2);
272  }
273  }
274 
275  bool passed;
276 
277  (outliers > outliersThreshold ? passed = false : passed = true);
278 
279  JResultTitle title(testName, parameterName , passed , 100*outliers);
280 
281  h3->SetTitle(title.getTitle().c_str());
282 
283  JTestResult r (testName,
284  string (h1->GetDirectory()->GetPath()).append(h1->GetName()),
285  string (h2->GetDirectory()->GetPath()).append(h2->GetName()),
286  h1->GetDirectory()->GetFile()->GetName(),
287  h2->GetDirectory()->GetFile()->GetName(),
288  parameterName, 100*outliers, 100*outliersThreshold, h3, passed);
289 
290  return r;
291  };
do $JPP JMEstimator M
Definition: JMEstimator.sh:37
Class dedicated to standardize the title of the graphical objects produced by the JTest_t() derived c...
Definition: JTest_t.hh:22
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:151
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
data_type r[M+1]
Definition: JPolint.hh:742
Structure containing the result of the test.
Definition: JTest_t.hh:164
#define ERROR(A)
Definition: JMessage.hh:66
alias put_queue eval echo n
Definition: qlib.csh:19
std::string to_string(const T &value)
Convert value to string.
int j
Definition: JPolint.hh:666
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37

Member Data Documentation

double JTestChi2_2D::threshold
private

threshold p-value to decide if test is passed.

Definition at line 145 of file JTestChi2_2D.hh.

double JTestChi2_2D::failuresThreshold
private

threshold p-value to decide if test is passed.

Definition at line 146 of file JTestChi2_2D.hh.

char JTestChi2_2D::slice
private

axis to slice.

Definition at line 147 of file JTestChi2_2D.hh.

const std::string JTestChi2_2D::options = ""
private

options for the ROOT chi2 test.

Definition at line 148 of file JTestChi2_2D.hh.

const std::string JTestChi2_2D::name = "Chi2_2D"
private

test name.

Definition at line 149 of file JTestChi2_2D.hh.

const std::string JTestChi2_2D::parameter = "p-Value(chi2)"
private

parameter name.

Definition at line 150 of file JTestChi2_2D.hh.

std::vector<JTestResult> JTest_t::results
inherited

Buffer to store results of multiple tests.

Definition at line 251 of file JTest_t.hh.


The documentation for this class was generated from the following file: