Skip to content

Commit f1d7a67

Browse files
author
Sam
committed
Version used for data taking,
Also added some basic py scripts for presenting the data
1 parent 239345a commit f1d7a67

7 files changed

+571
-88
lines changed

camac_control.py

+38-31
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class CAMACError(Exception):
2121
pass
2222

2323

24+
class CAMACreadfail(Exception):
25+
pass
26+
2427
def status(sock):
2528
"""
2629
returns the status as received from the socket, sock.
@@ -52,13 +55,16 @@ def status(sock):
5255

5356
return res
5457

55-
def status_check(data):
58+
def status_check(data, location=""):
5659
"""
5760
Checks the status and qresp for the passed packed"""
58-
if data["status"][1] != 1:
59-
raise CAMACError("Status failure code: %i" % data["status"][1])
60-
elif data["qresp"][1] != 0x8000:
61-
raise CAMACError("Qresp failure %i" % data["qresp"][1])
61+
if (data["status"][1] == 90) and (data["qresp"][1] == 0):
62+
# warning only (no xresp)
63+
return
64+
elif data["status"][1] != 1:
65+
raise CAMACError("Status failure code: %i %s" % (data["status"][1], location))
66+
elif (data["qresp"][1] != 0x8000):
67+
raise CAMACError("Qresp failure %i %s" % (data["qresp"][1], location))
6268

6369
def cccc(sock):
6470
"""
@@ -159,9 +165,9 @@ def rsync(sock, maxTries = 10):
159165
"readClrGrp1": (2, "subaddr"),
160166
"readClrGrp2": (3, "subaddr"),
161167
"testLAM": (8, ),
162-
"clearGrp1": (9, "subaddr"),
168+
"clearGrp1": (9, ),
163169
"clearLAM": (10, ),
164-
"clearGrp2": (11, "subaddr"),
170+
"clearGrp2": (11, ),
165171
"overWriteGrp1": (16, "subaddr", "data"),
166172
"overWriteGrp2": (17, "subaddr", "data"),
167173
"maskOverWriteGrp1": (18, "subaddr", "data"),
@@ -181,13 +187,13 @@ def naf(n, f, a = None, data = None ):
181187
# get the function info required (number and whether an address etc are needed)
182188
funcCode = (f, ) if str(f).isdigit() else camacFunction[f]
183189
if (("subaddr" in funcCode) and (a == None)):
184-
raise Exception("function requires a subaddress")
190+
raise CAMACError("function requires a subaddress")
185191
elif (("data" in funcCode) and (data == None)):
186-
raise Exception("function requires a data")
192+
raise CAMACError("function requires data")
187193

188194
a = a if a else 0
189195
data = pack('H', data) if (data >= 0) else b''
190-
# print funcCode[0], n, a
196+
# print 'n is', n, 'a is', a, 'f is', funcCode[0], '\n'
191197
res = funcCode[0] <<10 | n <<5 | a << 1 | 0
192198
res = pack('H', res)
193199
return res + data
@@ -217,28 +223,29 @@ def main_test(sock):
217223
ccci(sock)
218224
print("\ncompleted i")
219225

220-
cssa(sock, n=22, f=26, a=0)
226+
print cssa(sock, n=22, f='clearLAM', a=0, data=0)
221227
# cssa(sock, n=22, f=26, a=1)
222-
# cssa(sock, n=22, f=9, a=0)
223-
# cssa(sock, n=22, f=11, a=0)
224-
count = 0
225-
while(True):
226-
t = cssa(sock, n=22, f=8, a=0)
227-
cssa(sock, n= 22, f='clearLAM')
228-
cssa(sock = sock, n = 23, f = 16, a = 0, data=1)
229-
cssa(sock = sock, n = 23, f = 16, a = 0, data=0)
230-
sleep(1)
231-
if t["qresp"][1] != 0:
232-
# print "wow!"
233-
# print count
234-
# print t
235-
dat = cssa(sock, 21, "readGrp1", 8)
236-
cssa (sock, 21, "clearGrp1", 8)
237-
print dat["data"]
238-
else:
239-
count += 1
240-
241-
228+
# cssa(sock, n=22, f=9, a=0)
229+
# cssa(sock, n=22, f=11, a=0)
230+
# count = 0
231+
# while(True):
232+
# t = cssa(sock, n=22, f=8, a=0)
233+
# cssa(sock, n= 22, f='clearLAM')
234+
# break
235+
# cssa(sock = sock, n = 23, f = 16, a = 0, data=1)
236+
# cssa(sock = sock, n = 23, f = 16, a = 0, data=0)
237+
# sleep(1)
238+
# if t["qresp"][1] != 0:
239+
# # print "wow!"
240+
# # print count
241+
# # print t
242+
# dat = cssa(sock, 21, "readGrp1", 8)
243+
# cssa (sock, 21, "clearGrp1", 8)
244+
# print dat["data"]
245+
# else:
246+
# count += 1
247+
#
248+
#
242249

243250
# msg = gettop(flags = 0x0300, ops = 5, cmd = "cmd_attach_lam", mod = 21)
244251
# msg += naf(22, 0, 0) + naf(21,0,8) + naf(21, 0, 9)

