Hi there,
I’m trying to optimize my python code because it feels quite slow.
So in principal I am looping over a Root file with a TTree containing several branches (10GB).
If I draw a 1D histogram of “ADCEnergy” (array of size 2) it takes 4-5 seconds and the total number of entries is 3.5 million.
In my small python script I’m basically doing the same, except for some cuts, however it takes 77 seconds.
file = ROOT.TFile.Open('SomeFile.root', 'read')
Tree = file.Get('tree')
hist1D = ROOT.TH1F('Name', 'Time; ADC Energy [keV]; Events',200, 0, 10000)
canv= ROOT.TCanvas()
Tree.SetBranchStatus('*', 0)
Tree.SetBranchStatus('Par1', 1)
Tree.SetBranchStatus('Par2', 1)
Tree.SetBranchStatus('Par3', 1)
Tree.SetBranchStatus('ADCEnergy', 1)
for event in Tree:
Par1 = event.Par1
if Par1 <= 0.1 or Par1 >= 0.2:
continue
Par2 = event.Par2
if Par2[0] == 0 and Par2[2] == 0 and Par2[3] == 0:
Par3 = event.Par3[0]
if Par3 > 0 and Par3 != 11:
Energy = event.ADCEnergy[0]
hist1D.Fill(Energy)
outputfile = ROOT.TFile('SomeOutputFile.root', 'RECREATE')
hist1D.Write()
hist1D.Draw()
canv.SaveAs('SomePDFFile.pdf','pdf')
Profiling the code leads to this:
ncalls tottime percall cumtime percall filename:lineno(function)
1774819 38.554 0.000 38.554 0.000 ROOT.py:263(TTree__iter_)
1 37.310 37.310 77.711 77.711 testfile.py:3()
1 1.435 1.435 1.460 1.460 ROOT.py:509(__finalSetup)
Is there any way to do it faster? 5s to 77s feels somehow wrong (even the TTree__iter takes 38s though it should be executed in C++, right?).
I tried to use “SetBranchAdress” according to wlav from Iteration over a tree in pyroot - performance issue, but it didn’t work (result is a blank histogram, maybe my syntax is wrong?):
Par1= array('d',[0])
Tree.SetBranchAddress('Par1', Par1)
for event in Tree:
#Par1 = event.Par1
if Par1 <= 0.1 or Par1 >= 0.2:
continue
The branch that is looped over looks like this:
*............................................................................*
*Br 35 :ADCEnergy : ADCEnergy[2]/F *
*Entries : 1774818 : Total Size= 14240332 bytes File Size = 7896807 *
*Baskets : 432 : Basket Size= 52224 bytes Compression= 1.80 *
*............................................................................*
Thanks a lot for your help!