PYROOT TGraph blank canvas from function

Hello,

I am attempting to create a simple TGraph of information from a root file. All the PyROOT demos work appropriately and I am essentially attempting to recreate the graph.py demo. However, when I run my code from the command line as:

python Plot.py

The resulting canvas is blank. If I run the code from the interpreter, the canvas is again blank until I ‘quit’, due to the piece of code at the end of my Plot.py file to keep the canvas from disappearing once the code was executed. After I ‘quit’ the code, then the canvas is filled with the TGraph I intended to plot. It also works if I remove the code from the function definition and simply execute the file from the interpreter with execfile(). Below is the code I have:

def fileTimePlots(path, Act, Eng):
    L = []
    X = []
    fname = path + "/INFO/Activation_Scalers.txt"
    with open(fname,"r") as f:
        line1 = int(f.readline().strip().split()[-1])
        line2 = int(f.readline().strip().split()[-1])
    T0 = line1-line2
    fname = path + "/DATA/gamma/" + "GammaData_{}_{}MeV.root".format(Act.replace("_",""), Eng)
    tfile = ROOT.TFile(fname)
    ttree = tfile.fileInfo
    for ev in ttree:
        L.append((datetime.strptime((ev.DateTime).rstrip('\x00'),"%Y%m%d.%H%M%S")-datetime(1970,1,1)).total_seconds())
    L.sort()
    L = [int(T-T0) for T in L]
    x = array('i', L)
    for key in tfile.GetListOfKeys():
        if key.GetName() != "fileInfo":
            X.append(int((key.GetName()).split("_")[3]))
    y = array('i',X)
    n = len(L)
    print x
    print y
    gr = ROOT.TGraph(n,x,y)
    gr.SetMarkerStyle(20);
    gr.SetMarkerSize(.6);
    gr.Draw("ZPA")

I am trying to understand why the TGraph does not appear when this function is run.

I was able to get the same behavior from the graph.py tutorial by adding

## wait for input to keep the GUI (which lives on a ROOT event dispatcher) alive
if __name__ == '__main__':
   rep = ''
   while not rep in [ 'q', 'Q' ]:
      rep = raw_input( 'enter "q" to quit: ' )
      if 1 < len(rep):
         rep = rep[0]

and then running the graph.py from the command line with ‘python graph.py’. There again, the canvas was blank.

I am wanting to call this function from a gui, and see the resulting plot. I have checked that all the data in the arrays is correct, but there must be some pythonic issue that I don’t understand that keeps the TGraph from actually plotting.

Thank you.


ROOT Version: 6.14.06
Platform: OSX 10.14
Compiler: Not Provided


Have you tried adding an Update after the draw?

ROOT.gPad.Update()

What happens if you click on the canvas, does it refresh and the graph appears?

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