Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt FWCore/PythonUtilities to python3 #28439

Merged
merged 3 commits into from Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion FWCore/PythonUtilities/python/XML2Python.py
Expand Up @@ -197,7 +197,7 @@ def root (self):

def topLevel (self):
'''Returns top level object'''
return self._root.attributes().values()[0]
return list(self._root.attributes().values())[0]


@staticmethod
Expand Down
37 changes: 20 additions & 17 deletions FWCore/PythonUtilities/scripts/edmDumpEventContent
@@ -1,12 +1,15 @@
#! /usr/bin/env python

from __future__ import print_function
from builtins import object
import os
import sys
import copy
import optparse
import re
import copy
import subprocess
import six

# define regex
wrapperRE = re.compile (r'edm::Wrapper<(.+)\s*>$')
Expand Down Expand Up @@ -42,8 +45,8 @@ def expandFilename (filename, options):
stdout, stderr = getOutput ('edmFileUtil -d %s' % filename)
if stdout:
return stdout.strip()
raise RuntimeError, "Could not successfully run 'edmFileUtil.' Error %s" \
% stderr
raise RuntimeError("Could not successfully run 'edmFileUtil.' Error %s" \
% stderr)
fabiocos marked this conversation as resolved.
Show resolved Hide resolved


class Branch (object):
Expand All @@ -60,11 +63,11 @@ class Branch (object):
self.type = match.group (1)
else:
# this isn't a type we're interested in
raise ValueError, "Not edm::Wrapper"
raise ValueError("Not edm::Wrapper")
name = trailingDotRE.sub ('', branchInfo.GetName())
pieces = underscoreRE.split (name)
if len (pieces) != 4:
raise ValueError, "%s not formatted as expected" % name
raise ValueError("%s not formatted as expected" % name)
self.typeString = pieces[0] # not currently used
self.module = pieces[1]
self.label = pieces[2]
Expand All @@ -84,8 +87,8 @@ class Branch (object):
found = True
break
if not found:
raise ValueError, "%s does not match regexs provided" \
% self.fullname
raise ValueError("%s does not match regexs provided" \
% self.fullname)
fabiocos marked this conversation as resolved.
Show resolved Hide resolved


def __str__ (self):
Expand Down Expand Up @@ -113,12 +116,12 @@ class Branch (object):
# that order here.
order = ['type', 'module', 'label', 'process']
for obj in branchList:
for key, twoList in lengthDict.iteritems():
for key, twoList in six.iteritems(lengthDict):
attribute = getattr (obj, key)
if len (attribute) + 2 > twoList[0]:
twoList[0] = len (attribute) + 2
totalLength = 0
for twoList in lengthDict.values():
for twoList in list(lengthDict.values()):
totalLength += twoList[0]
useMin = True
if Branch.forceColumns or totalLength < 70:
Expand Down Expand Up @@ -149,10 +152,10 @@ class Branch (object):
'label' : 'Label',
'process' : 'Process',
'fullname' : 'Full Name'}
print title
print '-' * len(title)
print(title)
print('-' * len(title))
for branch in branchList:
print branch
print(branch)

#branches = ''
if __name__ == "__main__":
Expand Down Expand Up @@ -181,17 +184,17 @@ if __name__ == "__main__":
help="Forces printouts to be in nice columns")
options, args = parser.parse_args()
if not args:
print parser.print_usage()
print(parser.print_usage())
sys.exit()
filename = expandFilename (args[0], options)
###################
# process options #
###################
# check for illegal combinations
if options.name and options.all:
raise RuntimeError, "Can notuse '--name' and '--all' options together."
raise RuntimeError("Can notuse '--name' and '--all' options together.")
if options.lumi and options.run:
raise RuntimeError, "Can not use '--lumi' and '--run' options together."
raise RuntimeError("Can not use '--lumi' and '--run' options together.")
if options.name:
Branch.mode = 'name'
elif options.all:
Expand All @@ -218,12 +221,12 @@ if __name__ == "__main__":
# saying we don't have a dictionary for all of the objects in the
# root file. When we've loaded the file, we're turn it back
oldStderr = os.dup( sys.stderr.fileno() )
newStderr = file ( '/dev/null', 'w')
newStderr = open ( '/dev/null', 'w')
os.dup2( newStderr.fileno(), sys.stderr.fileno() )
rootfile = ROOT.TFile.Open( filename )
os.dup2( oldStderr, sys.stderr.fileno() )
if not rootfile:
raise RuntimeError, "Could not open file '%s'." % filename
raise RuntimeError("Could not open file '%s'." % filename)
Tree = rootfile.Get (treeName)
branches = Tree.GetListOfBranches()
branchList = []
Expand All @@ -232,7 +235,7 @@ if __name__ == "__main__":
branch = Branch (branchInfo, regexList)
except Exception as detail:
if options.skipping:
print "Skipping ", detail
print("Skipping ", detail)
continue
branchList.append (branch)
Branch.printList (branchList)
Expand Down
34 changes: 19 additions & 15 deletions FWCore/PythonUtilities/scripts/generateEDF.py
@@ -1,6 +1,10 @@
#! /usr/bin/env python

