1 | #!/usr/bin/python -tti
|
---|
2 |
|
---|
3 | import time
|
---|
4 | from factdimserver import *
|
---|
5 | import numpy as np
|
---|
6 | import types
|
---|
7 | import sys
|
---|
8 |
|
---|
9 | last_drive_kwargs = {}
|
---|
10 | last_drive_method = None
|
---|
11 |
|
---|
12 | bias_calibration = {}
|
---|
13 |
|
---|
14 | report_length = 0
|
---|
15 |
|
---|
16 | def wait_nice(self, state_num, timeout=None):
|
---|
17 | global report_length
|
---|
18 | if not hasattr(self, 'stn'):
|
---|
19 | raise TypeError(self.name+' has no CMD called STATE')
|
---|
20 | if timeout == None:
|
---|
21 | timeout = float('inf')
|
---|
22 | else:
|
---|
23 | timeout = float(timeout)
|
---|
24 | start = time.time()
|
---|
25 | intermed = time.time()-1.
|
---|
26 | while not self.stn == state_num:
|
---|
27 | time.sleep(0.1)
|
---|
28 | if time.time() - intermed >= 1.:
|
---|
29 | report = str(fad_control.events()[0]) + ' events @ ' + str( ftm_control.trigger_rates()[3]) + ' Hz'
|
---|
30 |
|
---|
31 | sys.stdout.write('\r'+' '*report_length)
|
---|
32 | sys.stdout.flush()
|
---|
33 | sys.stdout.write('\r'+report)
|
---|
34 | sys.stdout.flush()
|
---|
35 | report_length = len(report)
|
---|
36 |
|
---|
37 | intermed = time.time()
|
---|
38 |
|
---|
39 | if time.time() >= start+timeout:
|
---|
40 | report_length=0
|
---|
41 | return False
|
---|
42 | report_length=0
|
---|
43 | return True
|
---|
44 |
|
---|
45 | fad_control.wait_nice = types.MethodType( wait_nice, fad_control)
|
---|
46 |
|
---|
47 |
|
---|
48 | def FadConnectCrate( crate ):
|
---|
49 | cratenum = None
|
---|
50 |
|
---|
51 | if crate == 'all':
|
---|
52 | print "connecting to all crates"
|
---|
53 | for i in range( 40 ):
|
---|
54 | time.sleep(3.8)
|
---|
55 | fad_control.connect(i)
|
---|
56 | print "... done"
|
---|
57 | else:
|
---|
58 | try:
|
---|
59 | cratenum = int(crate)
|
---|
60 | except ValueError as e:
|
---|
61 | print "cannot convert crate parameter to integer. crate=", crate
|
---|
62 | print e
|
---|
63 | raise
|
---|
64 |
|
---|
65 | if cratenum != None:
|
---|
66 | print "connecting to crate", cratenum
|
---|
67 | for i in range(cratenum*10, (cratenum+1)*10 ):
|
---|
68 | time.sleep(3.8)
|
---|
69 | fad_control.connect(i)
|
---|
70 | print "... done"
|
---|
71 |
|
---|
72 |
|
---|
73 | def FadDisconnectCrate( crate ):
|
---|
74 | cratenum = None
|
---|
75 |
|
---|
76 | if crate == 'all':
|
---|
77 | print "connecting to all crates"
|
---|
78 | for i in range( 40 ):
|
---|
79 | fad_control.disconnect(i)
|
---|
80 | print "... done"
|
---|
81 | else:
|
---|
82 | try:
|
---|
83 | cratenum = int(crate)
|
---|
84 | except ValueError as e:
|
---|
85 | print "cannot convert crate parameter to integer. crate=", crate
|
---|
86 | print e
|
---|
87 | raise
|
---|
88 |
|
---|
89 | if cratenum != None:
|
---|
90 | print "connecting to crate", cratenum
|
---|
91 | for i in range(cratenum*10, (cratenum+1)*10 ):
|
---|
92 | print 'disconnecting from i', time.time()
|
---|
93 | fad_control.disconnect(i)
|
---|
94 | print "... done"
|
---|
95 |
|
---|
96 |
|
---|
97 |
|
---|
98 | def IsReadyForDataTaking():
|
---|
99 | """ Checking the system statuses if they are ready for data taking
|
---|
100 | """
|
---|
101 |
|
---|
102 | print "--------------------------------------"
|
---|
103 | print "Checking the system statuses of:"
|
---|
104 | print "FEEDBACK, BIAS, FAD and DRIVE_CONTROL"
|
---|
105 | print "--------------------------------------"
|
---|
106 |
|
---|
107 | print "...waiting for FEEDBACK"
|
---|
108 | print " to be in state 12: CurrentControl"
|
---|
109 | feedback.wait(12)
|
---|
110 |
|
---|
111 | print "...waiting for BIAS_CONTROL"
|
---|
112 | print " to be in state 9: VoltageOn"
|
---|
113 | bias_control.wait(9)
|
---|
114 |
|
---|
115 | print "...waiting for FAD_CONTROL"
|
---|
116 | print " to be in state 4: Connected"
|
---|
117 | fad_control.wait(4)
|
---|
118 |
|
---|
119 | print "...waiting for drive_CONTROL"
|
---|
120 | print " to be in state 8: Tracking"
|
---|
121 | drive_control.wait(8)
|
---|
122 |
|
---|
123 | print "...system statuses OK"
|
---|
124 | print "--------------------------------------"
|
---|
125 |
|
---|
126 | def NotReadyForDataTaking( servers_n_targets = [ (feedback, 12),
|
---|
127 | (bias_control, 9),
|
---|
128 | (fad_control, 4) ] ):
|
---|
129 | """ Checking the system statuses if they are ready for data taking
|
---|
130 | return list of servers, which are NOT ready for Data Taking
|
---|
131 | so one can use this function like this
|
---|
132 | if not NotReadyForDataTaking():
|
---|
133 | # freak out
|
---|
134 | else:
|
---|
135 | # start data taking
|
---|
136 | """
|
---|
137 | not_ready = []
|
---|
138 |
|
---|
139 | print "--------------------------------------"
|
---|
140 | print "Checking the system statuses of:"
|
---|
141 | for server,target in servers_n_targets:
|
---|
142 | print server.__name__ , ','
|
---|
143 | print
|
---|
144 | print "--------------------------------------"
|
---|
145 |
|
---|
146 | for n, server, target in enumerate(servers_n_targets):
|
---|
147 | if server.stn != target:
|
---|
148 | print server.__name__, "NOT in state ", target
|
---|
149 | not_ready.apppend((server,target))
|
---|
150 |
|
---|
151 | return not_ready
|
---|
152 |
|
---|
153 |
|
---|
154 |
|
---|
155 |
|
---|
156 | def PrepareBiasForDataTaking():
|
---|
157 | """ should have the original behaviour, no new fancy features
|
---|
158 | check feedback state before switching BIAS ON and ramping up to nominal Voltage
|
---|
159 | """
|
---|
160 |
|
---|
161 | start = time.time()
|
---|
162 | while (feedback.stn != 12):
|
---|
163 | time.sleep(0.1)
|
---|
164 | if time.time() > start + 10.:
|
---|
165 | print "==================================================="
|
---|
166 | print " feedback is not in state 'CurrentControl' "
|
---|
167 | print " OPERATOR: "
|
---|
168 | print " goto feedback console and check the state of "
|
---|
169 | print " feedback by typing [st] to find out what the"
|
---|
170 | print " current state means and maybe needs to be done"
|
---|
171 | print " this script will wait for state 'CurrentControl'"
|
---|
172 | print "==================================================="
|
---|
173 | feedback.wait(12)
|
---|
174 |
|
---|
175 | bias_control.set_global_dac(1)
|
---|
176 |
|
---|
177 | start = time.time()
|
---|
178 | while (bias_control.stn != 9):
|
---|
179 | time.sleep(0.1)
|
---|
180 | if time.time() > start + 10.:
|
---|
181 | print '==================================================='
|
---|
182 | print ' switching on bias not successfull'
|
---|
183 | print ' biasctrl is not in state "VoltageOn"'
|
---|
184 | print ''
|
---|
185 | print ' OPERATOR:'
|
---|
186 | print ' goto biasctrl console and check the state of'
|
---|
187 | print ' biasctrl by typing [st] to find out what the'
|
---|
188 | print ' current state means and maybe needs to be done'
|
---|
189 | print ''
|
---|
190 | print ' this script will wait for state "VoltageOn"'
|
---|
191 | print '==================================================='
|
---|
192 | bias_control.wait(9)
|
---|
193 |
|
---|
194 | bias_control.wait(5)
|
---|
195 | bias_control.wait(9)
|
---|
196 |
|
---|
197 | print "bias is on, and feedback-program is working, but we wait 45sec for the current readings..."
|
---|
198 | time.sleep(45)
|
---|
199 | print "...done"
|
---|
200 |
|
---|
201 |
|
---|
202 | def StopTracking():
|
---|
203 | """ should have the original behaviour, no new fancy features
|
---|
204 | stop drivectrl tracking the current source
|
---|
205 | """
|
---|
206 | drive_control.stop()
|
---|
207 | drive_control.wait(6) #Armed
|
---|
208 |
|
---|
209 | print "Drive Armed"
|
---|
210 | print "Tracking Stopped"
|
---|
211 |
|
---|
212 |
|
---|
213 | def SwitchOnBias():
|
---|
214 | """ should have the original behaviour, no new fancy features
|
---|
215 | bring Feedback to state CurrentControIdle and switch on Bias
|
---|
216 | """
|
---|
217 | print ' switching on current controll feedback'
|
---|
218 | feedback.stop()
|
---|
219 | print ' ... starting current control feedback '
|
---|
220 | feedback.start_current_control(0.)
|
---|
221 | feedback.enable_output(1) # 1 means True here ... this needs improvement.
|
---|
222 |
|
---|
223 | print ' ...waiting for FEEDBACK to be in state 9: CurrentCtrlIdle'
|
---|
224 | feedback.wait(9)
|
---|
225 | print '... feedback is running.'
|
---|
226 |
|
---|
227 | print ' switching bias on by, setting DAC to 1 globally'
|
---|
228 | bias_control.set_global_dac(1)
|
---|
229 |
|
---|
230 | print ' ...waiting for BIAS to be in state 9: VoltageOn'
|
---|
231 | bias_control.wait(9)
|
---|
232 | print ' ...1 DAC globally set'
|
---|
233 |
|
---|
234 |
|
---|
235 | print ' ...waiting for BIAS to be in state 5: Ramping'
|
---|
236 | bias_control.wait(5)
|
---|
237 | print ' ...ramping to nominal voltage'
|
---|
238 |
|
---|
239 | print ' ...waiting for BIAS to be in state 9: VoltageOn'
|
---|
240 | bias_control.wait(9)
|
---|
241 | print ' ...bias on'
|
---|
242 |
|
---|
243 | print ' waiting 45sec for the current control to stabilize...'
|
---|
244 | time.sleep(45.)
|
---|
245 | print ' ... done, bias on'
|
---|
246 |
|
---|
247 |
|
---|
248 | # the feedback should be in state 'CurrentCtrlIdle'(9) now since 30.05.12
|
---|
249 | # if feedback.stn != 9:
|
---|
250 | # print "feedback is in state:", feedback.sts , "(", feedback.stn, ")"
|
---|
251 | # print "but is should be in state CurrentCtrlIdle (9)"
|
---|
252 | # print "aborting"
|
---|
253 | # return
|
---|
254 |
|
---|
255 |
|
---|
256 |
|
---|
257 | def WaitForTracking( verbose = False):
|
---|
258 | """ Wait for drivectrl to reply that its tracking the given source
|
---|
259 | """
|
---|
260 | if verbose:
|
---|
261 | print "...waiting for DRIVE_CONTROL"
|
---|
262 | print " to be in state 7: Moving"
|
---|
263 | drive_control.wait(7)
|
---|
264 | if verbose:
|
---|
265 | print "...moving"
|
---|
266 |
|
---|
267 | if verbose:
|
---|
268 | print "...waiting for DRIVE_CONTROL"
|
---|
269 | print " to be in state 8: Tracking"
|
---|
270 | drive_control.wait(8)
|
---|
271 | if verbose:
|
---|
272 | print "...tracking requested wobble position"
|
---|
273 |
|
---|
274 | if verbose:
|
---|
275 | print "waiting 10 sec for drive to calm down"
|
---|
276 | print "and tracking beeing stable"
|
---|
277 | time.sleep(10)
|
---|
278 |
|
---|
279 |
|
---|
280 |
|
---|
281 | def Take( time, num_events, runtype, verbose=False):
|
---|
282 | """ more general version of e.g. TakePedestalOnRun
|
---|
283 | Note: One has to check, if Ready for *Take* by oneself
|
---|
284 | """
|
---|
285 | runtype += '\0'
|
---|
286 |
|
---|
287 | if verbose:
|
---|
288 | print ' taking', runtype,'. ', num_events, 'events in', time, 'seconds'
|
---|
289 | mcp.start(time, num_events, runtype)
|
---|
290 |
|
---|
291 | if verbose:
|
---|
292 | print '...waiting for FAD to be in state 8: Writing Data'
|
---|
293 | fad_control.wait(8) # Writing Data
|
---|
294 | if verbose:
|
---|
295 | print '...waiting for FAD to be in state 4: Connected'
|
---|
296 | fad_control.wait_nice(4) # Connected
|
---|
297 | if verbose:
|
---|
298 | print '... done'
|
---|
299 |
|
---|
300 |
|
---|
301 | def TakeData():
|
---|
302 | """ taking a Data Set (1x Pedestal On, 1x LPext, 4x5min DataRun)
|
---|
303 | """
|
---|
304 | # take a Pedestal run
|
---|
305 | IsReadyForDataTaking()
|
---|
306 | print 'taking External Light Pulser with BIAS on 1000 ...'
|
---|
307 | Take(-1, 1000, 'pedestal')
|
---|
308 |
|
---|
309 | # take a ExtLP run
|
---|
310 | IsReadyForDataTaking()
|
---|
311 | print 'taking External Light Pulser with BIAS on 1000 ...'
|
---|
312 | Take(-1, 1000, 'light-pulser-ext')
|
---|
313 |
|
---|
314 | #Data Taking with Full Trigger Area (4x5min)
|
---|
315 | for run in range(4):
|
---|
316 | print 'taking data run', run+1, 'out of 4'
|
---|
317 | IsReadyForDataTaking()
|
---|
318 | Take( 300, -1, 'data')
|
---|
319 |
|
---|
320 | #schedule = [ [drive, bias/feedback, FAD && pre=start_drs_calibration|, time, events runtype, ] ]
|
---|
321 | #[ source_W1 && tracking(8), CC(12)&Off(7) , start_drs_calibration && connected(4), -1, 1000, 'drs-pedestal' ],
|
---|
322 | #[ source_W1 && tracking(8), CC(12)&Off(7) , connected(4), -1, 1000, 'drs-gain' ],
|
---|
323 | #[ check for underflow ?],
|
---|
324 | #[ source_W1 && tracking(8), CC(12)&Off(7) , connected(4), -1, 1000, 'drs-pedestal' ],
|
---|
325 | #[ source_W1 && tracking(8), CC(12)&Off(7) , set_file_format(2) && connected(4), -1, 1000, 'drs-pedestal' ],
|
---|
326 |
|
---|
327 |
|
---|
328 |
|
---|
329 | def TakeDrsCalibration():
|
---|
330 | """ script for DRS-Calibration before Data taking
|
---|
331 | """
|
---|
332 | print 'script for DRS-Calibration before Data taking'
|
---|
333 | print 'starting up...'
|
---|
334 |
|
---|
335 | feedback.enable_output(1)
|
---|
336 | # Making sure bias is off, before the DRS calibration starts
|
---|
337 | bias_control.set_zero_voltage()
|
---|
338 | print '...ramping Voltage down'
|
---|
339 | print ' ...waiting for BIAS to be in state 7: Voltage Off'
|
---|
340 | bias_control.wait(7) #VoltageOff
|
---|
341 | print '...BIAS voltage is switched off'
|
---|
342 |
|
---|
343 | # starting the DRS calibration
|
---|
344 | fad_control.start_drs_calibration()
|
---|
345 |
|
---|
346 | # taking first DRS:Pedestal with 1000 Events and ROI 1024
|
---|
347 | print 'taking DRS:Pedestal 1000 ...'
|
---|
348 | fad_control.wait(4) #Connected
|
---|
349 | Take(-1, 1000, 'drs-pedestal')
|
---|
350 |
|
---|
351 | # taking DRS:Gain with 1000 Events and ROI 1024
|
---|
352 | print ' taking DRS:Gain 1000 ...'
|
---|
353 | fad_control.wait(4) #Connected
|
---|
354 | Take(-1, 1000, 'drs-gain')
|
---|
355 |
|
---|
356 | # taking DRS:Pedestal 1000 Events and ROI 1024
|
---|
357 | print 'taking DRS:Pedestal 1000 ...'
|
---|
358 | fad_control.wait(4) #Connected
|
---|
359 | Take(-1, 1000, 'drs-pedestal')
|
---|
360 |
|
---|
361 | print ' ... done'
|
---|
362 |
|
---|
363 | # taking again a DRS:Pedestal with 1000 Events and ROI 1024 for a crosscheck of calculated calibrations constants
|
---|
364 | print ' taking crosscheck DRS:Pedestal 1000 ...'
|
---|
365 | fad_control.set_file_format(2)
|
---|
366 | fad_control.wait(4) #Connected
|
---|
367 | Take(-1, 1000, 'drs-pedestal')
|
---|
368 |
|
---|
369 |
|
---|
370 | # taking DRS:Time with 1000 Events and ROI 1024
|
---|
371 | print ' taking DRS:Time 1000 ...'
|
---|
372 | fad_control.wait(4) #Connected
|
---|
373 | Take(-1, 1000, 'drs-time')
|
---|
374 |
|
---|
375 |
|
---|
376 | # taking DRS:Time upshifted 1000 Events and ROI 1024
|
---|
377 | print 'taking DRS:Time upshifted 1000 ...'
|
---|
378 | fad_control.wait(4) #Connected
|
---|
379 | Take(-1, 1000, 'drs-time-upshifted')
|
---|
380 |
|
---|
381 | # taking a Pedestal with 1000 Events and ROI 300 for secondary baseline...
|
---|
382 | print 'taking Pedestal 1000 for secondary baseline... with ROI=300'
|
---|
383 | fad_control.reset_secondary_drs_baseline()
|
---|
384 | fad_control.wait(4) #Connected
|
---|
385 | Take(-1, 1000, 'pedestal')
|
---|
386 |
|
---|
387 | # taking crosscheck Pedestal 1000 Events and ROI 300
|
---|
388 | print ' taking crosscheck Pedestal 1000 ...with ROI=300'
|
---|
389 | fad_control.set_file_format(2)
|
---|
390 | fad_control.wait(4) #Connected
|
---|
391 | Take(-1, 1000, 'pedestal')
|
---|
392 |
|
---|
393 | print '----------------------------------------------------'
|
---|
394 | print 'This is the end of the'
|
---|
395 | print 'DRS-Calibration before Data taking'
|
---|
396 | print '----------------------------------------------------'
|
---|
397 |
|
---|
398 |
|
---|
399 | def FirstDrsCalib( SkipCurrentCalib=False ):
|
---|
400 | """ performs the everything, which is done in the FirstDrsCalib Script as well .
|
---|
401 | """
|
---|
402 | # As a First step we want to calibrate the current, which are read from the bias crate,
|
---|
403 | # and not take a DRS calibration, as it is mentioned in the data taking page...
|
---|
404 | # so for this we should get the feedback and biasctrl programs into known states
|
---|
405 | # I think it is good to try a RECONNECT to the bias, and make sure the voltage is off
|
---|
406 | # Since we do not know, what the feedback program is doing at the moment, we should as well,
|
---|
407 | # tell it to keep its mouth shut ... just to be sure, we know whats going on
|
---|
408 | print "stopping feedback"
|
---|
409 | feedback.stop()
|
---|
410 |
|
---|
411 | time.sleep(2)
|
---|
412 | # stopping should always be possible, and end in state 'Connected'(6)
|
---|
413 | print " ...waiting for FEEDBACK to be in state 6: Connected"
|
---|
414 | feedback.wait(6)
|
---|
415 | print "..done"
|
---|
416 |
|
---|
417 | #BIAS_CONTROL/RECONNECT
|
---|
418 | # If we were disconnected, and this was the first try of the night, the bias_ctrl should
|
---|
419 | # be in state 'VoltageOff'(7) more or less immediately
|
---|
420 | #.s BIAS_CONTROL 3
|
---|
421 | #.s BIAS_CONTROL 7 5000
|
---|
422 | # if these assumptions are all wrong, then we might have been properly connected anyway,
|
---|
423 | # and just have to ramp down... lets do it, but wait forever, in case it does not work
|
---|
424 | print " switching off bias"
|
---|
425 | bias_control.set_zero_voltage()
|
---|
426 | time.sleep(2)
|
---|
427 | print " ...waiting for BIAS to be in state 7: VoltageOff"
|
---|
428 | bias_control.wait(7)
|
---|
429 | print " ...done"
|
---|
430 |
|
---|
431 | if not SkipCurrentCalib:
|
---|
432 | # in case we reach this line, the voltages are all off, and the feedback does not do anything
|
---|
433 | # So lets do the current calibration, therefor we tell the bias crate to ramp up just 1 single DAC count(~22mV)
|
---|
434 | # the result of this action is, to get bias_ctrl into the state 'VoltageOn'(9), but since we only go one DAC count it shouldn't take long
|
---|
435 | print " setting bias globally to 1 DAC"
|
---|
436 | bias_control.set_global_dac(1)
|
---|
437 |
|
---|
438 | time.sleep(2)
|
---|
439 | print " ...waiting for BIAS to be in state 9: VoltageOn"
|
---|
440 | bias_control.wait(9)
|
---|
441 | print " ...done"
|
---|
442 |
|
---|
443 | # now we may tell the feedback program to calibrate the currents ...
|
---|
444 | # I do not understand, if I have to explicitely allow the feedback program to generate output,
|
---|
445 | # or if it just produces output...
|
---|
446 | # As far as I understand, the feedback output enable status is the same,
|
---|
447 | # as it was before I send the STOP command... so it is unknown at this point.
|
---|
448 | # and in addition enabling or disabling the output, when STOPed is not possible as far as I know...
|
---|
449 | # I try to enable it anyway.
|
---|
450 | print " enabling output for feedback"
|
---|
451 | feedback.enable_output(1)
|
---|
452 | time.sleep(2)
|
---|
453 | print " ...done"
|
---|
454 |
|
---|
455 | print " calibrating bias crate current readings..."
|
---|
456 | feedback.calibrate_currents()
|
---|
457 | time.sleep(5)
|
---|
458 | # in order to find out when the calibration ends, we have to wait for the transistion from state
|
---|
459 | # 'Calibrating'(13) back to 'Connected'(6)
|
---|
460 | print " ...waiting for FEEDBACK to be in state 13: Calibrating"
|
---|
461 | feedback.wait(13)
|
---|
462 | print " ...waiting for FEEDBACK to be in state 6: Connected"
|
---|
463 | feedback.wait(6)
|
---|
464 |
|
---|
465 | # Thomas Bretz told me, that the feedback, after this is step has disabled its output
|
---|
466 | # and is in the mode, we might call 'temperature control' even there is no temerature beeing controlled.
|
---|
467 | # I don't know where the voltage is ... in order to perform the calibration, the feedback had to
|
---|
468 | # ramp up to 2V below the operational voltage, i.e. about 1V below the breakdown voltage
|
---|
469 |
|
---|
470 | # We want to take a DRS amplitude calibration so we have to ramp down the bias voltage.
|
---|
471 | # this 10sec wait is needed in order for the bias not to disconect all the time...
|
---|
472 | print " ... current calibration done"
|
---|
473 | time.sleep(10)
|
---|
474 |
|
---|
475 | print " switching off bias"
|
---|
476 | bias_control.set_zero_voltage()
|
---|
477 | time.sleep(5)
|
---|
478 | print " ...waiting for BIAS to be in state 7: VoltageOff"
|
---|
479 | bias_control.wait(7)
|
---|
480 | print " ...done"
|
---|
481 |
|
---|
482 | # So now we can take the 3 runs, which are called DRS amplitude calibration:
|
---|
483 | # A pedestal run with ROI=1024
|
---|
484 | # A gain calibration run with ROI=1024
|
---|
485 | # and a second pedestal run, with the same ROI as our next data will be, i.e. ROI=300 in this case
|
---|
486 | print "taking DRS:Pedestal 1000 ..."
|
---|
487 | print "==================================================="
|
---|
488 | print "OPERATOR: "
|
---|
489 | print "observe Events tab and make sure there are no patches "
|
---|
490 | print "with strange behaviour, which can be caused "
|
---|
491 | print "by DRS-CHIP Problems"
|
---|
492 | print "==================================================="
|
---|
493 |
|
---|
494 | fad_control.start_drs_calibration()
|
---|
495 | time.sleep(0.5)
|
---|
496 | Take( -1, 1000, 'drs-pedestal')
|
---|
497 |
|
---|
498 | print "taking DRS:Gain 1000 ..."
|
---|
499 | Take( -1, 1000, 'drs-gain')
|
---|
500 |
|
---|
501 | time.sleep(2)
|
---|
502 | if GetDrsCalibGainRms():
|
---|
503 | print
|
---|
504 | print 'First DRS Calib Script will be aborted'
|
---|
505 | print 'operator, please power cycle FACT'
|
---|
506 | return False
|
---|
507 |
|
---|
508 | print "taking Pedestal 1000 ..."
|
---|
509 | Take( -1, 1000, 'pedestal')
|
---|
510 |
|
---|
511 | # okay this is the DRS calibration for the next few runs.
|
---|
512 | # we are now asked to take again a pedestal run, which can be used, to
|
---|
513 | # calculate the electronics noise for instance ... since the shutter is closed and the
|
---|
514 | # voltage is off .. there should not be alot of signal in it :-)
|
---|
515 | print "taking crosscheck Pedestal 1000 ..."
|
---|
516 | fad_control.set_file_format(2)
|
---|
517 | Take(-1, 1000, 'pedestal')
|
---|
518 |
|
---|
519 | # now we want to take a run, with dark counts events
|
---|
520 | # so we need to ramp up the voltage
|
---|
521 | # we want to use the 'current control' more so we give the commands for this...
|
---|
522 | print "switching on current controll feedback ..."
|
---|
523 | feedback.stop()
|
---|
524 | feedback.start_current_control(0.0)
|
---|
525 | feedback.enable_output(1)
|
---|
526 | # the feedback should be in state 'CurrentControl'(12) now
|
---|
527 | # the feedback should be in state 'CurrentCtrlIdle'(9) now since 30.05.12
|
---|
528 | print "...waiting for FEEDBACK to be in state 9: CurrentCtrlIdle"
|
---|
529 | feedback.wait(9)
|
---|
530 | print "... done"
|
---|
531 | print "switching on bias"
|
---|
532 | # now we give the feedback a hint, that it may ramp ...
|
---|
533 | bias_control.set_global_dac(1)
|
---|
534 | # after this command the bias_ctrl should be in state 'VoltageOn'(9) after a second or so
|
---|
535 | print "...waiting for BIAS to be in state 9: VoltageOn"
|
---|
536 | bias_control.wait(9)
|
---|
537 | print "...1 DAC globally set"
|
---|
538 | # then usually it takes some time until the feedback has enough information to really start controlling the voltage
|
---|
539 | # when the feedback actually kicks in, the bias is first in state 'Ramping'(5) for some seconds and finally in 'VoltageOn'(9)
|
---|
540 | # again
|
---|
541 | print "...waiting for BIAS to be in state 5: Ramping"
|
---|
542 | bias_control.wait(5)
|
---|
543 | print "...ramping to nominal voltage"
|
---|
544 | print "...waiting for BIAS to be in state 9: VoltageOn"
|
---|
545 | bias_control.wait(9)
|
---|
546 | print "...bias on"
|
---|
547 | # here we should wait 45 sec in order for the current control to get enough current readings and temp readings to stabilize..
|
---|
548 | print "waiting 45sec for the current control to stabilize..."
|
---|
549 | time.sleep(45)
|
---|
550 | print "... done"
|
---|
551 |
|
---|
552 | # so now we can take the dark count run ...
|
---|
553 | # this might be changed in the future ... either the number of events or the the ROI might be changed
|
---|
554 | # then the DRS calibration above, and the pedestal run in between have to be changed as well.
|
---|
555 | print "taking Pedestal with BIAS on 3000 ..."
|
---|
556 | Take(-1, 3000, 'pedestal')
|
---|
557 |
|
---|
558 | # at the end the bias voltage should be ramped down, since in a few seconds a shifter wit ha flashlight
|
---|
559 | # will come out to open the shutter...
|
---|
560 | print "switching OFF bias ..."
|
---|
561 | bias_control.set_zero_voltage()
|
---|
562 | print "...waiting for BIAS to be in state 7: VoltageOff"
|
---|
563 | bias_control.wait(7)
|
---|
564 | print "...done"
|
---|
565 | print "This is the end of First DRS Calibration"
|
---|
566 | print "----------------------------------------------------"
|
---|
567 | print ">"
|
---|
568 |
|
---|
569 | def Ratescan( ra=None, dec=None, sourcename=None):
|
---|
570 | """
|
---|
571 | # call it by: .x ScriptsForDimCtrl/Ratescan.dim mode=<trackmode> ra=<Right ascension> dec=<Declination> source=<source_name>
|
---|
572 | # mode=0: Manual tracking Mode: set tracking in drivectrl manually
|
---|
573 | # mode=1: Coordinate Mode: scripts sends tracking command to drivectrl with the given RaDec coordinates
|
---|
574 | # mode=2: source Mode: scripts sends tracking command to drivectrl with the given source_name
|
---|
575 |
|
---|
576 | """
|
---|
577 | print '======================================'
|
---|
578 | print 'RATESCAN'
|
---|
579 | print '======================================'
|
---|
580 | print 'Preparing Drive'
|
---|
581 |
|
---|
582 |
|
---|
583 | if None == ra and None == dec and None == sourcename:
|
---|
584 | print 'Manual tracking Mode'
|
---|
585 | print '---------------------'
|
---|
586 | print 'OPERATOR'
|
---|
587 | print 'change tracking in drivectrl manually'
|
---|
588 | print 'script will wait for drive'
|
---|
589 | print 'to be in state tracking'
|
---|
590 | elif None != ra or None != dec:
|
---|
591 | try:
|
---|
592 | ra = float(ra)
|
---|
593 | dec = float(dec)
|
---|
594 | except TypeError:
|
---|
595 | raise
|
---|
596 |
|
---|
597 | print '...stop tracking'
|
---|
598 | StopTracking()
|
---|
599 | print '...change tracking of telescope to:'
|
---|
600 | print '...Ra = ', ra
|
---|
601 | print '...Dec = ', dec
|
---|
602 | drive_control.track( ra, dec)
|
---|
603 | elif None != sourcename:
|
---|
604 | print '...stop tracking'
|
---|
605 | StopTracking()
|
---|
606 | print '...change tracking of telescope to:', sourcename
|
---|
607 | sourcename += '\0'
|
---|
608 | drive_control.track_source( 0, 0, sourcename)
|
---|
609 | else:
|
---|
610 | print 'type(ra)', type(ra), '\tra', ra
|
---|
611 | print 'type(dec)', type(dec), '\tdec', dec
|
---|
612 | print 'type(sourcename)', type(sourcename), '\tsourcename', sourcename
|
---|
613 | raise ValueError('RateScan does not know what to do with its parameters. Bug!')
|
---|
614 | return False
|
---|
615 |
|
---|
616 | IsReadyForDataTaking()
|
---|
617 |
|
---|
618 | print 'Starting Ratescan'
|
---|
619 | print '...waiting for Ratescan'
|
---|
620 | print ' to be in state 4: Connected'
|
---|
621 |
|
---|
622 |
|
---|
623 | if not rate_scan.wait(4, timeout=5.): #Connected
|
---|
624 | # we went into timeout!
|
---|
625 | print 'Rate_Scan not in correct state'
|
---|
626 | print 'OPERATOR:'
|
---|
627 | print '+ check connection to ftm control'
|
---|
628 | print 'we went into to 5sec. timeout while waiting for RATE_SCAN to be in state Connected'
|
---|
629 | print 'aborting'
|
---|
630 | return False
|
---|
631 |
|
---|
632 | rate_scan.start_threshold_scan( 50, 1000, -10)
|
---|
633 | if not rate_scan.wait( 6, timeout=10.): # Statename???
|
---|
634 | # we went into timeout
|
---|
635 | print 'ratescan not started'
|
---|
636 | print 'we went into 10sec. timeout while waiting for RATE_SCAN to start the Scan'
|
---|
637 | print 'aborting'
|
---|
638 | return False
|
---|
639 |
|
---|
640 | print '...processing ratescan'
|
---|
641 | if not rate_scan.wait( 4, timeout=2700.): # Connected
|
---|
642 | # we went into timeout
|
---|
643 | print 'we went into 2700sec. timeout while waiting for RATE_SCAN to finish'
|
---|
644 | print 'aborting'
|
---|
645 | return False
|
---|
646 |
|
---|
647 | print 'Ratescan finished successfully'
|
---|
648 | return True
|
---|
649 |
|
---|
650 |
|
---|
651 | def ResetCrate( crate_num ):
|
---|
652 | """ Reset Crate
|
---|
653 | crate_num = 0,1,2 or 3 the number of the crate to reset.
|
---|
654 | crate_num = 'all' is NOT YET SUPPORTED
|
---|
655 | """
|
---|
656 | c = int(crate_num)
|
---|
657 |
|
---|
658 | print '======================================'
|
---|
659 | print 'Crate-Reset for crate ', c
|
---|
660 | print '======================================'
|
---|
661 |
|
---|
662 | print '...resetting MCP'
|
---|
663 | mcp.reset()
|
---|
664 | time.sleep(5.)
|
---|
665 | print '...diconnecting FAD boards of crate ', c
|
---|
666 | FadDisconnectCrate( c )
|
---|
667 | time.sleep(2.)
|
---|
668 |
|
---|
669 | print '...disconnecting All FTUs'
|
---|
670 | ftm_control.enable_ftu( -1, 0) # -1 for all, and 0 for False
|
---|
671 | time.sleep(2.)
|
---|
672 |
|
---|
673 | print '...checking state of FTM_Control'
|
---|
674 | print '...waiting for state 3: Idle'
|
---|
675 | if not ftm_control.wait(3, 2.): # Idle
|
---|
676 | print '...stopping trigger'
|
---|
677 | ftm_control.stop_trigger()
|
---|
678 | ftm_control.wait(3) # wait for Idle endlessly
|
---|
679 |
|
---|
680 | print '...resetting crate'
|
---|
681 | ftm_control.reset_crate( c )
|
---|
682 | time.sleep(2.)
|
---|
683 |
|
---|
684 | print '...connecting All FTUs'
|
---|
685 | ftm_control.enable_ftu( -1, 1) # -1 for all, and 1 for yes, or True
|
---|
686 | time.sleep(4.)
|
---|
687 |
|
---|
688 | print '...pinging FTUs'
|
---|
689 | ftm_control.ping()
|
---|
690 |
|
---|
691 | print '...connecting FAD boards of crate', c
|
---|
692 | FadConnectCrate(c)
|
---|
693 | print '======================================'
|
---|
694 | print 'Crate-Reset for crate', c, 'finished'
|
---|
695 | print '======================================'
|
---|
696 |
|
---|
697 | def GetDrsCalibGainRms():
|
---|
698 | #drs_calibration(self) method of factdimserver.FAD_CONTROL instance
|
---|
699 | #DESC in SERVICE_DESC is empty ?!
|
---|
700 | #I:1;I:3;F:1474560;F:1474560;F:1474560;F:1474560;F:1474560;F:1474560;F:163840;F:163840
|
---|
701 | data = fad_control.drs_calibration()
|
---|
702 | N1 = data[0]
|
---|
703 | N3 = data[1:4]
|
---|
704 | OffsetMean = np.array(data[4:4+1024*1440]).reshape(1440,1024)
|
---|
705 | OffsetRms = np.array(data[4+1024*1440:4+1024*1440*2]).reshape(1440,1024)
|
---|
706 | GainMean = np.array(data[4+1024*1440*2:4+1024*1440*3]).reshape(1440,1024)
|
---|
707 | GainRms = np.array(data[4+1024*1440*3:4+1024*1440*4]).reshape(1440,1024)
|
---|
708 |
|
---|
709 | gr = GainRms.mean(axis=1)
|
---|
710 | lala = np.zeros(len(gr)/9)
|
---|
711 | for i,v in enumerate(lala):
|
---|
712 | lala[i] = gr[i*9:(i+1)*9].mean()
|
---|
713 |
|
---|
714 | # outliers
|
---|
715 | mean = lala.mean()
|
---|
716 | std = lala.std()
|
---|
717 |
|
---|
718 | print 'Mean DRS GainRms value:', mean
|
---|
719 | print 'std:', std
|
---|
720 | outs = np.where( lala > mean+7*std)[0]
|
---|
721 | if len(outs) > 0:
|
---|
722 | print 'WARNING possible DRS underflow detected!!!'
|
---|
723 | for out in outs:
|
---|
724 | out = int(out)
|
---|
725 | crate= out/40
|
---|
726 | board = (out-40*crate)/4
|
---|
727 | chip = out-40*crate-4*board
|
---|
728 | print 'possible DRS underflow in DRS:', crate, board, chip, '--> Mean-GainRms:', lala[out]
|
---|
729 | return outs
|
---|
730 | else:
|
---|
731 | return False
|
---|
732 |
|
---|
733 |
|
---|
734 |
|
---|
735 |
|
---|
736 | def GetBiasCalibration():
|
---|
737 | cali = feedback.calibration()
|
---|
738 | bias_calibration['Time'] = time.time()
|
---|
739 | bias_calibration['Calibration'] = cali
|
---|
740 |
|
---|
741 | def GetBiasCurrent(verbose = False):
|
---|
742 | """ return median, std, max and min current
|
---|
743 | """
|
---|
744 | if 'Time' in bias_calibration:
|
---|
745 | cali = bias_calibration['Calibration']
|
---|
746 | else:
|
---|
747 | GetBiasCalibration()
|
---|
748 | cali = bias_calibration['Calibration']
|
---|
749 |
|
---|
750 | r = np.array(cali[2*416:2*416+320])
|
---|
751 |
|
---|
752 | bias = bias_control
|
---|
753 |
|
---|
754 | I = np.array(bias.current()[0:320], dtype=float)
|
---|
755 | II = I/4096. * 5000
|
---|
756 | V = np.array(bias.voltage()[0:320])
|
---|
757 | if len(sys.argv) > 1:
|
---|
758 | i = int(sys.argv[1])
|
---|
759 | else: i=0
|
---|
760 | # print 'I:', I[i], 'dac\t', II[i], 'uA'
|
---|
761 | # print 'V:', V[i]
|
---|
762 | # print ''
|
---|
763 | # print 'GUI offset:', V[i]/r[i]*1e6
|
---|
764 | # print 'GUI feedback:', II[0] - V[0]/r[0]*1e6
|
---|
765 |
|
---|
766 | GUII = II-V/r*1e6
|
---|
767 | if verbose:
|
---|
768 | print 'median', np.median(GUII)
|
---|
769 | print 'mean', GUII.mean()
|
---|
770 | # print 'rms', ((GUII- GUII.mean())**2).sum()
|
---|
771 | print 'std', GUII.std()
|
---|
772 | print 'max', GUII.max()
|
---|
773 | print 'min', GUII.min()
|
---|
774 |
|
---|
775 | return GUII.mean(), GUII.std(), GUII.max(), GUII.min()
|
---|
776 |
|
---|
777 |
|
---|
778 |
|
---|
779 |
|
---|
780 | def TrackSourceWobbleX( sourcename, wobble_pos ):
|
---|
781 | """ general Tracking function
|
---|
782 | """
|
---|
783 | wp = int(wobble_pos)
|
---|
784 | if wp != 1 and wp != 2:
|
---|
785 | raise ValueError('wobble_pos *must* be 1 or 2')
|
---|
786 |
|
---|
787 | if sourcename not in sourcedict:
|
---|
788 | print sourcedict.keys()
|
---|
789 | raise ValueError('sourcename: '+ sourcename +' must be in sourcedict.')
|
---|
790 |
|
---|
791 | print 'moving telescope to wobble position ', wp, 'of ', sourcename
|
---|
792 | print '...waiting for DRIVE_CONTROL'
|
---|
793 | print ' to be in state 6: Armed'
|
---|
794 | drive_control.stop()
|
---|
795 | drive_control.wait(6) #Armed
|
---|
796 | print 'DRIVE: ARMED'
|
---|
797 | time.sleep(5.)
|
---|
798 |
|
---|
799 | wobble_offset = sourcedict[sourcename]['wobble_offset']
|
---|
800 | wobble_angle = sourcedict[sourcename]['wobble_angle'+str(wp)]
|
---|
801 | sourcename += '\0'
|
---|
802 |
|
---|
803 | last_drive_method = drive_control.track_source
|
---|
804 | last_drive_kwargs = { 'wobble_offset' : wobble_offset,
|
---|
805 | 'wobble_angle' : wobble_angle,
|
---|
806 | 'source_name' : sourcename }
|
---|
807 | kwa = last_drive_kwargs
|
---|
808 | last_drive_method(kwa['wobble_offset'], kwa['wobble_angle'], kwa['source_name'])
|
---|
809 |
|
---|
810 | print '... done'
|
---|
811 |
|
---|
812 | source_list = [
|
---|
813 | ['Crab', 0.6 , 50, -130],
|
---|
814 | ["1ES 2344+51.4", 0.6 , 90, -90 ],
|
---|
815 | ["Mrk 501", 0.6, -22, -22+180 ],
|
---|
816 | ["Mrk 421", 0.6, 90, -90 ],
|
---|
817 | ["1ES 1218+304", 0.6, -5, -5+180 ],
|
---|
818 | ["1ES 1959+650", 0.6, 155, 155-180 ],
|
---|
819 | ["Dark Patch 2", 0.6 , 90, -90 ],
|
---|
820 | ["Dark Patch 3", 0.6 , 90, -90 ],
|
---|
821 | ["H 1426+428", 0.6 , 90, -90 ],
|
---|
822 | ["IC 310", 0.6, -18, -18+180 ],
|
---|
823 | ["PKS 2155-304", 0.6, 90, -90 ] ]
|
---|
824 |
|
---|
825 | sourcedict = {}
|
---|
826 |
|
---|
827 | def make_sourcedict():
|
---|
828 | for s in source_list:
|
---|
829 | sourcedict[s[0]] = {}
|
---|
830 | sourcedict[s[0]]['wobble_offset'] = s[1]
|
---|
831 | sourcedict[s[0]]['wobble_angle1'] = s[2]
|
---|
832 | sourcedict[s[0]]['wobble_angle2'] = s[3]
|
---|
833 |
|
---|
834 |
|
---|
835 | def TakeSource( name ):
|
---|
836 | if name not in sourcedict:
|
---|
837 | print name, 'not in dict of sources, possible sources are:'
|
---|
838 | print sorted(sourcedict.keys())
|
---|
839 | raise ValueError('wrong source name')
|
---|
840 |
|
---|
841 | TrackSourceWobbleX( name, 1) # Track Wobble pos 1 of source
|
---|
842 | WaitForTracking()
|
---|
843 | # Take a DRS-Calibration before beginning to take physics Data
|
---|
844 | TakeDrsCalibration()
|
---|
845 | # check feedback state before switching BIAS ON and ramping up to nominal Voltage
|
---|
846 | PrepareBiasForDataTaking()
|
---|
847 | # taking a Data Set (1x Pedestal 1000 Bias On, 1x LPext 1000, 4x5min DataRun)
|
---|
848 | TakeData()
|
---|
849 | StopTracking()
|
---|
850 |
|
---|
851 | TrackSourceWobbleX( name, 2) # Track Wobble pos 2 of source
|
---|
852 | WaitForTracking()
|
---|
853 | # taking a Data Set (1x Pedestal 1000 Bias On, 1x LPext 1000, 4x5min DataRun)
|
---|
854 | TakeData()
|
---|
855 |
|
---|
856 | StopTracking()
|
---|
857 |
|
---|
858 |
|
---|
859 |
|
---|
860 |
|
---|
861 |
|
---|
862 |
|
---|
863 | def PrintCurrents( x = 1.0):
|
---|
864 | while True:
|
---|
865 | time.sleep( x )
|
---|
866 | print time.strftime('%d %b %Y %H:%M:%S UTC', time.gmtime()), GetBiasCurrent()
|
---|
867 |
|
---|
868 |
|
---|
869 | if __name__ == '__main__':
|
---|
870 | print 'Welcome to PyDimCtrl'
|
---|
871 | make_sourcedict()
|
---|
872 | print
|
---|
873 | print 'possible sources:'
|
---|
874 | print sorted( sourcedict.keys() )
|
---|