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

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

#include <JTestRuns_2D.hh>

Inheritance diagram for JCOMPAREHISTOGRAMS::JTestRuns_2D:
JCOMPAREHISTOGRAMS::JTest_t JCOMPAREHISTOGRAMS::JTestRuns_t

Public Member Functions

 JTestRuns_2D ()
 
std::istream & read (std::istream &in) override
 Read test parameters from input. More...
 
void test (TObject *o1, TObject *o2) override
 Tests the statistical compatibility of two ROOT TObjects. More...
 
std::ostream & write (std::ostream &out, const char delimiter= ' ', const bool onlyFailures=false) const
 Write test result to output. More...
 
virtual void save (TFile *f, const std::string &path, const bool onlyFailures=false)
 Writes the test result to root file. More...
 
virtual void clear ()
 Clear results. More...
 
const std::stringgetTestName ()
 Get test name. More...
 
const std::stringgetResultType ()
 Get result type. More...
 
JTestResult JRunsTest (TH1 *h1, TH1 *h2, double threshold, std::string testName, std::string parameterName)
 Implements the Wald-Wolfowitx runs test: https://en.wikipedia.org/wiki/Wald%E2%80%93Wolfowitz_runs_test In this, an expected number of runs and a standard deviation are computed from the number of bins and the number of "aboves" and "belows". More...
 
JTestResult JRunsTestSlice (TH2 *h1, TH2 *h2, double threshold, double failuresThreshold, std::string testName, std::string parameterName, char slice)
 Runs test for sliced 2D histograms. More...
 

Public Attributes

std::vector< JTestResultresults
 Buffer to store results of multiple tests.*/. More...
 

Protected Attributes

const std::string testName
 test name More...
 
const std::string resultType
 result type More...
 

Private Attributes

double threshold
 threshold value to decide if test is passed. More...
 
double failuresThreshold
 threshold value to decide if test is passed. More...
 
char slice
 Axis to slice. x or X for x-axis, y or Y for y-axis, n or N for None. More...
 

Detailed Description

Implementation of the Chi2 test for 2D histograms.


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 JRunsTest() test is applied to each slice.
This test uses the input parameter threshold() to evaluate whether the test is passed or failed for each slice.
The evaluation is done by comparing the threshold value with the result of the JRunsTest() test.
The results for each slice are stored in the results buffer (see JTest_t().)

Definition at line 26 of file JTestRuns_2D.hh.

Constructor & Destructor Documentation

JCOMPAREHISTOGRAMS::JTestRuns_2D::JTestRuns_2D ( )
inline

Definition at line 32 of file JTestRuns_2D.hh.

32  :
33  JTest_t("Runs_2D", "#sigma"),
34  JTestRuns_t()
35  {}
JTestRuns_t()
Default constructor.
Definition: JTestRuns_t.hh:26
JTest_t(const std::string &name, const std::string &type)
Constructor.
Definition: JTest_t.hh:57

Member Function Documentation

std::istream& JCOMPAREHISTOGRAMS::JTestRuns_2D::read ( std::istream &  in)
inlineoverridevirtual

Read test parameters from input.

Parameters
ininput stream
Returns
input stream

Implements JCOMPAREHISTOGRAMS::JTest_t.

Definition at line 43 of file JTestRuns_2D.hh.

43  {
44 
45  in >> threshold >> slice;
46 
47  if (!(slice == 'x' || slice == 'X' || slice == 'y' || slice == 'Y')) {
48  THROW(JLANG::JValueOutOfRange, "JTestRuns_2D::read(): Invalid slice option \'" << slice << "\'");
49  }
50 
51  return in;
52  };
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
double threshold
threshold value to decide if test is passed.
Definition: JTestRuns_2D.hh:81
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
char slice
Axis to slice. x or X for x-axis, y or Y for y-axis, n or N for None.
Definition: JTestRuns_2D.hh:86
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 JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
void JCOMPAREHISTOGRAMS::JTestRuns_2D::test ( TObject o1,
TObject o2 
)
inlineoverridevirtual

Tests the statistical compatibility of two ROOT TObjects.

