Skip to content

[hist] Improve limits of THLimitsFinder. #19605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions hist/hist/src/THLimitsFinder.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,16 @@ void THLimitsFinder::Optimize(Double_t A1, Double_t A2, Int_t nold ,
Int_t oldnbins = nbins;

Double_t atest = BinWidth*0.0001;
//if (TMath::Abs(BinLow-A1) >= atest) { BinLow += BinWidth; nbins--; } //replaced by Damir in 3.10/02
//if (TMath::Abs(BinHigh-A2) >= atest) { BinHigh -= BinWidth; nbins--; } //by the next two lines
if (al-BinLow >= atest) { BinLow += BinWidth; nbins--; }
if (BinHigh-ah >= atest) { BinHigh -= BinWidth; nbins--; }
if (al - BinLow >= atest && al < BinLow + BinWidth) {
// Suppress the first bin, but only if al doesn't fall into it
BinLow += BinWidth;
nbins--;
}
if (BinHigh - ah >= atest && BinHigh - BinWidth > ah) {
// Suppress the last bin, but only if ah doesn't fall into it
BinHigh -= BinWidth;
nbins--;
}
Comment on lines +340 to +349
Copy link
Collaborator

@ferdymercury ferdymercury Aug 14, 2025

Choose a reason for hiding this comment

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

Shouldn't it be al > BinLow + BinWidth ? instead of < ?
Or maybe even easier:

Suggested change
if (al - BinLow >= atest && al < BinLow + BinWidth) {
// Suppress the first bin, but only if al doesn't fall into it
BinLow += BinWidth;
nbins--;
}
if (BinHigh - ah >= atest && BinHigh - BinWidth > ah) {
// Suppress the last bin, but only if ah doesn't fall into it
BinHigh -= BinWidth;
nbins--;
}
if (al >= BinLow + BinWidth + atest) {
// Suppress the first bin, but only if al doesn't fall into it
BinLow += BinWidth;
nbins--;
}
if (ah <= BinHigh - BinWidth - atest) {
// Suppress the last bin, but only if ah doesn't fall into it
BinHigh -= BinWidth;
nbins--;
}

if (!optionTime && BinLow >= BinHigh) {
//this case may happen when nbins <=5
BinLow = oldBinLow;
Expand Down
13 changes: 13 additions & 0 deletions hist/hist/test/test_TH1.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ TEST(THLimitsFinder, Degenerate)
EXPECT_GE(xmax, centralValue + 5.);
}

// see https://root-forum.cern.ch/t/bug-or-feature-in-ttree-draw/62862
// Due to a poor binning choice in THLimitsFinder, the histograms in
// TTree::Draw might not contain contain all values.
TEST(THLimitsFinder, TTreeDraw_AutoBinning)
{
TH1F histo("limitsFinder", "", 100, 0, 0);
histo.Fill(-999);
histo.Fill(0);
histo.BufferEmpty(1);

EXPECT_EQ(histo.GetEntries(), histo.GetEffectiveEntries());
}

// Simple cross-check that TH1::SmoothArray() is not doing anything if input
// array is already smooth.
TEST(TH1, SmoothArrayCrossCheck)
Expand Down
19 changes: 19 additions & 0 deletions tree/tree/test/TTreeRegressions.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,22 @@ TEST(TTreeRegressions, TTreeFormulaMemberIndex)
ASSERT_FLOAT_EQ(mc.Get(-2)->x, h3->GetMean());
delete h3;
}

// see https://root-forum.cern.ch/t/bug-or-feature-in-ttree-draw/62862
// Due to a poor binning choice in THLimitsFinder, the histogram didn't contain
// all values.
TEST(TTreeRegressions, DrawAutoBinning)
{
TTree t;
Float_t x;
t.Branch("x", &x);
x = -999;
t.Fill();
x = 0;
t.Fill();
t.Draw("x");
auto h = (TH1 *)gROOT->FindObject("htemp");
ASSERT_NE(h, nullptr);
EXPECT_EQ(h->GetEntries(), h->GetEffectiveEntries());
delete h;
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
delete h;
delete h;
delete gROOT->FindObject("c1");

}
Loading