CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Public Attributes
JetAnalyzer.JetAnalyzer Class Reference
Inheritance diagram for JetAnalyzer.JetAnalyzer:

Public Member Functions

def __init__
 
def beginLoop
 
def declareHandles
 
def jerCorrection
 
def jesCorrection
 
def process
 
def testBJet
 
def testJet
 
def testJetID
 

Public Attributes

 btagSF
 
 is2012
 

Detailed Description

Analyze jets ;-)

This analyzer filters the jets that do not correspond to the leptons
stored in event.selectedLeptons, and puts in the event:
- jets: all jets passing the pt and eta cuts
- cleanJets: the collection of jets away from the leptons
- cleanBJets: the jets passing testBJet, and away from the leptons

Example configuration:

jetAna = cfg.Analyzer(
  'JetAnalyzer',
  jetCol = 'slimmedJets'
  # cmg jet input collection
  # pt threshold
  jetPt = 30,
  # eta range definition
  jetEta = 5.0,
  # seed for the btag scale factor
  btagSFseed = 0xdeadbeef,
  # if True, the PF and PU jet ID are not applied, and the jets get flagged
  relaxJetId = False,
)

Definition at line 16 of file JetAnalyzer.py.

Constructor & Destructor Documentation

def JetAnalyzer.JetAnalyzer.__init__ (   self,
  cfg_ana,
  cfg_comp,
  looperName 
)

Definition at line 42 of file JetAnalyzer.py.

42 
43  def __init__(self, cfg_ana, cfg_comp, looperName):
44  super(JetAnalyzer,self).__init__(cfg_ana, cfg_comp, looperName)
45  self.btagSF = BTagSF (cfg_ana.btagSFseed)
46  self.is2012 = isNewerThan('CMSSW_5_2_0')

Member Function Documentation

def JetAnalyzer.JetAnalyzer.beginLoop (   self,
  setup 
)

Definition at line 59 of file JetAnalyzer.py.

59 
60  def beginLoop(self, setup):
61  super(JetAnalyzer,self).beginLoop(setup)
62  self.counters.addCounter('jets')
63  count = self.counters.counter('jets')
64  count.register('all events')
65  count.register('at least 2 good jets')
66  count.register('at least 2 clean jets')
67  count.register('at least 1 b jet')
68  count.register('at least 2 b jets')
def JetAnalyzer.JetAnalyzer.declareHandles (   self)

Definition at line 47 of file JetAnalyzer.py.

References core.AutoHandle.AutoHandle.handles, core.Analyzer.Analyzer.handles, HTTP.RequestManager.handles, and core.Analyzer.Analyzer.mchandles.

47 
48  def declareHandles(self):
49  super(JetAnalyzer, self).declareHandles()
50 
51  self.handles['jets'] = AutoHandle( self.cfg_ana.jetCol,
52  'std::vector<pat::Jet>' )
53  if self.cfg_comp.isMC:
54  # and ("BB" in self.cfg_comp.name):
55  self.mchandles['genParticles'] = AutoHandle( 'packedGenParticles',
56  'std::vector<pat::PackedGenParticle>' )
57  self.mchandles['genJets'] = AutoHandle('slimmedGenJets',
58  'std::vector<reco::GenJet>')
def JetAnalyzer.JetAnalyzer.jerCorrection (   self,
  jet 
)
Adds JER correction according to first method at
https://twiki.cern.ch/twiki/bin/view/CMS/JetResolution

Requires some attention when genJet matching fails.

Definition at line 162 of file JetAnalyzer.py.

References funct.abs().

Referenced by JetAnalyzer.JetAnalyzer.process().

163  def jerCorrection(self, jet):
164  ''' Adds JER correction according to first method at
165  https://twiki.cern.ch/twiki/bin/view/CMS/JetResolution
166 
167  Requires some attention when genJet matching fails.
168  '''
169  if not hasattr(jet, 'matchedGenJet'):
170  return
171  #import pdb; pdb.set_trace()
172  corrections = [0.052, 0.057, 0.096, 0.134, 0.288]
173  maxEtas = [0.5, 1.1, 1.7, 2.3, 5.0]
174  eta = abs(jet.eta())
175  for i, maxEta in enumerate(maxEtas):
176  if eta < maxEta:
177  pt = jet.pt()
178  deltaPt = (pt - jet.matchedGenJet.pt()) * corrections[i]
179  totalScale = (pt + deltaPt) / pt
180 
181  if totalScale < 0.:
182  totalScale = 0.
183  jet.scaleEnergy(totalScale)
184  break
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def JetAnalyzer.JetAnalyzer.jesCorrection (   self,
  jet,
  scale = 0. 
)
Adds JES correction in number of sigmas (scale)

