source: fact/tools/pyscripts/sandbox/dneise/gain/gain.py@ 13633

Last change on this file since 13633 was 13447, checked in by neise, 13 years ago
is writing pulse heights to npz
  • Property svn:executable set to *
File size: 4.5 KB
Line 
1#!/usr/bin/python -tt
2#
3#
4
5from pyfact import RawData
6from drs_spikes import DRSSpikes
7from fir_filter import CFD
8from fir_filter import SlidingAverage
9
10import sys
11import numpy as np
12
13data_filename = 'data/20111017_010.fits.gz'
14calib_filename = 'data/20111017_006.drs.fits.gz'
15
16run = RawData(data_filename, calib_filename, return_dict = True, do_calibration=True)
17despike = DRSSpikes()
18smooth = SlidingAverage(7)
19cfd = CFD()
20
21thr = 3
22filter_delay = 3
23search_window_size = 12
24# shortcut
25sws = 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
43PEAKS_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.
52result_peak_positions = np.ones( (run.nevents, run.npix, PEAKS_MAX), dtype=np.int16) * -1
53result_peak_unfiltered_height = np.ones( (run.nevents, run.npix, PEAKS_MAX), dtype=np.float32) * -np.inf
54result_peak_smoothed_height = np.ones( (run.nevents, run.npix, PEAKS_MAX), dtype=np.float32) * -np.inf
55
56
57for 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
133np.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)
Note: See TracBrowser for help on using the repository browser.