display.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#! /usr/bin/python
2+
3+
"""
4+
reads in the data file and plots it in ROOT
5+
"""
6+
7+
import ROOT
8+
9+
if __name__ == '__main__':
10+
file_numbers = (262,) #range(209)
11+
file_fmt = 'data/test_%03i.txt'
12+
13+
# open the files and read them into a histogram
14+
hist = []
15+
for ch in range(4):
16+
h = ROOT.TH1F("ch%i" % ch, "Beam on 1nA pos: (0, 20)", 2**12, 0, 2**12 + 1)
17+
hist.append(h)
18+
19+
for n in file_numbers:
20+
file = open(file_fmt % n, "r")
21+
for line in file:
22+
if '-' in line: continue
23+
24+
dat = line.split()
25+
for ch in range(4):
26+
hist[ch].Fill(float(dat[ch]))
27+
28+
canvas = ROOT.TCanvas("dat", "dat")
29+
canvas.Divide(2, 2)
30+
31+
for pad in range (1, 5):
32+
canvas.cd(pad)
33+
hist[pad - 1].Draw()
34+
35+
run = True
36+
while run:
37+
d = raw_input()
38+
if d:
39+
run = False

displayXY.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#! /usr/bin/python
2+
3+
"""
4+
reads in the data file and plots it in ROOT
5+
"""
6+
7+
import ROOT
8+
9+
if __name__ == '__main__':
10+
file_numbers = (2,3,4)
11+
file_fmt = 'test_dat/test_%03i.txt'
12+
13+
# open the files and read them into a histogram
14+
hist = []
15+
for ch in range(1, 4):
16+
h = ROOT.TH2F("ch1&ch%i" % ch, "pedestal_data", 2000, 0, 2000, 2000, 0, 2000)
17+
hist.append(h)
18+
19+
for n in file_numbers:
20+
file = open(file_fmt % n, "r")
21+
for line in file:
22+
dat = line.split()
23+
for ch in range(1, 4):
24+
hist[ch - 1].Fill(float(dat[0]), float(dat[ch]))
25+
26+
canvas = ROOT.TCanvas("dat", "dat")
27+
canvas.Divide(2, 2)
28+
29+
for pad in range (1, 4):
30+
canvas.cd(pad)
31+
hist[pad - 1].Draw()
32+
33+
run = True
34+
while run:
35+
d = raw_input()
36+
if d:
37+
run = False

fileUpdater.py

+27-9
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,33 @@ class FileUpdater(file):
3333
is also plotted in n_data root histograms, the assumption being
3434
that each n_data corresponds to a channel"""
3535
file_number = 0 # class attribute
36-
canvas = None,
36+
canvas = None
3737

3838
def __init__(self, file_path = 'r_%03i.txt', mode="w", n_dat=4, header = '', TH1args=def_TH1, update_on=100):
39-
"""if file_path contains '%' then that is formatted as the file_number
39+
"""
40+
if file_path contains '%' then that is formatted as the file_number
4041
multiple uses of '%' cause an error. If included as an argument
4142
the header is added to the beginning of the file. Arguments for initialising
4243
the histogram should be included in the dictionary TH1F_kargs"""
44+
4345
# set instance attributes
4446
self.n_ch = n_dat
45-
self.n_run = FileUpdater.file_number
4647
self.entries = 0
4748
self.update_on = update_on
4849
FileUpdater.file_number +=1
49-
if '%' in file_path: file_path = file_path % self.n_run
50+
if '%' in file_path:
51+
while os.path.isfile(file_path % FileUpdater.file_number):
52+
FileUpdater.file_number += 1
53+
file_path = file_path % FileUpdater.file_number
54+
self.n_run = FileUpdater.file_number
5055

5156
# open the file
57+
print "creating file %s" % file_path
58+
print repr(file_path), repr(mode)
5259
file.__init__(self, file_path, mode)
53-
if header: file.write(self, header)
60+
if header:
61+
if header[-1] != '\n': header += '\n'
62+
file.write(self, header)
5463

5564
# initilse the histograms
5665
self.hist = []
@@ -62,7 +71,7 @@ def __init__(self, file_path = 'r_%03i.txt', mode="w", n_dat=4, header = '', TH1
6271
FileUpdater.canvas.Divide(*frameFactorer(self.n_ch))
6372
self.update()
6473

65-
def write(self, dataStr, sep=None):
74+
def write(self, dataStr, sep=None, alert = True):
6675
"""write the dataStr to file then splits it using sep to plot"""
6776
dat = dataStr.split(sep)
6877
if len(dat) != self.n_ch:
@@ -80,8 +89,9 @@ def write(self, dataStr, sep=None):
8089
for h in self.hist:
8190
del h
8291
del FileUpdater.canvas
83-
92+
self.entries += 1
8493
if (self.entries % self.update_on == 0):
94+
# print "%i complete, updating" % (self.entries/self.update_on)
8595
self.update()
8696

8797
def update(self):
@@ -95,6 +105,14 @@ def update(self):
95105

96106
FileUpdater.canvas.Update()
97107

108+
def closeFooter(self, footer):
109+
"""
110+
writes a footer then closes the file"""
111+
if footer:
112+
if footer[-1] != '\n': footer = footer + '\n'
113+
file.write(self, footer)
114+
self.close()
115+
98116
def close(self):
99117
"""close the file and delete the histograms"""
100118
FileUpdater.canvas = 0
@@ -140,7 +158,7 @@ def string_stripper(s):
140158

141159
n_files = 2
142160
for i in range (n_files):
143-
t = FileUpdater("test%i.txt")
161+
t = FileUpdater("test%i.txt",header='hello')
144162

145163
for i in range(n_tests):
146164
print "0"
@@ -162,4 +180,4 @@ def string_stripper(s):
162180
except Exception, e:
163181
pass
164182
finally:
165-
t.close()
183+
t.close("hello chicken!")

0 commit comments

Comments
 (0)