CMS 3D CMS Logo

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