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

Last change on this file since 13442 was 13442, checked in by neise, 13 years ago
...evolving...
  • Property svn:executable set to *
File size: 2.9 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
37
38for event in run:
39 print event['event_id'].value
40 data = event['acal_data']
41 data = despike(data)
42 data_orig = data.copy()
43 data = smooth(data)
44 filtered = cfd(data)
45 filtered = smooth(filtered)
46
47 # this is a loop over all pixel of this event
48
49 for dat, fil, orig in zip(data, filtered, data_orig):
50 plt.cla()
51 prod = fil[:-1] * fil[1:]
52 cand = np.where( prod <= 0)[0]
53 if len(cand) == 0:
54 continue
55 # zero crossing with rising edge
56 cross = cand[np.where(fil[cand] < 0)[0]]
57 if len(cross) == 0:
58 continue
59 over_thr = cross[np.where(dat[cross-4] > thr)[0]]
60
61 # Now since we have these values, we will throw away all those,
62 # which are probably on a falling edge of its predecessor
63 dover = np.diff(over_thr)
64 if len(dover) == 0:
65 good = over_thr
66 else:
67 good = []
68 good.append(over_thr[0])
69 for i in range(len(dover)):
70 if dover[-i-1] > 100:
71 good.append(over_thr[-i-1])
72
73 good = np.array(sorted(good))
74 # these positions, we just found, do not exactly point to the maximum
75 # of a peak, but the peak will be on the left side of it.
76 # we use the smoothed data to find the position of the local maximum
77 # and then stopre this position and the value of both
78 # the smoothed data and the original data.
79
80 max_pos = np.zeros( good.shape )
81 max_smoothed = np.zeros( good.shape )
82 max_orig = np.zeros( good.shape )
83
84 for i in range(len(good)):
85 # We search for a local maximum in a window of size 12
86 if len(dat[good[i]-sws:good[i]]) > 0:
87
88 max_pos[i] = good[i]-sws + np.argmax(dat[good[i]-sws:good[i]])
89 max_smoothed[i] = dat[max_pos[i]]
90 max_orig[i] = orig[max_pos[i]-filter_delay]
91
92 plt.plot(max_pos, max_smoothed, 'ro')
93 plt.plot(max_pos, max_orig, 'bo')
94 plt.plot(np.arange(len(dat)), dat, 'k:')
95
96 raw_input('bla')
97
98
99#output = open(out_filename, 'wb')
100#cPickle.dump(data_filename, output, -1)
101#cPickle.dump(calib_filename, output, -1)
102#cPickle.dump(peak_list, output, -1)
Note: See TracBrowser for help on using the repository browser.