No interactive ROOT Canvas with qtconsole & ROOT 6
See original GitHub issueI like to work with rootpy (great piece of work btw) within a jupyter qtconsole. As JSROOT still misses some features and I prefer drawables to be displayed in popup-windows rather than inline, I still rely on the classical ROOT canvases.
With ROOT 5.36/36 the following script brings up such a canvas – thanks to rootpy it can be used interactively:
import rootpy as rpy
import rootpy.ROOT as R
import rootpy.plotting
print R.gROOT.GetVersion() #call R once
R.gROOT.SetBatch(0)
get_ipython().display_formatter.formatters['image/png'].enabled = False
h = R.TH1F("test", "test", 100, -5, 5)
h.FillRandom("gaus")
can = R.TCanvas() #empty ROOT Canvas pops up
h.Draw() #interactive histogram is drawn into ROOT Canvas
However this is not possible with newer ROOT Versions, e.g. ROOT 6.10/06
In [1]: %run ~/Desktop/ext_canvas.py
Welcome to JupyROOT 6.10/06
ERROR:ROOT.TGClient.TGClient] only one instance of TGClient allowed
6.10/06
---------------------------------------------------------------------------
ROOTError Traceback (most recent call last)
/home/philipp/Desktop/ext_canvas.py in <module>()
10 h.FillRandom("gaus")
11
---> 12 can = R.TCanvas() #empty ROOT Canvas pops up
13 h.Draw() #interactive histogram is drawn into ROOT Canvas
/home/philipp/.virtualenvs/python2-CB/lib/python2.7/site-packages/rootpy-1.0.0.dev0-py2.7.egg/rootpy/__init__.pyc in __new__(self, *args, **kwargs)
247 class asrootpy_cls(result):
248 def __new__(self, *args, **kwargs):
--> 249 return asrootpy(thing(*args, **kwargs), warn=warn)
250 asrootpy_cls.__name__ = '{0}_asrootpy'.format(thing.__name__)
251 return asrootpy_cls
/home/philipp/.virtualenvs/python2-CB/lib/python2.7/site-packages/rootpy-1.0.0.dev0-py2.7.egg/rootpy/__init__.pyc in __new__(self, *args, **kwargs)
247 class asrootpy_cls(result):
248 def __new__(self, *args, **kwargs):
--> 249 return asrootpy(thing(*args, **kwargs), warn=warn)
250 asrootpy_cls.__name__ = '{0}_asrootpy'.format(thing.__name__)
251 return asrootpy_cls
/home/philipp/.virtualenvs/python2-CB/lib/python2.7/site-packages/rootpy-1.0.0.dev0-py2.7.egg/rootpy/logger/magic.pyc in intercept_next_line(f, why, *args)
243 if sys.version_info[0] < 3:
244 #raise exception.__class__, exception, traceback
--> 245 raise exception
246 raise exception.with_traceback(traceback)
247
ROOTError: level=3000, loc='TGClient::TGClient', msg='only one instance of TGClient allowed'
I suppose this to be related to the notebook features added to core ROOT 6. Now, not only rootpy but also JupyROOT both are trying to “catch the canvas” to be drawn, which results in above exception.
But it would be so nice to get the workflow outlined above back to rootpy also when run in conjunction with ROOT 6.
Btw, how does rootpy deals with features that are making it into core ROOT in general? E.g. the drawing of histograms/canvases as png images within an interactive jupyter session. I met it as first in rootpy, but nowadays it seems to be adapted by JupyROOT?
Once again, I really like rootpy – many thanks for providing it to the community!!
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (2 by maintainers)
Just a workaround, based on the previous post:
Seems if the GUI is properly initialised (after the ROOT import and creation of one canvas), then afterwards everything works.
@phi-mah You are absolutely correct and this is also more general and not related with jupyter only. The question is why does batch mode get activated without asking for it? I have already partially discussed this in my previous post: importing anything from rootpy.plotting activates plotting/func.py file too, then something gets broken and after rootpy’s
finalSetup()
will get activated there is no way to get interactive mode back.Temporary solution could be what you did, i.e triggering PyROOT’s version of
finalSetup()
with calls likeROOT.gROOT.SetBatch(0)
,ROOT.gPad
,ROOT.gStyle
, etc. If this is done before importing anything from rootpy.plotting, than sincefinalSetup()
is being done only once, rootpy’sfinalSetup()
becomes obsolete and we have interactive mode. So you could in principle place simpleROOT.gPad
before this line https://github.com/rootpy/rootpy/blob/457e074056a916fff848978ef68b7f5107856e47/rootpy/plotting/__init__.py#L11 and without complicated setups and worrryng about the import order in your code, everything should work fine.But again, as I said this makes rootpy’s
finalSetup()
obsolete, which by design isn’t very desirable. Better idea would be to investigate what’s wrong with TF[1-3] functions and fix it. I have already pointed problematic spot in the code (in my previous post) and have some idea how to fix it, but not very happy with my current solution, plus I still don’t understand what exactly forces rootpy to activate the batch mode. Anyway I will attempt to make a PR in this week and maybe somebody more experienced can get better explanation and solution then.