-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathscan_ext_trigger_gdac.py
68 lines (54 loc) · 3.59 KB
/
scan_ext_trigger_gdac.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
import logging
import numpy as np
import tables as tb
from scipy.interpolate import interp1d
from pybar.scans.scan_ext_trigger import ExtTriggerScan
from pybar.run_manager import RunManager
class ExtTriggerGdacScan(ExtTriggerScan):
'''External trigger scan with FE-I4 and adjustable GDAC range
For use with external scintillator (user RX0), TLU (use RJ45), USBpix self-trigger (loop back TX2 into RX0.)
'''
_default_run_conf = ExtTriggerScan._default_run_conf.copy()
_default_run_conf.update({
"scan_parameters": [('GDAC', None)], # list of values, string with calibration file name, None: use 50 GDAC values
"interpolate_calibration": True, # interpolate GDAC values to have equally spaced thresholds, otherwise take GDACs used during calibration
"interpolation_thresholds": range(30, 600, 5) # threshold values in PlsrDAC
})
def configure(self):
super(ExtTriggerGdacScan, self).configure()
# Set GDACs to be used during scan
if not self.scan_parameters.GDAC: # distribute logarithmically if no GDAC was specified
altc = self.register.get_global_register_value("Vthin_AltCoarse")
altf = self.register.get_global_register_value("Vthin_AltFine")
curr_gdac = self.register_utils.get_gdac(altc=altc, altf=altf)
self.gdacs = np.unique(np.logspace(np.log10(curr_gdac), np.log10(6000), 60).astype(np.int)).tolist() + range(6500, 25001, 500)
elif isinstance(self.scan_parameters.GDAC, basestring): # deduce GDACs from calibration file
if self.interpolate_calibration:
self.gdacs = self.get_gdacs_from_interpolated_calibration(self.scan_parameters.GDAC, self.interpolation_thresholds)
else:
self.gdacs = self.get_gdacs_from_calibration_file(self.scan_parameters.GDAC)
else: # Use defined GDACs
self.gdacs = self.scan_parameters.GDAC
logging.info("Scanning %s from %d to %d in %d steps", 'GDAC', self.gdacs[0], self.gdacs[-1], len(self.gdacs))
def scan(self):
for gdac in self.gdacs:
if self.abort_run.is_set():
break
self.register_utils.set_gdac(gdac)
self.set_scan_parameters(GDAC=gdac)
ExtTriggerScan.scan(self)
self.stop_run.clear()
def handle_data(self, data, new_file=True, flush=True):
super(ExtTriggerGdacScan, self).handle_data(data=data, new_file=new_file, flush=flush)
def get_gdacs_from_interpolated_calibration(self, calibration_file, thresholds):
logging.info('Interpolate GDAC calibration for the thresholds %s', str(thresholds))
with tb.open_file(calibration_file, mode="r") as in_file_calibration_h5: # read calibration file from calibrate_threshold_gdac scan
interpolation = interp1d(in_file_calibration_h5.root.MeanThresholdCalibration[:]['mean_threshold'], in_file_calibration_h5.root.MeanThresholdCalibration[:]['parameter_value'], kind='slinear', bounds_error=True)
return np.unique(interpolation(thresholds).astype(np.uint32))
def get_gdacs_from_calibration_file(self, calibration_file):
logging.info('Take GDAC values from calibration file')
with tb.open_file(calibration_file, mode="r") as in_file_calibration_h5: # read calibration file from calibrate_threshold_gdac scan
return in_file_calibration_h5.root.MeanThresholdCalibration[:]['parameter_value']
if __name__ == "__main__":
with RunManager('configuration.yaml') as runmngr:
runmngr.run_run(ExtTriggerGdacScan)