root-project / root

The official repository for ROOT: analyzing, storing and visualizing big data, scientifically
https://root.cern
Other
2.71k stars 1.28k forks source link

Strange overflow bin bar when plotting TH1D with X1 option #16976

Open karuboniru opened 12 hours ago

karuboniru commented 12 hours ago

Check duplicate issues.

Description

ROOT produces strange result when plotting after setting excessive range via SetRange/SetRangeUser, namely the "bar" over X axis can overlap between different bins plot

Reproducer

#include <TCanvas.h>
#include <TH1D.h>
#include <TRandom.h>
#include <TStyle.h>
#include <array>
#include <memory>

int main() {
  gStyle->SetOptStat(0);
  auto c1 = std::make_shared<TCanvas>("c1", "c1", 800, 600);
  auto x_bins = std::to_array<double>({0, 0.3, 0.5, 0.6, 1.0});
  auto h1 =
      std::make_shared<TH1D>("h1", "h1", x_bins.size() - 1, x_bins.data());
  for (int i = 0; i < 1000; i++) {
    h1->Fill(gRandom->Gaus(0.5, 1));
  }
  h1->GetXaxis()->SetRange(-1, 6);
  h1->Draw("X1 E0");
  c1->SaveAs("plot.eps");
}

It seems that ths issue is related to non-uniform bin width, maybe related to the code where the bar with of the overflow bin is determined.

ROOT version

   ------------------------------------------------------------------
  | Welcome to ROOT 6.32.06                        https://root.cern |
  | (c) 1995-2024, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Sep 23 2024, 00:00:00                 |
  | From tags/6-32-06@6-32-06                                        |
  | With clang version 18.1.8 (Fedora 18.1.8-3.fc41)                 |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------

This reproduces on many versions.

Installation method

Self built

Operating system

Linux Fedora 41

Additional context

No response

dpiparo commented 7 hours ago

@couet , was this fixed in master perhaps? I get:

Warning in <TAxis::TAxis::SetRange>: first < 0, 0 is used
Warning in <TAxis::TAxis::SetRange>: last > fNbins+1, fNbins+1 is used

image

karuboniru commented 4 hours ago

So in master TAxis::SetRange/SetRangeUser no longer includes the overflow bin in the plot? This I think would be great as I have faced an issue where the overflow bin appears unexpectedly and thought this was a feature.