_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