Jpp  15.0.4
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTestRuns_t.hh
Go to the documentation of this file.
1 #ifndef __JCOMPAREHISTOGRAMS__JTESTRUNS_T__
2 #define __JCOMPAREHISTOGRAMS__JTESTRUNS_T__
3 
4 #include <iostream>
5 
8 
9 
10 /**
11  * \author rgruiz
12  */
13 namespace JCOMPAREHISTOGRAMS {
14 
16 
17  /**
18  * Implementation of the different Runs-related tests.
19  */
20  class JTestRuns_t
21  {
22  public:
23  /**
24  * Default constructor.
25  */
27 
28  /**
29  * Implements the Wald-Wolfowitx runs test: <https://en.wikipedia.org/wiki/Wald%E2%80%93Wolfowitz_runs_test>
30  * 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".
31  * The test returns the difference between the observed number of runs and the expected number of runs, expressed in standard deviations.
32  * This is compared to the threshold input parameter.
33  *
34  * \param h1 First histogram
35  * \param h2 Second histogram
36  * \param threshold
37  * \param parameterName Name of the parameter used to test the histograms
38  * \param testName Name of the test used to compare the histograms
39  */
40  JTestResult JRunsTest(TH1* h1, TH1* h2, double threshold, std::string testName, std::string parameterName) {
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  double N = 1 + 2*p*q/(p+q) ;
80  double s = sqrt( 2*p*q*(2*p*q-p-q)/(p+q)/(p+q)/(p+q-1) );
81  double d = (n-N)/s;
82 
83  bool passed;
84 
85  (fabs(d) > threshold ? passed = false : passed = true);
86 
87  JResultTitle title(testName, parameterName , passed , fabs(d));
88 
89  h3->SetTitle(title.getTitle().c_str());
90 
91  JTestResult r (testName,
92  JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())),
93  JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())),
94  parameterName, fabs(d), threshold, h3, passed);
95 
96  return r;
97  };
98 
99  /**
100  * Runs test for sliced 2D histograms.\n
101  * The histograms are sliced along the axis specified by the slice parameter. A slice per bin is made.\n
102  * For each of the slices, the input parameter threshold, is used to evaluate whether the test is passed or failed.\n
103  * The evaluation is done by comparing the threshold value with the result of the Runs test.\n
104  * The fraction of failed tests is compared to the input parameter failuresThreshold. If this fraction is larger than failuresThreshold, the test fails.
105  *
106  * \param h1 First histogram
107  * \param h2 Second histogram
108  * \param threshold Threshold value for the test result
109  * \param failuresThreshold Threshold value for the fraction of failed tests.
110  * \param testName Name of the test used to compare the histograms
111  * \param parameterName Name of the parameter used to test the histograms
112  * \param slice The axis along which the histogram is sliced.
113  *
114  * \return Test result.
115  */
116  JTestResult JRunsTestSlice(TH2* h1, TH2* h2, double threshold, double failuresThreshold, std::string testName, std::string parameterName, char slice) {
117 
118  using namespace std;
119  using namespace JPP;
120 
121  int nFailures = 0;
122 
123  JTestResult r;
124 
125  if(slice == 'x' || slice == 'X'){
126 
127  int nSlices1 = h1->GetNbinsX();
128  int nSlices2 = h2->GetNbinsX();
129 
130  TH1* h3 = h1->ProjectionX(h1->GetName()==h2->GetName() ?
131  MAKE_CSTRING(to_string(h1->GetName())) :
132  MAKE_CSTRING(to_string(h1->GetName()) + "_VS_" + to_string(h2->GetName())));
133 
134  if(nSlices1 != nSlices2)
135  ERROR("Histograms with different binning. The objects: " << h1->GetName() << " and " << h2->GetName() << " can not be compared." << endl);
136 
137  for (int i=1 ; i<=nSlices1 ; ++i){
138 
139  std::string sliceName = MAKE_STRING(h1->GetName() + to_string("_") + to_string(i));
140 
141  TH1D* s1 = h1->ProjectionY (sliceName.c_str(),i,i);
142  TH1D* s2 = h2->ProjectionY (sliceName.c_str(),i,i);
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;
172 
173  (fabs(d) > threshold ? passed = false : passed = true);
174 
175  if (!passed) nFailures++;
176 
177  h3->SetBinContent(i,fabs(d));
178 
179  }
180 
181  bool passed;
182 
183  (nFailures/nSlices1 > failuresThreshold ? passed = false : passed = true);
184 
185  JResultTitle title(testName, parameterName, passed , nFailures);
186 
187  h3->SetTitle(title.getTitle().c_str());
188 
189  r = JTestResult(testName,
190  JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())),
191  JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())),
192  parameterName, nFailures, failuresThreshold, h3, passed);
193 
194  }else if (slice == 'y' || slice == 'Y'){
195 
196  int nSlices1 = h1->GetNbinsX();
197  int nSlices2 = h2->GetNbinsX();
198 
199  TH1* h3 = h1->ProjectionY(h1->GetName()==h2->GetName() ?
200  MAKE_CSTRING(to_string(h1->GetName())) :
201  MAKE_CSTRING(to_string(h1->GetName()) + "_VS_" + to_string(h2->GetName())));
202 
203  if(nSlices1 != nSlices2)
204  ERROR("Histograms with different binning. The objects: " << h1->GetName() << " and " << h2->GetName() << " can not be compared." << endl);
205 
206  for (int i=1 ; i<=nSlices1 ; ++i){
207 
208  std::string sliceName = MAKE_STRING(h1->GetName() + to_string("_") + to_string(i));
209 
210  TH1D* s1 = h1->ProjectionX (sliceName.c_str(),i,i);
211  TH1D* s2 = h2->ProjectionX (sliceName.c_str(),i,i);
212 
213  double R = s1->Integral();
214  double T = s2->Integral();
215 
216  int n = 1;
217  double p = 0;
218  double q = 0;
219 
220  bool a = ((T/R)*s1->GetBinContent(1) - s2->GetBinContent(1)) < 0;
221 
222  (a ? p++ : q++);
223 
224  for (int i = 2 ; i<s1->GetNbinsX() ; ++i){
225 
226  bool b = ((T/R)*s1->GetBinContent(i) - s2->GetBinContent(i)) < 0;
227 
228  (b ? p++ : q++);
229 
230  if (b != a){
231  n++;
232  a=b;
233  }
234  }
235 
236  double N = 1 + 2*p*q/(p+q) ;
237  double s = sqrt( 2*p*q*(2*p*q-p-q)/(p+q)/(p+q)/(p+q-1) );
238  double d = (n-N)/s;
239 
240  bool passed;
241 
242  (fabs(d) > threshold ? passed = false : passed = true);
243 
244  if (!passed) nFailures++;
245 
246  h3->SetBinContent(i,fabs(d));
247 
248  }
249 
250  bool passed;
251 
252  (nFailures/nSlices1 > failuresThreshold ? passed = false : passed = true);
253 
254  JResultTitle title(testName, parameterName, passed , nFailures);
255 
256  h3->SetTitle(title.getTitle().c_str());
257 
258  r = JTestResult (testName,
259  JRootObjectID(MAKE_STRING(h1->GetDirectory()->GetPath() << h1->GetName())),
260  JRootObjectID(MAKE_STRING(h2->GetDirectory()->GetPath() << h1->GetName())),
261  parameterName, nFailures, failuresThreshold, h3, passed);
262 
263  }
264 
265  return r;
266  };
267  };
268 }
269 
270 #endif
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:40
Class dedicated to standardize the title of the graphical objects produced by the JTest_t() derived c...
Definition: JResultTitle.hh:22
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:151
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:71
Auxiliary class to handle file name, ROOT directory and object name.
data_type r[M+1]
Definition: JPolint.hh:742
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:142
JTestRuns_t()
Default constructor.
Definition: JTestRuns_t.hh:26
const int n
Definition: JPolint.hh:660
do set_variable OUTPUT_DIRECTORY $WORKDIR T
#define ERROR(A)
Definition: JMessage.hh:66
Implementation of the different Runs-related tests.
Definition: JTestRuns_t.hh:20
then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:43
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JMuonPath.sh:47
then JCalibrateToT a
Definition: JTuneHV.sh:116
std::string to_string(const T &value)
Convert value to string.
Structure containing the result of the test.
Definition: JTestResult.hh:27
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:116
std::string getTitle()
Returns a standard string to be used as title of a graphical root object.
Definition: JResultTitle.hh:64