ROOT Python Plotting TTree with TGraph Formatting Issue

dt-stormer-verlet.root (708.2 KB)
readroot.py (1.5 KB)
I’m trying to format a TTree graph that I’m piping to a TGraph object (g1) in python 2.7.

Here’s a look at the libraries.

import ROOT
from ROOT import TTree,TFile,gROOT
from ROOT import TCanvas,TGraph,TPad,TBrowser

I read in the root tree file.

file=TFile("dt-stormer-verlet.root", "read");
tree=file.Get("time-sequence")

Then I try to plot the two branches (box.atoms.KE_PE_V_AVG:box.atoms.dt)

c=TCanvas("c", "canvas", 800, 800)
c.GetFrame().SetBorderSize(12)
c.cd()

tree.SetLineStyle(3)
tree.SetLineColor(2)
tree.SetLineWidth(4)
tree.SetMarkerColor(1)
tree.SetMarkerStyle(2)

tree.Draw("box.atoms.KE_PE_V_AVG:box.atoms.dt>>g1","dt<2","")
g1=TGraph(tree.GetSelectedRows(),tree.GetV2(),tree.GetV1())
g1.SetTitle( 'a simple graph' )
g1.Draw("")
g1.SetTitle( 'a simple graph' )
g1.GetXaxis().SetTitle('X title')
g1.GetYaxis().SetTitle("Y title")

c.SetGrid()
c.Modified()
c.Update()
text=raw_input()

The issue I am having is the title, and axis labels are not printing on the canvas and I am not sure why. Suggestions, questions, solutions would be greatly appreciated. Thanks!

So I ended up solving this on my own. I was not sure why the X and Y axis titles were not printing on the TGraph. Here is the solution:

There were two issues.

1.) the tree object will not filled or not actually being pointed to. So to access branches of the tree (that are std::vectors) I need to call tree in a for loop (I am not sure why this works).

file=TFile("dt-stormer-verlet.root", "read");
tree=file.Get("time-sequence")

for i, event in enumerate(tree):
    print("{}".format(event))

2.) I wasn’t using the Draw() module correctly. The order matters, here’s the order.

tree.Draw("box.atoms.KE_PE_V_AVG:box.atoms.dt","dt<2","L")
g1=TGraph(tree.GetSelectedRows(),tree.GetV2(), tree.GetV1())
g1.SetTitle('')
g1.GetXaxis().SetTitle('Omega_dt')
g1.GetYaxis().SetTitle("Average Total Energy")
g1.Draw("A*L")

Dear @Food ,
Thank you for posting the solution to your problem so that others may benefit from it. Glad it is working now.
Best,
Enric