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
JTestRuns_1D Class Reference

Implementation of the Runs test for 1D histograms. More...

#include <JTestRuns_1D.hh>

Inheritance diagram for JTestRuns_1D:
JTest_t JTestRuns_t

Public Member Functions

 JTestRuns_1D ()
 
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
 Tests the statistical compatibility of two ROOT TObjects. 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 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
 

Private Attributes

double threshold
 threshold value to decide if test is passed. More...
 
const std::string name = "Runs_1D"
 test name. More...
 
const std::string parameter = "#sigma"
 parameter name. More...
 

Detailed Description

Implementation of the Runs test for 1D 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.
This test uses the input parameter threshold() to evaluate whether the test is passed or failed.
The evaluation is done by comparing the threshold value with the result of the JRunsTest() test.

Definition at line 19 of file JTestRuns_1D.hh.

Constructor & Destructor Documentation

JTestRuns_1D::JTestRuns_1D ( )
inline

Definition at line 25 of file JTestRuns_1D.hh.

25 {}

Member Function Documentation

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

Read test parameters from input.

Parameters
ininput stream
Returns
input stream

Implements JTest_t.

Definition at line 33 of file JTestRuns_1D.hh.

33  {
34  return in >> threshold;
35  };
double threshold
threshold value to decide if test is passed.
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& JTestRuns_1D::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 45 of file JTestRuns_1D.hh.

45  {
46  for (std::vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r) {
47  if (onlyFailures){
48  if (!r->passed)
49  out << std::scientific << std::setprecision(2) << (r->passed ? JPP::GREEN : JPP::RED) << r->print(delimiter) << std::endl;
50  }else{
51  out << std::scientific << std::setprecision(2) << (r->passed ? JPP::GREEN : JPP::RED) << r->print(delimiter) << std::endl;
52  }
53  }
54  return out;
55  };
std::vector< JTestResult > results
Definition: JTest_t.hh:251
data_type r[M+1]
Definition: JPolint.hh:742
void JTestRuns_1D::test ( TObject o1,
TObject o2 
)
inlineoverridevirtual

Tests the statistical compatibility of two ROOT TObjects.

Parameters
o1First object
o2Second object

Implements JTest_t.

Definition at line 63 of file JTestRuns_1D.hh.

63  {
64 
65  using namespace std;
66 
67  if (!(dynamic_cast<TH2*>(o1) == NULL) || !(dynamic_cast<TH2*>(o2) == NULL)) {
68  ERROR("For 2D histograms call JTestRuns_2D: " << o1->GetName() << endl);
69 
70  } else if (!(dynamic_cast<TH1*>(o1) == NULL) && !(dynamic_cast<TH1*>(o2) == NULL)) {
71 
72  TH1D* h1 = dynamic_cast<TH1D*>(o1);
73  TH1D* h2 = dynamic_cast<TH1D*>(o2);
74 
75  if(h1 -> GetNbinsX() != h2 -> GetNbinsX())
76  ERROR("Histograms with different bining. The objects: " << h1 -> GetName() << " can not be compared." << endl);
77 
79 
80  results.push_back(r);
81  }
82  };
double threshold
threshold value to decide if test is passed.
std::vector< JTestResult > results
Definition: JTest_t.hh:251
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
const std::string parameter
parameter name.
const std::string name
test name.
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_te...
Definition: JTestRuns_t.hh:34
virtual void JTestRuns_1D::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 90 of file JTestRuns_1D.hh.

