-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadIn.py
executable file
·350 lines (309 loc) · 11.7 KB
/
readIn.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
347
348
349
350
#!/usr/bin/env python
# encoding: utf-8
"""
readIn.py
Created by Sam Cook on 2011-02-01.
Copyright (c) 2011 __MyCompanyName__. All rights reserved.
"""
import sys
import os
from camac_control import *
from time import sleep
from socket import *
from fileUpdater import *
from time import localtime, strftime
verbose = False
adc = {"n":21, "a":(8, 9, 10, 11)}
# clk = {"n":20, "a":(0,)}
scalor = {"n":20, "a":(1,)}
int_reg = {"n":22, "a":(0,)}
out_reg = {"n":23, "a":(0,), "subaddr":(1,)}
set_reg = {"n":23, "a":(0,), "subaddr":(2,)}
gate_width = 1
def camac_reader(out_file, maxEvents = 5000):
percent = int(round(maxEvents*0.1))
sock = sock_init()
n_error = 0
n_timeouts = 0
try:
camac_init(sock)
if verbose: print("CAMAC initialised")
camac(sock, f="enable", **int_reg) #enable the LAM
if verbose: print("int reg LAM enabled")
reset_trigger(sock, **out_reg)
if verbose: print("trigger reset")
print 'starting run'
for i in range(maxEvents):
if i%percent == 0: print i
try:
if verbose: print 'waiting for LAM'
waitForLAM(sock, maxpolls=5000, **int_reg)
if verbose: print 'LAM found'
camac(sock, f="disable", **int_reg)
if verbose: print 'disabled int reg'
data = read_mod(sock, **adc)
if verbose: print 'ADC read'
out_file.write(str(data))
if verbose: print 'written file'
except timeout: # caused by LAM
if verbose: print '>>>> timeout error'
n_timeouts += 1
continue
except CAMACError, e: # status & qresp errors
n_error += 1
print e
# reset everything
try:
sleep(0.001) # allow the crate to stabise then restart it
clear_mod(sock, **adc)
clear_mod(sock, **int_reg)
reset_trigger(sock, **out_reg)
if verbose: print 'adc and int reg cleared, trigger reset'
except CAMACError, e:
camac_init(sock)
print "Crate re-initialised"
n_error += 1
print e
finally:
# make sure that the veto hasn't jammed
reset_trigger(sock, **out_reg)
if verbose: print 'Reset trigger'
camac(sock, f="enable", **int_reg)
if verbose: print 'Enabled LAM'
print "Finished with %i errors and %i timeouts" % (n_error, n_timeouts)
finally:
print "errors:", n_error, "timeouts:", n_timeouts
sock.close()
def pedestal(out_file, maxEvents = 5000):
percent = int(round(maxEvents*0.1))
print("initialising the CAMAC crate")
sock = sock_init()
try:
camac_init(sock)
n_error = 0
n_timeouts = 0
camac(sock, f="enable", **int_reg) #enable the LAM
reset_trigger(sock, **out_reg)
print("trigger reset")
for i in range(maxEvents):
if i%percent == 0: print i
try:
set_out_reg(sock, **set_reg)
waitForLAM(sock, **int_reg)
data = read_mod(sock, **adc)
out_file.write(str(data))
except timeout:
n_timeouts += 1
print "LAM timeout"
continue
except CAMACError, e:
n_error += 1
print e.message
finally:
# make sure that the veto hasn't jammed
reset_trigger(sock, **out_reg)
print "done with %i errors & %i timeouts" % (n_error, n_timeouts)
finally:
sock.close()
print "REMEMBER: change back ADC gate line and gg set line"
def hit_rate(out_file, maxTries = 60):
print("initialising the CAMAC crate")
sock = sock_init()
try:
camac_init(sock)
n_error = 0
n_timeout = 0
sum = 0
camac(sock, f="enable", **int_reg) #enable the LAM
reset_trigger(sock, **out_reg)
print("trigger reset")
for i in range(maxTries):
try:
clear_mod(sock, **scalor)
set_out_reg(sock, **set_reg)
sleep(1)
# waitForLAM(sock, maxpolls=1000, **scalor)
data = read_mod(sock, **scalor)
sum += int(data)
# print "rate = %iHz" % (data)
out_file.write(str(data))
except timeout:
n_timeout += 1
print "LAM timeout"
continue
except CAMACError, e:
n_error += 1
print e.message
finally:
# make sure that the veto hasn't jammed
reset_trigger(sock, **out_reg)
print "average rate: %.3fHz" % (sum/maxTries)
print "done with %i errors %i timeouts" % (n_error, n_timeout)
finally:
sock.close()
print "REMEMBER: change back ADC gate line and gg set line"
def camac(sock, n, f, a=0, subaddr=None, data=None, single=False):
"""
Sends a camac command
if single is true the function is sent to addr 0 only
either data _or_ subaddr will be used, if both given
subaddr will be ignored
it is assumed that a, subaddr, and data will be indexable
even in single mode
"""
data = data if (data != None) else (subaddr if (subaddr != None) else (None, ))
if (single):
return cssa(sock, n=n, a=0, f=f, data=data[0])
else:
for i in range(len(a)):
return cssa(sock, n=n, a=a[i], f=f, data=data[i])
def camac_init(sock):
# rsync -> cccc -> cccz -> ccci
rsync(sock)
cccc(sock)
cccz(sock)
ccci(sock)
def read_mod(sock, n, a, grp1=True, str_fmt="%4i "):
"""
reads the CAMAC module at position, n,
subaddresses 0 to (sub_addr_max - 1)
assumes reading grp1, if false reads grp2
"""
res = ''
func = "readGrp1" if grp1 else "readGrp2"
func2 = "clearGrp1" if grp1 else "clearGrp2"
blanks = 0
for addr in a:
status = camac(sock, n, func, (addr,))
if status["qresp"][1] != 0x8000:
# try reading the module again
status = camac(sock, n, func, (addr,))
if status["qresp"][1] != 0x8000:
raise CAMACError("problem reading %i" % status["qresp"][1])
if (status["data"][1] == 1) or (status["data"][1] == 0): blanks += 1
res += str( status["data"][1] ) + ' '
camac(sock, n, func2, single=True)
return res
def waitForLAM(sock, n, a, maxpolls=1000, subaddr=None):
"""
polls the module for LAM, returns if there is a LAM
otherwise throws a timeout error"""
status = camac(sock, n, f="clearLAM", single=True)
status_check(status, 'wait for Lam clearLAM')
for i in range(maxpolls):
status = camac(sock, n, f="testLAM", a=a)
try:
status_check(status, 'wait for LAM: testLAM')
except CAMACError:
sleep(0.001) # sleep for 1us
continue
camac(sock, n, f="clearGrp1", single=True)
return
else:
raise timeout("timed out waiting for LAM")
def clear_mod(sock, n, a, grp1=True):
"""
clears LAM and any data saved in the module"""
func = "clearGrp1" if grp1 else "clearGrp2"
# while clear group clears all addrs it still needs one...
status = camac(sock, n=n, a=(0,), f=func)
# status_check(status, 'clear mod: clear grp')
status = camac(sock, n=n, a=(0,), f="clearLAM")
# status_check(status, 'clear mod: clear LAM')
def reset_trigger(sock, n, subaddr, grp1=True, a=0):
"""
Sets the register, in slot n, at addr to
value, subaddr, then sets it to 0. Unless grp1 is unset
this assumes that addr exists in group1
"""
func = "overWriteGrp1" if grp1 else "overWriteGrp2"
status = camac(sock, n=n, f=func, a=a, data=subaddr)
status_check(status, 'trigger reset: write 1')
# now clear the address
status = camac(sock, n=n, f=func, a=a, data=(0,))
status_check(status, 'trigger reset: write 2')
def sock_init(host = '192.168.0.2', sendPort = 240, recvPort = 59329):
"""
Initialises the socket for communication to the crate
"""
sendAddr = (host, sendPort)
recvAddr = ('', recvPort)
sock = socket(AF_INET, SOCK_DGRAM)
sock.bind(recvAddr)
sock.connect(sendAddr)
return sock
def set_out_reg(sock, n, subaddr, grp1=True, a=0):
func = "overWriteGrp1" if grp1 else "overWriteGrp2"
status = camac(sock, n=n, a=a, f=func, data=subaddr)
status_check(status, 'set out reg, write 1')
status = camac(sock, n=n, a=a, f=func, data=(0,))
status_check(status, 'set out reg, write 2')
def stall(record=False):
"""
Waits for user input to continue. Will exit the program on 'Q'
if record is set this will return any integer entered by the user
if a non-digit character is entered it will return None"""
run = True
while run:
d = raw_input()
if (d == 'Q') or (d == 'q'):
exit()
elif d and record:
for i in d:
if not i.isdigit(): return None
return int(d)
elif d:
return
else:
continue
if __name__ == '__main__':
n_files = 1
timefmt = "%d-%m-%y %H:%M:%S"
maxEvents = 100000
for i in range (n_files):
print '='*40
print "Please connect the GREY veto line then press any key and 'enter'"
print "Please measure the beam intensity"
print '='*40
stall()
th1_hit = ('hist', 100, 2.0, 4000)
hit_file = 'data/hit_rates_%03i.txt'
head = strftime(timefmt, localtime())
f_hit = FileUpdater(hit_file, n_dat=1, header = head, TH1args=th1_hit, update_on=1)
try:
hit_rate(f_hit, maxTries=40)
finally:
foot = strftime(timefmt, localtime())
f_hit.closeFooter(foot)
print '='*40
print "Please connect the GREEN veto line then press any key and 'enter'"
print 'max entries set to %i is this correct?' % maxEvents
print 'if not please enter how many events to take'
print "press 'Q' to quit"
print '\n\n\n', '='*40
d = stall(True)
maxEvents = d if d else maxEvents
th1_dat = ('hist', 100, 2.0, 2**12)
data_file = 'data/test_%03i.txt' #pedestal_%03i.txt'
head = strftime(timefmt, localtime())
f_adc = FileUpdater(data_file, n_dat=4, header = head, TH1args=th1_dat, update_on=200)
try:
camac_reader(f_adc, maxEvents=500)
# pedestal(f_adc, maxEvents=1000)
finally:
foot = strftime(timefmt, localtime())
f_adc.closeFooter(foot)
print '='*40
print "Please connect the GREY veto line then press any key and 'enter'"
print "Please measure the beam intensity"
print '='*40
stall()
hit_file = 'data/hit_rates_%03i.txt'
head = strftime(timefmt, localtime())
f_hit2 = FileUpdater(hit_file, n_dat=1, header = head, TH1args=th1_hit, update_on=1)
try:
hit_rate(f_hit2, maxTries=20)
finally:
foot = strftime(timefmt, localtime())
f_hit2.closeFooter(foot)
# End of for loop