Parameters
o1First object
o2Second object

Implements JCOMPAREHISTOGRAMS::JTest_t.

Definition at line 60 of file JTestRuns_2D.hh.

60  {
61 
62  using namespace std;
63  using namespace JPP;
64 
65  if (!(slice == 'x' || slice == 'X' || slice == 'y' || slice == 'Y')) {
66  THROW(JLANG::JValueOutOfRange, "JTestRuns_2D::test(): Invalid slice option \'" << slice << "\'");
67  }
68 
69  if (!(dynamic_cast<TH2*>(o1) == NULL) && !(dynamic_cast<TH2*>(o2) == NULL)) {
70 
71  TH2D* h1 = dynamic_cast<TH2D*>(o1);
72  TH2D* h2 = dynamic_cast<TH2D*>(o2);
73 
75 
76  results.push_back(r);
77 
78  } else if (!(dynamic_cast<TH1*>(o1) == NULL) && !(dynamic_cast<TH1*>(o2) == NULL)) {
79  THROW(JLANG::JValueOutOfRange, "JTestRuns_2D::test(): For 1D histograms call JChi2_1D: " << o1->GetName() << endl);
80  }
81  };
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
data_type r[M+1]
Definition: JPolint.hh:779
std::vector< JTestResult > results
Buffer to store results of multiple tests.*/.
Definition: JTest_t.hh:162
const std::string resultType
result type
Definition: JTest_t.hh:167
double threshold
threshold value to decide if test is passed.
Definition: JTestRuns_2D.hh:81
const std::string testName
test name
Definition: JTest_t.hh:166
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
double failuresThreshold
threshold value to decide if test is passed.
Definition: JTestRuns_2D.hh:85
JTestResult JRunsTestSlice(TH2 *h1, TH2 *h2, double threshold, double failuresThreshold, std::string testName, std::string parameterName, char slice)
Runs test for sliced 2D histograms.
Definition: JTestRuns_t.hh:114
char slice
Axis to slice. x or X for x-axis, y or Y for y-axis, n or N for None.
Definition: JTestRuns_2D.hh:86
std::ostream& JCOMPAREHISTOGRAMS::JTest_t::write ( std::ostream &  out,
const char  delimiter = ' ',
const bool  onlyFailures = false 
) const
inlineinherited

Write test result to output.

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

Definition at line 81 of file JTest_t.hh.

83  {
84 
85  for (std::vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r) {
86 
87  if (onlyFailures && r->passed) {
88  continue;
89  }
90 
91  print(out, *r, delimiter, true);
92  }
93 
94  return out;
95  }
std::ostream & print(std::ostream &out, const JTestSummary &summary, const char delimiter= ' ', const bool useColors=true)
Print test summary.
data_type r[M+1]
Definition: JPolint.hh:779
std::vector< JTestResult > results
Buffer to store results of multiple tests.*/.
Definition: JTest_t.hh:162
virtual void JCOMPAREHISTOGRAMS::JTest_t::save ( TFile *  f,
const std::string path,
const bool  onlyFailures = false 
)
inlinevirtualinherited

Writes the test result to root file.

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

Definition at line 113 of file JTest_t.hh.

