Dear Root experts,
I am encountering a memory leak while iterating over an TTree in pyRoot. Its a small local analysis, so using pretty dated versions on my apple laptop, but worked fine up until now - hope someone can help:
I commented out as much as I could to isolate the problem, the remaining code is:
#!/usr/bin/python
#
import string, numpy
import sys,os # for exit etc
import getopt # option parsing
import ROOT
from ROOT import gROOT, TFile, TTree, TCanvas, TPad, TH1F, TH2F, TImage, TGraph
from ROOT import TProfile2D, gPad, gStyle, gDirectory, TAxis, TF1, TLegend, TLine
from ROOT import TPaveLabel, TMultiGraph, TProfile, TSpectrum
from array import array
from locale import atof, atoi
# #######################################################################
# PARSE ARGUMENTS
# #######################################################################
# Usage(): print the usage and quit
def Usage ():
print " "
print "Usage: Plots of line shape per bin"
print " LSperBin.py [args]"
print " Where [args] are any of the following:"
print " --howto : print this help command"
print " --bin xval:yval : single grid bin to look at plots"
print " "
sys.exit(0)
try:
optlist,filelist = getopt.getopt(sys.argv[1:], "", ["howto","bin="])
except getopt.GetoptError, err:
print str(err)
Usage()
sys.exit()
singlebin = 0
doplots = 0
for o, a in optlist:
if o == "--howto":
Usage()
sys.exit()
elif o == "--bin":
singlebin = 1
doplots = 1
print a
xbin = a.split(":",)[0]
ybin = a.split(":",)[1]
# some drawing options and canvas definition for drawing in the loop below
gStyle.SetOptFit(1011);
gROOT.SetStyle("Plain")
gStyle.SetOptStat(0);
# ####################################################################################
# plot line shapes per scan bin
# #####################################################################################
# open the file
rtinput = TFile("tribic2017.root")
meas_tree = gDirectory.Get('measurements')
# get the data grid histogramm
datagrid = gDirectory.Get('dataGrid')
nbx = datagrid.GetXaxis().GetNbins();
nby = datagrid.GetYaxis().GetNbins();
# pick the bin choosen by the command line, or take all
if singlebin == 1:
xstart = int(xbin)
xend = xstart + 1
ystart = int(ybin)
yend = ystart +1
else:
xstart = 0
xend = nbx
ystart = 0
yend = nby
# Define Access to plots in ntuple branches
shapeC3 = TH1F()
meas_tree.SetBranchAddress("shapeC3",shapeC3);
shapeC4 = TH1F()
meas_tree.SetBranchAddress("shapeC4",shapeC4);
# loop over all bins to find those with outliers:
for xbin in range(xstart,xend):
for ybin in range(ystart,yend):
# Event loop
histfound = 0
nentries = meas_tree.GetEntries()
for jentry in xrange (nentries)
rtn = meas_tree.GetEntry(jentry)
if rtn <= 0: continue
if (xbin == meas_tree.gridXbin) and (ybin == meas_tree.gridYbin):
print xbin , ybin, histfound
(while running this loop and printing out the bins etc, python memory usage increases fast in the mac osx memory monitor, reaching several GB in about a minute, and continuing until the laptop runs out of memory.)
The input ntuple is about 43 MB large, and has about 32k entries, two leafs are THF1 histograms.
Any hint on how I could fix this would be most welcome.
Thanks!
Thorsten
PS: I can make the script and nuple available if needed.
ROOT Version: 5.34/23
Platform: Mac OS 10.11.6
Compiler: Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin