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 
16 class METAnalyzer( Analyzer ):
17  def __init__(self, cfg_ana, cfg_comp, looperName ):
18  super(METAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
19 
20  def declareHandles(self):
21  super(METAnalyzer, self).declareHandles()
22  self.handles['met'] = AutoHandle( 'slimmedMETs', 'std::vector<pat::MET>' )
23  self.handles['nopumet'] = AutoHandle( 'slimmedMETs', 'std::vector<pat::MET>' )
24  self.handles['cmgCand1'] = AutoHandle( self.cfg_ana.candidates, self.cfg_ana.candidatesTypes )
25  self.handles['cmgCand2'] = AutoHandle( self.cfg_ana.candidates, self.cfg_ana.candidatesTypes )
26  self.handles['cmgCand3'] = AutoHandle( self.cfg_ana.candidates, self.cfg_ana.candidatesTypes )
27  self.handles['cmgCand4'] = AutoHandle( self.cfg_ana.candidates, self.cfg_ana.candidatesTypes )
28  self.handles['cmgCand5'] = AutoHandle( self.cfg_ana.candidates, self.cfg_ana.candidatesTypes )
29  self.handles['cmgCand6'] = AutoHandle( self.cfg_ana.candidates, self.cfg_ana.candidatesTypes )
30  self.handles['cmgCand7'] = AutoHandle( self.cfg_ana.candidates, self.cfg_ana.candidatesTypes )
31  self.handles['cmgCand8'] = AutoHandle( self.cfg_ana.candidates, self.cfg_ana.candidatesTypes )
32  self.handles['vertices'] = AutoHandle( "offlineSlimmedPrimaryVertices", 'std::vector<reco::Vertex>', fallbackLabel="offlinePrimaryVertices" )
33 
34  def beginLoop(self, setup):
35  super(METAnalyzer,self).beginLoop(setup)
36  self.counters.addCounter('events')
37  count = self.counters.counter('events')
38  count.register('all events')
39 
40  def makeTkMETs(self, event):
41  event.tkMet = 0
42 
43  charged = []
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 and (abs(pfcands.at(i).dz())<=self.cfg_ana.dzMax )):
51 
52  charged.append(pfcands.at(i))
53 
54  import ROOT
55  event.tkMet = ROOT.reco.Particle.LorentzVector(-1.*(sum([x.px() for x in charged])) , -1.*(sum([x.py() for x in charged])), 0, 0 )
56 ## print 'tkmet',event.tkMet.pt(),'tkmetphi',event.tkMet.phi()
57 
58 
59  def makeMETNoMu(self, event):
60  event.metNoMu = copy.deepcopy(self.handles['met'].product()[0])
61  event.metNoMuNoPU = copy.deepcopy(self.handles['nopumet'].product()[0])
62 
63  mupx = 0
64  mupy = 0
65  #sum muon momentum
66  for mu in event.selectedMuons:
67  mupx += mu.px()
68  mupy += mu.py()
69 
70  #subtract muon momentum and construct met
71  if self.cfg_ana.recalibrate and hasattr(event, 'deltaMetFromJetSmearing'):
72  import ROOT
73  px,py = event.metNoMu.px()+event.deltaMetFromJetSmearing[0]-mupx, event.metNoMu.py()+event.deltaMetFromJetSmearing[1]-mupy
74  event.metNoMu.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
75  px,py = event.metNoMuNoPU.px()+event.deltaMetFromJetSmearing[0]-mupx, event.metNoMuNoPU.py()+event.deltaMetFromJetSmearing[1]-mupy
76  event.metNoMuNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, hypot(px,py)))
77  if self.cfg_ana.recalibrate and hasattr(event, 'deltaMetFromJEC') and event.deltaMetFromJEC[0] != 0 and event.deltaMetFromJEC[1] != 0:
78  import ROOT
79  px,py = event.metNoMu.px()+event.deltaMetFromJEC[0]-mupx, event.metNoMu.py()+event.deltaMetFromJEC[1]-mupy
80  event.met.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
81  px,py = event.metNoMuNoPU.px()+event.deltaMetFromJEC[0]-mupx, event.metNoMuNoPU.py()+event.deltaMetFromJEC[1]-mupy
82  event.metNoMuNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
83 
84 
85  def makeMETs(self, event):
86  event.met = self.handles['met'].product()[0]
87  event.metNoPU = self.handles['nopumet'].product()[0]
88  if self.cfg_ana.recalibrate and hasattr(event, 'deltaMetFromJetSmearing'):
89  import ROOT
90  px,py = event.met.px()+event.deltaMetFromJetSmearing[0], event.met.py()+event.deltaMetFromJetSmearing[1]
91  event.met.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
92  px,py = event.metNoPU.px()+event.deltaMetFromJetSmearing[0], event.metNoPU.py()+event.deltaMetFromJetSmearing[1]
93  event.metNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
94  if self.cfg_ana.recalibrate and hasattr(event, 'deltaMetFromJEC') and event.deltaMetFromJEC[0] != 0 and event.deltaMetFromJEC[1] != 0:
95  import ROOT
96  px,py = event.met.px()+event.deltaMetFromJEC[0], event.met.py()+event.deltaMetFromJEC[1]
97  event.met.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
98  px,py = event.metNoPU.px()+event.deltaMetFromJEC[0], event.metNoPU.py()+event.deltaMetFromJEC[1]
99  event.metNoPU.setP4(ROOT.reco.Particle.LorentzVector(px,py, 0, math.hypot(px,py)))
100 
101 
102  def process(self, event):
103  self.readCollections( event.input)
104  self.counters.counter('events').inc('all events')
105 
106  self.makeMETs(event)
107  event.tkMet = 0
108 
109  if self.cfg_ana.doTkMet:
110  self.makeTkMETs(event);
111 
112  if self.cfg_ana.doMetNoMu and hasattr(event, 'selectedMuons'):
113  self.makeMETNoMu(event)
114 
115  return True
116 
117 
118 setattr(METAnalyzer,"defaultConfig", cfg.Analyzer(
119  class_object = METAnalyzer,
120  recalibrate = True,
121  doTkMet = False,
122  doMetNoMu = False,
123  candidates='packedPFCandidates',
124  candidatesTypes='std::vector<pat::PackedCandidate>',
125  dzMax = 0.1,
126  )
127 )
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:59