-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsweep.py
346 lines (309 loc) · 11.8 KB
/
sweep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python
#########################
# sweep.py
# Generic module for running
# IPW sweeps with Tek scope
#
#########################
import os
from core import serial_command
from common import comms_flags
import math
import time
#try:
import utils
#except:
# pass
import sys
import readPklWaveFile
import calc_utils as calc
import numpy as np
port_name = "/dev/tty.usbserial-FTE3C0PG"
#port_name = "/dev/tty.usbserial-FTGA2OCZ"
## TODO: better way of getting the scope type
scope_name = "Tektronix3000"
_boundary = [0,1.5e-3,3e-3,7e-3,15e-3,30e-3,70e-3,150e-3,300e-3,700e-3,1000]
_v_div = [1e-3,2e-3,5e-3,10e-3,20e-3,50e-3,100e-3,200e-3,500e-3,1.0]
_v_div_1 = [1e-3,2e-3,5e-3,10e-3,20e-3,50e-3,100e-3,200e-3,500e-3,1.0,2.0]
#_v_div = [20e-3, 50e-3, 100e-3, 200e-3, 500e-3, 1] # For scope at sussex
#_v_div_1 = [20e-3, 50e-3, 100e-3, 200e-3, 500e-3, 1, 2]
sc = None
sc = serial_command.SerialCommand(port_name)
#initialise sc here, faster options setting
def start():
global sc
sc = serial_command.SerialCommand(port_name)
def set_port(port):
global port_name
port_name = port
def set_scope(scope):
global scope_name
if scope=="Tektronix3000" or scope=="LeCroy":
scope_name = scope
else:
raise Exception("Unknown scope")
def check_dir(dname):
"""Check if directory exists, create it if it doesn't"""
direc = os.path.dirname(dname)
try:
os.stat(direc)
except:
os.mkdir(direc)
print "Made directory %s...." % dname
return dname
def return_zero_result():
r = {}
r['pin'] = 0
r['width'], r['width error'] = 0, 0
r['rise'], r['rise error'] = 0, 0
r['fall'], r['fall error'] = 0, 0
r['area'], r['area error'] = 0, 0
r['peak'], r['peak error'] = 0, 0
return r
def save_scopeTraces(fileName, scope, channel, noPulses):
"""Save a number of scope traces to file - uses compressed .pkl"""
scope._get_preamble(channel)
results = utils.PickleFile(fileName, 1)
results.add_meta_data("timeform_1", scope.get_timeform(channel))
#ct = scope.acquire_time_check()
#if ct == False:
# print 'No triggers for this data point. Will skip and set data to 0.'
# results.save()
# results.close()
# return False
t_start, loopStart = time.time(),time.time()
for i in range(noPulses):
try:
ct = scope.acquire_time_check(timeout=.4)
results.add_data(scope.get_waveform(channel), 1)
except Exception, e:
print "Scope died, acquisition lost."
print e
if i % 100 == 0 and i > 0:
print "%d traces collected - This loop took : %1.1f s" % (i, time.time()-loopStart)
loopStart = time.time()
print "%d traces collected TOTAL - took : %1.1f s" % (i, (time.time()-t_start))
results.save()
results.close()
return True
def save_scopeTraces_Multiple(fileNames, scope, channels, noPulses):
"""Save a number of scope traces to file - uses compressed .pkl"""
results = []
for i in range(len(fileNames)):
scope._get_preamble(channels[i])
results.append(utils.PickleFile(fileNames[i], 1))
results[i].add_meta_data("timeform_1", scope.get_timeform(channels[i]))
#ct = scope.acquire_time_check()
#if ct == False:
# print 'No triggers for this data point. Will skip and set data to 0.'
# results.save()
# results.close()
# return False
t_start, loopStart = time.time(),time.time()
for i in range(noPulses):
try:
ct = scope.acquire_time_check(timeout=.4)
for j in range(len(results)):
results[j].add_data(scope.get_waveform(channels[j]), 1)
except Exception, e:
print "Scope died, acquisition lost."
print e
if i % 100 == 0 and i > 0:
print "%d traces collected - This loop took : %1.1f s" % (i, time.time()-loopStart)
loopStart = time.time()
print "%d traces collected TOTAL - took : %1.1f s" % (i, (time.time()-t_start))
for i in range(len(results)):
results[i].save()
return True
def find_and_set_scope_y_scale(channel,height,width,delay,scope,scaleGuess=None):
"""Finds best y_scaling for current pulses
"""
func_time = time.time()
sc.fire_continuous()
time.sleep(0.1)
# If no scale guess, try to find reasonable trigger crossings at each y_scale
ct = False
if scaleGuess==None:
for i, val in enumerate(_v_div):
scope.set_channel_y(channel,_v_div[-1*(i+1)], pos=3) # set scale, starting with largest
scope.set_edge_trigger( (-1*_v_div[-1*(i+1)]), channel, falling=True)
if i==0:
time.sleep(1) # Need to wait to clear previous triggered state
ct = scope.acquire_time_check(timeout=.5) # Wait for triggered acquisition
if ct == True:
break
else: #Else use the guess
if abs(scaleGuess) > 1:
guess_v_div = _v_div
else:
tmp_idx = np.where( np.array(_v_div) >= abs(scaleGuess) )[0][0]
guess_v_div = _v_div[0:tmp_idx]
for i, val in enumerate(guess_v_div):
scope.set_channel_y(channel,guess_v_div[-1*(i+1)],pos=3) # set scale, starting with largest
scope.set_edge_trigger( (-1*guess_v_div[-1*(i+1)]), channel, falling=True)
if i==0:
time.sleep(0.2) # Need to wait to clear previous triggered state
ct = scope.acquire_time_check() # Wait for triggered acquisition
if ct == True:
break
time.sleep(0.5) # Need to wait for scope to recognise new settings
scope._get_preamble(channel)
# Calc min value
mini, wave = np.zeros( 10 ), None
for i in range( len(mini) ):
# Check we get a trigger - even at the lowest setting we might see nothing
ct = scope.acquire_time_check(timeout=.4)
if ct == False:
print 'Triggers missed for this data point. Will skip and set data to 0.'
return False
wave = scope.get_waveform(channel)
mini[i] = min(wave) - np.mean(wave[0:10])
min_volt = np.mean(mini)
print "MINIMUM MEASUREMENT:", min_volt
if np.abs(min_volt) < 0.006:
return False
# Set scope
if -1*(min_volt/6) > _v_div_1[-1]:
scale = _v_div_1[-1]
else:
scale_idx = np.where( np.array(_v_div_1) >= -1*(min_volt/6) )[0][0]
scale = _v_div_1[scale_idx]
# Because of baseline noise!
if scale == 2e-3:
trig = -7.5e-3
#trig = -4e-3
elif scale == 1e-3:
trig = -7.5e-3
#trig = -3e-3
elif scale == 5e-3:
trig = -7.5e-3
else:
trig = -1.*scale
print "Preticted scale = %1.3fV, actual scale = %1.3fV, trigger @ %1.4fV" % (-1*(min_volt/6.6) , scale, trig)
scope.set_channel_y( channel, scale, pos=3) # set scale, starting with largest
scope.set_edge_trigger( trig, channel, falling=True)
print "TOTAL FUNC TIME = %1.2f s" % (time.time() - func_time)
sc.stop()
return True
def sweep(dir_out,box,channel,width,delay,scope,min_volt=None):
"""Perform a measurement using a default number of
pulses, with user defined width, channel and rate settings.
"""
print '____________________________'
print width
#fixed options
height = 16383
fibre_delay = 0
trigger_delay = 0
pulse_number = 100
#first select the correct channel and provide settings
logical_channel = (box-1)*8 + channel
sc.select_channel(logical_channel)
sc.set_pulse_width(width)
sc.set_pulse_height(16383)
sc.set_pulse_number(pulse_number)
sc.set_pulse_delay(delay)
sc.set_fibre_delay(fibre_delay)
sc.set_trigger_delay(trigger_delay)
# first, run a single acquisition with a forced trigger, effectively to clear the waveform
scope._connection.send("trigger:state ready")
time.sleep(0.1)
scope._connection.send("trigger force")
time.sleep(0.1)
# Get pin read
time.sleep(0.1)
sc.fire_sequence() # previously fire_sequence!
#wait for the sequence to end
tsleep = pulse_number * (delay*1e-3 + 210e-6)
time.sleep(tsleep) #add the offset in
pin = None
# while not comms_flags.valid_pin(pin,channel):
while pin==None:
pin,rms, _ = sc.tmp_read_rms()
print "PIN (sweep):",pin[logical_channel], rms[logical_channel]
sc.stop()
# File system stuff
check_dir("%s/raw_data/" % (dir_out))
directory = check_dir("%s/raw_data/Channel_%02d/" % (dir_out,logical_channel))
fname = "%sWidth%05d" % (directory,width)
# Check scope
ck = find_and_set_scope_y_scale(1,height,width,delay,scope,scaleGuess=min_volt)
if ck == True:
print "Saving raw files to: %s..." % fname
sc.fire_continuous()
time.sleep(0.2)
save_ck = save_scopeTraces(fname, scope, 1, 100)
sc.stop()
if save_ck == True:
# Calc and return params
x,y = calc.readPickleChannel(fname, 1)
results = calc.dictionary_of_params(x,y)
results["pin"] = pin[logical_channel]
results["pin error"] = rms[logical_channel]
calc.printParamsDict(results, width)
calc.plot_eg_pulses(x,y,10, fname='%s/LastMeasuredPulses.png' % dir_out.split("/")[0])
#os.system("open %s/LastMeasuredPulses.png" % dir_out.split("/")[0])
elif save_ck == False:
results = return_zero_result()
results['pin'] = pin[logical_channel]
else:
results = return_zero_result()
results['pin'] = pin[logical_channel]
results["pin error"] = rms[logical_channel]
sc.stop()
return results
def sweep_timing(dir_out,box,channel,width,delay,scope,min_volt=None):
"""Perform a measurement using a default number of
pulses, with user defined width, channel and rate settings.
"""
print '____________________________'
print width
#fixed options
height = 16383
fibre_delay = 0
trigger_delay = 0
pulse_number = 11100
#first select the correct channel and provide settings
logical_channel = (box-1)*8 + channel
sc.select_channel(logical_channel)
sc.set_pulse_width(width)
sc.set_pulse_height(16383)
sc.set_pulse_number(pulse_number)
sc.set_pulse_delay(delay)
sc.set_fibre_delay(fibre_delay)
sc.set_trigger_delay(trigger_delay)
# first, run a single acquisition with a forced trigger, effectively to clear the waveform
scope._connection.send("trigger:state ready")
time.sleep(0.1)
scope._connection.send("trigger force")
time.sleep(0.1)
# Get pin read
time.sleep(0.1)
sc.fire_sequence() # previously fire_sequence!
#wait for the sequence to end
tsleep = pulse_number * (delay*1e-3 + 210e-6)
time.sleep(tsleep) #add the offset in
sc.stop()
# File system stuff
check_dir("%s/raw_data/" % (dir_out))
directory = check_dir("%s/raw_data/Channel_%02d/" % (dir_out,logical_channel))
channels = [1,4]
fnames = []
fnames.append("%sPMTPulseWidth%05d" % (directory,width))
fnames.append("%sTriggerPulseWidth%05d" % (directory,width))
# Check scope
ck = find_and_set_scope_y_scale(1,height,width,delay,scope,scaleGuess=min_volt)
scope.set_edge_trigger(1.4, 4 , falling=False) # Rising edge trigger
if ck == True:
print "Saving raw files to: %s and %s" % (fnames[0],fnames[1])
sc.fire_continuous()
time.sleep(0.2)
save_ck = save_scopeTraces_Multiple(fnames, scope, channels, 100)
sc.stop()
time.sleep(5)
if save_ck == True:
# Calc and return params
xPMT,yPMT = calc.readPickleChannel(fnames[0], 1)
xTrigger,yTrigger = calc.readPickleChannel(fnames[1], 1)
meanJitter, JitterDev, JitterErrorOnMean = calc.calcJitter(xTrigger,yTrigger,xPMT,yPMT)
return meanJitter, JitterErrorOnMean