root-project / root

The official repository for ROOT: analyzing, storing and visualizing big data, scientifically
https://root.cern
Other
2.71k stars 1.28k forks source link

(Web) graphics does not work from PyROOT #14943

Open jblomer opened 8 months ago

jblomer commented 8 months ago

Check duplicate issues.

Description

When using Python in interactive mode, the web window does not launch properly (see screenshot). It works from a C++ macro. One can also save canvas contents in ROOT batch mode.

image

Reproducer

import ROOT
import numpy as np

def main():
    voltages = np.array([1,2,3], dtype=np.uint32)
    capacitances = np.array([4,5,6], dtype=np.float32)

    #ROOT.gROOT.SetBatch(True)
    df = ROOT.RDF.FromNumpy({'voltages': voltages, 'capacitances': capacitances})
    df = df.Define('inv', '1/(capacitances*capacitances)')

    c = ROOT.TCanvas()
    graph = df.Graph('voltages', 'inv')
    graph.Draw('APL')

    c.SaveAs("graph.pdf")

if __name__ == "__main__":
    main()

Output:

$ python3 -i repro.py 
Info in <THttpEngine::Create>: Starting HTTP server on port 127.0.0.1:9449
Opening in existing browser session.
[102887:102898:0312/160727.268507:ERROR:object_proxy.cc(576)] Failed to call method: org.freedesktop.DBus.Properties.Get: object_path= /org/freedesktop/portal/desktop: org.freedesktop.DBus.Error.InvalidArgs: No such interface “org.freedesktop.portal.FileChooser”
[102887:102898:0312/160727.268546:ERROR:select_file_dialog_linux_portal.cc(285)] Failed to read portal version property
[102887:102887:0312/160727.306050:ERROR:policy_logger.cc(156)] :components/enterprise/browser/controller/chrome_browser_cloud_management_controller.cc(161) Cloud management controller initialization aborted as CBCM is not enabled. Please use the `--enable-chrome-browser-cloud-management` command line flag to enable it if you are not using the official Google Chrome build.
13986 bytes written to file /home/jakob/Documents/CERN/ROOT/demo/graph.pdf
Info in <ProduceImage>: PDF file graph.pdf with 1 pages has been created
>>> 

ROOT version

master

Installation method

From sources

Operating system

Linux

Additional context

No response

jblomer commented 8 months ago

(Hold on, mixed up the reproducer, fixing...)

linev commented 8 months ago

Can you add ROOT.gROOT.SetWebDisplay("chrome") to the macro?

For me it works ok.

jblomer commented 8 months ago

Ok, now the reproducer is the correct one. Adding SetWebDisplay("chrome") doesn't help (also it is already on Chrome).

linev commented 8 months ago

It also does not work with SetWebDisplay("off").

Canvas destroyed before it can be drawn.

I have idea to provide special TCanvas::Draw() handling for python - but never find time for this.

dpiparo commented 8 months ago

This is a blocker for the web graphics.

linev commented 8 months ago

This is a blocker for the web graphics.

It is not.

Normal graphics is also not working with this macro. TCanvas just destroyed at the end of the function

jblomer commented 8 months ago

Right, thanks for pointing this out!

I suggest a change to the documentation (e.g., this page: https://root.cern/manual/python/#interactive-graphics) that mentions that the canvas is better not in function-local scope but in global scope.

linev commented 5 months ago

One can use ROOT.SetOwnership(c, False) at the end of such function - as in tutorials/dataframe/df103_NanoAODHiggsAnalysis.py. This prevent PyROOT from canvas destroying at the end