babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
bmaker_full_cfg.py
Go to the documentation of this file.
1 ###########################################################
2 ### Configuration file to make full babies from miniAOD
3 ###########################################################
4 import math, sys
5 from os import environ
6 from os.path import exists, join, basename
7 
8 def findFileInPath(theFile):
9  for s in environ["CMSSW_SEARCH_PATH"].split(":"):
10  attempt = join(s,theFile)
11  if exists(attempt):
12  return attempt
13  print "BABYMAKER: Could not find file "+theFile+". Not pre-applying JSON"
14  return None
15 
16 ###### Input parameters parsing
17 import FWCore.ParameterSet.Config as cms
18 from FWCore.ParameterSet.VarParsing import VarParsing
19 options = VarParsing ('analysis')
20 options.register('nEventsSample',
21  100,
22  VarParsing.multiplicity.singleton,
23  VarParsing.varType.int,
24  "Total number of events in dataset for event weight calculation.")
25 options.register('nEvents',
26  100,
27  VarParsing.multiplicity.singleton,
28  VarParsing.varType.int,
29  "Number of events to run over.")
30 options.register('json',
31  'babymaker/data/json/golden_Cert_271036-284044_13TeV_PromptReco_Collisions16_JSON_NoL1T.txt',
32  VarParsing.multiplicity.singleton,
33  VarParsing.varType.string,
34  "Path to json starting with babymaker/...")
35 options.register('condorSubTime',
36  '000000_000000',
37  VarParsing.multiplicity.singleton,
38  VarParsing.varType.string,
39  "Timestamp from condor submission")
40 options.parseArguments()
41 outName = options.outputFile
42 if outName == "output.root": # output filename not set
43  rootfile = basename(options.inputFiles[0])
44  outName = "fullbaby_"+rootfile.replace("file:","")
45 
46 doSystematics = True
47 if "FSPremix" in outName or "Fast" in outName: fastsim = True
48 else: fastsim = False
49 
50 
51 ## JECs must be undone and reapplied when rerunning b-tagging
52 ## => if doJEC = False, DeepCSV discriminator will not be included
53 doJEC = False
54 
55 #if doJEC: jets_label = "updatedPatJetsTransientCorrectedUpdatedJEC"
56 if doJEC: jets_label = "updatedPatJetsTransientCorrectedDeepFlavour"
57 else: jets_label = "slimmedJets"
58 
59 # to apply JECs with txt files in babymaker,
60 # prefix jecLabel with "onthefly_", e.g. onthefly_Spring16_25nsV6_MC
61 # systematics will also be calculated using this tag, even if JECs are not re-applied
62 # N.B. JECs change in the middle of RunF, thereby the Run2016F1 vs Run2016F2 distinction
63 jecLabel = 'onthefly_Spring16_23Sep2016V2_MC'
64 if ("Run2016B" in outName) or ("Run2016C" in outName) or ("Run2016D" in outName):
65  jecLabel = 'Summer16_23Sep2016BCDV3_DATA'
66 elif ("Run2016E" in outName) or ("Run2016F1" in outName):
67  jecLabel = 'Summer16_23Sep2016EFV3_DATA'
68 elif ("Run2016F2" in outName) or ("Run2016G" in outName):
69  jecLabel = 'Summer16_23Sep2016GV3_DATA'
70 elif ("Run2016H" in outName):
71  jecLabel = 'Summer16_23Sep2016HV3_DATA'
72 elif "RunIISpring16MiniAOD" in outName:
73  jecLabel = 'Spring16_23Sep2016V2_MC' # for ICHEP MC against re-reco data
74 elif "RunIISummer16MiniAOD" in outName:
75  jecLabel = 'Summer16_23Sep2016V3_MC'
76 elif "RunIIFall17MiniAODv2" in outName:
77  jecLabel = 'Fall17_17Nov2017_V8_MC'
78 elif "Run2017" in outName:
79  jecLabel = 'Summer16_23Sep2016GV3_DATA'
80 
81 # because FastSim naming for JECs variables inside db and txt files is really truly messed up...
82 if fastsim: jecLabel = 'Spring16_25nsFastSimV1_MC'
83 jecCorrLabel = jecLabel
84 if fastsim: jecCorrLabel = 'Spring16_25nsFastSimMC_V1'
85 jecBabyLabel = jecLabel
86 if fastsim:
87  if (doJEC): jecBabyLabel = 'Spring16_FastSimV1_MC'
88  else: jecBabyLabel = 'onthefly_Spring16_FastSimV1_MC'
89 
90 
91 if "Run201" in outName:
92  isData = True
93  # These only used for the official application of JECs
94  globalTag = "80X_dataRun2_2016SeptRepro_v6"
95  processRECO = "RECO"
96  jecLevels = ['L1FastJet', 'L2Relative', 'L3Absolute', 'L2L3Residual']
97 else:
98  isData = False
99  # These only used for the official application of JECs
100  globalTag = "80X_mcRun2_asymptotic_2016_miniAODv2"
101  if "RunIISummer16MiniAOD" in outName: globalTag = "80X_mcRun2_asymptotic_2016_TrancheIV_v7"
102  elif "RunIIFall17MiniAODv2" in outName: globalTag = "94X_mc2017_realistic_v14"
103  processRECO = "PAT"
104  jecLevels = ['L1FastJet', 'L2Relative', 'L3Absolute']
105 
106 ###### Defining Baby process, input and output files
107 process = cms.Process("Baby")
108 process.source = cms.Source("PoolSource",
109  fileNames = cms.untracked.vstring(options.inputFiles)
110 )
111 if isData: # Processing only lumis in JSON
112  import FWCore.PythonUtilities.LumiList as LumiList
113  jsonfile = findFileInPath(options.json)
114  process.source.lumisToProcess = LumiList.LumiList(filename = jsonfile).getVLuminosityBlockRange()
115  doSystematics = False
116 
117 process.load("Configuration.Geometry.GeometryRecoDB_cff")
118 process.load("Configuration.StandardSequences.MagneticField_cff")
119 
120 ###### Setting global tag
121 ## From https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookJetEnergyCorrections#JecGlobalTag
122 process.load('Configuration.StandardSequences.Services_cff')
123 process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff')
124 process.GlobalTag.globaltag = globalTag
125 
126 # DAK8
127 # needed for DeepAK8
128 # process.TransientTrackBuilderESProducer = cms.ESProducer("TransientTrackBuilderESProducer",
129 # ComponentName=cms.string('TransientTrackBuilder')
130 # )
131 
132 process.baby_full = cms.EDAnalyzer('bmaker_full',
133  condor_subtime = cms.string(options.condorSubTime),
134  outputFile = cms.string(outName),
135  inputFiles = cms.vstring(options.inputFiles),
136  json = cms.string(options.json),
137  jec = cms.string(jecBabyLabel),
138  met = cms.InputTag("slimmedMETs"),
139  met_nohf = cms.InputTag("slimmedMETsNoHF"),
140  jets = cms.InputTag(jets_label),
141  nEventsSample = cms.uint32(options.nEventsSample),
142  doMetRebalancing = cms.bool(True),
143  doSystematics = cms.bool(doSystematics),
144  addBTagWeights = cms.bool(True),
145  isFastSim = cms.bool(fastsim),
146  debugMode = cms.bool(False)
147 )
148 
149 ###### Setting up number of events, and reporing frequency
150 process.load("FWCore.MessageService.MessageLogger_cfi")
151 process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(options.nEvents) )
152 process.MessageLogger.cerr.FwkReport.reportEvery = 100000
153 
154 
155 if not fastsim:
156  process.load('Configuration.StandardSequences.Services_cff')
157 
158  ##_____________Bad charged candidate filter________________||
159  process.load('RecoMET.METFilters.BadChargedCandidateFilter_cfi')
160  process.BadChargedCandidateFilter.muons = cms.InputTag("slimmedMuons")
161  process.BadChargedCandidateFilter.PFCandidates = cms.InputTag("packedPFCandidates")
162  process.BadChargedCandidateFilter.taggingMode = cms.bool(True)
163  process.BadChargedCandidateFilter.debug = cms.bool(False)
164 
165  ##_____________Bad muon filter_____________________________||
166  process.load('RecoMET.METFilters.BadPFMuonFilter_cfi')
167  process.BadPFMuonFilter.muons = cms.InputTag("slimmedMuons")
168  process.BadPFMuonFilter.PFCandidates = cms.InputTag("packedPFCandidates")
169  process.BadPFMuonFilter.taggingMode = cms.bool(True)
170  process.BadPFMuonFilter.debug = cms.bool(False)
171 
172 if doJEC:
173  process.options = cms.untracked.PSet(
174  allowUnscheduled = cms.untracked.bool(True),
175  wantSummary = cms.untracked.bool(False)
176  )
177  ###### Setting sqlite file for the JECs that are in newer global tags
178  ## From https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookJetEnergyCorrections#JecSqliteFile
179  process.load("CondCore.DBCommon.CondDBCommon_cfi")
180  from CondCore.DBCommon.CondDBSetup_cfi import CondDBSetup
181  process.jec = cms.ESSource("PoolDBESSource",CondDBSetup,
182  connect = cms.string('sqlite_fip:babymaker/data/jec/'+jecLabel+'.db'),
183  toGet = cms.VPSet(
184  cms.PSet(
185  record = cms.string("JetCorrectionsRecord"),
186  tag = cms.string("JetCorrectorParametersCollection_"+jecCorrLabel+"_AK4PFchs"),
187  label = cms.untracked.string("AK4PFchs")
188  )
189  )
190  )
191  process.es_prefer_jec = cms.ESPrefer("PoolDBESSource","jec")
192  ###### Applying JECs and including deepCSV info
193  ## From https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookJetEnergyCorrections#CorrPatJets
194  from PhysicsTools.PatAlgos.tools.jetTools import updateJetCollection
195  updateJetCollection(
196  process,
197  jetSource = cms.InputTag('slimmedJets'),
198  labelName = 'UpdatedJEC',
199  jetCorrections = ('AK4PFchs', cms.vstring(jecLevels), 'None'),
200  btagDiscriminators = ["pfDeepCSVJetTags:probudsg",
201  "pfDeepCSVJetTags:probb",
202  "pfDeepCSVJetTags:probc",
203  "pfDeepCSVJetTags:probbb",
204  "pfDeepCSVJetTags:probcc"]
205  )
206 
207  ###### Apply new JECs to MET
208  ## From https://twiki.cern.ch/twiki/bin/view/CMS/MissingETUncertaintyPrescription
209  from PhysicsTools.PatUtils.tools.runMETCorrectionsAndUncertainties import runMetCorAndUncFromMiniAOD
210  ## If you only want to re-correct and get the proper uncertainties, no reclustering
211  runMetCorAndUncFromMiniAOD(process,
212  isData = isData,
213  fixEE2017 = True,
214  fixEE2017Params = {'userawPt': True, 'ptThreshold':50.0, 'miniEtaThreshold':2.65, 'maxEtaThreshold':3.139},
215  postfix = "ModifiedMET"
216 
217  )
218 
219 process.dump=cms.EDAnalyzer('EventContentAnalyzer')
220 process.p = cms.Path(process.baby_full)
221 
def findFileInPath(theFile)