Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:30

0001 # Copyright (C) 2014 Colin Bernet
0002 # https://github.com/cbernet/heppy/blob/master/LICENSE
0003 
0004 import math
0005 import copy
0006 
0007 class Value(object):
0008     def __init__(self, val, err):
0009         self.val = val
0010         self.err = err
0011 
0012     def relerr(self):
0013         return abs(self.err / self.val)
0014 
0015     def __eq__(self, other):
0016         return self.val == other.val and self.err == other.err
0017 
0018     def __iadd__(self, other):
0019         self.val += other.val
0020         self.err = math.sqrt( self.err*self.err + other.err*other.err)
0021         return self
0022 
0023     def __add__(self, other):
0024         new = copy.deepcopy(self)
0025         new += other
0026         return new
0027 
0028     def __isub__(self, other):
0029         self.val -= other.val
0030         self.err = math.sqrt( self.err*self.err + other.err*other.err)
0031         return self
0032 
0033     def __sub__(self, other):
0034         new = copy.deepcopy(self)
0035         new -= other
0036         return new
0037 
0038     def __idiv__(self, other):
0039         relerr = math.sqrt( self.relerr()*self.relerr() + other.relerr()*other.relerr())
0040         self.val /= other.val
0041         self.err = relerr * self.val
0042         return self
0043 
0044     def __div__(self, other):
0045         new = copy.deepcopy(self)
0046         new /= other
0047         return new
0048 
0049     def __str__(self):
0050         return '{val:10.3f} +- {err:8.3f} ({relerr:5.2f}%)'.format(val=self.val,
0051                                                                   err=self.err,
0052                                                                   relerr=self.relerr()*100)