Skip to content

Commit f00eaf4

Browse files
ahmedbilal9dpiparo
authored andcommitted
Fix Chi2Test: Support residuals for 2D/3D histograms (root-project#20798)
[hist] Chi2Test: Support residuals for 2D/3D histograms Fixed bug where Chi2Test only stored residuals for 1D histograms. For 2D/3D histograms, the residual array indexing ignored j and k loop indices, causing incorrect storage. Added resIndex counter for each comparison mode (UU, UW, WW) to properly index the residuals array for multi-dimensional histograms. Fixes root-project#20761 (cherry picked from commit 5b770f8)
1 parent 6a1bbfd commit f00eaf4

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

hist/hist/src/TH1.cxx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,9 +2222,9 @@ Double_t TH1::Chi2TestX(const TH1* h2, Double_t &chi2, Int_t &ndf, Int_t &igood
22222222

22232223
//THE TEST
22242224
Int_t m = 0, n = 0;
2225-
22262225
//Experiment - experiment comparison
22272226
if (comparisonUU) {
2227+
Int_t resIndex = 0;
22282228
Double_t sum = sum1 + sum2;
22292229
for (Int_t i = i_start; i <= i_end; ++i) {
22302230
for (Int_t j = j_start; j <= j_end; ++j) {
@@ -2254,15 +2254,15 @@ Double_t TH1::Chi2TestX(const TH1* h2, Double_t &chi2, Int_t &ndf, Int_t &igood
22542254
Double_t nexp1 = cntsum * sum1 / sum;
22552255
//Double_t nexp2 = binsum*sum2/sum;
22562256

2257-
if (res) res[i - i_start] = (cnt1 - nexp1) / TMath::Sqrt(nexp1);
2257+
if (res) res[resIndex] = (cnt1 - nexp1) / TMath::Sqrt(nexp1);
22582258

22592259
if (cnt1 < 1) ++m;
22602260
if (cnt2 < 1) ++n;
22612261

22622262
//Habermann correction for residuals
22632263
Double_t correc = (1. - sum1 / sum) * (1. - cntsum / sum);
2264-
if (res) res[i - i_start] /= TMath::Sqrt(correc);
2265-
2264+
if (res) res[resIndex] /= TMath::Sqrt(correc);
2265+
if (res) resIndex++;
22662266
Double_t delta = sum2 * cnt1 - sum1 * cnt2;
22672267
chi2 += delta * delta / cntsum;
22682268
}
@@ -2290,6 +2290,7 @@ Double_t TH1::Chi2TestX(const TH1* h2, Double_t &chi2, Int_t &ndf, Int_t &igood
22902290
// case of error = 0 and content not zero is treated without problems by excluding second chi2 sum
22912291
// and can be considered as a data-theory comparison
22922292
if ( comparisonUW ) {
2293+
Int_t resIndex = 0;
22932294
for (Int_t i = i_start; i <= i_end; ++i) {
22942295
for (Int_t j = j_start; j <= j_end; ++j) {
22952296
for (Int_t k = k_start; k <= k_end; ++k) {
@@ -2372,10 +2373,11 @@ Double_t TH1::Chi2TestX(const TH1* h2, Double_t &chi2, Int_t &ndf, Int_t &igood
23722373
Double_t temp2 = 1.0 + (sum1 * e2sq - sum2 * cnt2) / var2;
23732374
temp2 = temp1 * temp1 * sum1 * probb * (1.0 - probb) + temp2 * temp2 * e2sq / 4.0;
23742375
// invert sign here
2375-
res[i - i_start] = - delta2 / TMath::Sqrt(temp2);
2376+
res[resIndex] = - delta2 / TMath::Sqrt(temp2);
23762377
}
23772378
else
2378-
res[i - i_start] = delta1 / TMath::Sqrt(nexp1);
2379+
res[resIndex] = delta1 / TMath::Sqrt(nexp1);
2380+
resIndex++;
23792381
}
23802382
}
23812383
}
@@ -2397,6 +2399,7 @@ Double_t TH1::Chi2TestX(const TH1* h2, Double_t &chi2, Int_t &ndf, Int_t &igood
23972399

23982400
// weighted - weighted comparison
23992401
if (comparisonWW) {
2402+
Int_t resIndex = 0;
24002403
for (Int_t i = i_start; i <= i_end; ++i) {
24012404
for (Int_t j = j_start; j <= j_end; ++j) {
24022405
for (Int_t k = k_start; k <= k_end; ++k) {
@@ -2438,7 +2441,8 @@ Double_t TH1::Chi2TestX(const TH1* h2, Double_t &chi2, Int_t &ndf, Int_t &igood
24382441
Double_t s2 = e2sq * ( 1. - e1sq * sum2 * sum2 / sigma );
24392442
z = -d2 / TMath::Sqrt(s2);
24402443
}
2441-
res[i - i_start] = z;
2444+
res[resIndex] = z;
2445+
resIndex++;
24422446
}
24432447

24442448
if (e1sq > 0 && cnt1 * cnt1 / e1sq < 10) m++;

0 commit comments

Comments
 (0)