question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No interactive ROOT Canvas with qtconsole & ROOT 6

See original GitHub issue

I 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:open
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
fedxacommented, Apr 22, 2018

Just a workaround, based on the previous post:

import rootpy.ROOT as ROOT
ROOT.Canvas().Close()
import rootpy.plotting

Seems if the GUI is properly initialised (after the ROOT import and creation of one canvas), then afterwards everything works.

0reactions
rakabcommented, Aug 13, 2018

The problem with the TCanvas::Init routine arises if it get firstly called in batch mode (doing its init stuff within this state) and later on “real” canvases are requested to be drawn. So you have to ensure by all means that the init part is also done in interactive mode. In my specific case even my rootlogon.C led to such a call, which is why I ended up with this rather complicated startup routine:

@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 like ROOT.gROOT.SetBatch(0), ROOT.gPad, ROOT.gStyle, etc. If this is done before importing anything from rootpy.plotting, than since finalSetup() is being done only once, rootpy’s finalSetup() becomes obsolete and we have interactive mode. So you could in principle place simple ROOT.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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No root canvas window on Ubuntu - ROOT Forum
Hi, I've been using ROOT on Windows for historical reasons and now I'd really like to move to linux and ROOT 6 but...
Read more >
Module: display — IPython 8.7.0 documentation
Create a GeoJSON display object given raw data. Parameters. data (dict or list) – VegaLite data. Not an already-serialized JSON string. Scalar types...
Read more >
python display image on screen
Tap the Send icon. Method 2: Use the Python print () function The print () function in Python prints a message to the...
Read more >
IPython Documentation - Read the Docs
IPython has support for interactive, non- blocking control of GTK, Qt, WX, ... PR #2695: Key the root modules cache by sys.path entries....
Read more >
ROOT 6 Web Display
To display ROOT 6 canvases in the web browser. ... Implementation does not provide some interactive features - like custom mouse events handling....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found