TCanvas not saved as .png inside while loop


ROOT Version: 6.14
Compiler: gcc 5.3.3


Hi, I have a question about TCanvas.SaveAs(.png) in pyROOT

In my script below,

#!/usr/bin/env python
## \file
## \ingroup tutorial_pyroot
## \notebook
## A Simple Graph Example
##
## \macro_image
## \macro_output
## \macro_code
##
## \author Wim Lavrijsen

import os, sys, re
from string import *
from histutil import *
from array import array
from time import sleep
from ROOT import *
NAME = 'LED8data2'
INPFILENAME = '/home/jake/Downloads/LED/LEDAll/%s.txt'  % NAME
#-----------------------------------------------------------------------------

inp = open(INPFILENAME)
   
pt = array('d')
y  = array('d')
ptmin  = 0.0
ptmax  = 0.0
ii = 0
while 1:
   t = split(inp.readline())
   if len(t) == 0: break
   print(t)
   #ptmin, ptmax, count = map(atof, t)
   ptmax = float(t[-2])
   count = float(t[-1])
   pt.append(ptmax)
   y.append(count)
print(pt)
print(y)

c1 = TCanvas( 'Plots/plot_%s' % NAME, 'A Simple Graph Example', 200, 10, 700, 500 )

c1.SetFillColor( 23 )
c1.SetGrid()

n = len(pt)
print(pt)
print(y)

gr = TGraph( n, pt, y )
gr.SetLineColor( 1 )
gr.SetLineWidth( 1 )
gr.SetMarkerColor( 1 )
gr.SetMarkerStyle( 1 )
gr.SetTitle( 'IV curve_%s' % NAME)
gr.GetXaxis().SetTitle( 'Voltage(V)' )
gr.GetYaxis().SetTitle( 'Current(A)' )
gr.Draw( 'AC*' )

# TCanvas.Update() draws the frame, after which one can change it
c1.Update()
c1.GetFrame().SetFillColor( 21 )
c1.GetFrame().SetBorderSize( 12 )
c1.Modified()
c1.Update()
c1.SaveAs('.png')

the code successfully saves .png plot in my working directory ~/root/tutorial/pyroot/Plots

However, when I want to create .png files for multiple .txt files’, so I create a loop structure as below.

#!/usr/bin/env python
## \file
## \ingroup tutorial_pyroot
## \notebook
## A Simple Graph Example
##
## \macro_image
## \macro_output
## \macro_code
##
## \author Wim Lavrijsen

import os, sys, re
from string import *
from histutil import *
from array import array
from time import sleep
from ROOT import *
NAME = os.listdir('/home/jake/Downloads/LED/LEDAll')
GNAME =[]
for x in NAME:
    NEWNAME = '/home/jake/Downloads/LED/LEDAll/'+x
    GNAME.append(NEWNAME)
#-----------------------------------------------------------------------------

for INPFILENAME in GNAME:
   
   inp = open(INPFILENAME)
   

   pt = array('d')
   y  = array('d')
   ptmin  = 0.0
   ptmax  = 0.0
   ii = 0
   while 1:
      t = split(inp.readline())
      if len(t) == 0: break
      print(t)
      #ptmin, ptmax, count = map(atof, t)
      ptmax = float(t[-2])
      count = float(t[-1])
      pt.append(ptmax)
      y.append(count)
   print(pt)
   print(y)

   c1 = TCanvas( 'Plots/plot_%s' % INPFILENAME, 'A Simple Graph Example', 200, 10, 700, 500 )

   c1.SetFillColor( 23 )
   c1.SetGrid()

   n = len(pt)

   gr = TGraph( n, pt, y )
   gr.SetLineColor( 1 )
   gr.SetLineWidth( 1 )
   gr.SetMarkerColor( 1 )
   gr.SetMarkerStyle( 1 )
   gr.SetTitle( 'IV curve_%s' % INPFILENAME)
   gr.GetXaxis().SetTitle( 'Voltage(V)' )
   gr.GetYaxis().SetTitle( 'Current(A)' )
   gr.Draw( 'AC*' )

   # TCanvas.Update() draws the frame, after which one can change it
   c1.Update()
   c1.GetFrame().SetFillColor( 21 )
   c1.GetFrame().SetBorderSize( 12 )
   c1.Modified()
   c1.Update()
   c1.SaveAs('.png')

It does not generate any .png file.

With some argument, If I modify the last line as

HNAME = INPFILENAME[:-4]
c1.SaveAs('%s.png'%HNAME)

It saves .png files

BUT not in Plots directory but in the directory of location of .txt files.

Can someone explain this odd behavior and how I can resolve this issue so that I can save the while loop’s plots in Plots directory?

Thank you so much!!

Give the full path in c1.SaveAs() instead of in the canvas’ name

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.