Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add calibrated times and voltages. #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions drs4/binio.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
[
'event_id',
'timestamp',
'range_center',
'adc_data',
'voltage_data',
'time_data',
'scalers',
'trigger_cells',
'trigger_cell',
]
)

Expand All @@ -28,8 +28,8 @@ def __init__(self, filename):
assert self.read(4) == b'TIME', 'File does not contain TIME header'

self.board_ids = []
self.time_widths = {}
self.channels = {}
self.time_widths = {}

header = self.read(4)
while header.startswith(b'B#'):
Expand Down Expand Up @@ -73,6 +73,8 @@ def __next__(self):
scalers = {}
trigger_cells = {}
adc_data = {}
voltage_data = {}
time_data = {}

for board_id, channels in self.channels.items():
assert self.read(2) == b'B#'
Expand All @@ -83,20 +85,28 @@ def __next__(self):

scalers[board_id] = {}
adc_data[board_id] = {}
voltage_data[board_id] = {}
time_data[board_id] = {}

uncaltimes = np.array(range(1024))*0.2 #ns
for channel in channels:
assert self.read(4) == 'C{:03d}'.format(channel).encode('ascii')

scalers[board_id][channel], = struct.unpack('I', self.read(4))
adc_data[board_id][channel] = self._read_adc_data()
voltage_data[board_id][channel] = [ (adc/(2**16) + range_center - 0.5) for adc in adc_data[board_id][channel]] #V
time_data[board_id][channel] = self._calibrate_time(trigger_cells[board_id],self.time_widths[board_id][channel]) #ns
# time_data[board_id][channel] = uncaltimes

time_data = self._align_times(time_data,trigger_cells)

return Event(
event_id=event_id,
timestamp=timestamp,
range_center=range_center,
adc_data=adc_data,
voltage_data=voltage_data,
scalers=scalers,
trigger_cells=trigger_cells,
time_data=time_data,
trigger_cell=trigger_cells,
)

def _read_timewidth_array(self):
Expand All @@ -105,4 +115,33 @@ def _read_timewidth_array(self):
def _read_adc_data(self):
return np.frombuffer(self.read(1024 * 2), 'uint16')

def _calibrate_time(self,trigcell,twidths):
t_calib = twidths
# Make numpy array
t_calib = np.array(t_calib)
# Offset to trigger cell
t_calib = np.roll(t_calib,-trigcell)
# Add zero time and remove last element
t_calib = np.insert(t_calib, 0, 0., axis=0)
t_calib = np.delete(t_calib,-1)
# Compute calibrated sample times
t_calib = np.cumsum(t_calib)

return t_calib

def _align_times(self,times,trigcells):
'''
Align time on trigger cell with respect to first channel
'''
for board_id, channels in self.channels.items():
trigcell = trigcells[board_id]
first_channel = channels[0]
t1 = times[board_id][first_channel][(1024-trigcell) % 1024]
for channel in channels:
if channel == first_channel:
continue
t2 = times[board_id][channel][(1024-trigcell) % 1024]
dt = t1 - t2
times[board_id][channel] += dt

return times