Skip to content

[hist] Fix computation of number of entries in ResetStats#21254

Open
lmoneta wants to merge 1 commit intoroot-project:masterfrom
lmoneta:hist_fix_entries_after_reset
Open

[hist] Fix computation of number of entries in ResetStats#21254
lmoneta wants to merge 1 commit intoroot-project:masterfrom
lmoneta:hist_fix_entries_after_reset

Conversation

@lmoneta
Copy link
Member

@lmoneta lmoneta commented Feb 11, 2026

When computing the number of entries in TH1::ResetStats always include underflow/overflow. Also in number of FetEffectiveEntries()

Fix a bug in the computation of GetAllSumOfWeights() and improve the function to return (as an optional parameter) the total sum of weight square (including underflow/overflows).

Improve the handling of statistics in TH1::Add methods.
When performing additions of histograms with coefficient > 0 but not equal to 1 automatically compute the sum of weight squares, otherwise the bin errors computations (and the error on the statistics, mean, std) will not be correct.

The bug was happening for dimensions < 2. (see ROOT-21253)

This PR fixes #21253 and the old JIRA item https://its.cern.ch/jira/browse/ROOT-10567?filter=-1

For the JIRA item, in case of Addition/Subtraction of histogram, the nuber of entries is now computed using underflow/overflows. But in case of Additions/Subtraciton with coefficient not equal to 1, the histogram will be weighted and the number of effective entries will be used.

@lmoneta lmoneta self-assigned this Feb 11, 2026
@lmoneta lmoneta requested a review from hageboeck as a code owner February 11, 2026 15:23
@lmoneta lmoneta changed the title [hist] Fix computaiton of number of entries in ResetStats [hist] Fix computation of number of entries in ResetStats Feb 11, 2026
@github-actions
Copy link

github-actions bot commented Feb 11, 2026

Test Results

    20 files      20 suites   3d 3h 52m 54s ⏱️
 3 772 tests  3 771 ✅ 0 💤 1 ❌
69 396 runs  69 395 ✅ 0 💤 1 ❌

For more details on these failures, see this check.

Results for commit 34b8c9a.

♻️ This comment has been updated with latest results.

@dpiparo
Copy link
Member

dpiparo commented Feb 12, 2026

Thanks @lmoneta, could tests be added for the two issues fixed?

When computing the number of entries in TH1::ResetStats always include underflow/overflow. Also in number of FetEffectiveEntries()

Fix a bug in the computation of GetAllSumOfWeights() and improve the function to return (as an optional parameter) the total sum of weight square (including underflow/overflows).

The bug was happening for dimensions < 2. (see ROOT-21253)

Remove in UHI indexing test the check that number of effective entries is equal to number of entries. With the current changes the number of entries will contain underflows/overflows while number of effective entries not, because it follows the  convention used for the statistics (mean,std, etc..) including what is in the current histogram range
@lmoneta lmoneta force-pushed the hist_fix_entries_after_reset branch from 08e000f to 34b8c9a Compare February 12, 2026 13:27
Copy link
Collaborator

@ferdymercury ferdymercury left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lmoneta thanks for the fix! For the test, could you go to

https://github.com/lmoneta/root/blob/hist_fix_entries_after_reset/hist/hist/test/test_TH1.cxx#L115

and below all lines starting with

EXPECT_FLOAT_EQ(h*.GetSumOfWeights(), *);

add two additional lines:

EXPECT_FLOAT_EQ(h*.GetSumOfAllWeights(true), *);
EXPECT_FLOAT_EQ(h*.GetSumOfAllWeights(false), *);

Copy link
Collaborator

@ferdymercury ferdymercury left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also copy-paste a similar example into https://github.com/lmoneta/root/blob/hist_fix_entries_after_reset/hist/hist/test/test_TH2.cxx and https://github.com/lmoneta/root/blob/hist_fix_entries_after_reset/hist/hist/test/test_TH3.cxx

TEST(TH2, GetSumOfAllWeights)
{
   TH2F h2("h2", "h2", 10, 0, 1, 10, 0, 1);
   h2.SetBinContent(1, 2, 1.);
   h2.SetBinContent(3, 4, 5.);
   h2.SetBinContent(5, 1, 10.);
   h2.SetBinContent(7, 5, -1.);
   h2.SetBinContent(10, 2, 3.);
   h2.SetBinContent(150, 2, 3.);
   EXPECT_FLOAT_EQ(h2.GetEntries(), 6.);
   EXPECT_FLOAT_EQ(h2.GetSumOfWeights(), 18.);
   EXPECT_FLOAT_EQ(h2.GetSumOfAllWeights(true), 21.);
   EXPECT_FLOAT_EQ(h2.GetSumOfAllWeights(false), 18.);
   double sumsq;
   EXPECT_FLOAT_EQ(h2.GetSumOfAllWeights(true,&sumsq), 21.);
   EXPECT_FLOAT_EQ(sumsq, 145);
}

TEST(TH3, GetSumOfAllWeights)
{
   TH3F h3("h3", "h3", 10, 0, 1, 10, 0, 1, 3, 0, 1);
   h3.SetBinContent(1, 2, 1, 1.);
   h3.SetBinContent(3, 4, 2, 5.);
   h3.SetBinContent(5, 1, 3, 10.);
   h3.SetBinContent(7, 5, 1, -1.);
   h3.SetBinContent(10, 2, 2, 3.);
   h3.SetBinContent(150, 2, 3, 3.);
   EXPECT_FLOAT_EQ(h3.GetEntries(), 6.);
   EXPECT_FLOAT_EQ(h3.GetSumOfWeights(), 18.);
   EXPECT_FLOAT_EQ(h3.GetSumOfAllWeights(true), 21.);
   EXPECT_FLOAT_EQ(h3.GetSumOfAllWeights(false), 18.);
   double sumsq;
   EXPECT_FLOAT_EQ(h3.GetSumOfAllWeights(true,&sumsq), 21.);
   EXPECT_FLOAT_EQ(sumsq, 145);
}

For the JIRA issue, sth like this:

TEST(TH1, Add)
{
   TH1F h1("h1", "h1", 10, 0, 1);
   h1.SetBinContent(1, 1.);
   h1.SetBinContent(3, 5.);
   h1.SetBinContent(5, 10.);
   h1.SetBinContent(7, -1.);
   h1.SetBinContent(10, 3.);
   TH1F h2("h2", "h2", 10, 0, 1);
   h2.SetBinContent(1, 2.);
   h2.SetBinContent(3, 10.);
   h2.SetBinContent(5, 20.);
   h2.SetBinContent(7, -2.);
   h2.SetBinContent(10, 6.);
   h2.Add(&h1, 1.);
   EXPECT_FLOAT_EQ(h2.GetEntries(), 10.);
   EXPECT_FLOAT_EQ(h2.GetSumOfWeights(), ...);
}


// Create Sumw2 if h1 has Sumw2 set
if (fSumw2.fN == 0 && h1->GetSumw2N() != 0) Sumw2();
// Also Create Sumw2 if is not a simple addition , otherwise errors will not be correctly computed
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Also Create Sumw2 if is not a simple addition , otherwise errors will not be correctly computed
// Also Create Sumw2 if is not a simple addition, otherwise errors will not be correctly computed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TH1::GetSumOfAllWeights return 9xvalue for 1D and (3x) 2D histograms when including underflows/overflows

4 participants

Comments