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
8 import PhysicsTools.HeppyCore.framework.config
as cfg
13 from ROOT
import TLorentzVector, TVectorD
17 from copy
import deepcopy
20 def __init__(self, cfg_ana, cfg_comp, looperName ):
21 super(METAnalyzer,self).
__init__(cfg_ana,cfg_comp,looperName)
25 self.handles[
'met'] = AutoHandle( self.cfg_ana.metCollection,
'std::vector<pat::MET>' )
26 self.handles[
'nopumet'] = AutoHandle( self.cfg_ana.noPUMetCollection,
'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 self.mchandles[
'packedGen'] = AutoHandle(
'packedGenParticles',
'std::vector<pat::PackedGenParticle>' )
33 self.counters.addCounter(
'events')
34 count = self.counters.counter(
'events')
35 count.register(
'all events')
38 px,py = self.met.px()+deltaMet[0], self.met.py()+deltaMet[1]
39 met.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
48 pfcands = self.handles[
'cmgCand'].product()
50 for i
in xrange(pfcands.size()):
54 if (pfcands.at(i).
charge()!=0):
56 if abs(pfcands.at(i).dz())<=self.cfg_ana.dzMax:
57 charged.append(pfcands.at(i))
59 if pfcands.at(i).fromPV()>0:
60 chargedchs.append(pfcands.at(i))
62 if pfcands.at(i).fromPV()>1:
63 chargedPVLoose.append(pfcands.at(i))
65 if pfcands.at(i).fromPV()>2:
66 chargedPVTight.append(pfcands.at(i))
69 setattr(event,
"tkMet"+self.cfg_ana.collectionPostFix, \
70 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]))) ))
71 setattr(event,
"tkMetPVchs"+self.cfg_ana.collectionPostFix, \
72 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]))) ))
73 setattr(event,
"tkMetPVLoose"+self.cfg_ana.collectionPostFix, \
74 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]))) ))
75 setattr(event,
"tkMetPVTight"+self.cfg_ana.collectionPostFix, \
76 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]))) ))
80 event.tkSumEt = sum([x.pt()
for x
in charged])
81 event.tkPVchsSumEt = sum([x.pt()
for x
in chargedchs])
82 event.tkPVLooseSumEt = sum([x.pt()
for x
in chargedPVLoose])
83 event.tkPVTightSumEt = sum([x.pt()
for x
in chargedPVTight])
87 genCharged = [ x
for x
in self.mchandles[
'packedGen'].product()
if x.charge() != 0
and abs(x.eta()) < 2.4 ]
88 event.tkGenMet = ROOT.reco.Particle.LorentzVector(-1.*(sum([x.px()
for x
in genCharged])) , -1.*(sum([x.py()
for x
in genCharged])), 0, math.hypot((sum([x.px()
for x
in genCharged])),(sum([x.py()
for x
in genCharged]))) )
91 self.metNoMu = copy.deepcopy(self.met)
92 if self.cfg_ana.doMetNoPU: self.metNoMuNoPU = copy.deepcopy(self.metNoPU)
97 for mu
in event.selectedMuons:
102 px,py = self.metNoMu.px()+mupx, self.metNoMu.py()+mupy
103 self.metNoMu.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
104 px,py = self.metNoMuNoPU.px()+mupx, self.metNoMuNoPU.py()+mupy
105 self.metNoMuNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
106 setattr(event,
"metNoMu"+self.cfg_ana.collectionPostFix, self.metNoMu)
107 if self.cfg_ana.doMetNoPU: setattr(event,
"metNoMuNoPU"+self.cfg_ana.collectionPostFix, self.metNoMuNoPU)
111 self.metNoEle = copy.deepcopy(self.met)
112 if self.cfg_ana.doMetNoPU: self.metNoEleNoPU = copy.deepcopy(self.metNoPU)
117 for ele
in event.selectedElectrons:
122 px,py = self.metNoEle.px()+elepx, self.metNoEle.py()+elepy
123 self.metNoEle.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
125 px,py = self.metNoEleNoPU.px()+elepx, self.metNoEleNoPU.py()+elepy
126 self.metNoEleNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
127 setattr(event,
"metNoEle"+self.cfg_ana.collectionPostFix, self.metNoEle)
128 if self.cfg_ana.doMetNoPU: setattr(event,
"metNoEleNoPU"+self.cfg_ana.collectionPostFix, self.metNoEleNoPU)
131 self.metNoPhoton = copy.deepcopy(self.met)
136 for pho
in event.selectedPhotons:
141 px,py = self.metNoPhoton.px()+phopx, self.metNoPhoton.py()+phopy
142 self.metNoPhoton.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
143 setattr(event,
"metNoPhoton"+self.cfg_ana.collectionPostFix, self.metNoPhoton)
144 if self.cfg_ana.doMetNoPU:
145 self.metNoPhotonNoPU = copy.deepcopy(self.metNoPU)
146 px,py = self.metNoPhotonNoPU.px()+phopx, self.metNoPhotonNoPU.py()+phopy
147 self.metNoPhotonNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
148 setattr(event,
"metNoPhotonNoPU"+self.cfg_ana.collectionPostFix, self.metNoPhotonNoPU)
153 if self.cfg_ana.copyMETsByValue:
154 self.met = ROOT.pat.MET(self.handles[
'met'].product()[0])
155 if self.cfg_ana.doMetNoPU: self.metNoPU = ROOT.pat.MET(self.handles[
'nopumet'].product()[0])
157 self.met = self.handles[
'met'].product()[0]
158 if self.cfg_ana.doMetNoPU: self.metNoPU = self.handles[
'nopumet'].product()[0]
163 if not self.cfg_ana.copyMETsByValue:
164 for i
in range(self.met.METUncertaintySize):
165 m = ROOT.pat.MET(self.met)
168 m.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
170 setattr(event,
"met{0}_shifted_{1}".
format(self.cfg_ana.collectionPostFix, i), m)
172 self.met_sig = self.met.significance()
173 self.met_sumet = self.met.sumEt()
177 if not self.cfg_ana.copyMETsByValue:
178 self.metraw = self.met.shiftedPt(12, 0)
179 self.metType1chs = self.met.shiftedPt(12, 1)
180 setattr(event,
"metraw"+self.cfg_ana.collectionPostFix, self.metraw)
181 setattr(event,
"metType1chs"+self.cfg_ana.collectionPostFix, self.metType1chs)
183 if self.cfg_ana.recalibrate
and hasattr(event,
'deltaMetFromJetSmearing'+self.cfg_ana.jetAnalyzerCalibrationPostFix):
184 deltaMetSmear = getattr(event,
'deltaMetFromJetSmearing'+self.cfg_ana.jetAnalyzerCalibrationPostFix)
185 self.applyDeltaMet(self.met, deltaMetSmear)
186 if self.cfg_ana.doMetNoPU:
187 self.applyDeltaMet(self.metNoPU, deltaMetSmear)
188 if self.cfg_ana.recalibrate
and hasattr(event,
'deltaMetFromJEC'+self.cfg_ana.jetAnalyzerCalibrationPostFix):
189 deltaMetJEC = getattr(event,
'deltaMetFromJEC'+self.cfg_ana.jetAnalyzerCalibrationPostFix)
191 self.applyDeltaMet(self.met, deltaMetJEC)
192 if self.cfg_ana.doMetNoPU:
193 self.applyDeltaMet(self.metNoPU, deltaMetJEC)
196 setattr(event,
"met"+self.cfg_ana.collectionPostFix, self.met)
197 if self.cfg_ana.doMetNoPU: setattr(event,
"metNoPU"+self.cfg_ana.collectionPostFix, self.metNoPU)
198 setattr(event,
"met_sig"+self.cfg_ana.collectionPostFix, self.met_sig)
199 setattr(event,
"met_sumet"+self.cfg_ana.collectionPostFix, self.met_sumet)
201 if self.cfg_ana.doMetNoMu
and hasattr(event,
'selectedMuons'):
202 self.makeMETNoMu(event)
204 if self.cfg_ana.doMetNoEle
and hasattr(event,
'selectedElectrons'):
205 self.makeMETNoEle(event)
207 if self.cfg_ana.doMetNoPhoton
and hasattr(event,
'selectedPhotons'):
208 self.makeMETNoPhoton(event)
211 self.readCollections( event.input)
212 self.counters.counter(
'events').inc(
'all events')
216 if self.cfg_ana.doTkMet:
217 self.makeTkMETs(event);
219 if self.cfg_comp.isMC
and hasattr(event,
'genParticles'):
220 self.makeGenTkMet(event)
225 setattr(METAnalyzer,
"defaultConfig", cfg.Analyzer(
226 class_object = METAnalyzer,
227 metCollection =
"slimmedMETs",
228 noPUMetCollection =
"slimmedMETs",
229 copyMETsByValue =
False,
231 jetAnalyzerCalibrationPostFix =
"",
236 doMetNoPhoton =
False,
237 candidates=
'packedPFCandidates',
238 candidatesTypes=
'std::vector<pat::PackedCandidate>',
240 collectionPostFix =
"",
Abs< T >::type abs(const T &t)