CMS 3D CMS Logo

EnergyCorrector.py
Go to the documentation of this file.
1 from __future__ import print_function
2 from ROOT import TH1F, TH2F, TFile
3 
4 
6  """Generic energy corrector"""
7 
8  def __init__(self, fnam, histnam='h_cor'):
9  """
10  fnam is a root file containing a 1D histogram giving
11  the correction factor as a function of eta.
12  """
13  self.file = TFile(fnam)
14  if self.file.IsZombie():
15  raise ValueError(fnam+' cannot be opened')
16  self.hist = self.file.Get(histnam)
17  if self.hist==None:
18  raise ValueError('{h} cannot be found in {f}'.format(h=histnam,
19  f=fnam))
20 
21 
22  def correct_p4(self, p4):
23  """
24  returns the corrected 4-momentum.
25  The 4 momentum is expected to behave as the one of the Candidate class
26  """
27  eta = p4.eta()
28  pt = p4.pt()
29  return pt*self.correction_factor(pt, eta)
30 
31  def correction_factor(self, pt, eta):
32  """
33  returns the correction factor.
34  takes also pt as this class could be generalized for a 2D calibration.
35  """
36  etabin = self.hist.FindBin(eta)
37  shift = self.hist.GetBinContent(etabin)/100.
38  return shift
39 
40 
41 if __name__ == '__main__':
42 
43  import sys
44  c = JetEnergyCorrector( sys.argv[1] )
45  etas = [-5, -4.5, -4, -3, -2.5, -2, -1, 0, 1, 2, 2.5, 3, 4, 4.5, 5]
46  pt = 20.
47  print(pt)
48  for eta in etas:
49  print(eta, c.correction_factor(pt, eta))
50 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
def correction_factor(self, pt, eta)
def __init__(self, fnam, histnam='h_cor')