Opening canvas in a pyroot program

It looks like the file output.txt might be miss-formatted… can you post it here ? (or part of it)

Here is the function that writes two integers to output.txt for each iteration.

def output_1(ns2, searcharray, outputfile):
for i in range (0, ns2+1):
t = searcharray[i]
ix = t[0]
iy = t[1]
outputfile.write("%d %d \n" % (iy, ix))

Here is the code that calls the function.

outputfile = open(‘output.txt’, ‘w’)
output_1(ns2, searcharray, outputfile)

Thanks. But I think it will be simpler if you could post the file “output.txt”.

As an example, output.txt could look like this:
output.txt (15 Bytes)

It seems to be a Python issue. When I do the same in C++ it is fine.


root [0] TTree *prob_bins = new TTree("prob_bins", "prob_bins")
(TTree *) 0x7fb39d593810
root [1] prob_bins->ReadFile("output.txt", "y:x")
(Long64_t) 3
root [2] TH2F *hist = new TH2F("hist_name", "hist_title", 10, 0, 10, 10, 0, 10)
(TH2F *) 0x7fb39d2a1d40
root [3] prob_bins->Draw("y:x >> hist_name", "", "colz")
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
(Long64_t) 3
root [4] 

Yes, I think that PyROOT in general has some preconditions before a canvas will even display permanently on the screen:

To even get the canvases to display when I run my code, I have to write:

$ python

execfile(“prob_bin.py”)

In other words, if I run this program from the linux command line

$ python prob_bin.py

the canvases do not even display for more than a second before they vanish (see Display canvas)

I think this example might have (at the end) what you are looking for:

root.cern.ch/doc/master/demo_8py.html

This segment?

53 ## wait for input to keep the GUI (which lives on a ROOT event dispatcher) alive
54 if name == ‘main’:
55 rep = ''
56 while not rep in [ ‘q’, ‘Q’ ]:
57 rep = raw_input( 'enter “q” to quit: ’ )
58 if 1 < len(rep):
59 rep = rep[0]

Yes I think so…

Thanks for the tip. Although for the first time I run the code in a new python window the canvas still does not generate, for every subsequent time I rerun the code in the same window the canvas generates according to the parameters I set for that iteration.

your file in aaa.py:

import os, sys
import ROOT

#Create an empty histogram and fill it with the detected problem bins.
c2 = ROOT.TCanvas('c2', 'c2', 700, 500)
prob_bins = ROOT.TTree("prob_bins", "prob_bins")
prob_bins.ReadFile("output.txt", "y:x")
hist = ROOT.TH2F("hist_name", "hist_title", 10, 0, 10, 10, 0, 10)
c2.SetGrid()
prob_bins.Draw("y:x >> hist_name", "", "colz")

Then:

$ python

>>> execfile("aaa.py")

Here is the draft of the code in its entirety, and the corresponding example root file that it analyzes. I have added your suggestion but I still encounter the same problem.
dif_etaphi4.root (15.8 KB)
search_test2.2.py (5.88 KB)

Actually I found out that the suggestion is not needed.

Remove it.

Enter python just typing “python” and then at the python prompt type

excecfile(“search_test2.2.py”)

it works for me.

Still does not work for me. You mean “execfile”, not “excecfile”.

After “prob_bins.Draw(…)”, try to add:
c2.Modified(); c2.Update()

Hi,

I ran the script on my setup with a simple “python search_test2.2.py” successfully (I could even enter the cuts and refresh the plot accordingly).
Of course Olivier meant “execfile”…

D

Thank you much couet, Pepe Le Pew, and dpiparo for your suggestions…and for continuing advise me, many thanks.

Can I ask you what operating systems you are working in? I am working in Ubuntu 16.04 and wonder if this has something to do with it.

I am much happy that couet and dipiparo can run the program successfully on your machines.

Hi,

I tested it on 10.11.14 and kubuntu 15.
I wonder if you can reduce the problem to a minimal set of lines.

Cheers,
Danilo

So when I reduce the code (which I attach along with output2.txt and the same .root file) to just reading in data I have already written, I get no error message and the canvases both display…
output2.txt (12 Bytes)
test.py (714 Bytes)
dif_etaphi4.root (15.8 KB)