90  {
91 
92  using namespace std;
93 
94  for (vector<JTestResult>::const_iterator r = results.begin() ; r != results.end() ; ++r){
95 
96  if (onlyFailures){
97  if (!r->passed){
98  if (f -> GetDirectory(path.c_str())==0) f->mkdir(path.c_str());
99  f->cd(path.c_str());
100  r->obj->Write();
101  }
102  }else{
103  if (f -> GetDirectory(path.c_str())==0) f->mkdir(path.c_str());
104  f->cd(path.c_str());
105  r->obj->Write();
106  }
107  }
108  }
std::vector< JTestResult > results
Definition: JTest_t.hh:251
data_type r[M+1]
Definition: JPolint.hh:742
then JPizza f
Definition: JPizza.sh:46
do cat driver txt<< EOFevent ev_configure{RC_EVT%< ev_configure.txt > RC_DWRT path
void JTestRuns_1D::clear ( )
inlineoverridevirtual

Clear results.

Implements JTest_t.

Definition at line 113 of file JTestRuns_1D.hh.

113  {
114  results.clear();
115  }
std::vector< JTestResult > results
Definition: JTest_t.hh:251
std::string JTestRuns_1D::getName ( )
inlineoverridevirtual

Get test name.

Implements JTest_t.

Definition at line 120 of file JTestRuns_1D.hh.

120  {
121  return name;
122  }
const std::string name
test name.
std::string JTestRuns_1D::getParameter ( )
inlineoverridevirtual

Get parameter name.

Implements JTest_t.

Definition at line 127 of file JTestRuns_1D.hh.

127  {
128  return parameter;
129  }
const std::string parameter
parameter name.
JTestResult 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 34 of file JTestRuns_t.hh.

34  {
35 
36  using namespace std;
37  using namespace JPP;
38 
39  if(h1 -> GetNbinsX() != h2 -> GetNbinsX())
40  ERROR("Histograms with different bining. The objects: " << h1 -> GetName() << " can not be compared." << endl);
41 
42  double R = h1->Integral();
43  double T = h2->Integral();
44 
45  TH2D* h3 = (TH2D*)h1->Clone(h1->GetName()==h2->GetName() ?
46  MAKE_CSTRING(to_string(h1->GetName())) :
47  MAKE_CSTRING(to_string(h1->GetName()) + "_VS_" + to_string(h2->GetName())));
48 
49  for (int i=1 ; i<h1->GetNbinsX() ; ++i) {
50  h3->SetBinContent(i , (T/R)*h1->GetBinContent(i) - h2->GetBinContent(i));
51  }
52 
53  int n = 1;
54  double p = 0;
55  double q = 0;
56 
57  bool a = ((T/R)*h1->GetBinContent(1) - h2->GetBinContent(1)) < 0;
58 
59  (a ? p++ : q++);
60 
61  for (int i = 2 ; i<h1->GetNbinsX() ; ++i){
62 
63  bool b = ((T/R)*h1->GetBinContent(i) - h2->GetBinContent(i)) < 0;
64 
65  (b ? p++ : q++);
66 
67  if (b != a){
68  n++;
69  a=b;
70  }
71  }
72 
73  double N = 1 + 2*p*q/(p+q) ;
74  double s = sqrt( 2*p*q*(2*p*q-p-q)/(p+q)/(p+q)/(p+q-1) );
75  double d = (n-N)/s;
76 
77  bool passed;
78 
79  (fabs(d) > threshold ? passed = false : passed = true);
80 
81  JResultTitle title(testName, parameterName , passed , fabs(d));
82 
83  h3->SetTitle(title.getTitle().c_str());
84 
85  JTestResult r (testName,
86  string (h1->GetDirectory()->GetPath()).append(h1->GetName()),
87  string (h2->GetDirectory()->GetPath()).append(h2->GetName()),
88  h1->GetDirectory()->GetFile()->GetName(),
89  h2->GetDirectory()->GetFile()->GetName(),
90  parameterName, fabs(d), threshold, h3, passed);
91 
92  return r;
93  };
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
do set_variable OUTPUT_DIRECTORY $WORKDIR T
#define ERROR(A)
Definition: JMessage.hh:66
then echo Variable JPP_DIR undefined exit fi source $JPP_DIR setenv sh $JPP_DIR set_variable NORTH set_variable EAST set_variable SOUTH set_variable WEST set_variable WORKDIR tmp set_variable R set_variable CT set_variable YMAX set_variable YMIN if do_usage *then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:35
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JMuonPath.sh:45
then JCalibrateToT a
Definition: JTuneHV.sh:116
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:35
JTestResult 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 112 of file JTestRuns_t.hh.

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

Member Data Documentation

double JTestRuns_1D::threshold
private

threshold value to decide if test is passed.

Definition at line 132 of file JTestRuns_1D.hh.

const std::string JTestRuns_1D::name = "Runs_1D"
private

test name.

Definition at line 133 of file JTestRuns_1D.hh.

const std::string JTestRuns_1D::parameter = "#sigma"
private

parameter name.

Definition at line 134 of file JTestRuns_1D.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: