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

Last change on this file since 16858 was 13450, checked in by neise, 13 years ago
ready for testing
  • Property svn:executable set to *
File size: 5.4 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 os.path
11import sys
12import numpy as np
13try:
14 import h5py
15except:
16 print 'module h5py needed for this process ... aborting'
17 sys.exit(1)
18
19data_filename = 'data/20111017_010.fits.gz'
20calib_filename = 'data/20111017_006.drs.fits.gz'
21
22run = RawData(data_filename, calib_filename, return_dict = True, do_calibration=True)
23despike = DRSSpikes()
24smooth = SlidingAverage(7)
25cfd = CFD()
26
27thr = 3
28filter_delay = 3
29search_window_size = 12
30# shortcut
31sws = search_window_size
32
33
34# we try to determine the:
35# * Position
36# * and Height (in two versions: filtered, and unfiltered data)
37# of dark count peaks in:
38# a) all events
39# b) all pixel
40#
41# we limit ourselfs to 10 peaks per pixel max!
42# and 1000 events max!
43#
44# So in order to store this stuff, we prepare an n-dim-array
45
46PEAKS_MAX = 30
47
48# In order not to allocate so much mem, we use hdf5 files for storing this information
49f = h5py.File('20111017_010-006.h5', 'w')
50
51# we write some meta data into the file, so we know afterwards, where this stuff
52# came from
53f.attrs['data_file'] = os.path.realpath(data_filename)
54f.attrs['calib_file'] = os.path.realpath(calib_filename)
55# this script
56f.attrs['source_python_script'] = open(sys.argv[0], 'r').read()
57# SVN revision, should go here ... but of what? this file??
58f.attrs['settings'] = [
59 'threshold = 3',
60 'SlidingAverage = 7 --> Filter delay = 3'
61 'Search Window Size = 12'
62 'Maximum Number of Peaks, in data sets = 30'
63 'Default value of peak_positions = -1'
64 'Default value of peak_height_unfiltered = -np.inf'
65 'Default value of peak_height_smoothed = -np.inf'
66]
67
68result_peak_positions = f.create_dataset('peak_positions',
69 (run.nevents, run.npix, PEAKS_MAX),
70 dtype=np.int16,
71 fillvalue=-1,
72 compression='gzip')
73result_peak_unfiltered_height = f.create_dataset('peak_height_unfiltered',
74 (run.nevents, run.npix, PEAKS_MAX),
75 dtype=np.float32,
76 fillvalue=-np.inf,
77 compression='gzip')
78result_peak_smoothed_height = f.create_dataset('peak_height_smoothed',
79 (run.nevents, run.npix, PEAKS_MAX),
80 dtype=np.float32,
81 fillvalue=-np.inf,
82 compression='gzip')
83
84result_baseline = f.create_dataset('baseline',
85 (run.nevents, run.npix),
86 dtype=np.float32,
87
88
89
90try:
91
92 for event in run:
93 event_id = event['event_id'].value - 1
94
95 data = event['acal_data']
96 data = despike(data)
97 data_orig = data.copy()
98 data = smooth(data)
99 filtered = cfd(data)
100 filtered = smooth(filtered)
101
102 # this is a loop over all pixel of this event
103 pixel_id = -1
104 for dat, fil, orig in zip(data, filtered, data_orig):
105 pixel_id += 1
106 print event_id, pixel_id
107
108 prod = fil[:-1] * fil[1:]
109 cand = np.where( prod <= 0)[0]
110 if len(cand) == 0:
111 continue
112 # zero crossing with rising edge
113 cross = cand[np.where(fil[cand] < 0)[0]]
114 if len(cross) == 0:
115 continue
116 over_thr = cross[np.where(dat[cross-4] > thr)[0]]
117
118 # Now since we have these values, we will throw away all those,
119 # which are probably on a falling edge of its predecessor
120 dover = np.diff(over_thr)
121 if len(dover) == 0:
122 good = over_thr
123 else:
124 good = []
125 good.append(over_thr[0])
126 for i in range(len(dover)):
127 if dover[-i-1] > 100:
128 good.append(over_thr[-i-1])
129
130 good = np.array(sorted(good))
131 # these positions, we just found, do not exactly point to the maximum
132 # of a peak, but the peak will be on the left side of it.
133 # we use the smoothed data to find the position of the local maximum
134 # and then stopre this position and the value of both
135 # the smoothed data and the original data.
136
137 max_pos = np.zeros( good.shape )
138 max_smoothed = np.zeros( good.shape )
139 max_orig = np.zeros( good.shape )
140
141 for i in range(len(good)):
142 # We search for a local maximum in a window of size 12
143 if len(dat[good[i]-sws:good[i]]) > 0:
144
145 max_pos[i] = good[i]-sws + np.argmax(dat[good[i]-sws:good[i]])
146 max_smoothed[i] = dat[max_pos[i]]
147 max_orig[i] = orig[max_pos[i]-filter_delay]
148 if len(max_pos) > 0:
149 result_peak_positions[event_id,pixel_id, :len(max_pos)] = max_pos
150 result_peak_unfiltered_height[event_id,pixel_id, :len(max_pos)] =max_orig
151 result_peak_smoothed_height[event_id,pixel_id, :len(max_pos)] = max_smoothed
152
153
154 f.close()
155
156except:
157 print 'An Exception occured .. closing file'
158 f.close()
159 raise
160
161
162
Note: See TracBrowser for help on using the repository browser.