How to read certain parts of a text data for a graph

I am trying to make a graph from some data I took.

I have managed to write a script that will give me the two graphs I am looking for.

import numpy
import ROOT
from ROOT import TGraph
from array import array

filename = “testdata.txt”

x1, y1, z1, a1 = numpy.loadtxt(filename, unpack=True)

x = array(“d”, x1)
y = array(“d”, y1)
z = array(“d”, z1)

c1=ROOT.TCanvas(“c1”, “”, 700, 500)
gr = TGraph(len(x), x,y)
gr.SetTitle(“237 typeD”)
gr.GetXaxis().SetTitle(“Voltage”)
gr.GetYaxis().SetTitle(“Current”)
gr.Draw()
c1.SaveAs(“graph5.png”)

c2=ROOT.TCanvas(“c2”, “”, 700, 500)
gr1 = TGraph(len(x), x,z)
gr1.SetTitle(“2002 typeD”)
gr1.GetXaxis().SetTitle(“Voltage”)
gr1.GetYaxis().SetTitle(“Current”)
gr1.Draw()
c2.SaveAs(“graph6.png”)

This works fine.

The problem I am having, is that the data file I took isn’t so neat as the one I used here.
I took the right values from the real data file, and put them neatly into three columns into another text file, testdata.txt.

I want to be able to graph these same numbers from my original data file.
That is, how do I read only certain parts of the file:
Skip the headers a few rows down, and then certain columns only.
Is there a way to do this? Is it completely different that what I have here?
Thanks!

Hi,

your script is well done.
I guess that the procedure to adopt is not really related to ROOT but on the way the ascii file is organised.
Python is probably one of the most appropriate languages for this kind of operations.

Danilo

As Danilo said this kind of filtering can be conveniently done using Python.

I also wanted to point the TGraph constructor from a filename.
It has some “formatting facility” which might be of some help in your case:
root.cern.ch/doc/master/classTG … 4b6826a434

Of course depending of the complexity of the filtering you want to apply, the python filtering might be the only solution.