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