Skip to content

Commit

Permalink
Merge pull request #26411 from davidlange6/xrange_reco
Browse files Browse the repository at this point in the history
xrange migration for python3
  • Loading branch information
cmsbuild committed Apr 12, 2019
2 parents c4df47f + af7a3a7 commit 7965eaa
Show file tree
Hide file tree
Showing 98 changed files with 188 additions and 90 deletions.
@@ -1,3 +1,4 @@
from builtins import range
from PhysicsTools.Heppy.analyzers.core.TreeAnalyzerNumpy import TreeAnalyzerNumpy
from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
#from ROOT import TriggerBitChecker
Expand Down Expand Up @@ -168,7 +169,7 @@ def fillTree(self, event, resetFirst=True):
if isinstance(c, tuple) and isinstance(c[0], AutoHandle):
if not isMC and c[-1].mcOnly: continue
objects = self.handles[cn].product()
setattr(event, cn, [objects[i] for i in xrange(objects.size())])
setattr(event, cn, [objects[i] for i in range(objects.size())])
c = c[-1]
if not isMC and c.mcOnly: continue
if self.scalar:
Expand Down
@@ -1,4 +1,5 @@
from __future__ import print_function
from builtins import range
import itertools

from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
Expand Down Expand Up @@ -52,7 +53,7 @@ def process(self, event):
if eid != self.lastId:
#import pdb; pdb.set_trace()
history = event.input.object().processHistory()
for i in reversed(range(history.size())):
for i in reversed(list(range(history.size()))):
conf = history.at(i)
release = conf.releaseVersion().replace('"',"")
vnums = self.cmsswVNums(release)
Expand Down
7 changes: 4 additions & 3 deletions PhysicsTools/Heppy/python/analyzers/core/autovars.py
Expand Up @@ -6,6 +6,7 @@
#
# TODO: more documentation needed here!

