MH02 1-4 counters

This commit is contained in:
Erik Fröjdh
2024-11-06 21:32:03 +01:00
parent cbfd1f0b6c
commit b2e5c71f9c
12 changed files with 195 additions and 24 deletions

View File

@ -36,6 +36,7 @@ class CtbRawFile(_aare.CtbRawFile):
if frame_index is not None:
self.seek(frame_index)
header, data = super().read_frame()
if header.shape == (1,):
header = header[0]
@ -47,6 +48,8 @@ class CtbRawFile(_aare.CtbRawFile):
return header, *res
else:
return header, res
else:
return header, data
def read_n(self, n_frames:int) -> tuple:
"""Read several frames from the file.
@ -82,6 +85,13 @@ class CtbRawFile(_aare.CtbRawFile):
return header, data
def read(self) -> tuple:
"""Read the entire file.
Returns:
tuple: header, data
"""
return self.read_n(self.frames_in_file)
def seek(self, frame_index:int) -> None:
"""Seek to a specific frame in the file.

View File

@ -12,4 +12,17 @@ class Moench05Transform:
return np.take(data.view(np.uint16), self.pixel_map)
class Matterhorn02Transform:
def __init__(self):
self.pixel_map = _aare.GenerateMH02FourCounterPixelMap()
def __call__(self, data):
counters = int(data.size / 48**2 / 2)
if counters == 1:
return np.take(data.view(np.uint16), self.pixel_map[0])
else:
return np.take(data.view(np.uint16), self.pixel_map[0:counters])
#on import generate the pixel maps to avoid doing it every time
moench05 = Moench05Transform()
matterhorn02 = Matterhorn02Transform()

View File

@ -9,6 +9,50 @@ from aare import transform
print('transform imported')
from pathlib import Path
import json
def decode(frames, rawdata):
# rawdata = np.fromfile(f, dtype = np.uint16)
counters = int((np.shape(rawdata)[0]/frames-56)/(48*48))
print('Counters:', counters)
rawdata = rawdata.reshape(frames,-1)[:,56:]
rawdata = rawdata.reshape(frames,576*counters,4) #Data come in "blocks" of 4 pixels/receiver
tr1 = rawdata[:,0:576*counters:2] #Transceiver1
tr1=tr1.reshape((frames,48*counters,24))
tr2 = rawdata[:,1:576*counters:2] #Transceiver2
tr2=tr2.reshape((frames,48*counters,24))
data = np.append(tr1,tr2,axis=2)
return data
def get_Mh02_frames(fname):
# this function gives you the data from a file that is not a scan
# it returns a (frames,48*counters,48)
jsonf = open(fname)
jsonpar = json.load(jsonf)
jsonf.close()
frames=jsonpar["Frames in File"]
print('Frames:', frames)
rawf = fname.replace('master','d0_f0')
rawf = rawf.replace('.json','.raw')
with open(rawf, 'rb') as f:
rawdata = np.fromfile(f, dtype = np.uint16)
data = decode(frames, rawdata)
print('Data:', np.shape(data))
return data
#target format
# [frame, counter, row, col]
# plt.imshow(data[0,0])
# p = Path('/Users/erik/data/aare_test_data/jungfrau/jungfrau_single_master_0.json')
# f = aare.File(p)
@ -18,27 +62,75 @@ from pathlib import Path
# im = ax.imshow(frame, cmap='viridis')
fpath = Path('/Users/erik/data/Moench03old/test_034_irradiated_noise_g4_hg_exptime_2000us_master_0.json')
# fpath = Path('/Users/erik/data/Moench05/moench05_multifile_master_0.json')
# fpath = Path('/Users/erik/data/Moench03old/test_034_irradiated_noise_g4_hg_exptime_2000us_master_0.json')
# # fpath = Path('/Users/erik/data/Moench05/moench05_multifile_master_0.json')
# f = aare.CtbRawFile(fpath, transform = transform.moench05)
# with CtbRawFile(fpath, transform = transform.moench05) as f:
# for header, image in f:
# print(f'Frame number: {header["frameNumber"]}')
# # f = aare.CtbRawFile(fpath, transform = transform.moench05)
# # with CtbRawFile(fpath, transform = transform.moench05) as f:
# # for header, image in f:
# # print(f'Frame number: {header["frameNumber"]}')
# m = aare.RawMasterFile(fpath)
f = aare.File(fpath)
# # m = aare.RawMasterFile(fpath)
# f = aare.File(fpath)
cf = aare.ClusterFinder((400,400),(3,3))
# cf = aare.ClusterFinder((400,400),(3,3))
for i in range(100):
cf.push_pedestal_frame(f.read_frame())
# for i in range(100):
# cf.push_pedestal_frame(f.read_frame())
f.seek(0)
pd = f.read_n(100).mean(axis=0)
# f.seek(0)
# pd = f.read_n(100).mean(axis=0)
# clusters = cf.find_clusters_without_threshold(f.read_frame())
base = Path('/Users/erik/data/matterhorn/raw')
fpath = Path(base / 'scan_15keV_vrf700_vrsh700_th0_master_0.json')
f = aare.CtbRawFile(fpath, transform=transform.matterhorn02)
f.seek(100)
header1, image1 = f.read_frame()
fpath = Path(base / 'scan_all15keV_vrf500_vrsh700_th0_master_0.json')
f = aare.CtbRawFile(fpath, transform=transform.matterhorn02)
f.seek(100)
header4, image4 = f.read_frame()
# n_counters = image.shape[1] / 48**2 / 2
# for i in range(100):
# header, image = f.read_frame()
# print(header['frameNumber'])
#Data come in "blocks" of 4 pixels/receiver
data = get_Mh02_frames(fpath.as_posix())
# rawi = np.zeros(48*48*4+56, dtype = np.uint16)
# for i,v in enumerate(rawi[56:]):
# rawi[i+56] = i
# raw = image.view(np.uint16)
# pixel_map = decode(1, rawi)
# # img = np.take(raw, pixel_map)
# pm = np.zeros((4, 48,48), dtype = np.int64)
# for counter in range(4):
# for row in range(48):
# for col in range(48):
# pm[counter, row, col] = row*48 + col+counter*48*48
f2 = aare.CtbRawFile(fpath, transform=transform.matterhorn02)
header, data = f2.read()
plt.plot(data[:,0,20,20])
clusters = cf.find_clusters_without_threshold(f.read_frame())

