On new versions of python and ROOT, using the cmsHeader function results in a corrupted canvas where the buffer of the header is freed by python garbage collection. Attached is a minimal reproducer and a simplistic fix. My system information is:
ROOT Version: 6.40.00
Built for linuxx8664gcc on May 22 2026, 18:02:23
From tags/6-40-00@6-40-00
uv pip show cmsstyle
Using Python 3.14.5 environment at: ...
Name: cmsstyle
Version: 0.5.0
Location: ...
Requires: typing-extensions
Required-by:
#!/usr/bin/env python3
import cmsstyle as cms
import ROOT as rt
def fixed_cmsHeader(
leg,
legTitle,
textAlign=12,
textSize=0.04,
textFont=42,
textColor=rt.kBlack,
isToRemove=True,
):
header = rt.TLegendEntry(0, legTitle, "h")
header.SetTextFont(textFont)
header.SetTextSize(textSize)
header.SetTextAlign(textAlign)
header.SetTextColor(textColor)
if isToRemove:
leg.SetHeader(legTitle, "C")
leg.GetListOfPrimitives().Remove(leg.GetListOfPrimitives().At(0))
leg.GetListOfPrimitives().AddAt(header, 0)
else:
leg.GetListOfPrimitives().AddLast(header)
rt.SetOwnership(
header, False
) # This otherwise gets garbage collected as of ROOT 6.40 and python 3.14
def show_with_legend(header_text, header_func):
canvas = cms.cmsCanvas("canvas", 0, 1, 0, 1, "x", "y")
leg = cms.cmsLeg(0.2, 0.2, 0.8, 0.8)
header_func(leg, header_text)
canvas.Update()
canvas.Draw()
def run_reproducer():
show_with_legend("Test header text", fixed_cmsHeader)
input("A working canvas with a header is now visible. Press return to trigger crash")
print(
"Attempting to paint the canvas with a cmsHeader triggers segv.\n"
"Python garbage collection is at the same time already freeing the header."
)
show_with_legend("Broken header text", cms.cmsHeader)
input("Version not affected. Press return to exit.")
run_reproducer()
On new versions of python and ROOT, using the cmsHeader function results in a corrupted canvas where the buffer of the header is freed by python garbage collection. Attached is a minimal reproducer and a simplistic fix. My system information is:
ROOT version information
Python version information
cmsstyle version information
Reproducer