TFile Object is Not Iterable

_I have a python code where I read a .txt file that contains several hundred data lines. I write my python code such that I am only extracting the data lines I need:

from itertools import tee, islice
with open('datalines.txt') as f: #opening the file and creating a file object f
    f1, f2 = tee(f) #creating two iterable object f1 and f2 from f
    for l1, l2 in zip(islice(f1, 2, None, 5), islice(f2, 3, None, 5)): ##extracting the 2nd and every fifth line as f1 and the 3rd line and every fifth line as f2 zipping them to l1 and l2 so that f1 and f2 can be iterated over
        print(f'{l1} {l2}') #printing the data, works!
    
        with open('muonsdata.txt', 'a') as fw:                 
            fw.writelines(f'{l1} {l2}')
__

When I try to do the same thing but with a .root file:

from itertools import tee, islice
import ROOT
from ROOT import TFile, TCanvas, TPad, TPaveLabel, TPaveText, TTree, TH1F, TF1
f = ROOT.TFile.Open('data.root', 'read') #opening the file and creating a file object f
f1, f2 = tee(f) #creating two iterable object f1 and f2 from f
for l1, l2 in zip(islice(f1, 2, None, 5), islice(f2, 3, None, 5)): ##extracting the 2nd and every fifth line as 
  f1 and the 3rd line and every fifth line as f2 zipping them to l1 and l2 so that f1 and f2 can be iterated over
    #print(f'{l1} {l2}') #printing the data, works!

f.Close()
    
fw = ROOT.TFile.Open('rdata.root', 'new')
fw.Close()

I get this error: Traceback (most recent call last):
File “”, line 1, in
File “writeroot.py”, line 5, in
f1, f2 = tee(f) #creating two iterable object f1 and f2 from f
TypeError: ‘TFile’ object is not iterable

What am I doing wrong? and How can I fix it?

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


A ROOT file isn’t a text file.

What do you expect tee(f) to do?

And which version of ROOT do you use?

So, I am trying to extract only certain lines from my data file (data.txt, but now I am working with data.root). The file I sent worked fine when I was reading a text file. I was creating two iterable objects f1 and f2 from my file object f, and I wanted f1 and f2 to be the 3rd, 4th, 7th, 8th, 12, 13, etc line; so that is why I put it in a for loop so that f1 and f2 are every fifth line after the 3rd and 4th line, respectively until the end of the line. Can I not do the same thing with a .root file? How can I read and extract data the same way with a .root file? I am using the lastest version of root. 6.18.02.

A ROOT file contains objects - and I bet (looking at your other questions here) that you want to iterate over the entries of a TNtuple. So get that from a file, and then you can iterate:

fw = ROOT.TFile.Open('rdata.root', 'read')
ntuple = fw.ntuplename
for e in ntuple:
  print(e.column1, e.column2)

When I write your suggestion, I get this errror:

Traceback (most recent call last):

File “”, line 1, in
File “/home/cucip/builddir/lib/ROOT.py”, line 533, in _importhook
return _orig_ihook( name, *args, **kwds )
File “/mnt/c/1/writeroot.py”, line 5, in
ntuple = f.ntuplename
AttributeError: TFile object has no attribute ‘ntuplename’

ntuplename is a placeholder for the name you gave your ntuple. I don’t know what it is. Open your file and type gFile.ls() and you should see what it’s called.

Axel.

TNtuple

ROOT User Guides and Manuals

ROOT Tutorials -> Tree tutorials

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