CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
METAnalyzer.py
Go to the documentation of this file.
1 import random
2 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
3 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
4 from PhysicsTools.Heppy.physicsobjects.PhysicsObjects import Jet
6 from PhysicsTools.HeppyCore.statistics.counter import Counter, Counters
7 from PhysicsTools.Heppy.physicsutils.JetReCalibrator import JetReCalibrator
9 
10 import operator
11 import itertools
12 import copy
13 from ROOT import TLorentzVector, TVectorD
14 import ROOT
15 import math
16 
17 from copy import deepcopy
18 
19 class METAnalyzer( Analyzer ):
20  def __init__(self, cfg_ana, cfg_comp, looperName ):
21  super(METAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
22 
23  def declareHandles(self):
24  super(METAnalyzer, self).declareHandles()
25  self.handles['met'] = AutoHandle( 'slimmedMETs', 'std::vector<pat::MET>' )
26  self.handles['nopumet'] = AutoHandle( 'slimmedMETs', 'std::vector<pat::MET>' )
27  self.handles['cmgCand'] = AutoHandle( self.cfg_ana.candidates, self.cfg_ana.candidatesTypes )
28  self.handles['vertices'] = AutoHandle( "offlineSlimmedPrimaryVertices", 'std::vector<reco::Vertex>', fallbackLabel="offlinePrimaryVertices" )
29 
30  def beginLoop(self, setup):
31  super(METAnalyzer,self).beginLoop(setup)
32  self.counters.addCounter('events')
33  count = self.counters.counter('events')
34  count.register('all events')
35 
36  def makeTkMETs(self, event):
37  event.tkMet = 0
38 
39  charged = []
40  chargedchs = []
41  chargedPVLoose = []
42  chargedPVTight = []
43 
44  pfcands = self.handles['cmgCand'].product()
45 
46  for i in xrange(pfcands.size()):
47 
48 ## ===> require the Track Candidate charge and with a minimum dz
49 
50  if (pfcands.at(i).charge()!=0):
51 
52  if abs(pfcands.at(i).dz())<=self.cfg_ana.dzMax:
53  charged.append(pfcands.at(i))
54 
55  if pfcands.at(i).fromPV()>0:
56  chargedchs.append(pfcands.at(i))
57 
58  if pfcands.at(i).fromPV()>1:
59  chargedPVLoose.append(pfcands.at(i))
60 
61  if pfcands.at(i).fromPV()>2:
62  chargedPVTight.append(pfcands.at(i))
63 
64  import ROOT
65  event.tkMet = ROOT.reco.Particle.LorentzVector(-1.*(sum([x.px() for x in charged])) , -1.*(sum([x.py() for x in charged])), 0, math.hypot((sum([x.px() for x in charged])),(sum([x.py() for x in charged]))) )
66  event.tkMetchs = ROOT.reco.Particle.LorentzVector(-1.*(sum([x.px() for x in chargedchs])) , -1.*(sum([x.py() for x in chargedchs])), 0, math.hypot((sum([x.px() for x in chargedchs])),(sum([x.py() for x in chargedchs]))) )
67  event.tkMetPVLoose = ROOT.reco.Particle.LorentzVector(-1.*(sum([x.px() for x in chargedPVLoose])) , -1.*(sum([x.py() for x in chargedPVLoose])), 0, math.hypot((sum([x.px() for x in chargedPVLoose])),(sum([x.py() for x in chargedPVLoose]))) )
68  event.tkMetPVTight = ROOT.reco.Particle.LorentzVector(-1.*(sum([x.px() for x in chargedPVTight])) , -1.*(sum([x.py() for x in chargedPVTight])), 0, math.hypot((sum([x.px() for x in chargedPVTight])),(sum([x.py() for x in chargedPVTight]))) )
69 ## print 'tkmet',event.tkMet.pt(),'tkmetphi',event.tkMet.phi()
70 
71 
72  def makeMETNoMu(self, event):
73  event.metNoMu = copy.deepcopy(event.met)
74  event.metNoMuNoPU = copy.deepcopy(event.metNoPU)
75 
76  mupx = 0
77  mupy = 0
78  #sum muon momentum
79  for mu in event.selectedMuons:
80  mupx += mu.px()
81  mupy += mu.py()
82 
83  #subtract muon momentum and construct met
84  px,py = event.metNoMu.px()-mupx, event.metNoMu.py()-mupy
85  event.metNoMu.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
86  px,py = event.metNoMuNoPU.px()-mupx, event.metNoMuNoPU.py()-mupy
87  event.metNoMuNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
88 
89  def makeMETNoPhoton(self, event):
90  event.metNoPhoton = copy.deepcopy(event.met)
91  event.metNoPhotonNoPU = copy.deepcopy(event.metNoPU)
92 
93  phopx = 0
94  phopy = 0
95  #sum photon momentum
96  for pho in event.selectedPhotons:
97  phopx += pho.px()
98  phopy += pho.py()
99 
100  #subtract photon momentum and construct met
101  px,py = event.metNoPhoton.px()-phopx, event.metNoPhoton.py()-phopy
102  event.metNoPhoton.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
103  px,py = event.metNoPhotonNoPU.px()-phopx, event.metNoPhotonNoPU.py()-phopy
104  event.metNoPhotonNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
105 
106 
107  def makeMETs(self, event):
108  event.met = self.handles['met'].product()[0]
109  event.metNoPU = self.handles['nopumet'].product()[0]
110 
111  #Shifted METs
112  #Uncertainties defined in https://github.com/cms-sw/cmssw/blob/CMSSW_7_2_X/DataFormats/PatCandidates/interface/MET.h#L168
113  for i in range(event.met.METUncertaintySize):
114  m = ROOT.pat.MET(event.met)
115  px = m.shiftedPx(i);
116  py = m.shiftedPy(i);
117  m.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
118  setattr(event, "met_shifted_{0}".format(i), m)
119  event.met_sig = event.met.significance()
120  event.met_sumet = event.met.sumEt()
121  #event.met_sigm = event.met.getSignificanceMatrix()
122 
123  ###https://github.com/cms-sw/cmssw/blob/CMSSW_7_2_X/DataFormats/PatCandidates/interface/MET.h
124  event.metraw = event.met.shiftedPt(12, 0)
125  event.metType1chs = event.met.shiftedPt(12, 1)
126 
127  if self.cfg_ana.recalibrate and hasattr(event, 'deltaMetFromJetSmearing'):
128  px,py = event.met.px()+event.deltaMetFromJetSmearing[0], event.met.py()+event.deltaMetFromJetSmearing[1]
129  event.met.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
130  px,py = event.metNoPU.px()+event.deltaMetFromJetSmearing[0], event.metNoPU.py()+event.deltaMetFromJetSmearing[1]
131  event.metNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
132  if self.cfg_ana.recalibrate and hasattr(event, 'deltaMetFromJEC') and event.deltaMetFromJEC[0] != 0 and event.deltaMetFromJEC[1] != 0:
133  px,py = event.met.px()+event.deltaMetFromJEC[0], event.met.py()+event.deltaMetFromJEC[1]
134  event.met.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
135  px,py = event.metNoPU.px()+event.deltaMetFromJEC[0], event.metNoPU.py()+event.deltaMetFromJEC[1]
136  event.metNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
137 
138  if self.cfg_ana.doMetNoMu and hasattr(event, 'selectedMuons'):
139  self.makeMETNoMu(event)
140 
141  if self.cfg_ana.doMetNoPhoton and hasattr(event, 'selectedPhotons'):
142  self.makeMETNoPhoton(event)
143 
144  def process(self, event):
145  self.readCollections( event.input)
146  self.counters.counter('events').inc('all events')
147 
148  self.makeMETs(event)
149  event.tkMet = 0
150 
151  if self.cfg_ana.doTkMet:
152  self.makeTkMETs(event);
153 
154 
155 
156  return True
157 
158 
159 setattr(METAnalyzer,"defaultConfig", cfg.Analyzer(
160  class_object = METAnalyzer,
161  recalibrate = True,
162  doTkMet = False,
163  doMetNoMu = False,
164  doMetNoPhoton = False,
165  candidates='packedPFCandidates',
166  candidatesTypes='std::vector<pat::PackedCandidate>',
167  dzMax = 0.1,
168  )
169 )
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def makeMETNoMu
print &#39;tkmet&#39;,event.tkMet.pt(),&#39;tkmetphi&#39;,event.tkMet.phi()
Definition: METAnalyzer.py:72