source: fact/tools/pyscripts/pyfact/extractor.py@ 14912

Last change on this file since 14912 was 14468, checked in by neise, 12 years ago
added 2D support
File size: 6.9 KB
Line 
1#!/usr/bin/python -tt
2#
3# Dominik Neise, Werner Lustermann
4# TU Dortmund, ETH Zurich
5#
6import numpy as np
7from generator import *
8from fir_filter import *
9
10class GlobalMaxFinder(object):
11 """ Pulse Extractor
12 Finds the global maximum in the given window.
13 (Best used with filtered data)
14 """
15
16 def __init__(self, min=30, max=250 , name = 'GlobalMaxFinder'):
17 """ initialize search Window
18
19 """
20 self.__module__="extractor"
21 self.min = min
22 self.max = max
23 self.name = name
24
25 def __call__(self, data):
26 if data.ndim > 1:
27 time = np.argmax( data[ : , self.min:self.max ], 1)
28 amplitude = np.max( data[ : , self.min:self.max], 1)
29 else:
30 time = np.argmax( data[self.min:self.max])
31 amplitude = np.max( data[self.min:self.max])
32 return amplitude, time+self.min
33
34 def __str__(self):
35 s = self.name + '\n'
36 s += 'window:\n'
37 s += '(min,max) = (' + str(self.min) + ',' + str(self.max) + ')'
38 return s
39
40 def test(self):
41 pass
42
43
44class WindowIntegrator(object):
45 """ Integrates in a given intergration window around the given position
46 """
47
48 def __init__(self, min=13, max=23 , name = 'WindowIntegrator'):
49 """ initialize integration Window
50 """
51 self.__module__="extractor"
52 self.min = min
53 self.max = max
54 self.name = name
55
56 def __call__(self, data, pos):
57 integral = np.empty( data.shape[0] )
58 for pixel in range( data.shape[0] ):
59 integral[pixel] = data[pixel, (pos[pixel]-self.min):(pos[pixel]+self.max)].sum()
60 return integral
61
62 def __str__(self):
63 s = self.name + '\n'
64 s += 'window:\n'
65 s += '(min,max) = (' + str(self.min) + ',' + str(self.max) + ')'
66 return s
67
68class FixedWindowIntegrator(object):
69 """ Integrates in a given intergration window
70 """
71
72 def __init__(self, min=55, max=105 , name = 'FixedWindowIntegrator'):
73 """ initialize integration Window
74 """
75 self.__module__="extractor"
76 self.min = min
77 self.max = max
78 self.name = name
79
80 def __call__(self, data):
81 integral = np.empty( data.shape[0] )
82 for pixel in range( data.shape[0] ):
83 integral[pixel] = data[pixel, self.min:self.max].sum()
84 return integral
85
86 def __str__(self):
87 s = self.name + '\n'
88 s += 'window:\n'
89 s += '(min,max) = (' + str(self.min) + ',' + str(self.max) + ')'
90 return s
91
92class ZeroXing(object):
93 """ Finds zero crossings in given data
94 (should be used on CFD output for peak finding)
95 returns list of lists of time_of_zero_crossing
96 """
97 def __init__(self, slope=1, name = 'ZeroXing'):
98 self.__module__="extractor"
99 if (slope >= 0):
100 self.slope = 1 # search for rising edge crossing
101 elif (slope < 0):
102 self.slope = -1 # search for falling edge crossing
103 self.name = name
104
105
106 def __call__(self, data, zero_level = 0):
107 all_hits = []
108 if data.ndim == 2:
109 for pix_data in data:
110 hits = []
111 for i in range( data.shape[1]-1 ):
112 dat = pix_data[i] - zero_level
113 next_dat = pix_data[i+1] - zero_level
114 if ( self.slope > 0 ):
115 if ( dat > 0 ):
116 continue
117 else:
118 if ( dat < 0):
119 continue
120 if ( dat * next_dat <= 0 ):
121 # interpolate time of zero crossing with
122 # linear polynomial: y = ax + b
123 a = (next_dat - dat) / ((i+1) - i)
124 time = -1.0/a * dat + i
125 hits.append(time)
126 all_hits.append(hits)
127
128 if data.ndim == 1:
129 for i in range( data.shape[0]-1 ):
130 dat = data[i] - zero_level
131 next_dat = data[i+1] - zero_level
132 if ( self.slope > 0 ):
133 if ( dat > 0 ):
134 continue
135 else:
136 if ( dat < 0):
137 continue
138 if ( dat * next_dat <= 0 ):
139 # interpolate time of zero crossing with
140 # linear polynomial: y = ax + b
141 a = (next_dat - dat) / ((i+1) - i)
142 time = -1.0/a * dat + i
143 all_hits.append(time)
144
145 return all_hits
146
147 def __str__(self):
148 s = self.name + '\n'
149 if (self.slope == 1):
150 s += 'search for rising edge crossing.\n'
151 else:
152 s += 'search for falling edge crossing.\n'
153 return s
154
155
156
157def _test_GlobalMaxFinder():
158 gmf = GlobalMaxFinder(30,250)
159 print gmf
160 amplitude, time = gmf(event)
161 if abs(amplitude.mean() - 10) < 0.5:
162 print "Test 1: OK GlobalMaxFinder found amplitude correctly", amplitude.mean()
163 if abs(time.mean() - 65) < 2:
164 print "Test 1: OK GlobalMaxFinder found time correctly", time.mean()
165 else:
166 print "BAD: time mean:", time.mean()
167
168def _test_FixedWindowIntegrator():
169 fwi = FixedWindowIntegrator(50,200)
170 print fwi
171 integral = fwi(event)
172 #value of integral should be: 150*bsl + 8*10/2 + 100*10/2 = 465
173 if abs( integral.mean() - 465) < 2:
174 print "Test 2: OK FixedWindowIntegrator found integral correctly", integral.mean()
175 else:
176 print "Test 2: X FixedWindowIntegrator integral.mean failed:", integral.mean()
177
178def _test_ZeroXing():
179 cfd = CFD()
180 sa = SlidingAverage(8)
181 print sa
182 cfd_out = sa(event)
183 cfd_out = cfd(cfd_out )
184 cfd_out = sa(cfd_out)
185 zx = ZeroXing()
186 print zx
187 list_of_list_of_times = zx(cfd_out)
188 times = []
189 for list_of_times in list_of_list_of_times:
190 times.extend(list_of_times)
191 times = np.array(times)
192
193 hist,bins = np.histogram(times,3000,(0,300))
194 most_probable_time = np.argmax(hist)
195 print 'most probable time of zero-crossing', most_probable_time/10.
196 print 'this includes filter delays ... for average filter setting 8 this turns out to be 78.8 most of the time'
197
198if __name__ == '__main__':
199 import matplotlib.pyplot as plt
200 """ test the extractors """
201
202 # Generate a fake event, with a triangular pulse at slice 65
203 sg = SignalGenerator()
204 pulse_str = 'len 300 bsl -0.5 noise 0.5 triangle 65 10 8 100'
205 pulse = sg(pulse_str)
206 event = []
207 for i in range(1440):
208 event.append(sg(pulse_str))
209 event = np.array(event)
210 print 'test event with 1000 pixel generated, like this:'
211 print pulse_str
212 print
213
214 print '_test_GlobalMaxFinder()'
215 _test_GlobalMaxFinder()
216 print
217 print
218 print '_test_FixedWindowIntegrator()'
219 _test_FixedWindowIntegrator()
220 print
221 print
222 print '_test_ZeroXing()'
223 _test_ZeroXing()
224 print
Note: See TracBrowser for help on using the repository browser.