from builtins import range
class NTupleVariable:
"""Branch containing an individual variable (either of the event or of an object), created with a name and a function to compute it
- name, type, help, default: obvious
Expand Down Expand Up @@ -174,7 +175,7 @@ def makeBranchesScalar(self,treeNumpy,isMC):
treeNumpy.var("n"+self.name, int)
allvars = self.objectType.allVars(isMC)
for v in allvars:
for i in xrange(1,self.maxlen+1):
for i in range(1,self.maxlen+1):
h = v.help
if self.help: h = "%s for %s [%d]" % ( h if h else v.name, self.help, i-1 )
treeNumpy.var("%s%d_%s" % (self.name, i, v.name), type=v.type, default=v.default, title=h, filler=v.filler)
Expand All @@ -195,7 +196,7 @@ def fillBranchesScalar(self,treeNumpy,collection,isMC):
num = min(self.maxlen,len(collection))
treeNumpy.fill("n"+self.name, num)
allvars = self.objectType.allVars(isMC)
for i in xrange(num):
for i in range(num):
o = collection[i]
for v in allvars:
treeNumpy.fill("%s%d_%s" % (self.name, i+1, v.name), v(o))
Expand All @@ -209,7 +210,7 @@ def fillBranchesVector(self,treeNumpy,collection,isMC):
allvars = self.objectType.allVars(isMC)
for v in allvars:
name="%s_%s" % (self.name, v.name) if v.name != "" else self.name
treeNumpy.vfill(name, [ v(collection[i]) for i in xrange(num) ])
treeNumpy.vfill(name, [ v(collection[i]) for i in range(num) ])
def __repr__(self):
return "<NTupleCollection[%s]>" % self.name

Expand Down
@@ -1,3 +1,4 @@
from builtins import range
import operator
import itertools
import copy
Expand Down
9 changes: 5 additions & 4 deletions PhysicsTools/Heppy/python/analyzers/gen/GeneratorAnalyzer.py
@@ -1,4 +1,5 @@
from __future__ import print_function
from builtins import range
from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
from PhysicsTools.Heppy.physicsutils.genutils import isNotFromHadronicShower, realGenMothers, realGenDaughters
Expand Down Expand Up @@ -84,12 +85,12 @@ def makeMCInfo(self, event):
# " ".join("%d[%d]" % (p.daughter(i).pdgId(), p.daughter(i).status()) for i in xrange(p.numberOfDaughters())))
if id in self.savePreFSRParticleIds:
# for light objects, we want them pre-radiation
if any((p.mother(j).pdgId() == p.pdgId()) for j in xrange(p.numberOfMothers())):
if any((p.mother(j).pdgId() == p.pdgId()) for j in range(p.numberOfMothers())):
#print " fail auto-decay"
continue
else:
# everything else, we want it after radiation, i.e. just before decay
if any((p.daughter(j).pdgId() == p.pdgId() and p.daughter(j).status() > 2) for j in xrange(p.numberOfDaughters())):
if any((p.daughter(j).pdgId() == p.pdgId() and p.daughter(j).status() > 2) for j in range(p.numberOfDaughters())):
#print " fail auto-decay"
continue
# FIXME find a better criterion to discard there
Expand All @@ -110,7 +111,7 @@ def makeMCInfo(self, event):
if interestingPdgId(mom.pdgId()) or (getattr(mom,'rawIndex',-1) in keymap):
#print " interesting mom"
# exclude extra x from p -> p + x
if not any(mom.daughter(j2).pdgId() == mom.pdgId() for j2 in xrange(mom.numberOfDaughters())):
if not any(mom.daughter(j2).pdgId() == mom.pdgId() for j2 in range(mom.numberOfDaughters())):
#print " pass no-self-decay"
ok = True
# Account for generator feature with Higgs decaying to itself with same four-vector but no daughters
Expand Down Expand Up @@ -170,7 +171,7 @@ def makeMCInfo(self, event):
print("%3d {%6d}: %+8d %3d : %8.2f %+5.2f %+5.2f : %d %2d : %+8d {%3d}: %s" % ( ip,p.rawIndex,
p.pdgId(), p.status(), p.pt(), p.eta(), p.phi(), len(moms), p.numberOfDaughters(),
p.motherId, p.motherIndex,
" ".join("%d[%d]" % (p.daughter(i).pdgId(), p.daughter(i).status()) for i in xrange(p.numberOfDaughters()))))
" ".join("%d[%d]" % (p.daughter(i).pdgId(), p.daughter(i).status()) for i in range(p.numberOfDaughters()))))
if verbose:
print("\n\n")

Expand Down
3 changes: 2 additions & 1 deletion PhysicsTools/Heppy/python/analyzers/gen/LHEAnalyzer.py
@@ -1,3 +1,4 @@
from builtins import range
from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
import PhysicsTools.HeppyCore.framework.config as cfg
Expand Down Expand Up @@ -43,7 +44,7 @@ def process(self, event):
lBar=None
nu=None
nuBar=None
for i in xrange(0,len(pup)):
for i in range(0,len(pup)):
id=hepeup.IDUP[i]
status = hepeup.ISTUP[i]
idabs=abs(id)
Expand Down
1 change: 1 addition & 0 deletions PhysicsTools/Heppy/python/analyzers/objects/JetAnalyzer.py
@@ -1,4 +1,5 @@
from __future__ import print_function
from builtins import range
import math, os
from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
Expand Down
@@ -1,4 +1,5 @@
from __future__ import print_function
from builtins import range
from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
from PhysicsTools.Heppy.physicsobjects.Electron import Electron
Expand Down Expand Up @@ -543,7 +544,7 @@ def plausible(rec,gen):
lep.mcLep=gen

def isFromB(self,particle,bid=5, done={}):
for i in xrange( particle.numberOfMothers() ):
for i in range( particle.numberOfMothers() ):
mom = particle.mother(i)
momid = abs(mom.pdgId())
if momid / 1000 == bid or momid / 100 == bid or momid == bid:
Expand Down
1 change: 1 addition & 0 deletions PhysicsTools/Heppy/python/physicsobjects/Jet.py
@@ -1,3 +1,4 @@
from builtins import range
from PhysicsTools.Heppy.physicsobjects.PhysicsObject import *
from PhysicsTools.HeppyCore.utils.deltar import deltaPhi
import math
Expand Down
3 changes: 2 additions & 1 deletion PhysicsTools/Heppy/python/physicsutils/JetReCalibrator.py
@@ -1,4 +1,5 @@
from __future__ import print_function
from builtins import range
import ROOT
import os, types
from math import *
Expand Down Expand Up @@ -88,7 +89,7 @@ def rawP4forType1MET_(self, jet):
if emf > self.type1METParams['skipEMfractionThreshold']:
return None
if self.type1METParams['skipMuons']:
for idau in xrange(jet.numberOfDaughters()):
for idau in range(jet.numberOfDaughters()):
pfcand = jet.daughter(idau)
if pfcand.isGlobalMuon() or pfcand.isStandAloneMuon():
p4 -= pfcand.p4()
Expand Down
@@ -1,4 +1,5 @@
from __future__ import print_function
from builtins import range
import ROOT
import math

Expand Down
9 changes: 5 additions & 4 deletions PhysicsTools/Heppy/python/physicsutils/genutils.py
@@ -1,3 +1,4 @@
from builtins import range
from PhysicsTools.Heppy.physicsobjects.PhysicsObjects import printOut
from PhysicsTools.Heppy.physicsobjects.PhysicsObjects import GenParticle

Expand Down Expand Up @@ -63,7 +64,7 @@ def isPromptLepton(lepton, beforeFSR, includeMotherless=True, includeTauDecays=F


def isNotFromHadronicShower(l):
for x in xrange(l.numberOfMothers()):
for x in range(l.numberOfMothers()):
mom = l.mother(x)
if mom.status() > 2: return True
id = abs(mom.pdgId())
Expand All @@ -85,7 +86,7 @@ def realGenDaughters(gp,excludeRadiation=True):
realGenDaughters(X, excludeRadiation=True) = { b, c }
realGenDaughters(X, excludeRadiation=False) = { a, b, c }"""
ret = []
for i in xrange(gp.numberOfDaughters()):
for i in range(gp.numberOfDaughters()):
dau = gp.daughter(i)
if dau.pdgId() == gp.pdgId():
if excludeRadiation:
Expand All @@ -100,7 +101,7 @@ def realGenMothers(gp):
"""Get the mothers of a particle X going through intermediate X -> X' chains.
e.g. if Y -> X, X -> X' realGenMothers(X') = Y"""
ret = []
for i in xrange(gp.numberOfMothers()):
for i in range(gp.numberOfMothers()):
mom = gp.mother(i)
if mom.pdgId() == gp.pdgId():
ret += realGenMothers(mom)
Expand All @@ -110,7 +111,7 @@ def realGenMothers(gp):