115  {
116 
117  using namespace std;
118 
119  if (f -> GetDirectory(path.c_str())==0) {
120  f->mkdir(path.c_str());
121  }
122 
123  f->cd(path.c_str());
124 
125  for (vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r){
126 
127  if (!onlyFailures || !r->passed){
128  r->obj->Write();
129  }
130  }
131  }
o $QUALITY_ROOT d $DEBUG!CHECK_EXIT_CODE JPlot1D f
Definition: JDataQuality.sh:76
data_type r[M+1]
Definition: JPolint.hh:779
std::vector< JTestResult > results
Buffer to store results of multiple tests.*/.
Definition: JTest_t.hh:162
$WORKDIR driver txt done cat $WORKDIR driver txt<< EOFprocess ${DATAFILTER}$FILTER_HOST csh-c '(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&(JDataFilter-H\$SERVER\$-M\$LOGGER\$-d $DEBUG-u ${DATAFILTER}-P $PORT</dev/null > &/dev/null &))';process ${DATAWRITER}$WRITER_HOST csh-c '(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&(JDataWriter-H\$SERVER\$-M\$LOGGER\$-d $DEBUG-u ${DATAWRITER}</dev/null > &/dev/null &))';print enterevent ev_init{RC_CMD}event ev_reset{RC_CMD}event ev_init{RC_CMD}event ev_configure{RC_DFLTR%<$WORKDIR/ev_configure_datafilter.txt > RC_DQSIM<$WORKDIR/ev_configure_dqsimulator.txt > RC_DWRT path
virtual void JCOMPAREHISTOGRAMS::JTest_t::clear ( )
inlinevirtualinherited

Clear results.

Definition at line 137 of file JTest_t.hh.

137  {
138  results.clear();
139  }
std::vector< JTestResult > results
Buffer to store results of multiple tests.*/.
Definition: JTest_t.hh:162
const std::string& JCOMPAREHISTOGRAMS::JTest_t::getTestName ( )
inlineinherited

Get test name.

Returns
test name

Definition at line 147 of file JTest_t.hh.

147  {
148  return testName;
149  }
const std::string testName
test name
Definition: JTest_t.hh:166
const std::string& JCOMPAREHISTOGRAMS::JTest_t::getResultType ( )
inlineinherited

Get result type.

Returns
result type

Definition at line 157 of file JTest_t.hh.

157  {
158  return resultType;
159  }
const std::string resultType
result type
Definition: JTest_t.hh:167
JTestResult JCOMPAREHISTOGRAMS::JTestRuns_t::JRunsTest ( TH1 *  h1,
TH1 *  h2,
double  threshold,
std::string  testName,
std::string  parameterName 
)
inlineinherited

Implements the Wald-Wolfowitx runs test: https://en.wikipedia.org/wiki/Wald%E2%80%93Wolfowitz_runs_test In this, an expected number of runs and a standard deviation are computed from the number of bins and the number of "aboves" and "belows".

The test returns the difference between the observed number of runs and the expected number of runs, expressed in standard deviations. This is compared to the threshold input parameter.

Parameters
h1First histogram
h2Second histogram
threshold
parameterNameName of the parameter used to test the histograms
testNameName of the test used to compare the histograms

Definition at line 40 of file JTestRuns_t.hh.

40  {
41 
42  using namespace std;
43  using namespace JPP;
44 
45  if(h1 -> GetNbinsX() != h2 -> GetNbinsX())
46  ERROR("Histograms with different bining. The objects: " << h1 -> GetName() << " can not be compared." << endl);
47 
48  double R = h1->Integral();
49  double T = h2->Integral();
50 
51  TH2D* h3 = (TH2D*)h1->Clone(h1->GetName()==h2->GetName() ?
52  MAKE_CSTRING(to_string(h1->GetName())) :
53  MAKE_CSTRING(to_string(h1->GetName()) + "_VS_" + to_string(h2->GetName())));
54 
55  for (int i=1 ; i<h1->GetNbinsX() ; ++i) {
56  h3->SetBinContent(i , (T/R)*h1->GetBinContent(i) - h2->GetBinContent(i));
57  }
58 
59  int n = 1;
60  double p = 0;
61  double q = 0;
62 
63  bool a = ((T/R)*h1->GetBinContent(1) - h2->GetBinContent(1)) < 0;
64 
65  (a ? p++ : q++);
66 
67  for (int i = 2 ; i<h1->GetNbinsX() ; ++i){
68 
69  bool b = ((T/R)*h1->GetBinContent(i) - h2->GetBinContent(i)) < 0;
70 
71  (b ? p++ : q++);
72 
73  if (b != a){
74  n++;
75  a=b;
76  }
77  }
78 
79  const double N = 1 + 2*p*q/(p+q) ;
80  const double s = sqrt( 2*p*q*(2*p*q-p-q)/(p+q)/(p+q)/(p+q-1) );
81  const double d = (n-N)/s;
82 
83  const bool passed = (fabs(d) > threshold ? false : true);
84 
85  JResultTitle title(testName, parameterName , passed , fabs(d));
86 
87  h3->SetTitle(title.getTitle().c_str());
88 
89  JTestResult r (testName,
90  JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())),
91  JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())),
92  parameterName, fabs(d), threshold, h3, passed);
93 
94  return r;
95  };
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:136
data_type r[M+1]
Definition: JPolint.hh:779
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
const int n
Definition: JPolint.hh:697
then JCalibrateToT a
Definition: JTuneHV.sh:116
do set_variable OUTPUT_DIRECTORY $WORKDIR T
#define ERROR(A)
Definition: JMessage.hh:66
then JCookie sh JDataQuality D $DETECTOR_ID R
Definition: JDataQuality.sh:41
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JMuonPath.sh:47
std::string to_string(const T &value)
Convert value to string.
JTestResult JCOMPAREHISTOGRAMS::JTestRuns_t::JRunsTestSlice ( TH2 *  h1,
TH2 *  h2,
double  threshold,
double  failuresThreshold,
std::string  testName,
std::string  parameterName,
char  slice 
)
inlineinherited

