Looping over large ROOT files killed

Dear experts,

I am using a simple python script to loop over large ROOT files (a total of around 20 GB) and store the weights. At some point the process is killed because of large memory consumption (I suppose!). Here’s the script:

Files = sorted(glob(os.path.join(os.getcwd() , "*.root")))
wtlist = []
for rootFile in Files:
    chain = TChain("Delphes")
    chain.Add(rootFile)
    treeReader = ROOT.ExRootTreeReader(chain)
    numberOfEntries = treeReader.GetEntries()
    branchEvent = treeReader.UseBranch("Event")
    for entry in range(0, numberOfEntries):
        treeReader.ReadEntry(entry)
        if branchEvent.GetEntries() > 0:
           event = branchEvent.At(0)
           wtlist.append(event.Weight)

I am using the ExRootTreeReader since I couldn’t find another way of reading the weights from the tree.
What could be causing the problem?

Thank you,
Amin

Try

Files = sorted(glob(os.path.join(os.getcwd() , "*.root")))
wtlist = []
for rootFile in Files:
    chain = TChain("Delphes")
    chain.Add(rootFile)

treeReader = ROOT.ExRootTreeReader(chain)
numberOfEntries = treeReader.GetEntries()
branchEvent = treeReader.UseBranch("Event")
for entry in range(0, numberOfEntries):
    treeReader.ReadEntry(entry)
    if branchEvent.GetEntries() > 0:
        event = branchEvent.At(0)
        wtlist.append(event.Weight)

Thanks @pcanal
In this form, the list “wtlist” only contains the weights of one file (last ROOT file). I want to store the weights of all ROOT files in the directory.

right … indeed one more line needs to be moved …


Files = sorted(glob(os.path.join(os.getcwd() , "*.root")))
wtlist = []
chain = TChain("Delphes")
for rootFile in Files:
    chain.Add(rootFile)

treeReader = ROOT.ExRootTreeReader(chain)
numberOfEntries = treeReader.GetEntries()
branchEvent = treeReader.UseBranch("Event")
for entry in range(0, numberOfEntries):
    treeReader.ReadEntry(entry)
    if branchEvent.GetEntries() > 0:
        event = branchEvent.At(0)
        wtlist.append(event.Weight)

Works nicely! Thank you @pcanal

Best,
Amin

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