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

Last change on this file since 13336 was 13333, checked in by neise, 13 years ago
supports automodule
File size: 6.1 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):
107 all_hits = []
108 for pix_data in data:
109 hits = []
110 for i in range( data.shape[1]-1 ):
111 if ( self.slope > 0 ):
112 if ( pix_data[i] > 0 ):
113 continue
114 else:
115 if ( pix_data[i] < 0):
116 continue
117 if ( pix_data[i] * pix_data[i+1] <= 0 ):
118 # interpolate time of zero crossing with
119 # linear polynomial: y = ax + b
120 a = (pix_data[i+1] - pix_data[i]) / ((i+1) - i)
121 time = -1.0/a * pix_data[i] + i
122 hits.append(time)
123 all_hits.append(hits)
124 return all_hits
125
126 def __str__(self):
127 s = self.name + '\n'
128 if (self.slope == 1):
129 s += 'search for rising edge crossing.\n'
130 else:
131 s += 'search for falling edge crossing.\n'
132 return s
133
134
135
136def _test_GlobalMaxFinder():
137 gmf = GlobalMaxFinder(30,250)
138 print gmf
139 amplitude, time = gmf(event)
140 if abs(amplitude.mean() - 10) < 0.5:
141 print "Test 1: OK GlobalMaxFinder found amplitude correctly", amplitude.mean()
142 if abs(time.mean() - 65) < 2:
143 print "Test 1: OK GlobalMaxFinder found time correctly", time.mean()
144 else:
145 print "BAD: time mean:", time.mean()
146
147def _test_FixedWindowIntegrator():
148 fwi = FixedWindowIntegrator(50,200)
149 print fwi
150 integral = fwi(event)
151 #value of integral should be: 150*bsl + 8*10/2 + 100*10/2 = 465
152 if abs( integral.mean() - 465) < 2:
153 print "Test 2: OK FixedWindowIntegrator found integral correctly", integral.mean()
154 else:
155 print "Test 2: X FixedWindowIntegrator integral.mean failed:", integral.mean()
156
157def _test_ZeroXing():
158 cfd = CFD()
159 sa = SlidingAverage(8)
160 print sa
161 cfd_out = sa(event)
162 cfd_out = cfd(cfd_out )
163 cfd_out = sa(cfd_out)
164 zx = ZeroXing()
165 print zx
166 list_of_list_of_times = zx(cfd_out)
167 times = []
168 for list_of_times in list_of_list_of_times:
169 times.extend(list_of_times)
170 times = np.array(times)
171
172 hist,bins = np.histogram(times,3000,(0,300))
173 most_probable_time = np.argmax(hist)
174 print 'most probable time of zero-crossing', most_probable_time/10.
175 print 'this includes filter delays ... for average filter setting 8 this turns out to be 78.8 most of the time'
176
177if __name__ == '__main__':
178 import matplotlib.pyplot as plt
179 """ test the extractors """
180
181 # Generate a fake event, with a triangular pulse at slice 65
182 sg = SignalGenerator()
183 pulse_str = 'len 300 bsl -0.5 noise 0.5 triangle 65 10 8 100'
184 pulse = sg(pulse_str)
185 event = []
186 for i in range(1440):
187 event.append(sg(pulse_str))
188 event = np.array(event)
189 print 'test event with 1000 pixel generated, like this:'
190 print pulse_str
191 print
192
193 print '_test_GlobalMaxFinder()'
194 _test_GlobalMaxFinder()
195 print
196 print
197 print '_test_FixedWindowIntegrator()'
198 _test_FixedWindowIntegrator()
199 print
200 print
201 print '_test_ZeroXing()'
202 _test_ZeroXing()
203 print
Note: See TracBrowser for help on using the repository browser.