View File

@ -48,6 +48,7 @@ void define_file_io_bindings(py::module &m) {
})
.def("seek", &CtbRawFile::seek)
.def("tell", &CtbRawFile::tell)
.def("master", &CtbRawFile::master)
.def_property_readonly("image_size_in_bytes", &CtbRawFile::image_size_in_bytes)
.def_property_readonly("frames_in_file", &CtbRawFile::frames_in_file);
@ -167,7 +168,8 @@ void define_file_io_bindings(py::module &m) {
.def_property_readonly("bitdepth", &RawMasterFile::bitdepth)
.def_property_readonly("analog_samples", &RawMasterFile::analog_samples)
.def_property_readonly("digital_samples",
&RawMasterFile::digital_samples);
&RawMasterFile::digital_samples)
.def_property_readonly("transceiver_samples", &RawMasterFile::transceiver_samples);
// py::class_<ClusterHeader>(m, "ClusterHeader")
// .def(py::init<>())

View File

@ -14,11 +14,19 @@ using namespace::aare;
void define_pixel_map_bindings(py::module &m) {
m.def("GenerateMoench03PixelMap", []() {
auto ptr = new NDArray<size_t,2>(GenerateMoench03PixelMap());
auto ptr = new NDArray<ssize_t,2>(GenerateMoench03PixelMap());
return return_image_data(ptr);
})
.def("GenerateMoench05PixelMap", []() {
auto ptr = new NDArray<size_t,2>(GenerateMoench05PixelMap());
auto ptr = new NDArray<ssize_t,2>(GenerateMoench05PixelMap());
return return_image_data(ptr);
})
.def("GenerateMH02SingleCounterPixelMap", []() {
auto ptr = new NDArray<ssize_t,2>(GenerateMH02SingleCounterPixelMap());
return return_image_data(ptr);
})
.def("GenerateMH02FourCounterPixelMap", []() {
auto ptr = new NDArray<ssize_t,3>(GenerateMH02FourCounterPixelMap());
return return_image_data(ptr);
});