Runs 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 Runs test.
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.
testNameName of the test used to compare the histograms
parameterNameName of the parameter used to test the histograms
sliceThe axis along which the histogram is sliced.
Returns
Test result.

Definition at line 114 of file JTestRuns_t.hh.

114  {
115 
116  using namespace std;
117  using namespace JPP;
118 
119  int nFailures = 0;
120 
121  JTestResult r;
122 
123  if(slice == 'x' || slice == 'X'){
124 
125  int nSlices1 = h1->GetNbinsX();
126  int nSlices2 = h2->GetNbinsX();
127 
128  TH1* h3 = h1->ProjectionX(MAKE_CSTRING(h1->GetName() << "_VS_" <<
129  h2->GetName() << "_RunsTestSliceX"));
130 
131  if (nSlices1 != nSlices2) {
132  ERROR("Histograms with different binning. The objects: " << h1->GetName() << " and " << h2->GetName() << " can not be compared." << endl);
133  }
134 
135  for (int i=1 ; i<=nSlices1 ; ++i){
136 
137  std::string sliceName = MAKE_STRING(h3->GetName() << "_" << to_string(i));
138 
139  TH1D* s1 = h1->ProjectionY (sliceName.c_str(),i,i);
140  TH1D* s2 = h2->ProjectionY (sliceName.c_str(),i,i);
141 
142  if (!(s1->GetSumOfWeights() > 0 && s2->GetSumOfWeights() > 0)) { continue; }
143 
144  double R = s1->Integral();
145  double T = s2->Integral();
146 
147  int n = 1;
148  double p = 0;
149  double q = 0;
150 
151  bool a = ((T/R)*s1->GetBinContent(1) - s2->GetBinContent(1)) < 0;
152 
153  (a ? p++ : q++);
154 
155  for (int i = 2 ; i<s1->GetNbinsX() ; ++i){
156 
157  bool b = ((T/R)*s1->GetBinContent(i) - s2->GetBinContent(i)) < 0;
158 
159  (b ? p++ : q++);
160 
161  if (b != a){
162  n++;
163  a=b;
164  }
165  }
166 
167  double N = 1 + 2*p*q/(p+q) ;
168  double s = sqrt( 2*p*q*(2*p*q-p-q)/(p+q)/(p+q)/(p+q-1) );
169  double d = (n-N)/s;
170 
171  bool passed = (fabs(d) > threshold ? false : true);
172 
173  if (!passed) nFailures++;
174 
175  h3->SetBinContent(i,fabs(d));
176 
177  }
178 
179  bool passed = (nFailures/nSlices1 > failuresThreshold ? false : true);
180 
181  JResultTitle title(testName, parameterName, passed , nFailures);
182 
183  h3->SetTitle(title.getTitle().c_str());
184 
185  r = JTestResult(testName,
186  JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())),
187  JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())),
188  parameterName, nFailures, failuresThreshold, h3, passed);
189 
190  }else if (slice == 'y' || slice == 'Y'){
191 
192  int nSlices1 = h1->GetNbinsX();
193  int nSlices2 = h2->GetNbinsX();
194 
195  TH1* h3 = h1->ProjectionY(h1->GetName()==h2->GetName() ?
196  MAKE_CSTRING(to_string(h1->GetName())) :
197  MAKE_CSTRING(to_string(h1->GetName()) + "_VS_" + to_string(h2->GetName())));
198 
199  if(nSlices1 != nSlices2) {
200  ERROR("Histograms with different binning. The objects: " << h1->GetName() << " and " << h2->GetName() << " can not be compared." << endl);
201  }
202 
203  for (int i=1 ; i<=nSlices1 ; ++i){
204 
205  std::string sliceName = MAKE_STRING(h3->GetName() << "_" << to_string(i));
206 
207  TH1D* s1 = h1->ProjectionX (sliceName.c_str(),i,i);
208  TH1D* s2 = h2->ProjectionX (sliceName.c_str(),i,i);
209 
210  if (!(s1->GetSumOfWeights() > 0 && s2->GetSumOfWeights() > 0)) { continue; }
211 
212  double R = s1->Integral();
213  double T = s2->Integral();
214 
215  int n = 1;
216  double p = 0;
217  double q = 0;
218 
219  bool a = ((T/R)*s1->GetBinContent(1) - s2->GetBinContent(1)) < 0;
220 
221  (a ? p++ : q++);
222 
223  for (int i = 2 ; i<s1->GetNbinsX() ; ++i){
224 
225  bool b = ((T/R)*s1->GetBinContent(i) - s2->GetBinContent(i)) < 0;
226 
227  (b ? p++ : q++);
228 
229  if (b != a){
230  n++;
231  a=b;
232  }
233  }
234 
235  double N = 1 + 2*p*q/(p+q) ;
236  double s = sqrt( 2*p*q*(2*p*q-p-q)/(p+q)/(p+q)/(p+q-1) );
237  double d = (n-N)/s;
238 
239  bool passed = (fabs(d) > threshold ? false : true);
240 
241  if (!passed) nFailures++;
242 
243  h3->SetBinContent(i,fabs(d));
244 
245  }
246 
247  bool passed = (nFailures/nSlices1 > failuresThreshold ? false : true);
248 
249  JResultTitle title(testName, parameterName, passed , nFailures);
250 
251  h3->SetTitle(title.getTitle().c_str());
252 
253  r = JTestResult (testName,
254  JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())),
255  JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())),
256  parameterName, nFailures, failuresThreshold, h3, passed);
257 
258  }
259 
260  return r;
261  };
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:136
data_type r[M+1]
Definition: JPolint.hh:779
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:127
const int n
Definition: JPolint.hh:697
then JCalibrateToT a
Definition: JTuneHV.sh:116
do set_variable OUTPUT_DIRECTORY $WORKDIR T
#define ERROR(A)
Definition: JMessage.hh:66
then awk string
then JCookie sh JDataQuality D $DETECTOR_ID R
Definition: JDataQuality.sh:41
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JMuonPath.sh:47
std::string to_string(const T &value)
Convert value to string.

Member Data Documentation

double JCOMPAREHISTOGRAMS::JTestRuns_2D::threshold
private

threshold value to decide if test is passed.

Definition at line 81 of file JTestRuns_2D.hh.

double JCOMPAREHISTOGRAMS::JTestRuns_2D::failuresThreshold
private

threshold value to decide if test is passed.

Definition at line 85 of file JTestRuns_2D.hh.

char JCOMPAREHISTOGRAMS::JTestRuns_2D::slice
private

Axis to slice. x or X for x-axis, y or Y for y-axis, n or N for None.

Definition at line 86 of file JTestRuns_2D.hh.

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

Buffer to store results of multiple tests.*/.

Definition at line 162 of file JTest_t.hh.

const std::string JCOMPAREHISTOGRAMS::JTest_t::testName
protectedinherited

test name

Definition at line 166 of file JTest_t.hh.

const std::string JCOMPAREHISTOGRAMS::JTest_t::resultType
protectedinherited

result type

Definition at line 167 of file JTest_t.hh.


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