Definition at line 185 of file JetAnalyzer.py.

Referenced by JetAnalyzer.JetAnalyzer.process().

186  def jesCorrection(self, jet, scale=0.):
187  ''' Adds JES correction in number of sigmas (scale)
188  '''
189  # Do nothing if nothing to change
190  if scale == 0.:
191  return
192  unc = jet.uncOnFourVectorScale()
193  totalScale = 1. + scale * unc
194  if totalScale < 0.:
195  totalScale = 0.
196  jet.scaleEnergy(totalScale)
def JetAnalyzer.JetAnalyzer.process (   self,
  event 
)

Definition at line 69 of file JetAnalyzer.py.

References funct.abs(), analyzer.Analyzer.cfg_ana, analyzer.Analyzer.cfg_comp, looper.Looper.cfg_comp, deltar.cleanObjectCollection(), reco.deltaR2(), core.AutoHandle.AutoHandle.handles, core.Analyzer.Analyzer.handles, HTTP.RequestManager.handles, JetAnalyzer.JetAnalyzer.jerCorrection(), JetAnalyzer.JetAnalyzer.jesCorrection(), deltar.matchObjectCollection(), core.Analyzer.Analyzer.mchandles, SingleMuPt40Fwdv2_cfi_GEN_SIM.pdgId, core.Analyzer.Analyzer.readCollections(), JetAnalyzer.JetAnalyzer.testBJet(), and JetAnalyzer.JetAnalyzer.testJet().

Referenced by ConfigBuilder.ConfigBuilder.addExtraStream(), ConfigBuilder.ConfigBuilder.completeInputCommand(), ConfigBuilder.ConfigBuilder.doNotInlineEventContent(), ConfigBuilder.ConfigBuilder.PrintAllModules.leave(), ConfigBuilder.ConfigBuilder.prepare_HLT(), ConfigBuilder.ConfigBuilder.prepare_LHE(), ConfigBuilder.ConfigBuilder.prepare_PATFILTER(), ConfigBuilder.ConfigBuilder.prepare_VALIDATION(), ConfigBuilder.ConfigBuilder.renameHLTprocessInSequence(), ConfigBuilder.ConfigBuilder.renameInputTagsInSequence(), and ConfigBuilder.ConfigBuilder.scheduleSequence().

69 
70  def process(self, event):
71 
72  self.readCollections( event.input )
73  miniaodjets = self.handles['jets'].product()
74 
75  allJets = []
76  event.jets = []
77  event.bJets = []
78  event.cleanJets = []
79  event.cleanBJets = []
80 
81  leptons = []
82  if hasattr(event, 'selectedLeptons'):
83  leptons = event.selectedLeptons
84 
85  genJets = None
86  if self.cfg_comp.isMC:
87  genJets = map( GenJet, self.mchandles['genJets'].product() )
88 
89  for maodjet in miniaodjets:
90  jet = Jet( maodjet )
91  allJets.append( jet )
92  if self.cfg_comp.isMC and hasattr( self.cfg_comp, 'jetScale'):
93  scale = random.gauss( self.cfg_comp.jetScale,
94  self.cfg_comp.jetSmear )
95  jet.scaleEnergy( scale )
96  if genJets:
97  # Use DeltaR = 0.25 matching like JetMET
98  pairs = matchObjectCollection( [jet], genJets, 0.25*0.25)
99  if pairs[jet] is None:
100  pass
101  else:
102  jet.matchedGenJet = pairs[jet]
103  #Add JER correction for MC jets. Requires gen-jet matching.
104  if self.cfg_comp.isMC and hasattr(self.cfg_ana, 'jerCorr') and self.cfg_ana.jerCorr:
105  self.jerCorrection(jet)
106  #Add JES correction for MC jets.
107  if self.cfg_comp.isMC and hasattr(self.cfg_ana, 'jesCorr'):
108  self.jesCorrection(jet, self.cfg_ana.jesCorr)
109  if self.testJet( jet ):
110  event.jets.append(jet)
111  if self.testBJet(jet):
112  event.bJets.append(jet)
113 
114  self.counters.counter('jets').inc('all events')
115 
116  event.cleanJets, dummy = cleanObjectCollection( event.jets,
117  masks = leptons,
118  deltaRMin = 0.5 )
119  event.cleanBJets, dummy = cleanObjectCollection( event.bJets,
120  masks = leptons,
121  deltaRMin = 0.5 )
122 
123  pairs = matchObjectCollection( leptons, allJets, 0.5*0.5)
124  # associating a jet to each lepton
125  for lepton in leptons:
126  jet = pairs[lepton]
127  if jet is None:
128  lepton.jet = lepton
129  else:
130  lepton.jet = jet
131 
132  # associating a leg to each clean jet
133  invpairs = matchObjectCollection( event.cleanJets, leptons, 99999. )
134  for jet in event.cleanJets:
135  leg = invpairs[jet]
136  jet.leg = leg
137 
138  for jet in event.cleanJets:
139  jet.matchGenParton=999.0
140 
141  if self.cfg_comp.isMC and "BB" in self.cfg_comp.name:
142  genParticles = self.mchandles['genParticles'].product()
143  event.genParticles = map( GenParticle, genParticles)
144  for gen in genParticles:
145  if abs(gen.pdgId())==5 and gen.mother() and abs(gen.mother().pdgId())==21:
146  for jet in event.cleanJets:
147  dR=deltaR2(jet.eta(), jet.phi(), gen.eta(), gen.phi() )
148  if dR<jet.matchGenParton:
149  jet.matchGenParton=dR
150 
151  event.jets30 = [jet for jet in event.jets if jet.pt()>30]
152  event.cleanJets30 = [jet for jet in event.cleanJets if jet.pt()>30]
153  if len( event.jets30 )>=2:
154  self.counters.counter('jets').inc('at least 2 good jets')
155  if len( event.cleanJets30 )>=2:
156  self.counters.counter('jets').inc('at least 2 clean jets')
157  if len(event.cleanBJets)>0:
158  self.counters.counter('jets').inc('at least 1 b jet')
159  if len(event.cleanBJets)>1:
160  self.counters.counter('jets').inc('at least 2 b jets')
161  return True
def cleanObjectCollection
Definition: deltar.py:119
def matchObjectCollection
Definition: deltar.py:152
Definition: Jet.py:1
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
T1 deltaR2(T1 eta1, T2 phi1, T3 eta2, T4 phi2)
Definition: deltaR.h:36
def JetAnalyzer.JetAnalyzer.testBJet (   self,
  jet 
)