def lastGenCopy(gp):
me = gp.pdgId();
for i in xrange(gp.numberOfDaughters()):
for i in range(gp.numberOfDaughters()):
if gp.daughter(i).pdgId() == me:
return False
return True
Expand Down
1 change: 1 addition & 0 deletions PhysicsTools/Heppy/scripts/cmsBatch.py
Expand Up @@ -3,6 +3,7 @@
# batch mode for cmsRun, March 2009

from __future__ import print_function
from builtins import range
import os, sys, imp, re, pprint, string, time,shutil,copy,pickle,math
from optparse import OptionParser

Expand Down
3 changes: 2 additions & 1 deletion PhysicsTools/Heppy/scripts/cmsStageWithFailover.py
Expand Up @@ -3,6 +3,7 @@
#this script runs cmsStage multiple times in the case where it failes for some reason

from __future__ import print_function
from builtins import range
if __name__ == '__main__':

import PhysicsTools.HeppyCore.utils.eostools as eostools
Expand Down Expand Up @@ -31,7 +32,7 @@

sleep_lengths = [1,10,60,600,1800]
return_code = 0
for i in xrange(5):
for i in range(5):

#sleep for a while before running
time.sleep(sleep_lengths[i])
Expand Down
3 changes: 2 additions & 1 deletion PhysicsTools/Heppy/scripts/heppy_report.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python
from __future__ import print_function
from builtins import range
from optparse import OptionParser
import json
import six
Expand All @@ -15,7 +16,7 @@ def root2map(dir,ana,treename):
print("Error: rootfile %s/%s/%s.root does not contain a TTree %s" % (dir,ana,treename,treename))
return None
jsonind = {}
for e in xrange(tree.GetEntries()):
for e in range(tree.GetEntries()):
tree.GetEntry(e)
run,lumi = tree.run, tree.lumi
if run not in jsonind:
Expand Down
3 changes: 2 additions & 1 deletion PhysicsTools/Heppy/test/crab/heppy_crab_script.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python
from __future__ import print_function
from builtins import range
import os
# probably easier to fetch everything without subdirs, but that's up to user preferences
#import PhysicsTools.HeppyCore.framework.config as cfg
Expand All @@ -14,7 +15,7 @@
print(crabFiles)
firstInput = crabFiles[0]
print("--------------- using edmFileUtil to convert PFN to LFN -------------------------")
for i in xrange(0,len(crabFiles)) :
for i in range(0,len(crabFiles)) :
pfn=os.popen("edmFileUtil -d %s"%(crabFiles[i])).read()
pfn=re.sub("\n","",pfn)
print(crabFiles[i],"->",pfn)
Expand Down
3 changes: 2 additions & 1 deletion PhysicsTools/HeppyCore/python/framework/heppy_loop.py
Expand Up @@ -3,6 +3,7 @@
# https://github.com/cbernet/heppy/blob/master/LICENSE

from __future__ import print_function
from builtins import range
import os
import shutil
import glob
Expand Down Expand Up @@ -99,7 +100,7 @@ def split(comps):
splitComps = []
for comp in comps:
if hasattr( comp, 'fineSplitFactor') and comp.fineSplitFactor>1:
subchunks = range(comp.fineSplitFactor)
subchunks = list(range(comp.fineSplitFactor))
for ichunk, chunk in enumerate([(f,i) for f in comp.files for i in subchunks]):
newComp = copy.deepcopy(comp)
newComp.files = [chunk[0]]
Expand Down
1 change: 1 addition & 0 deletions PhysicsTools/HeppyCore/python/framework/looper.py
Expand Up @@ -3,6 +3,7 @@
# Copyright (C) 2014 Colin Bernet
# https://github.com/cbernet/heppy/blob/master/LICENSE

