Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:49

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