forked from alphatwirl/alphatwirl
/
Round.py
executable file
·111 lines (82 loc) · 3.23 KB
/
Round.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Tai Sakuma <tai.sakuma@cern.ch>
import math
import logging
##__________________________________________________________________||
def returnTrue(x): return True
##__________________________________________________________________||
class Round(object):
def __init__(self, width = 1, aBoundary = None,
min = None, underflow_bin = None,
max = None, overflow_bin = None,
valid = returnTrue, retvalue = 'lowedge'
):
supportedRetvalues = ('center', 'lowedge')
if retvalue not in supportedRetvalues:
raise ValueError("The retvalue '%s' is not supported! " % (retvalue, ) + "Supported values are '" + "', '".join(supportedRetvalues) + "'")
self.width = width
self.aBoundary = aBoundary
self.halfWidth = self.width/2 if self.width % 2 == 0 else float(self.width)/2
if aBoundary is None: aBoundary = self.halfWidth
self.boundaries = [aBoundary - width, aBoundary, aBoundary + width]
self.lowedge = (retvalue == 'lowedge')
self.min = min
self.underflow_bin = underflow_bin
self.max = max
self.overflow_bin = overflow_bin
self.valid = valid
def __repr__(self):
return '{}(width = {!r}, aBoundary = {!r}, min = {!r}, underflow_bin = {!r}, max = {!r}, overflow_bin = {!r}, valid = {!r})'.format(
self.__class__.__name__,
self.width,
self.aBoundary,
self.min,
self.underflow_bin,
self.max,
self.overflow_bin,
self.valid
)
def __call__(self, val):
if not self.valid(val):
return None
if self.min is not None:
if not self.min <= val:
return self.underflow_bin
if self.max is not None:
if not val < self.max:
return self.overflow_bin
if math.isinf(val):
logger = logging.getLogger(__name__)
logger.warning('val = {}. will return {}'.format(val, None))
return None
self._updateBoundaries(val)
bin = self.boundaries[0]
for b in self.boundaries[1:]:
if b <= val: bin = b
else: break
if not self.lowedge:
bin += self.halfWidth
return bin
def _updateBoundaries(self, val):
while val < self.boundaries[0]:
self.boundaries.insert(0, self.boundaries[0] - self.width)
while val > self.boundaries[-1]:
self.boundaries.append(self.boundaries[-1] + self.width)
def next(self, bin):
bin = self.__call__(bin)
if bin is None:
return None
if bin == self.underflow_bin:
return self.__call__(self.min)
if bin == self.overflow_bin:
return self.overflow_bin
self._updateBoundaries(bin)
self._updateBoundaries(bin + self.width)
nbin = self.boundaries[0]
for b in self.boundaries[1:]:
if b <= bin: nbin = b
else: break
ret = self.boundaries[self.boundaries.index(nbin) + 1]
if not self.lowedge:
ret += self.halfWidth
return ret
##__________________________________________________________________||