TH1D disappears from TCanvas in pyROOT


ROOT Version: 5.34/32
Platform: Mac OSX 10.10.5
Compiler: Not Provided


I’m having a problem getting TH1Ds to stay on the TCanvas in pyROOT. Using the code below, I can get the TH1D to appear on the TCanvas, but as soon as I click on the TCanvas, the TH1D disappears. Is there a way to fix this? I am running with ‘python -i test.py’

def test():
  c0 = TCanvas('c0', 'c0', 900, 600)
  c0.cd()

  h0 = TH1D('h0', 'h0', 100, -5, 5)
  h0.FillRandom('gaus', 10000)
  h0.Draw()

  c0.Update()
  return c0

ctest = test()

I had this same issue for what I believe is the same version and OS. I also had both the anaconda and for lack of better term, native, versions of python on my Mac. I do not have the the issue anymore after removing anaconda completely and upgrading both OS and ROOT. Currently running OSX 10.14 with ROOT 6.14.06.

Do you know how you built root? I tired rebuilding it multiple times and different assignments of cmake options to the different version of python I had and nothing ever seemed to fix this. I ‘think’ it is not an actual ROOT issue per se but a confusion in the build process over python version (could be wrong about this).

You might try building one of the newer releases (6.14.06 for example) with defaults as I did, it seemed to do the trick.

It was incredibly frustrating to see PYROOT appear to work and then once you interact with the canvas it just disappear without any type of error coming up.

Replace h0.Draw() with h0.DrawCopy()

This works as it avoids the python garbage collector. h0 disappears as it goes out of scope and gets deleted.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.