forked from cms-lightHiggsAnalysis/DevTools-Ntuplizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniTree_cfg.py
341 lines (301 loc) · 13.2 KB
/
MiniTree_cfg.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import os
import FWCore.ParameterSet.Config as cms
from FWCore.ParameterSet.VarParsing import VarParsing
options = VarParsing('analysis')
options.outputFile = 'miniTree.root'
#options.inputFiles= '/store/mc/RunIIFall17MiniAOD/WZ_TuneCP5_13TeV-pythia8/MINIAODSIM/94X_mc2017_realistic_v10-v1/60000/1C7C7FB8-14E6-E711-90A9-0025905A60FE.root' # WZ
options.inputFiles = '/store/data/Run2017F/SingleMuon/MINIAOD/17Nov2017-v1/010000/183BE17C-A5EC-E711-8F69-0025905A607E.root' # ReReco
options.maxEvents = -1
options.register('skipEvents', 0, VarParsing.multiplicity.singleton, VarParsing.varType.int, "Events to skip")
options.register('reportEvery', 100, VarParsing.multiplicity.singleton, VarParsing.varType.int, "Report every")
options.register('isMC', 0, VarParsing.multiplicity.singleton, VarParsing.varType.int, "Sample is MC")
options.register('isREMINIAOD', 1, VarParsing.multiplicity.singleton, VarParsing.varType.int, "Sample is ReMiniAOD")
options.register('reHLT', 0, VarParsing.multiplicity.singleton, VarParsing.varType.int, "Sample is reHLT")
options.register('runMetFilter', 0, VarParsing.multiplicity.singleton, VarParsing.varType.int, "Run the recommended MET filters")
options.register('crab', 0, VarParsing.multiplicity.singleton, VarParsing.varType.int, "Make changes needed for crab")
options.parseArguments()
#####################
### setup process ###
#####################
process = cms.Process("MiniNtuple")
process.load('Configuration.Geometry.GeometryRecoDB_cff')
process.load('Configuration.StandardSequences.MagneticField_38T_cff')
process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_condDBv2_cff')
process.load('Configuration.StandardSequences.Services_cff')
process.load("Geometry.CaloEventSetup.CaloTowerConstituents_cfi") # Needed by EGamma energy correction
process.options = cms.untracked.PSet(
allowUnscheduled = cms.untracked.bool(True),
)
process.RandomNumberGeneratorService = cms.Service(
"RandomNumberGeneratorService",
calibratedPatElectrons = cms.PSet(
initialSeed = cms.untracked.uint32(1),
engineName = cms.untracked.string('TRandom3')
),
calibratedPatPhotons = cms.PSet(
initialSeed = cms.untracked.uint32(2),
engineName = cms.untracked.string('TRandom3')
),
mRoch = cms.PSet(
initialSeed = cms.untracked.uint32(3),
engineName = cms.untracked.string('TRandom3')
),
)
#################
### GlobalTag ###
#################
envvar = 'mcgt' if options.isMC else 'datagt'
from Configuration.AlCa.GlobalTag import GlobalTag
#GT = {'mcgt': 'auto:run2_mc', 'datagt': 'auto:run2_data'}
# https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookMiniAOD
# https://twiki.cern.ch/twiki/bin/view/CMS/JECDataMC
GT = {'mcgt': '94X_mc2017_realistic_v10', 'datagt': '94X_dataRun2_ReReco_EOY17_v2'}
process.GlobalTag = GlobalTag(process.GlobalTag, GT[envvar], '')
##################
### JEC source ###
##################
# this is if we need to override the jec in global tag
#sqfile = 'DevTools/Ntuplizer/data/{0}.db'.format('Summer16_23Sep2016V3_MC' if options.isMC else 'Summer16_23Sep2016AllV3_DATA')
#if options.crab: sqfile = 'src/{0}'.format(sqfile) # uncomment to submit to crab
#tag = 'JetCorrectorParametersCollection_Summer16_23Sep2016AllV3_DATA_AK4PFchs'
#if options.isMC: tag = 'JetCorrectorParametersCollection_Summer16_23Sep2016V3_MC_AK4PFchs' # MoriondMC
#process.load("CondCore.DBCommon.CondDBCommon_cfi")
#from CondCore.DBCommon.CondDBSetup_cfi import *
#process.jec = cms.ESSource("PoolDBESSource",
# DBParameters = cms.PSet(
# messageLevel = cms.untracked.int32(0)
# ),
# timetype = cms.string('runnumber'),
# toGet = cms.VPSet(
# cms.PSet(
# record = cms.string('JetCorrectionsRecord'),
# tag = cms.string(tag),
# label = cms.untracked.string('AK4PFchs')
# ),
# ),
# connect = cms.string('sqlite:{0}'.format(sqfile)),
#)
#process.es_prefer_jec = cms.ESPrefer('PoolDBESSource','jec')
#############################
### Setup rest of running ###
#############################
process.load("FWCore.MessageService.MessageLogger_cfi")
process.MessageLogger.cerr.FwkReport.reportEvery = options.reportEvery
process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(options.maxEvents) )
process.source = cms.Source("PoolSource",
fileNames = cms.untracked.vstring(options.inputFiles),
skipEvents = cms.untracked.uint32(options.skipEvents),
)
process.TFileService = cms.Service("TFileService",
fileName = cms.string(options.outputFile),
)
process.schedule = cms.Schedule()
###########################
### Profiling utilities ###
###########################
#process.ProfilerService = cms.Service (
# "ProfilerService",
# firstEvent = cms.untracked.int32(2),
# lastEvent = cms.untracked.int32(500),
# paths = cms.untracked.vstring('schedule')
#)
#process.SimpleMemoryCheck = cms.Service(
# "SimpleMemoryCheck",
# ignoreTotal = cms.untracked.int32(1)
#)
### To use IgProf's neat memory profiling tools, uncomment the following
### lines then run this cfg with igprof like so:
### $ igprof -d -mp -z -o igprof.mp.gz cmsRun ...
### this will create a memory profile every 250 events so you can track use
### Turn the profile into text with
### $ igprof-analyse -d -v -g -r MEM_LIVE igprof.yourOutputFile.gz > igreport_live.res
### To do a performance profile instead of a memory profile, change -mp to -pp
### in the first command and remove -r MEM_LIVE from the second
### For interpretation of the output, see http://igprof.org/text-output-format.html
#from IgTools.IgProf.IgProfTrigger import igprof
#process.load("IgTools.IgProf.IgProfTrigger")
#process.igprofPath = cms.Path(process.igprof)
#process.igprof.reportEventInterval = cms.untracked.int32(250)
#process.igprof.reportToFileAtBeginJob = cms.untracked.string("|gzip -c>igprof.begin-job.gz")
#process.igprof.reportToFileAtEvent = cms.untracked.string("|gzip -c>igprof.%I.%E.%L.%R.event.gz")
#process.schedule.append(process.igprofPath)
# first create collections to analyze
collections = {
'genParticles' : 'prunedGenParticles',
'electrons' : 'slimmedElectrons',
'muons' : 'slimmedMuons',
'taus' : 'slimmedTaus',
'photons' : 'slimmedPhotons',
'jets' : 'slimmedJets',
'pfmet' : 'slimmedMETs',
'rho' : 'fixedGridRhoFastjetAll',
'vertices' : 'offlineSlimmedPrimaryVertices',
'packed' : 'packedPFCandidates',
}
# the selections for each object (to be included in ntuple)
# will always be the last thing done to the collection, so can use embedded things from previous steps
selections = {
'electrons' : 'pt>7 && abs(eta)<2.5',
'muons' : 'pt>4 && abs(eta)<2.4',
'taus' : 'pt>17 && abs(eta)<2.3',
'photons' : 'pt>10 && abs(eta)<3.0',
'jets' : 'pt>15 && abs(eta)<4.7',
}
if options.isMC:
selections['genParticles'] = 'pt>4'
# requirements to store events
minCounts = {
'electrons' : 1,
'muons' : 1,
#'taus' : 1,
'photons' : 0,
#'jets' : 1,
}
# maximum candidates to store
# selects the first n in the collection
# patobjects are pt ordered
# vertices has pv first
maxCounts = {
'vertices': 1,
}
# selection for cleaning (objects should match final selection)
# just do at analysis level
cleaning = {
#'jets' : {
# 'electrons' : {
# 'cut' : 'pt>10 && abs(eta)<2.5 && userInt("cutBasedElectronID-Spring15-25ns-V1-standalone-medium")>0.5 && userInt("WWLoose")>0.5',
# 'dr' : 0.3,
# },
# 'muons' : {
# 'cut' : 'pt>10 && abs(eta)<2.4 && isMediumMuon>0.5 && trackIso/pt<0.4 && userFloat("dxy")<0.02 && userFloat("dz")<0.1 && (pfIsolationR04().sumChargedHadronPt+max(0.,pfIsolationR04().sumNeutralHadronEt+pfIsolationR04().sumPhotonEt-0.5*pfIsolationR04().sumPUPt))/pt<0.15',
# 'dr' : 0.3,
# },
# 'taus' : {
# 'cut' : 'pt>20 && abs(eta)<2.3 && tauID("byMediumCombinedIsolationDeltaBetaCorr3Hits")>0.5 && tauID("decayModeFinding")>0.5',
# 'dr' : 0.3,
# },
#},
}
# filters
filters = []
# met filters
#if options.runMetFilter:
# run all the time and store result
print 'Preparing MET filters'
from HLTrigger.HLTfilters.hltHighLevel_cfi import hltHighLevel
hltFilter = hltHighLevel.clone()
# PAT if miniaod by itself (MC) and RECO if at the same time as reco (data)
hltFilter.TriggerResultsTag = cms.InputTag('TriggerResults', '', 'PAT') if options.isMC else cms.InputTag('TriggerResults', '', 'RECO')
hltFilter.throw = cms.bool(True)
# ICHEP recommendation
# https://twiki.cern.ch/twiki/bin/viewauth/CMS/MissingETOptionalFiltersRun2#MiniAOD_8011_ICHEP_dataset
for flag in ['HBHENoiseFilter','HBHENoiseIsoFilter','EcalDeadCellTriggerPrimitiveFilter','goodVertices','eeBadScFilter','globalTightHalo2016Filter']:
mod = hltFilter.clone(HLTPaths=cms.vstring('Flag_{0}'.format(flag)))
modName = 'filter{0}'.format(flag)
setattr(process,modName,mod)
filters += [getattr(process,modName)]
process.load('RecoMET.METFilters.BadChargedCandidateFilter_cfi')
process.BadChargedCandidateFilter.muons = cms.InputTag("slimmedMuons")
process.BadChargedCandidateFilter.PFCandidates = cms.InputTag("packedPFCandidates")
filters += [process.BadChargedCandidateFilter]
# now do any customization/cleaning
print 'Customizing jets'
from DevTools.Ntuplizer.customizeJets import customizeJets
collections = customizeJets(
process,
collections,
isMC=bool(options.isMC),
isREMINIAOD=bool(options.isREMINIAOD),
reHLT=bool(options.reHLT),
)
print 'Customizing electrons'
from DevTools.Ntuplizer.customizeElectrons import customizeElectrons
collections = customizeElectrons(
process,
collections,
isMC=bool(options.isMC),
isREMINIAOD=bool(options.isREMINIAOD),
reHLT=bool(options.reHLT),
)
print 'Customizing muons'
from DevTools.Ntuplizer.customizeMuons import customizeMuons
collections = customizeMuons(
process,
collections,
isMC=bool(options.isMC),
isREMINIAOD=bool(options.isREMINIAOD),
reHLT=bool(options.reHLT),
)
print 'Customizing taus'
from DevTools.Ntuplizer.customizeTaus import customizeTaus
collections = customizeTaus(
process,
collections,
isMC=bool(options.isMC),
isREMINIAOD=bool(options.isREMINIAOD),
reHLT=bool(options.reHLT),
)
print 'Customizing photons'
from DevTools.Ntuplizer.customizePhotons import customizePhotons
collections = customizePhotons(
process,
collections,
isMC=bool(options.isMC),
isREMINIAOD=bool(options.isREMINIAOD),
reHLT=bool(options.reHLT),
)
print 'Customizing METs'
from DevTools.Ntuplizer.customizeMets import customizeMets
collections = customizeMets(
process,
collections,
isMC=bool(options.isMC),
isREMINIAOD=bool(options.isREMINIAOD),
reHLT=bool(options.reHLT),
)
# select desired objects
print 'Selecting objects'
from DevTools.Ntuplizer.objectTools import objectSelector, objectCleaner
for coll in selections:
collections[coll] = objectSelector(process,coll,collections[coll],selections[coll])
# TODO: memory problem
#for coll in cleaning:
# collections[coll] = objectCleaner(process,coll,collections[coll],collections,cleaning[coll])
# add the analyzer
process.load("DevTools.Ntuplizer.MiniTree_cfi")
process.miniTree.isData = not options.isMC
process.miniTree.filterResults = cms.InputTag('TriggerResults', '', 'PAT') if options.isMC else cms.InputTag('TriggerResults', '', 'RECO')
#process.miniTree.filterResults = cms.InputTag('TriggerResults', '', 'PAT')
process.miniTree.vertexCollections.vertices.collection = collections['vertices']
if options.isMC:
from DevTools.Ntuplizer.branchTemplates import genParticleBranches
process.miniTree.collections.genParticles = cms.PSet(
collection = cms.InputTag(collections['genParticles']),
branches = genParticleBranches,
)
process.miniTree.collections.electrons.collection = collections['electrons']
process.miniTree.collections.muons.collection = collections['muons']
process.miniTree.collections.taus.collection = collections['taus']
process.miniTree.collections.photons.collection = collections['photons']
process.miniTree.collections.jets.collection = collections['jets']
process.miniTree.collections.pfmet.collection = collections['pfmet']
process.miniTree.rho = collections['rho']
for coll, count in minCounts.iteritems():
if process.miniTree.vertexCollections.hasParameter(coll):
getattr(process.miniTree.vertexCollections,coll).minCount = cms.int32(count)
if process.miniTree.collections.hasParameter(coll):
getattr(process.miniTree.collections,coll).minCount = cms.int32(count)
else:
print 'Unrecognized collection {0}'.format(coll)
for coll, count in maxCounts.iteritems():
if process.miniTree.vertexCollections.hasParameter(coll):
getattr(process.miniTree.vertexCollections,coll).maxCount = cms.int32(count)
elif process.miniTree.collections.hasParameter(coll):
getattr(process.miniTree.collections,coll).maxCount = cms.int32(count)
else:
print 'Unrecognized collection {0}'.format(coll)
process.miniTreePath = cms.Path()
for f in filters:
process.miniTreePath += cms.ignore(f)
process.miniTreePath += process.miniTree
process.schedule.append(process.miniTreePath)