CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
JetReCalibrator.py
Go to the documentation of this file.
1 import ROOT
2 import os
3 from math import *
5 
7  def __init__(self,globalTag,jetFlavour,doResidualJECs,jecPath,upToLevel=3):
8  """Create a corrector object that reads the payloads from the text dumps of a global tag under
9  CMGTools/RootTools/data/jec (see the getJec.py there to make the dumps).
10  It will apply the L1,L2,L3 and possibly the residual corrections to the jets."""
11  # Make base corrections
12  path = jecPath #"%s/src/CMGTools/RootTools/data/jec" % os.environ['CMSSW_BASE'];
13  self.L1JetPar = ROOT.JetCorrectorParameters("%s/%s_L1FastJet_%s.txt" % (path,globalTag,jetFlavour),"");
14  self.L2JetPar = ROOT.JetCorrectorParameters("%s/%s_L2Relative_%s.txt" % (path,globalTag,jetFlavour),"");
15  self.L3JetPar = ROOT.JetCorrectorParameters("%s/%s_L3Absolute_%s.txt" % (path,globalTag,jetFlavour),"");
16  self.vPar = ROOT.vector(ROOT.JetCorrectorParameters)()
17  self.vPar.push_back(self.L1JetPar);
18  if upToLevel >= 2: self.vPar.push_back(self.L2JetPar);
19  if upToLevel >= 3: self.vPar.push_back(self.L3JetPar);
20  # Add residuals if needed
21  if doResidualJECs :
22  self.ResJetPar = ROOT.JetCorrectorParameters("%s/%s_L2L3Residual_%s.txt" % (path,globalTag,jetFlavour))
23  self.vPar.push_back(self.ResJetPar);
24  #Step3 (Construct a FactorizedJetCorrector object)
25  self.JetCorrector = ROOT.FactorizedJetCorrector(self.vPar)
26  self.JetUncertainty = ROOT.JetCorrectionUncertainty("%s/%s_Uncertainty_%s.txt" % (path,globalTag,jetFlavour));
27  def correctAll(self,jets,rho,delta=0,metShift=[0,0]):
28  """Applies 'correct' to all the jets, discard the ones that have bad corrections (corrected pt <= 0)"""
29  badJets = []
30  for j in jets:
31  ok = self.correct(j,rho,delta,metShift)
32  if not ok: badJets.append(j)
33  if len(badJets) > 0:
34  print "Warning: %d out of %d jets flagged bad by JEC." % (len(badJets), len(jets))
35  for bj in badJets:
36  jets.remove(bj)
37  def correct(self,jet,rho,delta=0,metShift=[0,0]):
38  """Corrects a jet energy (optionally shifting it also by delta times the JEC uncertainty)
39  If a two-component list is passes as 'metShift', it will be modified adding to the first and second
40  component the change to the MET along x and y due to the JEC, defined as the negative difference
41  between the new and old jet 4-vectors, for jets with corrected pt > 10."""
42  self.JetCorrector.setJetEta(jet.eta())
43  self.JetCorrector.setJetPt(jet.pt() * jet.rawFactor())
44  self.JetCorrector.setJetA(jet.jetArea())
45  self.JetCorrector.setRho(rho)
46  corr = self.JetCorrector.getCorrection()
47  self.JetUncertainty.setJetEta(jet.eta())
48  self.JetUncertainty.setJetPt(corr * jet.pt() * jet.rawFactor())
49  try:
50  jet.jetEnergyCorrUncertainty = self.JetUncertainty.getUncertainty(True)
51  except RuntimeError, r:
52  print "Caught %s when getting uncertainty for jet of pt %.1f, eta %.2f\n" % (r,corr * jet.pt() * jet.rawFactor(),jet.eta())
53  jet.jetEnergyCorrUncertainty = 0.5
54  if jet.component(4).fraction() < 0.9 and jet.pt()*corr*jet.rawFactor() > 10:
55  metShift[0] -= jet.px()*(corr*jet.rawFactor() - 1)*(1-jet.component(3).fraction())
56  metShift[1] -= jet.py()*(corr*jet.rawFactor() - 1)*(1-jet.component(3).fraction())
57  if delta != 0:
58  #print " jet with corr pt %6.2f has an uncertainty %.2f " % (jet.pt()*jet.rawFactor()*corr, jet.jetEnergyCorrUncertainty)
59  corr *= max(0, 1+delta*jet.jetEnergyCorrUncertainty)
60  if jet.pt()*jet.rawFactor()*corr > 10:
61  metShift[0] -= jet.px()*jet.rawFactor()*corr*delta*jet.jetEnergyCorrUncertainty
62  metShift[1] -= jet.py()*jet.rawFactor()*corr*delta*jet.jetEnergyCorrUncertainty
63  #print " jet with raw pt %6.2f eta %+5.3f phi %+5.3f: previous corr %.4f, my corr %.4f " % (jet.pt()*jet.rawFactor(), jet.eta(), jet.phi(), 1./jet.rawFactor(), corr)
64  if corr <= 0:
65  return False
66  jet.setP4(jet.p4() * (corr * jet.rawFactor()))
67  jet.setRawFactor(1.0/corr)
68  return True
69 
71  def __init__(self,globalTag,jetFlavour,doResidualJECs):
72  """Create a corrector object that reads the payloads from the text dumps of a global tag under
73  CMGTools/RootTools/data/jec (see the getJec.py there to make the dumps).
74  It will make the Type1 MET."""
75  # Make base corrections
76  path = "%s/src/CMGTools/RootTools/data/jec" % os.environ['CMSSW_BASE'];
77  self.L1JetPar = ROOT.JetCorrectorParameters("%s/%s_L1FastJet_%s.txt" % (path,globalTag,jetFlavour));
78  self.L2JetPar = ROOT.JetCorrectorParameters("%s/%s_L2Relative_%s.txt" % (path,globalTag,jetFlavour));
79  self.L3JetPar = ROOT.JetCorrectorParameters("%s/%s_L3Absolute_%s.txt" % (path,globalTag,jetFlavour));
80  self.vPar = ROOT.vector(ROOT.JetCorrectorParameters)()
81  self.vPar.push_back(self.L1JetPar);
82  self.vPar.push_back(self.L2JetPar);
83  self.vPar.push_back(self.L3JetPar);
84  # Add residuals if needed
85  if doResidualJECs :
86  self.ResJetPar = ROOT.JetCorrectorParameters("%s/%s_L2L3Residual_%s.txt" % (path,globalTag,jetFlavour))
87  self.vPar.push_back(self.ResJetPar);
88  #Step3 (Construct a FactorizedJetCorrector object)
89  self.JetCorrector = ROOT.FactorizedJetCorrector(self.vPar)
90  self.JetUncertainty = ROOT.JetCorrectionUncertainty("%s/%s_Uncertainty_%s.txt" % (path,globalTag,jetFlavour));
91  self.vPar1 = ROOT.vector(ROOT.JetCorrectorParameters)()
92  self.vPar1.push_back(self.L1JetPar);
93  self.JetCorrectorL1 = ROOT.FactorizedJetCorrector(self.vPar1)
94  def getMETCorrection(self,jets,rho,muons):
95  px0, py0 = 0, 0
96  for j in jets:
97  if j.component(4).fraction() > 0.9: continue
98  jp4 = j.p4().__class__(j.p4());
99  jp4 *= j.rawFactor()
100  mup4 = j.p4().__class__(0,0,0,0)
101  for mu in muons:
102  if mu.sourcePtr().track().isNonnull() and (mu.sourcePtr().isGlobalMuon() or mu.sourcePtr().isStandAloneMuon()) and deltaR(mu.eta(),mu.phi(),j.eta(),j.phi()) < 0.5:
103  mup4 += mu.p4()
104  jp4 -= mup4
105  self.JetCorrector.setJetEta(j.eta())
106  self.JetCorrector.setJetPt(j.pt()*j.rawFactor()) # NOTE: we don't subtract the muon here!
107  self.JetCorrector.setJetA(j.jetArea())
108  self.JetCorrector.setRho(rho)
109  corr3 = self.JetCorrector.getCorrection()
110  self.JetCorrectorL1.setJetEta(j.eta())
111  self.JetCorrectorL1.setJetPt(j.pt()*j.rawFactor()) # NOTE: we don't subtract the muon here!
112  self.JetCorrectorL1.setJetA(j.jetArea())
113  self.JetCorrectorL1.setRho(rho)
114  corr1 = self.JetCorrectorL1.getCorrection()
115  if jp4.pt() * corr3 < 10:
116  #print " no correction from jet of pt %8.2f / %8.2f , phi %+5.3f / %+5.3f (corr3 %5.3f, corr1 %5.3f): %+7.3f %+7.3f" % (j.pt(),jp4.pt(),j.phi(), jp4.phi(), corr3, corr1, -jp4.X()*(corr3 -corr1),-jp4.Y()*(corr3-corr1))
117  continue
118  #print " correction from jet of pt %8.2f, phi %+5.3f / %+5.3f (corr3 %5.3f, corr1 %5.3f): %+7.3f %+7.3f" % (j.pt(),j.phi(), jp4.phi(), corr3, corr1, -jp4.X()*(corr3-corr1), -jp4.Y()*(corr3-
119  px0 -= jp4.X()*(corr3-corr1)
120  py0 -= jp4.Y()*(corr3-corr1)
121  return [px0, py0]
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17