PyRoot: Iteration to a TTree

Dear experts,
I’m using pyroot installed by conda. I am trying to add a new branch to an existing root file, and write it to a new .root file.
So, I wrote

old_Tree= TChain("tree")
old_Tree.Add(inputfile)    # Contain 1 million events , with the storage of 1.3 GB
Outputfile =TFile(inputfile,"RECREATE")
Outputfile.cd()

Efficiency = get_eff()  # a function to get a weigt factor event-by-event , and return a numpy object
new_Tree=old_Tree.CloneTree(0)
newBranches={
    "Eff" : array.array('f', [0]) , 
}
for key, val in newBranches.items():
    newTree.Branch(key, val, f"{key}/F")

Then, I looped over the old tree to fill the new tree, and write it to a file

''' Method #1 ''' 
evt_i = 0 
for evt in old_Tree:
    newBranches["Eff"][0] = Efficiency[evt_i]
    newTree.Fill()
    evt_i += 1 
newTree.Write()
Outputfile.Close()

But it raised an Error saying

Error in <TBufferFile::WriteByteCount>: bytecount too large (more than 1073741822)
Error in <TBufferFile::WriteByteCount>: bytecount too large (more than 1073741822)
Error in <TList::Clear>: A list is accessing an object (0x7ffc5f02b080) already deleted (list name = TList)

And I tried another way to loop over the tree via

''' Method #2 ''' 
for evt_i in range(old_Tree.GetEntries()):
   Input_Chain.GetEntry(evt_i)
   newBranches["Eff"][0] = Efficiency[evt_i]
   newTree.Fill()
newTree.Write()
Outputfile.Close()

At this time, no error raised by the TBufferFile

Could anyone tell me what’s the difference between such two method when looping a TTree in pyroot


ROOT Version: 6.28/04
Platform: centos7


For the Error of TBufferFile. Another clues found .
The one which will raise the Error, I coulde use

if evt_i > 100000:
   break

to avoid the Error

But for the output file , when I type

root -l output.root
root [0] 
Attaching file output.root as _file0...
(TFile *) 0x555e3d7c9450
root [1] .ls
TFile**		output.root	
 TFile*		output.root	
  KEY: TTree	DecayTree;1	DecayTree

Whille the successful one , it is something like this

root -l output.root
root [0] 
Attaching file output.root as _file0...
ls(TFile *) 0x55765e24d960
root [1] .ls
TFile**		output.root	
 TFile*		output.root	
  KEY: TTree	DecayTree;24	DecayTree[current cycle]
  KEY: TTree	DecayTree;23	DecayTree[backup cycle]

There is [current cycle] and [backup cycle] in the TFile for the successful one. Maybe it is the reason.

And on the other hand, just after i solve the problem by Method #2 . I can not repeat the Problem via Method #1 , even if I use git reset --hard to reset my code to previous commit.

It looks so confusing, but it acctually happened.

In my opinion , the most probabable situation is that it is just a bug of the Write() function in TTree Class ?

Hi,

Thanks for the investigation!
If I understand correctly, you cannot reproduce the initial problem, do you?

Cheers,
Danilo

Hi, Danilo,

Yes, I can not reproduce it now. Even if I use git reset --hard to set my code to the previous version.

Thanks for reporting.
Let’s keep an eye on this, and if the issues re-appear, we can rediscuss and if needed open an issue on GH.

Cheers,
Danilo

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