Skip to content

Commit 64bf02a

Browse files
authored
Python3 + pylint + pep8
1 parent a455d31 commit 64bf02a

38 files changed

+3857
-2381
lines changed

crab/PSet.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
#this fake PSET is needed for local test and for crab to figure the output filename
2-
#you do not need to edit it unless you want to do a local test using a different input file than
3-
#the one marked below
1+
# this fake PSET is needed for local test and for crab to figure the output
2+
# filename you do not need to edit it unless you want to do a local test using
3+
# a different input file than the one marked below
44
import FWCore.ParameterSet.Config as cms
55
process = cms.Process('NANO')
6-
process.source = cms.Source("PoolSource", fileNames = cms.untracked.vstring(),
7-
# lumisToProcess=cms.untracked.VLuminosityBlockRange("254231:1-254231:24")
6+
process.source = cms.Source(
7+
"PoolSource",
8+
fileNames=cms.untracked.vstring(),
9+
# lumisToProcess=cms.untracked.VLuminosityBlockRange("254231:1-254231:24")
810
)
911
process.source.fileNames = [
10-
'../../NanoAOD/test/lzma.root' ##you can change only this line
12+
'../../NanoAOD/test/lzma.root' # you can change only this line
1113
]
12-
process.maxEvents = cms.untracked.PSet(input = cms.untracked.int32(10))
13-
process.output = cms.OutputModule("PoolOutputModule", fileName = cms.untracked.string('tree.root'))
14+
process.maxEvents = cms.untracked.PSet(input=cms.untracked.int32(10))
15+
process.output = cms.OutputModule("PoolOutputModule",
16+
fileName=cms.untracked.string('tree.root'))
1417
process.out = cms.EndPath(process.output)
15-

crab/crab_cfg.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
config.section_("General")
77
config.General.requestName = 'NanoPost1'
8-
config.General.transferLogs=True
8+
config.General.transferLogs = True
99
config.section_("JobType")
1010
config.JobType.pluginName = 'Analysis'
1111
config.JobType.psetName = 'PSet.py'
1212
config.JobType.scriptExe = 'crab_script.sh'
13-
config.JobType.inputFiles = ['crab_script.py','../scripts/haddnano.py'] #hadd nano will not be needed once nano tools are in cmssw
14-
config.JobType.sendPythonFolder = True
13+
# hadd nano will not be needed once nano tools are in cmssw
14+
config.JobType.inputFiles = ['crab_script.py', '../scripts/haddnano.py']
15+
config.JobType.sendPythonFolder = True
1516
config.section_("Data")
1617
config.Data.inputDataset = '/DYJetsToLL_1J_TuneCP5_13TeV-amcatnloFXFX-pythia8/RunIIFall17NanoAOD-PU2017_12Apr2018_94X_mc2017_realistic_v14-v1/NANOAODSIM'
1718
#config.Data.inputDBS = 'phys03'
@@ -21,13 +22,13 @@
2122
config.Data.unitsPerJob = 2
2223
config.Data.totalUnits = 10
2324

24-
config.Data.outLFNDirBase = '/store/user/%s/NanoPost' % (getUsernameFromSiteDB())
25+
config.Data.outLFNDirBase = '/store/user/%s/NanoPost' % (
26+
getUsernameFromSiteDB())
2527
config.Data.publication = False
2628
config.Data.outputDatasetTag = 'NanoTestPost'
2729
config.section_("Site")
2830
config.Site.storageSite = "T2_DE_DESY"
2931

3032
#config.Site.storageSite = "T2_CH_CERN"
31-
#config.section_("User")
33+
# config.section_("User")
3234
#config.User.voGroup = 'dcms'
33-

crab/crab_script.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#!/usr/bin/env python
22
import os
3-
from PhysicsTools.NanoAODTools.postprocessing.framework.postprocessor import *
3+
from PhysicsTools.NanoAODTools.postprocessing.framework.postprocessor import *
44

5-
#this takes care of converting the input files from CRAB
6-
from PhysicsTools.NanoAODTools.postprocessing.framework.crabhelper import inputFiles,runsAndLumis
5+
# this takes care of converting the input files from CRAB
6+
from PhysicsTools.NanoAODTools.postprocessing.framework.crabhelper import inputFiles, runsAndLumis
77

8-
from PhysicsTools.NanoAODTools.postprocessing.examples.exampleModule import *
9-
p=PostProcessor(".",inputFiles(),"Jet_pt>200",modules=[exampleModuleConstr()],provenance=True,fwkJobReport=True,jsonInput=runsAndLumis())
8+
from PhysicsTools.NanoAODTools.postprocessing.examples.exampleModule import *
9+
p = PostProcessor(".",
10+
inputFiles(),
11+
"Jet_pt>200",
12+
modules=[exampleModuleConstr()],
13+
provenance=True,
14+
fwkJobReport=True,
15+
jsonInput=runsAndLumis())
1016
p.run()
1117

12-
print "DONE"
13-
18+
print("DONE")
Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
#!/usr/bin/env python
2-
import os, sys
2+
from PhysicsTools.NanoAODTools.postprocessing.framework.eventloop import Module
3+
from PhysicsTools.NanoAODTools.postprocessing.framework.datamodel import Collection
4+
from PhysicsTools.NanoAODTools.postprocessing.framework.postprocessor import PostProcessor
5+
from importlib import import_module
6+
import os
7+
import sys
38
import ROOT
49
ROOT.PyConfig.IgnoreCommandLineOptions = True
5-
from importlib import import_module
6-
from PhysicsTools.NanoAODTools.postprocessing.framework.postprocessor import PostProcessor
7-
810

9-
from PhysicsTools.NanoAODTools.postprocessing.framework.datamodel import Collection
10-
from PhysicsTools.NanoAODTools.postprocessing.framework.eventloop import Module
1111

1212
class ExampleAnalysis(Module):
1313
def __init__(self):
14-
self.writeHistFile=True
14+
self.writeHistFile = True
1515

16-
def beginJob(self,histFile=None,histDirName=None):
17-
Module.beginJob(self,histFile,histDirName)
16+
def beginJob(self, histFile=None, histDirName=None):
17+
Module.beginJob(self, histFile, histDirName)
1818

19-
self.h_vpt=ROOT.TH1F('sumpt', 'sumpt', 100, 0, 1000)
20-
self.addObject(self.h_vpt )
19+
self.h_vpt = ROOT.TH1F('sumpt', 'sumpt', 100, 0, 1000)
20+
self.addObject(self.h_vpt)
2121

2222
def analyze(self, event):
2323
electrons = Collection(event, "Electron")
2424
muons = Collection(event, "Muon")
2525
jets = Collection(event, "Jet")
2626
eventSum = ROOT.TLorentzVector()
2727

28-
#select events with at least 2 muons
29-
if len(muons) >=2 :
30-
for lep in muons : #loop on muons
31-
eventSum += lep.p4()
32-
for lep in electrons : #loop on electrons
33-
eventSum += lep.p4()
34-
for j in jets : #loop on jets
35-
eventSum += j.p4()
36-
self.h_vpt.Fill(eventSum.Pt()) #fill histogram
28+
# select events with at least 2 muons
29+
if len(muons) >= 2:
30+
for lep in muons: # loop on muons
31+
eventSum += lep.p4()
32+
for lep in electrons: # loop on electrons
33+
eventSum += lep.p4()
34+
for j in jets: # loop on jets
35+
eventSum += j.p4()
36+
self.h_vpt.Fill(eventSum.Pt()) # fill histogram
3737

3838
return True
3939

4040

41-
preselection="Jet_pt[0] > 250"
42-
files=[" root://cms-xrd-global.cern.ch//store/mc/RunIISummer16NanoAOD/TTJets_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/NANOAODSIM/PUMoriond17_05Feb2018_94X_mcRun2_asymptotic_v2-v1/40000/2CE738F9-C212-E811-BD0E-EC0D9A8222CE.root"]
43-
p=PostProcessor(".",files,cut=preselection,branchsel=None,modules=[ExampleAnalysis()],noOut=True,histFileName="histOut.root",histDirName="plots")
41+
preselection = "Jet_pt[0] > 250"
42+
files = [" root://cms-xrd-global.cern.ch//store/mc/RunIISummer16NanoAOD/TTJets_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/NANOAODSIM/PUMoriond17_05Feb2018_94X_mcRun2_asymptotic_v2-v1/40000/2CE738F9-C212-E811-BD0E-EC0D9A8222CE.root"]
43+
p = PostProcessor(".", files, cut=preselection, branchsel=None, modules=[
44+
ExampleAnalysis()], noOut=True, histFileName="histOut.root", histDirName="plots")
4445
p.run()
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
1+
from PhysicsTools.NanoAODTools.postprocessing.framework.datamodel import Collection
2+
from PhysicsTools.NanoAODTools.postprocessing.framework.eventloop import Module
13
import ROOT
24
ROOT.PyConfig.IgnoreCommandLineOptions = True
35

4-
from PhysicsTools.NanoAODTools.postprocessing.framework.datamodel import Collection
5-
from PhysicsTools.NanoAODTools.postprocessing.framework.eventloop import Module
66

77
class exampleProducer(Module):
88
def __init__(self, jetSelection):
99
self.jetSel = jetSelection
1010
pass
11+
1112
def beginJob(self):
1213
pass
14+
1315
def endJob(self):
1416
pass
17+
1518
def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
1619
self.out = wrappedOutputTree
17-
self.out.branch("EventMass", "F");
20+
self.out.branch("EventMass", "F")
21+
1822
def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
1923
pass
24+
2025
def analyze(self, event):
2126
"""process event, return True (go to next module) or False (fail, go to next event)"""
2227
electrons = Collection(event, "Electron")
2328
muons = Collection(event, "Muon")
2429
jets = Collection(event, "Jet")
2530
eventSum = ROOT.TLorentzVector()
26-
for lep in muons :
31+
for lep in muons:
2732
eventSum += lep.p4()
28-
for lep in electrons :
33+
for lep in electrons:
2934
eventSum += lep.p4()
30-
for j in filter(self.jetSel,jets):
35+
for j in filter(self.jetSel, jets):
3136
eventSum += j.p4()
32-
self.out.fillBranch("EventMass",eventSum.M())
37+
self.out.fillBranch("EventMass", eventSum.M())
3338
return True
3439

3540

3641
# define modules using the syntax 'name = lambda : constructor' to avoid having them loaded when not needed
3742

38-
exampleModuleConstr = lambda : exampleProducer(jetSelection= lambda j : j.pt > 30)
39-
43+
exampleModuleConstr = lambda: exampleProducer(jetSelection=lambda j: j.pt > 30)
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
#!/usr/bin/env python
2-
import os, sys
2+
from exampleModule import *
3+
from PhysicsTools.NanoAODTools.postprocessing.modules.jme.jetmetHelperRun2 import *
4+
from PhysicsTools.NanoAODTools.postprocessing.modules.jme.jetmetUncertainties import *
5+
from PhysicsTools.NanoAODTools.postprocessing.framework.postprocessor import PostProcessor
6+
from importlib import import_module
7+
import os
8+
import sys
39
import ROOT
410
ROOT.PyConfig.IgnoreCommandLineOptions = True
5-
from importlib import import_module
6-
from PhysicsTools.NanoAODTools.postprocessing.framework.postprocessor import PostProcessor
7-
##soon to be deprecated
8-
from PhysicsTools.NanoAODTools.postprocessing.modules.jme.jetmetUncertainties import *
9-
##new way of using jme uncertainty
10-
from PhysicsTools.NanoAODTools.postprocessing.modules.jme.jetmetHelperRun2 import *
11+
# soon to be deprecated
12+
# new way of using jme uncertainty
1113

12-
from exampleModule import *
1314

14-
##Function parameters
15-
##(isMC=True, dataYear=2016, runPeriod="B", jesUncert="Total", redojec=False, jetType = "AK4PFchs", noGroom=False)
16-
##All other parameters will be set in the helper module
15+
# Function parameters
16+
# (isMC=True, dataYear=2016, runPeriod="B", jesUncert="Total", redojec=False, jetType = "AK4PFchs", noGroom=False)
17+
# All other parameters will be set in the helper module
1718

18-
jmeCorrections = createJMECorrector(True, "2016", "B", "Total", True, "AK4PFchs", False)
19+
jmeCorrections = createJMECorrector(
20+
True, "2016", "B", "Total", True, "AK4PFchs", False)
1921

20-
fnames=["/eos/cms/store/mc/RunIISummer16NanoAODv5/DYJetsToLL_M-50_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8/NANOAODSIM/PUMoriond17_Nano1June2019_102X_mcRun2_asymptotic_v7_ext2-v1/120000/FF69DF6E-2494-F543-95BF-F919B911CD23.root"]
22+
fnames = ["/eos/cms/store/mc/RunIISummer16NanoAODv5/DYJetsToLL_M-50_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8/NANOAODSIM/PUMoriond17_Nano1June2019_102X_mcRun2_asymptotic_v7_ext2-v1/120000/FF69DF6E-2494-F543-95BF-F919B911CD23.root"]
2123

22-
#p=PostProcessor(".",fnames,"Jet_pt>150","",[jetmetUncertainties2016(),exampleModuleConstr()],provenance=True)
23-
p=PostProcessor(".",fnames,"Jet_pt>150","",[jmeCorrections(),exampleModuleConstr()],provenance=True)
24+
# p=PostProcessor(".",fnames,"Jet_pt>150","",[jetmetUncertainties2016(),exampleModuleConstr()],provenance=True)
25+
p = PostProcessor(".", fnames, "Jet_pt>150", "", [
26+
jmeCorrections(), exampleModuleConstr()], provenance=True)
2427
p.run()
Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,65 @@
1+
from PhysicsTools.NanoAODTools.postprocessing.framework.eventloop import Module
12
import ROOT
23
import os
34
ROOT.PyConfig.IgnoreCommandLineOptions = True
45

5-
from PhysicsTools.NanoAODTools.postprocessing.framework.eventloop import Module
66

7-
class mhtjuProducerCpp(Module): # MHT producer, unclean jets only (no lepton overlap cleaning, no jet selection)
7+
# MHT producer, unclean jets only (no lepton overlap cleaning, no jet selection)
8+
class mhtjuProducerCpp(Module):
89
def __init__(self):
910
if "/mhtjuProducerCppWorker_cc.so" not in ROOT.gSystem.GetLibraries():
10-
print "Load C++ mhtjuProducerCppWorker worker module"
11+
print("Load C++ mhtjuProducerCppWorker worker module")
1112
base = os.getenv("NANOAODTOOLS_BASE")
1213
if base:
13-
ROOT.gROOT.ProcessLine(".L %s/src/mhtjuProducerCppWorker.cc+O"%base)
14+
ROOT.gROOT.ProcessLine(
15+
".L %s/src/mhtjuProducerCppWorker.cc+O" % base)
1416
else:
15-
base = "%s/src/PhysicsTools/NanoAODTools"%os.getenv("CMSSW_BASE")
17+
base = "%s/src/PhysicsTools/NanoAODTools" % os.getenv(
18+
"CMSSW_BASE")
1619
ROOT.gSystem.Load("libPhysicsToolsNanoAODTools.so")
17-
ROOT.gROOT.ProcessLine(".L %s/interface/mhtjuProducerCppWorker.h"%base)
20+
ROOT.gROOT.ProcessLine(
21+
".L %s/interface/mhtjuProducerCppWorker.h" % base)
1822
self.worker = ROOT.mhtjuProducerCppWorker()
1923
pass
24+
2025
def beginJob(self):
2126
pass
27+
2228
def endJob(self):
2329
pass
30+
2431
def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
25-
self.initReaders(inputTree) # initReaders must be called in beginFile
32+
self.initReaders(inputTree) # initReaders must be called in beginFile
2633
self.out = wrappedOutputTree
27-
self.out.branch("MHTju_pt", "F");
28-
self.out.branch("MHTju_phi", "F");
34+
self.out.branch("MHTju_pt", "F")
35+
self.out.branch("MHTju_phi", "F")
36+
2937
def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
3038
pass
3139

32-
def initReaders(self,tree): # this function gets the pointers to Value and ArrayReaders and sets them in the C++ worker class
40+
# this function gets the pointers to Value and ArrayReaders and sets them in the C++ worker class
41+
def initReaders(self, tree):
3342
self.nJet = tree.valueReader("nJet")
3443
self.Jet_pt = tree.arrayReader("Jet_pt")
3544
self.Jet_phi = tree.arrayReader("Jet_phi")
36-
self.worker.setJets(self.nJet,self.Jet_pt,self.Jet_phi)
37-
self._ttreereaderversion = tree._ttreereaderversion # self._ttreereaderversion must be set AFTER all calls to tree.valueReader or tree.arrayReader
45+
self.worker.setJets(self.nJet, self.Jet_pt, self.Jet_phi)
46+
# self._ttreereaderversion must be set AFTER all calls to tree.valueReader or tree.arrayReader
47+
self._ttreereaderversion = tree._ttreereaderversion
3848

3949
def analyze(self, event):
4050
"""process event, return True (go to next module) or False (fail, go to next event)"""
4151

42-
if event._tree._ttreereaderversion > self._ttreereaderversion: # do this check at every event, as other modules might have read further branches
52+
# do this check at every event, as other modules might have read further branches
53+
if event._tree._ttreereaderversion > self._ttreereaderversion:
4354
self.initReaders(event._tree)
4455
# do NOT access other branches in python between the check/call to initReaders and the call to C++ worker code
4556
output = self.worker.getHT()
4657

4758
self.out.fillBranch("MHTju_pt", output[0])
48-
self.out.fillBranch("MHTju_phi", -output[1]) # note the minus
59+
self.out.fillBranch("MHTju_phi", -output[1]) # note the minus
4960
return True
5061

5162

5263
# define modules using the syntax 'name = lambda : constructor' to avoid having them loaded when not needed
5364

54-
mhtju = lambda : mhtjuProducerCpp()
65+
mhtju = lambda: mhtjuProducerCpp()
Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
import re
22

3-
class BranchSelection:
4-
def __init__(self,filename):
3+
4+
class BranchSelection():
5+
def __init__(self, filename):
56
comment = re.compile(r"#.*")
67
ops = []
7-
for line in open(filename,'r'):
8+
for line in open(filename, 'r'):
89
line = line.strip()
9-
if len(line) == 0 or line[0] == '#': continue
10-
line = re.sub(comment,"",line)
10+
if len(line) == 0 or line[0] == '#':
11+
continue
12+
line = re.sub(comment, "", line)
1113
while line[-1] == "\\":
1214
line = line[:-1] + " " + file.next().strip()
13-
line = re.sub(comment,"",line)
15+
line = re.sub(comment, "", line)
1416
try:
15-
(op,sel) = line.split()
16-
if op == "keep": ops.append( (sel, 1) )
17-
elif op == "drop": ops.append( (sel, 0) )
18-
elif op == "keepmatch": ops.append( (re.compile("(:?%s)$" % sel), 1) )
19-
elif op == "dropmatch": ops.append( (re.compile("(:?%s)$" % sel), 0) )
17+
(op, sel) = line.split()
18+
if op == "keep":
19+
ops.append((sel, 1))
20+
elif op == "drop":
21+
ops.append((sel, 0))
22+
elif op == "keepmatch":
23+
ops.append((re.compile("(:?%s)$" % sel), 1))
24+
elif op == "dropmatch":
25+
ops.append((re.compile("(:?%s)$" % sel), 0))
2026
else:
21-
print "Error in file %s, line '%s': it's not (keep|keepmatch|drop|dropmatch) <branch_pattern>" % (filename, line)
22-
except ValueError, e:
23-
print "Error in file %s, line '%s': it's not (keep|keepmatch|drop|dropmatch) <branch_pattern>" % (filename, line)
27+
print("Error in file %s, line '%s': it's not (keep|keepmatch|drop|dropmatch) <branch_pattern>" % (filename, line))
28+
except ValueError as e:
29+
print("Error in file %s, line '%s': it's not (keep|keepmatch|drop|dropmatch) <branch_pattern>" % (filename, line))
2430
self._ops = ops
25-
def selectBranches(self,tree):
26-
tree.SetBranchStatus("*",1)
27-
branchNames = [ b.GetName() for b in tree.GetListOfBranches() ]
31+
32+
def selectBranches(self, tree):
33+
tree.SetBranchStatus("*", 1)
34+
branchNames = [b.GetName() for b in tree.GetListOfBranches()]
2835
for bre, stat in self._ops:
2936
if type(bre) == re._pattern_type:
30-
for n in branchNames:
31-
if re.match(bre, n): tree.SetBranchStatus(n, stat)
37+
for n in branchNames:
38+
if re.match(bre, n):
39+
tree.SetBranchStatus(n, stat)
3240
else:
3341
tree.SetBranchStatus(bre, stat)

0 commit comments

Comments
 (0)