from __future__ import print_function
from __future__ import division
from builtins import zip
from builtins import object
from past.utils import old_div
from builtins import range
import sys
import re
Expand Down Expand Up @@ -56,7 +60,7 @@ def __init__ (self, line):
self.numXings += 1
except:
raise RuntimeError("Inst Lumi Info malformed")
self.aveInstLum = self.totInstLum / (self.numXings)
self.aveInstLum = old_div(self.totInstLum, (self.numXings))
self.xingInfo = True
self.key = (self.run, self.lumi)
self.keyString = self.key.__str__()
Expand All @@ -76,7 +80,7 @@ def fixXingInfo (self):
self.numXings = 1
xing = 1
self.aveInstLum = self.totInstLum = lum = \
self.delivered / LumiInfo.lumiSectionLength
old_div(self.delivered, LumiInfo.lumiSectionLength)
self.instLums.append( (xing, lum) )
self.xingInfo = True
return True
Expand All @@ -85,7 +89,7 @@ def fixXingInfo (self):
def deadtime (self):
if not self.delivered:
return 1
return 1 - (self.recorded / self.delivered)
return 1 - (old_div(self.recorded, self.delivered))


def __str__ (self):
Expand Down Expand Up @@ -199,7 +203,7 @@ def _integrateContainer (self):
for key, lumi in six.iteritems(self):
total += lumi.recorded
lumi.totalRecorded = total
lumi.fracRecorded = total / self.totalRecLum
lumi.fracRecorded = old_div(total, self.totalRecLum)
# calculate numbers for average xing instantaneous luminosity
if not self.xingInfo:
# nothing to do here
Expand All @@ -224,8 +228,8 @@ def _integrateContainer (self):
lumi = self[tup[1]]
total += lumi.recorded
lumi.totalAXILrecorded = total
lumi.fracAXILrecorded = total / self.totalRecLum
lumi.fracAXIL = lumi.aveInstLum / maxAveInstLum
lumi.fracAXILrecorded = old_div(total, self.totalRecLum)
lumi.fracAXIL = old_div(lumi.aveInstLum, maxAveInstLum)


#############################
Expand Down Expand Up @@ -313,7 +317,7 @@ def makeEDFplot (lumiCont, eventsDict, totalWeight, outputFile, options):
weight += event[1]
if not usePoints:
continue
factor = weight / totalWeight
factor = old_div(weight, totalWeight)
try:
intLum = lumiCont[key].totalRecorded
except:
Expand All @@ -322,7 +326,7 @@ def makeEDFplot (lumiCont, eventsDict, totalWeight, outputFile, options):
if lumiCont.minIntLum and lumiCont.minIntLum > intLum or \
lumiCont.maxIntLum and lumiCont.maxIntLum < intLum:
continue
lumFrac = intLum / lumiCont.totalRecLum
lumFrac = old_div(intLum, lumiCont.totalRecLum)
xVals.append( lumiCont[key].totalRecorded)
yVals.append (factor)
expectedVals.append (lumFrac)
Expand All @@ -337,7 +341,7 @@ def makeEDFplot (lumiCont, eventsDict, totalWeight, outputFile, options):
## Reset Expected ##
####################
if options.resetExpected:
slope = (yVals[-1] - yVals[0]) / (xVals[-1] - xVals[0])
slope = old_div((yVals[-1] - yVals[0]), (xVals[-1] - xVals[0]))
print("slope", slope)
for index, old in enumerate (expectedVals):
expectedVals[index] = yVals[0] + \
Expand Down Expand Up @@ -388,8 +392,8 @@ def makeEDFplot (lumiCont, eventsDict, totalWeight, outputFile, options):
for thisRange in rangeList:
upper = thisRange[1]
lower = thisRange[0]
slope = (yVals[upper] - yVals[lower]) / \
(xVals[upper] - xVals[lower])
slope = old_div((yVals[upper] - yVals[lower]), \
(xVals[upper] - xVals[lower]))
print("slope", slope)
# now go over the range inclusively
pairList = []
Expand Down Expand Up @@ -421,7 +425,7 @@ def makeEDFplot (lumiCont, eventsDict, totalWeight, outputFile, options):
eventTupList.sort()
for eventTup in eventTupList:
weight += eventTup[5]
factor = weight / totalWeight
factor = old_div(weight, totalWeight)
if 'instLum' == options.edfMode:
xVals.append (eventTup[0])
else:
Expand All @@ -433,15 +437,15 @@ def makeEDFplot (lumiCont, eventsDict, totalWeight, outputFile, options):
raise RuntimeError("It looks like Charles screwed up if you are seeing this.")

size = len (xVals)
step = int (math.sqrt(size) / 2 + 1)
step = int (old_div(math.sqrt(size), 2) + 1)
if options.printValues:
for index in range (size):
print("%8f %8f %8f" % (xVals[index], yVals[index], expectedVals[index]), end=' ')
if index > step:
denom = xVals[index] - xVals[index - step]
numer = yVals[index] - yVals[index - step]
if denom:
print(" %8f" % (numer / denom), end=' ')
print(" %8f" % (old_div(numer, denom)), end=' ')
if 0 == index % step:
print(" **", end=' ') ## indicates statistically independent
## slope measurement
Expand All @@ -462,7 +466,7 @@ def makeEDFplot (lumiCont, eventsDict, totalWeight, outputFile, options):

# run statistical tests
if options.weights:
print("average weight per event:", weight / ( size - 1))
print("average weight per event:", old_div(weight, ( size - 1)))
maxDistance = ROOT.TMath.KolmogorovTest (size, yArray,
size, expected,
"M")
Expand Down