I have been working on making a few plots and I have ran into a bit of unexpected behaviour. It may be that I am doing things in an unusual way and there is a better way to do it, in which case I would be grateful for pointers to lead me in the correct direction. Or it could also be that I have ran into a bug, in which case I would be happy to submit a bug report if it is warranted.
Essentially, I have a histogram with uniform bins where the first bin starts at 0 (the code actually has several histograms, but one is enough to demonstrate the issue). I need to display the histogram with a logarithmic x axis. When I do this, ROOT chooses the lower edge for the first bin with some criteria I am not aware of. I want to specify this lower edge in order to have control of the visual of the plot. I have tried calling SetAxisRange()
on the histogram, but it does not allow me to set an edge in the middle of a bin (even though ROOT has already done so), the same stands for GetXaxis().SetRange()
and GetXaxis().SetRangeUser()
.
I then tried to plot a separate histogram which serves as a “frame”, with a different range, in order to define the plot axis limits and then draw the histogram I want on top with the option “same”. This works perfect and exactly as expected for a linear x-axis. But with the logarithmic x-axis, the edges of the bins are actually shifted and modified, which is very unexpected and I would argue is a bug.
I show below a MWE where I exaggerate the lower edge of the plot in order to make the shifting of the bin edges in the last step clearly evident.
So, if someone could confirm this last thing is indeed a bug, I would be happy to submit the bug report. In addition, I would be very happy if someone could help point me toward a solution to plot a histogram starting at 0, with a logarithmic x-axis but controlling the place where the x-axis starts.
ROOT Version: 6.24
Platform: Working on SWAN, using a python notebook (i.e. PyROOT)
MWE, for Jupyter below:
from ROOT import TCanvas, TH2D, TH1D
frame = TH2D("frame", "", 100, 24, 100, 100, 10, 100)
frame.SetStats(False)
frame.GetXaxis().SetTitle("Deadtime [ns]")
frame.GetYaxis().SetTitle("Pad Size [#mum]")
hist = TH1D("padSize_vs_deadtime", "Pad Size vs Deadtime", 4, 0, 100)
hist.SetBinContent(1, 60)
hist.SetBinContent(2, 45)
hist.SetBinContent(3, 30)
hist.SetBinContent(4, 15)
# Histogram bin edges are at: 0, 25, 50, 75, 100
canv = TCanvas("padSize_vs_deadtime", "Pad Size vs Deadtime", 1200, 800)
canv.Divide(3,2)
pad = canv.cd(1)
pad.SetLogx()
pad.SetTicks()
hist.Draw("")
hist.SetAxisRange(10, 100, "X")
# Displayed plot x-axis edge is ~0.025
pad = canv.cd(2)
pad.SetLogx()
pad.SetTicks()
hist.Draw("")
hist.GetXaxis().SetRange(10, 100)
# Displayed plot x-axis edge is ~0.025
pad = canv.cd(3)
pad.SetLogx()
pad.SetTicks()
hist.Draw("")
hist.GetXaxis().SetRangeUser(10, 100)
# Displayed plot x-axis edge is ~0.025
pad = canv.cd(4)
pad.SetTicks()
frame.Draw()
hist.Draw("same")
# Works exactly as expected and the edge of the plot x-axis is 24
# Displayed histogram bin edges are at: 25, 50, 75 (as expected)
pad = canv.cd(5)
pad.SetLogx()
pad.SetTicks()
frame.Draw()
hist.Draw("same")
# The edge of the plot x-axis is 24 (as desired)
# Displayed histogram bin edges are shifted significantly: ~40 (25 expected), ~60 (50 expected), ~80 (75 expected)
# Histogram data is misrepresented, bug?
canv.Draw()