Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTestChi2_t.hh
Go to the documentation of this file.
1 #ifndef __JTESTCHI2_T__
2 #define __JTESTCHI2_T__
3 
4 #include <iostream>
5 #include "JTest_t.hh"
6 
7 using namespace std;
8 /*
9  * \author rgruiz
10  */
11 
12 /*
13  * Implementation of the Chi2 test.
14  */
16 {
17 public:
18 
19  /*
20  * Default constructor.
21  */
23 
24  /*
25  * Tests the statistical compatibility of two ROOT TObjects
26  *
27  * \param h1 First object
28  * \param h2 Second object
29  * \param threshold p-value
30  *
31  * \return Test result.
32  */
33  JTestResult JChi2Test(TH1* h1, TH1* h2, double threshold) {
34 
35  JTestResult r;
36 
37  if(h1 -> GetNbinsX() != h2 -> GetNbinsX())
38  ERROR("Histograms with different bining. The objects: " << h1 -> GetName() << " can not be compared." << endl);
39 
40  double chi2 = h1 -> Chi2Test (h2 , "WW");
41 
42  double M = h1->Integral();
43  double N = 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  h3->Reset();
50 
51  for (int i=1 ; i < h1->GetNbinsX() ; ++i){
52 
53  double m = h1->GetBinContent(i);
54  double n = h2->GetBinContent(i);
55  if(n!=0 && m!=0){
56 
57  double c = (M*n - N*m)/sqrt((n+m)*(N*M));
58  h3->SetBinContent(i,c);
59  }
60  }
61 
62  (chi2 < threshold ? r.passed = false : r.passed = true);
63 
64  r.message = MAKE_STRING(scientific << setprecision(2)<< "Test: " << "Chi2" <<
65  " HA: " << h1->GetUniqueID() << " " << h1->GetName() <<
66  " HB: " << h2->GetUniqueID() << " " << h2->GetName() <<
67  " Threshold: " << threshold << " Result: " << chi2 << " " << (r.passed ? "PASSED" : "FAILED"));
68 
69 
70  JResultTitle title("Chi2_2D", "Deviation[#sigma] ", r.passed , chi2);
71 
72  h3->SetTitle(title.getTitle().c_str());
73 
74  r.obj = h3;
75 
76  return r;
77  };
78 
79  /*
80  * Tests the statistical compatibility of two ROOT TObjects
81  *
82  * \param h1 First object
83  * \param h2 Second object
84  * \param outliersThreshold fraction of bins allowed to fail the test
85  * \param chi2Threshold p-value
86  *
87  * \return Test result.
88  */
89  JTestResult JChi2TestBin_2D(TH2* h1, TH2* h2, double outliersThreshold, double chi2Threshold) {
90 
91  int nx1 = h1->GetNbinsX();
92  int nx2 = h2->GetNbinsX();
93  int ny1 = h1->GetNbinsY();
94  int ny2 = h2->GetNbinsY();
95 
96  double M = h1->Integral();
97  double N = h2->Integral();
98 
99  if(nx1 != nx2 || ny1 != ny2 || M == 0 || N == 0)
100  ERROR("Histograms with different binning. The objects: " << h1->GetName() << " can not be compared." << endl);
101 
102  TH2D* h3 = (TH2D*)h1->Clone(h1->GetName()==h2->GetName() ?
103  MAKE_CSTRING(to_string(h1->GetName())) :
104  MAKE_CSTRING(to_string(h1->GetName()) + "_VS_" + to_string(h2->GetName())));
105 
106  h3->Reset();
107 
108  JTestResult r;
109 
110  double outliers = 0;
111 
112  for (int i=1 ; i<nx1 ; ++i){
113  for (int j=1 ; j<ny1 ; ++j){
114 
115  double m = h1 -> GetBinContent(i,j);
116  double n = h2 -> GetBinContent(i,j);
117  double chi2 = (n-m*N/M)/sqrt(m*N/M);
118  (fabs(chi2) > chi2Threshold ? outliers+=1./(nx1*ny1) : outliers+=0 );
119  h3->SetBinContent(i,j,chi2);
120  }
121  }
122 
123  (outliers > outliersThreshold ? r.passed = false : r.passed = true);
124 
125  r.message = MAKE_STRING(scientific << setprecision(2)<< "Test: " << "Chi2_bin" <<
126  " HA: " << h1->GetUniqueID() << " " << h1->GetName() <<
127  " HB: " << h2->GetUniqueID() << " " << h2->GetName() <<
128  " Threshold: " << outliersThreshold << " Result: " << outliers << " " << (r.passed ? "PASSED" : "FAILED"));
129 
130  JResultTitle title("Chi2_bin", "Outliers[%] ", r.passed , 100*outliers);
131 
132  h3->SetTitle(title.getTitle().c_str());
133 
134  r.obj = h3;
135 
136  return r;
137  };
138 };
139 
140 #endif
do $JPP JMEstimator M
Definition: JMEstimator.sh:37
#define MAKE_CSTRING(A)
Make C-string.
Definition: JPrint.hh:708
then for HISTOGRAM in h0 h1
Definition: JMatrixNZ.sh:69
data_type r[M+1]
Definition: JPolint.hh:709
#define MAKE_STRING(A)
Make string.
Definition: JPrint.hh:699
JTestResult JChi2Test(TH1 *h1, TH1 *h2, double threshold)
Definition: JTestChi2_t.hh:33
JTestResult JChi2TestBin_2D(TH2 *h1, TH2 *h2, double outliersThreshold, double chi2Threshold)
Definition: JTestChi2_t.hh:89
#define ERROR(A)
Definition: JMessage.hh:66
string getTitle()
Definition: JTest_t.hh:48
string message
Definition: JTest_t.hh:65
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:634
bool passed
Definition: JTest_t.hh:64
TObject * obj
Definition: JTest_t.hh:66
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37