-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathProduction.py
314 lines (273 loc) · 14.6 KB
/
Production.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Produce TauTuple.
import re
import importlib
import FWCore.ParameterSet.Config as cms
from FWCore.ParameterSet.VarParsing import VarParsing
from RecoMET.METPUSubtraction.deepMETProducer_cfi import deepMETProducer
import os
options = VarParsing('analysis')
options.register('sampleType', '', VarParsing.multiplicity.singleton, VarParsing.varType.string,
"Indicates the sample type")
options.register('era', '', VarParsing.multiplicity.singleton, VarParsing.varType.string,
"Indicates the era")
options.register('fileList', '', VarParsing.multiplicity.singleton, VarParsing.varType.string,
"List of root files to process.")
options.register('fileNamePrefix', '', VarParsing.multiplicity.singleton, VarParsing.varType.string,
"Prefix to add to input file names.")
options.register('output', 'eventTuple.root', VarParsing.multiplicity.singleton, VarParsing.varType.string,
"Event tuple file.")
options.register('lumiFile', '', VarParsing.multiplicity.singleton, VarParsing.varType.string,
"JSON file with lumi mask.")
options.register('eventList', '', VarParsing.multiplicity.singleton, VarParsing.varType.string,
"List of events to process.")
options.register('dumpPython', False, VarParsing.multiplicity.singleton, VarParsing.varType.bool,
"Dump full config into stdout.")
options.register('numberOfThreads', 1, VarParsing.multiplicity.singleton, VarParsing.varType.int,
"Number of threads.")
options.register('selector', 'None', VarParsing.multiplicity.singleton, VarParsing.varType.string,
"Name of the tauJet selector.")
options.register('triggers', '', VarParsing.multiplicity.singleton, VarParsing.varType.string,
"Store only events that pass the specified HLT paths.")
options.register("disabledBranches", [], VarParsing.multiplicity.list, VarParsing.varType.string,
"Not store following branches in tupleOutput file.")
options.register("enabledBranches", [], VarParsing.multiplicity.list, VarParsing.varType.string,
"Branches to store in tupleOutput file (if empty list: stores all the branches).")
options.register('storeJetsWithoutTau', False, VarParsing.multiplicity.singleton, VarParsing.varType.bool,
"Store jets that don't match to any pat::Tau.")
options.register('requireGenMatch', True, VarParsing.multiplicity.singleton, VarParsing.varType.bool,
"Store only tau jets that have genLepton_index >= 0 or genJet_index >= 0.")
options.register('requireGenORRecoTauMatch', False, VarParsing.multiplicity.singleton, VarParsing.varType.bool,
"""Store only tau jets that satisfy the following condition:
tau_index >= 0 || boostedTau_index >= 0 || (genLepton_index >= 0 && genLepton_kind == 5)""")
options.register('applyRecoPtSieve', False, VarParsing.multiplicity.singleton, VarParsing.varType.bool,
"Randomly drop jet->tau fakes depending on reco tau pt to balance contributions from low and higt pt.")
options.register('reclusterJets', True, VarParsing.multiplicity.singleton, VarParsing.varType.bool,
" If 'reclusterJets' set true a new collection of uncorrected ak4PFJets is built to seed taus (as at RECO), otherwise standard slimmedJets are used")
options.register('rerunTauReco', '', VarParsing.multiplicity.singleton, VarParsing.varType.string,
"""If not empty, tau reconstruction is re-run on MINIAOD (available modes:
1) signalCone - larger signal cone and no DM finding filter,
2) displacedTau - loose max Impact Parameter and signal quality cuts)""")
options.register('runTauSpinner', False, VarParsing.multiplicity.singleton, VarParsing.varType.bool,
"Run TauPOGSpinner to store CP weights")
options.parseArguments()
import importlib
import os
import sys
def load(module_file, default_path):
module_path = os.path.join(default_path, module_file)
if not os.path.exists(module_path):
module_path = os.path.join(os.path.dirname(__file__), module_file)
if not os.path.exists(module_path):
module_path = os.path.join(os.getenv("CMSSW_BASE"), 'src', module_file)
if not os.path.exists(module_path):
raise RuntimeError(f"Cannot find path to {module_file}.")
module_name, module_ext = os.path.splitext(module_file)
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
sampleConfig = load('sampleConfig.py', os.path.join(os.getenv("CMSSW_BASE"), 'src/TauMLTools/Production/python'))
from sampleConfig import Era, SampleType
sampleType = SampleType[options.sampleType]
era = Era[options.era]
isData = sampleType == SampleType.Data
isEmbedded = sampleType == SampleType.Embedded
isRun2 = sampleConfig.isRun2(era)
isRun3 = sampleConfig.isRun3(era)
isPhase2 = sampleConfig.isPhase2(era)
era_cfg = sampleConfig.getEraCfg(era)
globalTag = sampleConfig.getGlobalTag(era, sampleType)
if not (isRun2 or isRun3 or isPhase2):
raise RuntimeError("Support for era = {} is not implemented".format(era.name))
processName = 'tupleProduction'
process = cms.Process(processName, era_cfg)
process.options = cms.untracked.PSet()
process.options.wantSummary = cms.untracked.bool(False)
process.options.allowUnscheduled = cms.untracked.bool(True)
process.options.numberOfThreads = cms.untracked.uint32(options.numberOfThreads)
process.options.numberOfStreams = cms.untracked.uint32(0)
process.load('Configuration.StandardSequences.MagneticField_cff')
# TauSpinner
process.TauSpinnerReco = cms.EDProducer( "TauPOGSpinner",
isReco = cms.bool(True),
isTauolaConfigured = cms.bool(False),
isLHPDFConfigured = cms.bool(False),
LHAPDFname = cms.untracked.string('NNPDF30_nlo_as_0118'),
CMSEnergy = cms.double(13000.0),
gensrc = cms.InputTag('prunedGenParticles')
)
process.RandomNumberGeneratorService = cms.Service('RandomNumberGeneratorService',
TauSpinnerReco = cms.PSet(
initialSeed = cms.untracked.uint32(123456789),
engineName = cms.untracked.string('HepJamesRandom')
)
)
# DeepMET
process.deepMETProducer = deepMETProducer.clone()
# include Phase2 specific configuration only after 11_0_X
if isPhase2:
process.load('Configuration.Geometry.GeometryExtended2026D49Reco_cff')
elif isRun2 or isRun3:
process.load('Configuration.Geometry.GeometryRecoDB_cff')
process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff')
from Configuration.AlCa.GlobalTag import GlobalTag
process.GlobalTag = GlobalTag(process.GlobalTag, globalTag, '')
process.source = cms.Source('PoolSource', fileNames = cms.untracked.vstring())
process.TFileService = cms.Service('TFileService', fileName = cms.string(options.output) )
process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(-1) )
readFileList = load('readFileList.py', os.path.join(os.getenv("CMSSW_BASE"), 'src/TauMLTools/Production/python'))
from readFileList import *
if len(options.fileList) > 0:
readFileList(process.source.fileNames, options.fileList, options.fileNamePrefix)
elif len(options.inputFiles) > 0:
addFilesToList(process.source.fileNames, options.inputFiles, options.fileNamePrefix)
if options.maxEvents > 0:
process.maxEvents.input = options.maxEvents
if len(options.lumiFile) > 0:
import FWCore.PythonUtilities.LumiList as LumiList
process.source.lumisToProcess = LumiList.LumiList(filename = options.lumiFile).getVLuminosityBlockRange()
if options.eventList != '':
process.source.eventsToProcess = cms.untracked.VEventRange(re.split(',', options.eventList))
tau_collection = 'slimmedTaus'
if options.rerunTauReco:
tau_collection = 'selectedPatTaus'
import RecoTauTag.Configuration.tools.adaptToRunAtMiniAOD as tauAtMiniConfig
tauAtMiniTools = tauAtMiniConfig.adaptToRunAtMiniAOD(process, runBoosted=False)
tauAtMiniTools.addTauReReco()
tauAtMiniTools.adaptTauToMiniAODReReco(reclusterJets = options.reclusterJets)
if isData:
from PhysicsTools.PatAlgos.tools.coreTools import runOnData
runOnData(process, names = ['Taus'], outputModules = [])
import TauMLTools.Production.setupTauReReco as setupTauReReco
if options.rerunTauReco == "signalCone":
setupTauReReco.reReco_SigCone(process)
elif options.rerunTauReco == "displacedTau":
setupTauReReco.reReco_DisTau(process)
else:
raise RuntimeError('rerunTauReco = "{}" mode is not supported.'.format(options.rerunTauReco))
# include Phase2 specific configuration only after 11_0_X
if isPhase2:
tauIdConfig = importlib.import_module('RecoTauTag.RecoTau.tools.runTauIdMVA')
updatedTauName = "slimmedTausNewID"
tauIdEmbedder = tauIdConfig.TauIDEmbedder(
process, cms, originalTauName = tau_collection, updatedTauName = updatedTauName,
toKeep = [ "2017v2", "dR0p32017v2", "newDM2017v2", "deepTau2017v2p1", "newDMPhase2v1"]
)
tauIdEmbedder.runTauID() # note here, that with the official CMSSW version of 'runTauIdMVA' slimmedTaus are hardcoded as input tau collection
elif isRun2 or isRun3:
tauIdConfig = importlib.import_module('RecoTauTag.RecoTau.tools.runTauIdMVA')
updatedTauName = "slimmedTausNewID"
tauIdEmbedder = tauIdConfig.TauIDEmbedder(
process, cms, originalTauName = tau_collection, updatedTauName = updatedTauName,
toKeep = [ "deepTau2017v2p1", "deepTau2018v2p5" ]
)
tauIdEmbedder.runTauID()
boostedTaus_InputTag = cms.InputTag('slimmedTausBoosted')
taus_InputTag = cms.InputTag('slimmedTausNewID')
if isPhase2:
process.slimmedElectronsMerged = cms.EDProducer("SlimmedElectronMerger",
src = cms.VInputTag("slimmedElectrons","slimmedElectronsHGC")
)
electrons_InputTag = cms.InputTag('slimmedElectronsMerged')
vtx_InputTag = cms.InputTag('offlineSlimmedPrimaryVertices4D')
elif isRun2 or isRun3:
electrons_InputTag = cms.InputTag('slimmedElectrons')
vtx_InputTag = cms.InputTag('offlineSlimmedPrimaryVertices')
tauJetBuilderSetup = cms.PSet(
genLepton_genJet_dR = cms.double(0.4),
genLepton_tau_dR = cms.double(0.2),
genLepton_boostedTau_dR = cms.double(0.2),
genLepton_jet_dR = cms.double(0.4),
genLepton_fatJet_dR = cms.double(0.8),
genJet_tau_dR = cms.double(0.4),
genJet_boostedTau_dR = cms.double(0.4),
genJet_jet_dR = cms.double(0.4),
genJet_fatJet_dR = cms.double(0.8),
tau_boostedTau_dR = cms.double(0.2),
tau_jet_dR = cms.double(0.4),
tau_fatJet_dR = cms.double(0.8),
jet_fatJet_dR = cms.double(0.8),
jet_maxAbsEta = cms.double(3.4),
fatJet_maxAbsEta = cms.double(3.8),
genLepton_cone = cms.double(0.5),
genJet_cone = cms.double(0.5),
tau_cone = cms.double(0.5),
boostedTau_cone = cms.double(0.5),
jet_cone = cms.double(0.8),
fatJet_cone = cms.double(0.8),
)
process.tauTupleProducer = cms.EDAnalyzer('TauTupleProducer',
isMC = cms.bool(not isData),
isEmbedded = cms.bool(isEmbedded),
requireGenMatch = cms.bool(options.requireGenMatch),
requireGenORRecoTauMatch = cms.bool(options.requireGenORRecoTauMatch),
applyRecoPtSieve = cms.bool(options.applyRecoPtSieve),
disabledBranches = cms.vstring(options.disabledBranches),
enabledBranches = cms.vstring(options.enabledBranches),
tauJetBuilderSetup = tauJetBuilderSetup,
selector = cms.string(options.selector),
lheEventProduct = cms.InputTag('externalLHEProducer'),
genEvent = cms.InputTag('generator'),
genParticles = cms.InputTag('prunedGenParticles'),
puInfo = cms.InputTag('slimmedAddPileupInfo'),
vertices = vtx_InputTag,
secondVertices = cms.InputTag('slimmedSecondaryVertices'),
rho = cms.InputTag('fixedGridRhoAll'),
electrons = electrons_InputTag,
photons = cms.InputTag('slimmedPhotons'),
muons = cms.InputTag('slimmedMuons'),
taus = taus_InputTag,
boostedTaus = boostedTaus_InputTag,
jets = cms.InputTag('slimmedJets'),
fatJets = cms.InputTag('slimmedJetsAK8'),
pfCandidates = cms.InputTag('packedPFCandidates'),
isoTracks = cms.InputTag('isolatedTracks'),
lostTracks = cms.InputTag('lostTracks'),
genJets = cms.InputTag('slimmedGenJets'),
genJetFlavourInfos = cms.InputTag('slimmedGenJetsFlavourInfos'),
METs = cms.InputTag('slimmedMETs'),
puppiMETs = cms.InputTag('slimmedMETsPuppi'),
deepMETs = cms.InputTag('deepMETProducer', ''),
genMETs = cms.InputTag('genMetTrue'),
triggerResults = cms.InputTag('TriggerResults', '', 'HLT'),
triggerObjects = cms.InputTag('slimmedPatTrigger'),
tauSpinnerWTEven = cms.InputTag('TauSpinnerReco','TauSpinnerWTEven'),
tauSpinnerWTOdd = cms.InputTag('TauSpinnerReco','TauSpinnerWTOdd'),
tauSpinnerWTMM = cms.InputTag('TauSpinnerReco','TauSpinnerWTMM'),
)
process.tupleProductionSequence = cms.Sequence(process.tauTupleProducer)
if isPhase2:
process.p = cms.Path(
process.slimmedElectronsMerged +
getattr(process, 'rerunMvaIsolationSequence') +
getattr(process, updatedTauName) +
process.tupleProductionSequence
)
elif isRun2 or isRun3:
process.p = cms.Path(
process.deepMETProducer +
getattr(process, 'rerunMvaIsolationSequence') +
getattr(process, updatedTauName) +
process.tupleProductionSequence
)
if options.runTauSpinner:
process.p.insert(0, process.TauSpinnerReco)
if options.rerunTauReco:
process.p.insert(0, getattr(process,'miniAODTausSequence'))
if len(options.triggers) > 0:
hlt_paths = options.triggers.split(',')
process.hltFilter = cms.EDFilter('TriggerResultsFilter',
hltResults = cms.InputTag('TriggerResults', '', 'HLT'),
l1tResults = cms.InputTag(''),
l1tIgnoreMaskAndPrescale = cms.bool(False),
throw = cms.bool(True),
triggerConditions = cms.vstring(hlt_paths),
)
process.p.insert(0, process.hltFilter)
process.load('FWCore.MessageLogger.MessageLogger_cfi')
x = process.maxEvents.input.value()
x = x if x >= 0 else 10000
process.MessageLogger.cerr.FwkReport.reportEvery = max(1, min(1000, x // 10))
if options.dumpPython:
print(process.dumpPython())