-
Notifications
You must be signed in to change notification settings - Fork 0
/
el737counter_recv.py
executable file
·318 lines (265 loc) · 9.97 KB
/
el737counter_recv.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
#!/usr/bin/python
#
# fake SINQ EL737 counter box
#
# I am using 1000cts/sec for m1, 500 for m2, 300 for m3 and 2000 for m4
#
# Mark Koennecke, July 2015
#----------------------------------------------------------------------
from twisted.internet import reactor, protocol
from twisted.protocols.basic import LineReceiver
import zmq
import time
import sys,os
from datetime import datetime
import threading
import json
from nexus2event import *
from neventarray import *
class EL737Controller(LineReceiver):
def __init__(self):
self.remotestate = 0
self.delimiter = '\r'
self.mode = 'timer'
self.preset = .0
self.starttime = time.time()
self.endtime = time.time()
self.counting = False
self.mypaused = True
self.pausestart = 0
self.pausedTime = 0.
self.threshold = 0
self.thresholdcounter = 1
self.context = zmq.Context()
self.socket = []
self.source = []
self.size = 0
self.count = 0
self.dtype = event_t
self.Stop = False
self.of = []
def write(self, data):
print "transmitted:", data
if self.transport is not None:
self.transport.write(data)
def calculateCountStatus(self):
if self.counting and not self.mypaused :
runtime = time.time() - self.starttime - self.pausedTime
print(str(runtime) + ' versus ' + str(self.preset))
if self.mode == 'timer':
if runtime >= self.preset:
self.counting = False
self.endtime = self.starttime + self.preset
self.pausedTime = 0
else:
if runtime*1000 >= self.preset:
self.counting = False
self.endtime = self.starttime + self.preset/1000
print('count flag after calculateCountStatus ' + str(self.counting))
def lineReceived(self, data):
print "lineReceived:", data
if self.remotestate == 0:
if data.startswith('init'):
"""Enbles the event reader. Syntax:
>>> init <address>:<port>
Args:
port (int): tcp/ip address to listen for
port (int): the port to use for zmq communications
It does not start the reader, simply sets up the
environment. To run the reader use >>> run
>>> init tcp://127.0.0.1:1234
"""
self.socket = self.context.socket(zmq.PULL)
self.socket.connect(data.split(" ")[1])
self.write("zmq connected to "+data.split(" ")[1])
self.write("\r")
self.remotestate = 1
return
data = data.lower().strip()
if self.remotestate == 0:
if data.startswith('rmt 1'):
self.remotestate = 1
self.write("\r")
else:
self.write("?loc\r")
return
if self.remotestate == 1:
if data.startswith('echo 2'):
self.remotestate = 2
self.write("\r")
else:
if data.startswith("run"):
"""Runs the event reader. Syntax:
>>> run
Args:
Reads data from :NeXus file:, eventually applies
:multiplier: and start sending data. Every 10s return
statistics.
>>> run
"""
self.counting = True
self.mypaused = False
self.remotestate = 2
thread = threading.Thread(target=self.start)
thread.daemon = True
thread.start()
else:
if data.startswith("debug"):
"""Runs the event generator in debug mode. Syntax:
>>> debug
"""
self.counting = True
self.mypaused = False
self.dtype = debug_t
self.of = open("dump_"+datetime.now().strftime("%Y-%m-%dT%H:%M:%S")+".txt","w")
self.remotestate = 2
thread = threading.Thread(target=self.start)
thread.daemon = True
thread.start()
else:
self.write("?loc\r")
return
if self.remotestate == 2:
if data.startswith('rmt 1') or data.startswith('echo'):
self.write('\r')
return
if data.startswith('mp'):
l = data.split()
self.mode = 'monitor'
self.preset = float(l[1])
self.starttime = time.time()
self.mypaused = False
self.pausedTime = 0.
self.counting = True
self.write('\r')
return
if data.startswith('tp'):
l = data.split()
self.mode = 'timer'
self.preset = float(l[1])
self.starttime = time.time()
self.mypaused = False
self.pausedTime = 0.
self.counting = True
self.write('\r')
return
if data.startswith('s'):
self.counting = False
self.endtime = time.time()
self.write('\r')
return
if data.startswith('ps'):
self.counting = True
self.mypaused = True
self.pausestart = time.time()
self.write('\r')
return
if data.startswith('co'):
if not self.mypaused:
pass
else:
self.mypaused = False
self.pausedTime += time.time() - self.pausestart
self.write('\r')
return
if data.startswith('dl'):
l = data.split()
if len(l) >= 3:
self.threshold = float(l[2])
self.write('\r')
else:
self.write(str(self.threshold) + '\r')
if data.startswith('dr'):
l = data.split()
if len(l) >= 2:
self.thresholdcounter = int(l[1])
self.write('\r')
else:
self.write(str(self.thresholdcounter) + '\r')
if data.startswith('rs'):
self.calculateCountStatus()
if self.counting:
if self.mypaused:
if self.mode == 'timer':
self.write('9\r')
else:
self.write('10\r')
else:
if self.mode == 'timer':
self.write('1\r')
else:
self.write('2\r')
else:
self.write('0\r')
return
if data.startswith('ra'):
self.calculateCountStatus()
if self.counting:
if self.mypaused:
pausetime = time.time() - self.pausestart
else:
pausetime = self.pausedTime
diff = time.time() - self.starttime - pausetime
else:
diff = self.endtime - self.starttime
rlist = []
rlist.append(str(diff))
rlist.append(str(int(diff*1000)))
rlist.append(str(int(diff*1500)))
rlist.append(str(int(diff*500)))
rlist.append(str(int(diff*300)))
rlist.append(str(int(diff*2000)))
rlist.append('0')
rlist.append('0')
rlist.append('0')
rastring = ' '.join(rlist)
self.write(rastring +'\r')
return
if data.startswith('id'):
self.write('EL737 Neutron Counter V8.02\r')
return
self.write('?2\r')
def stats(self) :
while not self.Stop:
time.sleep(10)
print "Received",self.count,"events (",self.size/1.e6,"MB) @ ",self.size*self.count/(10.*1e6)," MB/s"
self.count = 0
def start(self):
pulseID=0
thread = threading.Thread(target=self.stats)
thread.daemon = True
thread.start()
while not self.Stop:
if not self.mypaused:
dataHeader = self.socket.recv_json()
buf = self.socket.recv(copy=True)
data = np.frombuffer(buffer(buf),dtype=event_t)
if self.dtype == debug_t:
self.of.write(json.dumps(dataHeader))
for i in data:
self.of.write(str(i)+"\n")
self.of.flush()
os.fsync(self.of.fileno())
self.count += 1
self.size = (data.size*event_t.itemsize+
sys.getsizeof(dataHeader))
if dataHeader["ts"] > pulseID:
while not int(dataHeader["ts"]) == (pulseID+1):
print "Lost pulse ",pulseID
pulseID += 1
if dataHeader["ts"] < pulseID:
pulseID = dataHeader["ts"]
pulseID += 1
print "execution stopped"
thread.stop()
def main(argv):
if len(argv) > 1:
port = int(argv[1])
else:
port = 62000
factory = protocol.ServerFactory()
factory.protocol = EL737Controller
reactor.listenTCP(port, factory)
reactor.run()
if __name__ == "__main__":
main(sys.argv)