1 | #!/usr/bin/python -tti
|
---|
2 | #
|
---|
3 | #
|
---|
4 |
|
---|
5 | from pyfact import RawData
|
---|
6 | from drs_spikes import DRSSpikes
|
---|
7 | from fir_filter import CFD
|
---|
8 | from fir_filter import SlidingAverage
|
---|
9 |
|
---|
10 | from plotters import Plotter
|
---|
11 |
|
---|
12 | import sys
|
---|
13 | import cPickle
|
---|
14 | import matplotlib.pyplot as plt
|
---|
15 | import numpy as np
|
---|
16 |
|
---|
17 | data_filename = 'data/20111017_009.fits.gz'
|
---|
18 | calib_filename = 'data/20111017_006.drs.fits.gz'
|
---|
19 | out_filename = 'test.pkl'
|
---|
20 |
|
---|
21 | run = RawData(data_filename, calib_filename, return_dict = True, do_calibration=True)
|
---|
22 | despike = DRSSpikes()
|
---|
23 | smooth = SlidingAverage(7)
|
---|
24 | cfd = CFD()
|
---|
25 |
|
---|
26 | thr = 3
|
---|
27 | filter_delay = 3
|
---|
28 | search_window_size = 12
|
---|
29 | # shortcut
|
---|
30 | sws = search_window_size
|
---|
31 |
|
---|
32 | plt.ion()
|
---|
33 | fig = plt.figure()
|
---|
34 | fig.hold(True)
|
---|
35 |
|
---|
36 | # we try to determine the:
|
---|
37 | # * Position
|
---|
38 | # * and Height (in two versions: filtered, and unfiltered data)
|
---|
39 | # of dark count peaks in:
|
---|
40 | # a) all events
|
---|
41 | # b) all pixel
|
---|
42 | #
|
---|
43 | # we limit ourselfs to 10 peaks per pixel max!
|
---|
44 | # and 1000 events max!
|
---|
45 | #
|
---|
46 | # So in order to store this stuff, we prepare an n-dim-array
|
---|
47 |
|
---|
48 | PEAKS_MAX = 10
|
---|
49 | N_INFO = 3 # Position, Height of unfiltered peak, Height of Filtered peak
|
---|
50 |
|
---|
51 | # TODO: this operation might under certain circumstances need way to much mem.
|
---|
52 | # I would like to solve this, using try except, and in case of a 'MemoryError'
|
---|
53 | # reduce the number of events.
|
---|
54 | # So I will process the events, in e.g. chunks of 1000 and save the results
|
---|
55 | # The user will then find a certain nuber of ndarray in the output npz file.
|
---|
56 | # But this should not be a major problem.
|
---|
57 | peaks = np.ones( ( N_INFO, run.nevents, run.npix, PEAKS_MAX), dtype=np.float32)
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 | for event in run:
|
---|
62 | event_id = event['event_id'].value - 1
|
---|
63 | print 'event_id', event_id
|
---|
64 |
|
---|
65 | data = event['acal_data']
|
---|
66 | data = despike(data)
|
---|
67 | data_orig = data.copy()
|
---|
68 | data = smooth(data)
|
---|
69 | filtered = cfd(data)
|
---|
70 | filtered = smooth(filtered)
|
---|
71 |
|
---|
72 |
|
---|
73 |
|
---|
74 |
|
---|
75 | # this is a loop over all pixel of this event
|
---|
76 | pixel_id = -1
|
---|
77 | for dat, fil, orig in zip(data, filtered, data_orig):
|
---|
78 | pixel_id += 1
|
---|
79 | print 'pixel id:', pixel_id
|
---|
80 | plt.cla()
|
---|
81 | prod = fil[:-1] * fil[1:]
|
---|
82 | cand = np.where( prod <= 0)[0]
|
---|
83 | if len(cand) == 0:
|
---|
84 | continue
|
---|
85 | # zero crossing with rising edge
|
---|
86 | cross = cand[np.where(fil[cand] < 0)[0]]
|
---|
87 | if len(cross) == 0:
|
---|
88 | continue
|
---|
89 | over_thr = cross[np.where(dat[cross-4] > thr)[0]]
|
---|
90 |
|
---|
91 | # Now since we have these values, we will throw away all those,
|
---|
92 | # which are probably on a falling edge of its predecessor
|
---|
93 | dover = np.diff(over_thr)
|
---|
94 | if len(dover) == 0:
|
---|
95 | good = over_thr
|
---|
96 | else:
|
---|
97 | good = []
|
---|
98 | good.append(over_thr[0])
|
---|
99 | for i in range(len(dover)):
|
---|
100 | if dover[-i-1] > 100:
|
---|
101 | good.append(over_thr[-i-1])
|
---|
102 |
|
---|
103 | good = np.array(sorted(good))
|
---|
104 | # these positions, we just found, do not exactly point to the maximum
|
---|
105 | # of a peak, but the peak will be on the left side of it.
|
---|
106 | # we use the smoothed data to find the position of the local maximum
|
---|
107 | # and then stopre this position and the value of both
|
---|
108 | # the smoothed data and the original data.
|
---|
109 |
|
---|
110 | max_pos = np.zeros( good.shape )
|
---|
111 | max_smoothed = np.zeros( good.shape )
|
---|
112 | max_orig = np.zeros( good.shape )
|
---|
113 |
|
---|
114 | for i in range(len(good)):
|
---|
115 | # We search for a local maximum in a window of size 12
|
---|
116 | if len(dat[good[i]-sws:good[i]]) > 0:
|
---|
117 |
|
---|
118 | max_pos[i] = good[i]-sws + np.argmax(dat[good[i]-sws:good[i]])
|
---|
119 | max_smoothed[i] = dat[max_pos[i]]
|
---|
120 | max_orig[i] = orig[max_pos[i]-filter_delay]
|
---|
121 |
|
---|
122 | plt.plot(max_pos, max_smoothed, 'ro')
|
---|
123 | plt.plot(max_pos, max_orig, 'bo')
|
---|
124 | plt.plot(np.arange(len(dat)), dat, 'k:')
|
---|
125 |
|
---|
126 | ret = raw_input('pixel-loop?')
|
---|
127 | if ret == 'q':
|
---|
128 | break
|
---|
129 | ret = raw_input('event-loop?')
|
---|
130 | if ret == 'q':
|
---|
131 | break
|
---|
132 |
|
---|
133 |
|
---|
134 |
|
---|
135 | #output = open(out_filename, 'wb')
|
---|
136 | #cPickle.dump(data_filename, output, -1)
|
---|
137 | #cPickle.dump(calib_filename, output, -1)
|
---|
138 | #cPickle.dump(peak_list, output, -1)
|
---|