Dear ROOT experts,
I encounter a bug regarding the horizontal size of any TLatex or TText placed inside a pad with a height smaller than the canvas’ if the font is set to precision 3. Essentially, if the pad is constructed with
pad_top = root.TPad('pad_top', '', 0, 0.27, 1, 1)
instead of
pad_top = root.TPad('pad_top', '', 0, 0, 1, 1)
then label.GetXsize()
returns the wrong value, where label
is a TLatex. This also happens with TText, if using label.GetBoundingBox(width, height)
to get the label’s width. If the font is set to a lower precision, the problem goes away.
The following script illustrates the problem:
import ctypes
import ROOT as root
root.gStyle.SetTextFont(43)
root.gStyle.SetTextSize(29)
#root.gStyle.SetTextFont(42)
#root.gStyle.SetTextSize(0.05)
canvas_hist = root.TCanvas('canvas', '', 0, 0, 800, 800)
#pad_top = root.TPad('pad_top', '', 0, 0, 1, 1)
pad_top = root.TPad('pad_top', '', 0, 0.27, 1, 1)
pad_top.Draw()
pad_top.cd()
hist = root.TH1D('test', '', 7, 0, 7)
hist.FillRandom("gaus", 10000)
hist.Draw()
pad_top.Update()
plotLabel = root.TLatex()
label = plotLabel.DrawLatexNDC(0.2, 0.77, 'MC mc16d, Data 2017')
w, h = ctypes.c_uint(), ctypes.c_uint()
label.GetBoundingBox(w, h)
x0 = root.gPad.PixeltoX(root.gPad.UtoPixel(label.GetX()))
x1 = root.gPad.PixeltoX(root.gPad.UtoPixel(label.GetX()) + w.value)
print('Label:', f'{x0=}', f'{x1=}')
print('Same values with label.GetXsize():')
print(f'{x1=}, {x0 + label.GetXsize()=}')
canvas_hist.Print('test.eps')
ROOT Version: 6.24/06, installed through Anaconda
Platform: Arch Linux / ROOT conda docker container
Compiler: GCC