from builtins import range
import os
import sys
import imp
Expand Down
1 change: 1 addition & 0 deletions PhysicsTools/HeppyCore/python/statistics/counter.py
@@ -1,6 +1,7 @@
# Copyright (C) 2014 Colin Bernet
# https://github.com/cbernet/heppy/blob/master/LICENSE

from builtins import range
import pickle
from PhysicsTools.HeppyCore.utils.diclist import diclist

Expand Down
3 changes: 2 additions & 1 deletion PhysicsTools/HeppyCore/python/utils/dataset.py
Expand Up @@ -2,6 +2,7 @@

from __future__ import print_function
from __future__ import absolute_import
from builtins import range
import os
import pprint
import re
Expand Down Expand Up @@ -170,7 +171,7 @@ def buildListOfFiles(self, pattern='.*root'):
if num_files > limit:
num_steps = int(num_files/limit)+1
self.files = []
for i in xrange(num_steps):
for i in range(num_steps):
DBSFiles=self.buildListOfFilesDBS(pattern,
i*limit,
((i+1)*limit)-1)
Expand Down
1 change: 1 addition & 0 deletions PhysicsTools/HeppyCore/python/utils/dataset_test.py
@@ -1,4 +1,5 @@
from __future__ import absolute_import
from builtins import range
from .dataset import *

import unittest
Expand Down
3 changes: 2 additions & 1 deletion PhysicsTools/HeppyCore/python/utils/edmIntegrityCheck.py
Expand Up @@ -5,6 +5,7 @@
from __future__ import print_function
from __future__ import absolute_import

from builtins import range
import datetime, fnmatch, json, os, shutil, sys, tempfile, time
import subprocess

Expand Down Expand Up @@ -145,7 +146,7 @@ def getCrabIndex(base):
good_duplicates = {}
bad_jobs = set()
sum_dup = 0
for i in xrange(mmin, mmax+1):
for i in range(mmin, mmax+1):
if i in files:
duplicates = sorted(files[i])

Expand Down
3 changes: 2 additions & 1 deletion PhysicsTools/HeppyCore/python/utils/production_tasks.py
@@ -1,6 +1,7 @@
from __future__ import print_function
from __future__ import absolute_import

from builtins import range
import copy, datetime, inspect, fnmatch, os, re, subprocess, sys, tempfile, time
import glob
import gzip
Expand Down Expand Up @@ -606,7 +607,7 @@ def parseHeader(header):
"""Parse the header from bjobs"""
tokens = [t for t in header.split(' ') if t]
result = {}
for i in xrange(len(tokens)):
for i in range(len(tokens)):
result[tokens[i]] = i

return result
Expand Down
1 change: 1 addition & 0 deletions PhysicsTools/HeppyCore/python/utils/testtree.py
@@ -1,4 +1,5 @@
from __future__ import print_function
from builtins import range
from ROOT import TFile
from PhysicsTools.HeppyCore.statistics.tree import Tree

Expand Down
3 changes: 2 additions & 1 deletion PhysicsTools/HeppyCore/scripts/heppy_batch.py
@@ -1,6 +1,7 @@
#!/bin/env python

from __future__ import print_function
from builtins import range
import sys
import imp
import copy
Expand Down Expand Up @@ -362,7 +363,7 @@ def PrepareJobUser(self, jobDir, value ):
handle.close()

components = split( [comp for comp in config.components if len(comp.files)>0] )
listOfValues = range(0, len(components))
listOfValues = list(range(0, len(components)))
listOfNames = [comp.name for comp in components]

batchManager.PrepareJobs( listOfValues, listOfNames )
Expand Down
3 changes: 2 additions & 1 deletion PhysicsTools/NanoAOD/test/inspectNanoFile.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python

from builtins import range
import sys, os.path, json
from collections import defaultdict
import six
Expand Down Expand Up @@ -93,7 +94,7 @@ def inspectRootFile(infile):
entries = tree.GetEntries()
trees[treeName] = tree
branchList = tree.GetListOfBranches()
allbranches = [ Branch(tree,branchList.At(i)) for i in xrange(branchList.GetSize()) ]
allbranches = [ Branch(tree,branchList.At(i)) for i in range(branchList.GetSize()) ]
branchmap = dict((b.name,b) for b in allbranches)
branchgroups = {}
# make list of counters and countees
Expand Down

0 comments on commit 7965eaa

Please sign in to comment.