From 9bf21244b05e1f54dafab8d13638967e300f5f3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Thu, 27 Nov 2025 17:17:43 +0100 Subject: [PATCH] added custom io for my302 --- python/CMakeLists.txt | 2 + python/aare/experimental/__init__.py | 0 python/aare/experimental/custom_io.py | 58 +++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 python/aare/experimental/__init__.py create mode 100644 python/aare/experimental/custom_io.py diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index ec2962c..6b6eed9 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -39,6 +39,8 @@ set( PYTHON_FILES aare/transform.py aare/ScanParameters.py aare/utils.py + aare/experimental/__init__.py + aare/experimental/custom_io.py ) diff --git a/python/aare/experimental/__init__.py b/python/aare/experimental/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python/aare/experimental/custom_io.py b/python/aare/experimental/custom_io.py new file mode 100644 index 0000000..28f41dd --- /dev/null +++ b/python/aare/experimental/custom_io.py @@ -0,0 +1,58 @@ + +import numpy as np + + +n_counters = 64*3 +bitfield_size = 64 + +header_dt = [('frameNumber',np.uint64), + ('expLength',np.uint32), + ('packetNumber', np.uint32), + ('bunchId', np.uint64), + ('timestamp', np.uint64), + ('modId', np.uint16), + ('row', np.uint16), + ('col', np.uint16), + ('reserved', np.uint16), + ('debug', np.uint32), + ('roundRNumber', np.uint16), + ('detType', np.uint8), + ('version', np.uint8)] + + + + +def ExtractBits(raw_data, dr=24, bits = (17,6)): + bits = np.uint64(bits) + data = np.zeros(0, dtype = np.uint64) + for bit in bits: + tmp = (raw_data >> bit) & np.uint64(1) + data = np.hstack((data, tmp)) + + #Shift the bits to the righ place + for i in np.arange(dr, dtype = np.uint64): + data[i::dr] = data[i::dr] << i + + data = data.reshape(data.size//dr, dr) + return data.sum(axis = 1) + + + + +def read_my302_file(fname, dr=24, bits = (17,6), + offset=48, tail = 72, n_frames=1): + header = np.zeros(n_frames, header_dt) + data = np.zeros((n_frames, n_counters), dtype = np.uint64) + with open(fname, 'rb') as f: + for i in range(n_frames): + header[i], raw_data = _read_my302_frame(f, offset, tail, dr) + data[i] = ExtractBits(raw_data, dr=dr, bits = bits) + return header, data + + +def _read_my302_frame(f, offset, tail, dr): + header = np.fromfile(f, count=1, dtype = header_dt) + f.seek(bitfield_size+offset, 1) + data = np.fromfile(f, count = int(n_counters*dr/2), dtype = np.uint64) + f.seek(tail, 1) + return header, data