Definition at line 211 of file JetAnalyzer.py.

References funct.abs(), JetAnalyzer.JetAnalyzer.is2012, and JetAnalyzer.JetAnalyzer.testJetID().

Referenced by JetAnalyzer.JetAnalyzer.process().

212  def testBJet(self, jet):
213  # medium csv working point
214  # https://twiki.cern.ch/twiki/bin/viewauth/CMS/BTagPerformanceOP#B_tagging_Operating_Points_for_3
215  jet.btagMVA = jet.btag("combinedSecondaryVertexBJetTags")
216  jet.btagFlag = self.btagSF.BTagSFcalc.isbtagged(
217  jet.pt(),
218  jet.eta(),
219  jet.btag("combinedSecondaryVertexBJetTags"),
220  abs(jet.partonFlavour()),
221  not self.cfg_comp.isMC,
222  0,0,
223  self.is2012
224  )
225  return jet.pt()>20 and \
226  abs( jet.eta() ) < 2.4 and \
227  self.testJetID(jet)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def JetAnalyzer.JetAnalyzer.testJet (   self,
  jet 
)

Definition at line 206 of file JetAnalyzer.py.

References funct.abs(), and JetAnalyzer.JetAnalyzer.testJetID().

Referenced by JetAnalyzer.JetAnalyzer.process().

207  def testJet( self, jet ):
208  return jet.pt() > self.cfg_ana.jetPt and \
209  abs( jet.eta() ) < self.cfg_ana.jetEta and \
210  self.testJetID(jet)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def JetAnalyzer.JetAnalyzer.testJetID (   self,
  jet 
)

Definition at line 197 of file JetAnalyzer.py.

Referenced by JetAnalyzer.JetAnalyzer.testBJet(), and JetAnalyzer.JetAnalyzer.testJet().

198  def testJetID(self, jet):
199  jet.puJetIdPassed = jet.puJetId()
200  jet.pfJetIdPassed = jet.jetID("POG_PFID_Loose")
201  if self.cfg_ana.relaxJetId:
202  return True
203  else:
204  return jet.puJetIdPassed and jet.pfJetIdPassed
205 

Member Data Documentation

JetAnalyzer.JetAnalyzer.btagSF

Definition at line 44 of file JetAnalyzer.py.

JetAnalyzer.JetAnalyzer.is2012

Definition at line 45 of file JetAnalyzer.py.

Referenced by JetAnalyzer.JetAnalyzer.testBJet().