Skip to content

Commit 626650d

Browse files
hageboeckdpiparo
authored andcommitted
[hist] Fix error calculation in THnBase::ProjectionAny.
Thanks to jgrosseo! Fix #19241 (cherry picked from commit f386fc2)
1 parent ba7a5e7 commit 626650d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

hist/hist/src/THnBase.cxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,9 @@ TObject* THnBase::ProjectionAny(Int_t ndim, const Int_t* dim,
697697
Bool_t haveErrors = GetCalculateErrors();
698698
Bool_t wantErrors = haveErrors || (option && (strchr(option, 'E') || strchr(option, 'e')));
699699

700+
if (wantNDim && wantErrors)
701+
hn->Sumw2();
702+
700703
Int_t* bins = new Int_t[ndim];
701704
Long64_t myLinBin = 0;
702705

hist/hist/test/THn.cxx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,33 @@ TEST(THn, GetBinCenter)
144144
EXPECT_DOUBLE_EQ(centers.at(0), 2.5);
145145
EXPECT_DOUBLE_EQ(centers.at(1), -1.5);
146146
}
147+
148+
TEST(THn, ErrorsOfProjection)
149+
{
150+
const int bins[] = {10, 10, 10, 10};
151+
const double xmin[] = {0, 0, 0, 0};
152+
const double xmax[] = {10, 10, 10, 10};
153+
THnF thn("thn", "", 4, bins, xmin, xmax);
154+
thn.Sumw2();
155+
156+
const double coordinates1[]{0.5, 0.5, 0.5, 0.5};
157+
158+
for (int i = 0; i < 9; i++) {
159+
thn.Fill(coordinates1, 0.1);
160+
const double coordinates2[]{1.5, 1.5, 0.5 + i, 0.5 + i};
161+
thn.Fill(coordinates2, 2.);
162+
}
163+
164+
const Int_t dimensions[] = {0, 1};
165+
// Despite the option "E", the errors are resetted to sqrt(N) instead of keeping the original ones
166+
std::unique_ptr<THnBase> proj{thn.ProjectionND(2, dimensions, "E")};
167+
168+
const auto projectedBin = proj->GetBin(coordinates1);
169+
EXPECT_FLOAT_EQ(proj->GetBinContent(projectedBin), 0.9);
170+
EXPECT_FLOAT_EQ(proj->GetBinError(projectedBin), 0.3);
171+
172+
const double coordinates2[]{1.5, 1.5};
173+
const auto projectedBin2 = proj->GetBin(coordinates2);
174+
EXPECT_FLOAT_EQ(proj->GetBinContent(projectedBin2), 18.);
175+
EXPECT_FLOAT_EQ(proj->GetBinError(projectedBin2), 6.);
176+
}

0 commit comments

Comments
 (0)