Display canvas

HI everybody :smiley:

I’m trying to use a pyROOT, i have took the example that it is on the guide of ROOT:
from ROOT import gROOT, TCanvas, TF1

gROOT.Reset()
c1 = TCanvas( ‘c1’, ‘Example with Formula’, 200, 10, 700, 500 )

Create a one dimensional function and draw it

fun1 = TF1( ‘fun1’, ‘abs(sin(x)/x)’, 0, 10 )
c1.SetGridx()
c1.SetGridy()
fun1.Draw()
c1.Update()

I have saved in the file with .py extension and from terminal I have launched the macro, but I don’t see the canvas… and I obtain the same result with the example in the tutorial folder of ROOT.
Why?? How can I do?

Thank you :wink:

$ cd $ROOTSYS/tutorials/pyroot
$ python hsimple.py 

produces a canvas for me.

This comand produce a canvas for 1 or 2 second and than disappears…
I need that the canvas remains

[code]$ cd $ROOTSYS/tutorials/pyroot
$ python

execfile(“hsimple.py”);
quit();[/code]

YESSS!!! Thank you!! Now the canvas remains visible =D>

What command is execfile(“hsimple.py”); ??

And I have do this command for each macro that i produce?

And there is a method for import all lib ROOT, because if i do :
import ROOT don’t work, I have to do for example:
from ROOT import gROOT, TCanvas, TF1

for all object…

import ROOT
ROOT.gROOT
ROOT.TCanvas(…)
ROOT.TF1(…)

See also: PyROOT

This I know, but the question was, it is possible with only 1 command import all?

from ROOT import *; # not supported under IPython

If I do:

from ROOT import gROOT, TCanvas, TF1 , TGraph , ecc

work

Sorry Pepe Le Pew,

Can you tell me if there is a method for not write always " execfile(“macro.py”) " for to work a macro?

I have the same problem here, I have successfully installed ROOT + PyROOT (ver. 6.06/08 +python34 on OSX using macports). Everything works, the only problem is that the plots are shown on the screen, but immediately disappear. I have tried the above suggestions which did not help. Also tried:

import os os.popen(‘python graph.py’)

because execfile seems to be deprecated. also didn’t help. Any ideas?

Hi,

in all cases, you have to make sure that the python process that is displaying the canvas is alive for the duration that you want to have the canvas displayed.

E.g. in this:import os os.popen(‘python graph.py’)you create a subprocess that exists after the end of the script.

As for execfile, you can compile the script and exec it, but what is wrong with simply importing it?

Cheers,
Wim

I managed to do it using the following hint from http://stackoverflow.com/a/437857 by:

inside a python session. Plot is shown and stays. But this does not seem to be the most convenient way. I was expecting a sort of way to tell PyROOT to wait after plotting, like plots in matplotlib, so that one can just run the script as a stand alone program by:

After some digging, it seems there might be some hints in the call to ROOT.gROOT.IsBatch(). But I am absolutely no expert, so I am not sure.

Finally I found a trick. I added the following line at the end of the tutorial file graph.py:

Then it worked as expected. The reason is that the outer python process is kept alive.

cheers!
:smiley: