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

Last change on this file since 13444 was 13444, checked in by neise, 13 years ago
...evolving...
  • Property svn:executable set to *
File size: 4.1 KB
Line 
1#!/usr/bin/python -tti
2#
3#
4
5from pyfact import RawData
6from drs_spikes import DRSSpikes
7from fir_filter import CFD
8from fir_filter import SlidingAverage
9
10from plotters import Plotter
11
12import sys
13import cPickle
14import matplotlib.pyplot as plt
15import numpy as np
16
17data_filename = 'data/20111017_009.fits.gz'
18calib_filename = 'data/20111017_006.drs.fits.gz'
19out_filename = 'test.pkl'
20
21run = RawData(data_filename, calib_filename, return_dict = True, do_calibration=True)
22despike = DRSSpikes()
23smooth = SlidingAverage(7)
24cfd = CFD()
25
26thr = 3
27filter_delay = 3
28search_window_size = 12
29# shortcut
30sws = search_window_size
31
32plt.ion()
33fig = plt.figure()
34fig.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
48PEAKS_MAX = 10
49N_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.
57peaks = np.ones( ( N_INFO, run.nevents, run.npix, PEAKS_MAX), dtype=np.float32)
58
59
60
61for 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)
Note: See TracBrowser for help on using the repository browser.