Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:57:50

0001 from __future__ import print_function
0002 from ROOT import TH1F, TH2F, TFile
0003 
0004 
0005 class EnergyCorrector( object ):
0006     """Generic energy corrector"""
0007     
0008     def __init__(self, fnam, histnam='h_cor'):
0009         """
0010         fnam is a root file containing a 1D histogram giving
0011         the correction factor as a function of eta.
0012         """
0013         self.file = TFile(fnam)
0014         if self.file.IsZombie():
0015             raise ValueError(fnam+' cannot be opened')
0016         self.hist = self.file.Get(histnam)
0017         if self.hist==None:
0018             raise ValueError('{h} cannot be found in {f}'.format(h=histnam,
0019                                                                  f=fnam))
0020             
0021 
0022     def correct_p4(self, p4):
0023         """
0024         returns the corrected 4-momentum.
0025         The 4 momentum is expected to behave as the one of the Candidate class
0026         """
0027         eta = p4.eta()
0028         pt = p4.pt()
0029         return pt*self.correction_factor(pt, eta)
0030 
0031     def correction_factor(self, pt, eta):
0032         """
0033         returns the correction factor.
0034         takes also pt as this class could be generalized for a 2D calibration.
0035         """
0036         etabin = self.hist.FindBin(eta)
0037         shift = self.hist.GetBinContent(etabin)/100.
0038         return shift
0039     
0040         
0041 if __name__ == '__main__':
0042 
0043     import sys
0044     c = JetEnergyCorrector( sys.argv[1] )
0045     etas = [-5, -4.5, -4, -3, -2.5, -2, -1, 0, 1, 2, 2.5, 3, 4, 4.5, 5]
0046     pt = 20.
0047     print(pt)
0048     for eta in etas:
0049         print(eta, c.correction_factor(pt, eta))
0050