mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2025-04-19 21:30:02 +02:00
ported reading clusters (#95)
This commit is contained in:
parent
dc889dab76
commit
7ffd732d98
@ -243,6 +243,7 @@ endif()
|
|||||||
|
|
||||||
set(PUBLICHEADERS
|
set(PUBLICHEADERS
|
||||||
include/aare/ClusterFinder.hpp
|
include/aare/ClusterFinder.hpp
|
||||||
|
include/aare/ClusterFile.hpp
|
||||||
include/aare/CtbRawFile.hpp
|
include/aare/CtbRawFile.hpp
|
||||||
include/aare/defs.hpp
|
include/aare/defs.hpp
|
||||||
include/aare/Dtype.hpp
|
include/aare/Dtype.hpp
|
||||||
@ -265,6 +266,7 @@ set(PUBLICHEADERS
|
|||||||
|
|
||||||
set(SourceFiles
|
set(SourceFiles
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/src/CtbRawFile.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/src/CtbRawFile.cpp
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/src/ClusterFile.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/src/defs.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/src/defs.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/src/Dtype.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/src/Dtype.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/src/Frame.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/src/Frame.cpp
|
||||||
|
59
include/aare/ClusterFile.hpp
Normal file
59
include/aare/ClusterFile.hpp
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include "aare/defs.hpp"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
namespace aare {
|
||||||
|
|
||||||
|
struct Cluster {
|
||||||
|
int16_t x;
|
||||||
|
int16_t y;
|
||||||
|
int32_t data[9];
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
cBottomLeft = 0,
|
||||||
|
cBottomRight = 1,
|
||||||
|
cTopLeft = 2,
|
||||||
|
cTopRight = 3
|
||||||
|
} corner;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
pBottomLeft = 0,
|
||||||
|
pBottom = 1,
|
||||||
|
pBottomRight = 2,
|
||||||
|
pLeft = 3,
|
||||||
|
pCenter = 4,
|
||||||
|
pRight = 5,
|
||||||
|
pTopLeft = 6,
|
||||||
|
pTop = 7,
|
||||||
|
pTopRight = 8
|
||||||
|
} pixel;
|
||||||
|
|
||||||
|
struct ClusterAnalysis {
|
||||||
|
uint32_t c;
|
||||||
|
int32_t tot;
|
||||||
|
double etax;
|
||||||
|
double etay;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ClusterFile {
|
||||||
|
FILE *fp{};
|
||||||
|
uint32_t m_num_left{};
|
||||||
|
|
||||||
|
public:
|
||||||
|
ClusterFile(const std::filesystem::path &fname);
|
||||||
|
std::vector<Cluster> read_clusters(size_t n_clusters);
|
||||||
|
std::vector<Cluster>
|
||||||
|
read_cluster_with_cut(size_t n_clusters, double *noise_map, int nx, int ny);
|
||||||
|
|
||||||
|
int analyze_data(int32_t *data, int32_t *t2, int32_t *t3, char *quad,
|
||||||
|
double *eta2x, double *eta2y, double *eta3x, double *eta3y);
|
||||||
|
int analyze_cluster(Cluster cl, int32_t *t2, int32_t *t3, char *quad,
|
||||||
|
double *eta2x, double *eta2y, double *eta3x,
|
||||||
|
double *eta3y);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace aare
|
@ -53,7 +53,7 @@ class ClusterFinder {
|
|||||||
return m_pedestal.mean();
|
return m_pedestal.mean();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Cluster>
|
std::vector<DynamicCluster>
|
||||||
find_clusters_without_threshold(NDView<FRAME_TYPE, 2> frame,
|
find_clusters_without_threshold(NDView<FRAME_TYPE, 2> frame,
|
||||||
// Pedestal<PEDESTAL_TYPE> &pedestal,
|
// Pedestal<PEDESTAL_TYPE> &pedestal,
|
||||||
bool late_update = false) {
|
bool late_update = false) {
|
||||||
@ -64,7 +64,7 @@ class ClusterFinder {
|
|||||||
};
|
};
|
||||||
std::vector<pedestal_update> pedestal_updates;
|
std::vector<pedestal_update> pedestal_updates;
|
||||||
|
|
||||||
std::vector<Cluster> clusters;
|
std::vector<DynamicCluster> clusters;
|
||||||
std::vector<std::vector<eventType>> eventMask;
|
std::vector<std::vector<eventType>> eventMask;
|
||||||
for (int i = 0; i < frame.shape(0); i++) {
|
for (int i = 0; i < frame.shape(0); i++) {
|
||||||
eventMask.push_back(std::vector<eventType>(frame.shape(1)));
|
eventMask.push_back(std::vector<eventType>(frame.shape(1)));
|
||||||
@ -115,7 +115,7 @@ class ClusterFinder {
|
|||||||
if (eventMask[iy][ix] == PHOTON &&
|
if (eventMask[iy][ix] == PHOTON &&
|
||||||
(frame(iy, ix) - m_pedestal.mean(iy, ix)) >= max) {
|
(frame(iy, ix) - m_pedestal.mean(iy, ix)) >= max) {
|
||||||
eventMask[iy][ix] = PHOTON_MAX;
|
eventMask[iy][ix] = PHOTON_MAX;
|
||||||
Cluster cluster(m_cluster_sizeX, m_cluster_sizeY,
|
DynamicCluster cluster(m_cluster_sizeX, m_cluster_sizeY,
|
||||||
Dtype(typeid(PEDESTAL_TYPE)));
|
Dtype(typeid(PEDESTAL_TYPE)));
|
||||||
cluster.x = ix;
|
cluster.x = ix;
|
||||||
cluster.y = iy;
|
cluster.y = iy;
|
||||||
@ -149,11 +149,11 @@ class ClusterFinder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// template <typename FRAME_TYPE, typename PEDESTAL_TYPE>
|
// template <typename FRAME_TYPE, typename PEDESTAL_TYPE>
|
||||||
std::vector<Cluster>
|
std::vector<DynamicCluster>
|
||||||
find_clusters_with_threshold(NDView<FRAME_TYPE, 2> frame,
|
find_clusters_with_threshold(NDView<FRAME_TYPE, 2> frame,
|
||||||
Pedestal<PEDESTAL_TYPE> &pedestal) {
|
Pedestal<PEDESTAL_TYPE> &pedestal) {
|
||||||
assert(m_threshold > 0);
|
assert(m_threshold > 0);
|
||||||
std::vector<Cluster> clusters;
|
std::vector<DynamicCluster> clusters;
|
||||||
std::vector<std::vector<eventType>> eventMask;
|
std::vector<std::vector<eventType>> eventMask;
|
||||||
for (int i = 0; i < frame.shape(0); i++) {
|
for (int i = 0; i < frame.shape(0); i++) {
|
||||||
eventMask.push_back(std::vector<eventType>(frame.shape(1)));
|
eventMask.push_back(std::vector<eventType>(frame.shape(1)));
|
||||||
@ -230,7 +230,7 @@ class ClusterFinder {
|
|||||||
if (eventMask[iy][ix] == PHOTON &&
|
if (eventMask[iy][ix] == PHOTON &&
|
||||||
frame(iy, ix) - pedestal.mean(iy, ix) >= max) {
|
frame(iy, ix) - pedestal.mean(iy, ix) >= max) {
|
||||||
eventMask[iy][ix] = PHOTON_MAX;
|
eventMask[iy][ix] = PHOTON_MAX;
|
||||||
Cluster cluster(m_cluster_sizeX, m_cluster_sizeY,
|
DynamicCluster cluster(m_cluster_sizeX, m_cluster_sizeY,
|
||||||
Dtype(typeid(FRAME_TYPE)));
|
Dtype(typeid(FRAME_TYPE)));
|
||||||
cluster.x = ix;
|
cluster.x = ix;
|
||||||
cluster.y = iy;
|
cluster.y = iy;
|
||||||
|
@ -41,7 +41,7 @@ namespace aare {
|
|||||||
void assert_failed(const std::string &msg);
|
void assert_failed(const std::string &msg);
|
||||||
|
|
||||||
|
|
||||||
class Cluster {
|
class DynamicCluster {
|
||||||
public:
|
public:
|
||||||
int cluster_sizeX;
|
int cluster_sizeX;
|
||||||
int cluster_sizeY;
|
int cluster_sizeY;
|
||||||
@ -53,36 +53,36 @@ class Cluster {
|
|||||||
std::byte *m_data;
|
std::byte *m_data;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Cluster(int cluster_sizeX_, int cluster_sizeY_,
|
DynamicCluster(int cluster_sizeX_, int cluster_sizeY_,
|
||||||
Dtype dt_ = Dtype(typeid(int32_t)))
|
Dtype dt_ = Dtype(typeid(int32_t)))
|
||||||
: cluster_sizeX(cluster_sizeX_), cluster_sizeY(cluster_sizeY_),
|
: cluster_sizeX(cluster_sizeX_), cluster_sizeY(cluster_sizeY_),
|
||||||
dt(dt_) {
|
dt(dt_) {
|
||||||
m_data = new std::byte[cluster_sizeX * cluster_sizeY * dt.bytes()]{};
|
m_data = new std::byte[cluster_sizeX * cluster_sizeY * dt.bytes()]{};
|
||||||
}
|
}
|
||||||
Cluster() : Cluster(3, 3) {}
|
DynamicCluster() : DynamicCluster(3, 3) {}
|
||||||
Cluster(const Cluster &other)
|
DynamicCluster(const DynamicCluster &other)
|
||||||
: Cluster(other.cluster_sizeX, other.cluster_sizeY, other.dt) {
|
: DynamicCluster(other.cluster_sizeX, other.cluster_sizeY, other.dt) {
|
||||||
if (this == &other)
|
if (this == &other)
|
||||||
return;
|
return;
|
||||||
x = other.x;
|
x = other.x;
|
||||||
y = other.y;
|
y = other.y;
|
||||||
memcpy(m_data, other.m_data, other.bytes());
|
memcpy(m_data, other.m_data, other.bytes());
|
||||||
}
|
}
|
||||||
Cluster &operator=(const Cluster &other) {
|
DynamicCluster &operator=(const DynamicCluster &other) {
|
||||||
if (this == &other)
|
if (this == &other)
|
||||||
return *this;
|
return *this;
|
||||||
this->~Cluster();
|
this->~DynamicCluster();
|
||||||
new (this) Cluster(other);
|
new (this) DynamicCluster(other);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
Cluster(Cluster &&other) noexcept
|
DynamicCluster(DynamicCluster &&other) noexcept
|
||||||
: cluster_sizeX(other.cluster_sizeX),
|
: cluster_sizeX(other.cluster_sizeX),
|
||||||
cluster_sizeY(other.cluster_sizeY), x(other.x), y(other.y),
|
cluster_sizeY(other.cluster_sizeY), x(other.x), y(other.y),
|
||||||
dt(other.dt), m_data(other.m_data) {
|
dt(other.dt), m_data(other.m_data) {
|
||||||
other.m_data = nullptr;
|
other.m_data = nullptr;
|
||||||
other.dt = Dtype(Dtype::TypeIndex::ERROR);
|
other.dt = Dtype(Dtype::TypeIndex::ERROR);
|
||||||
}
|
}
|
||||||
~Cluster() { delete[] m_data; }
|
~DynamicCluster() { delete[] m_data; }
|
||||||
template <typename T> T get(int idx) {
|
template <typename T> T get(int idx) {
|
||||||
(sizeof(T) == dt.bytes())
|
(sizeof(T) == dt.bytes())
|
||||||
? 0
|
? 0
|
||||||
@ -95,10 +95,6 @@ class Cluster {
|
|||||||
: throw std::invalid_argument("[ERROR] Type size mismatch");
|
: throw std::invalid_argument("[ERROR] Type size mismatch");
|
||||||
return memcpy(m_data + idx * dt.bytes(), &val, (size_t)dt.bytes());
|
return memcpy(m_data + idx * dt.bytes(), &val, (size_t)dt.bytes());
|
||||||
}
|
}
|
||||||
// auto x() const { return x; }
|
|
||||||
// auto y() const { return y; }
|
|
||||||
// auto x(int16_t x_) { return x = x_; }
|
|
||||||
// auto y(int16_t y_) { return y = y_; }
|
|
||||||
|
|
||||||
template <typename T> std::string to_string() const {
|
template <typename T> std::string to_string() const {
|
||||||
(sizeof(T) == dt.bytes())
|
(sizeof(T) == dt.bytes())
|
||||||
|
@ -5,6 +5,7 @@ from . import _aare
|
|||||||
from ._aare import File, RawFile, RawMasterFile, RawSubFile
|
from ._aare import File, RawFile, RawMasterFile, RawSubFile
|
||||||
from ._aare import Pedestal, ClusterFinder, VarClusterFinder
|
from ._aare import Pedestal, ClusterFinder, VarClusterFinder
|
||||||
from ._aare import DetectorType
|
from ._aare import DetectorType
|
||||||
|
from ._aare import ClusterFile
|
||||||
|
|
||||||
from .CtbRawFile import CtbRawFile
|
from .CtbRawFile import CtbRawFile
|
||||||
from .ScanParameters import ScanParameters
|
from .ScanParameters import ScanParameters
|
@ -1,95 +1,11 @@
|
|||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import numpy as np
|
import numpy as np
|
||||||
plt.ion()
|
plt.ion()
|
||||||
|
|
||||||
import aare
|
|
||||||
from aare import CtbRawFile
|
|
||||||
print('aare imported')
|
|
||||||
from aare import transform
|
|
||||||
print('transform imported')
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from aare import ClusterFile
|
||||||
|
|
||||||
import json
|
base = Path('~/data/aare_test_data/clusters').expanduser()
|
||||||
|
# f = ClusterFile(base / 'beam_En700eV_-40deg_300V_10us_d0_f0_100.clust')
|
||||||
def decode(frames, rawdata):
|
f = ClusterFile(base / 'single_frame_97_clustrers.clust')
|
||||||
# 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])
|
|
||||||
|
|
||||||
|
|
||||||
base = Path('/mnt/sls_det_storage/matterhorn_data/aare_test_data/ci/aare_test_data')
|
|
||||||
# p = Path(base / 'jungfrau/jungfrau_single_master_0.json')
|
|
||||||
|
|
||||||
# f = aare.File(p)
|
|
||||||
# for i in range(10):
|
|
||||||
# frame = f.read_frame()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# # f2 = aare.CtbRawFile(fpath, transform=transform.matterhorn02)
|
|
||||||
# # header, data = f2.read()
|
|
||||||
# # plt.plot(data[:,0,20,20])
|
|
||||||
# from aare import RawMasterFile, File, RawSubFile, DetectorType, RawFile
|
|
||||||
# base = Path('/mnt/sls_det_storage/matterhorn_data/aare_test_data/Jungfrau10/Jungfrau_DoubleModule_1UDP_ROI/SideBySide/')
|
|
||||||
# fpath = Path('241019_JF_12keV_Si_FF_GaAs_FF_7p88mmFilter_PedestalStart_ZPos_5.5_master_0.json')
|
|
||||||
# raw = Path('241019_JF_12keV_Si_FF_GaAs_FF_7p88mmFilter_PedestalStart_ZPos_5.5_d0_f0_0.raw')
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# m = RawMasterFile(base / fpath)
|
|
||||||
# # roi = m.roi
|
|
||||||
# # rows = roi.ymax-roi.ymin+1
|
|
||||||
# # cols = 1024-roi.xmin
|
|
||||||
# # sf = RawSubFile(base / raw, DetectorType.Jungfrau, rows, cols, 16)
|
|
||||||
|
|
||||||
from aare import RawFile
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
from aare import RawFile, File
|
|
||||||
|
|
||||||
base = Path('/mnt/sls_det_storage/matterhorn_data/aare_test_data/Jungfrau10/Jungfrau_DoubleModule_1UDP_ROI/')
|
|
||||||
fname = base / Path('SideBySide/241019_JF_12keV_Si_FF_GaAs_FF_7p88mmFilter_PedestalStart_ZPos_5.5_master_0.json')
|
|
||||||
# fname = Path(base / 'jungfrau/jungfrau_single_master_0.json')
|
|
||||||
# fname = base / 'Stacked/241024_JF10_m450_m367_KnifeEdge_TestBesom_9keV_750umFilter_PedestalStart_ZPos_-6_master_0.json'
|
|
||||||
|
|
||||||
|
|
||||||
f = RawFile(fname)
|
|
||||||
h,img = f.read_frame()
|
|
||||||
print(f'{h["frameNumber"]}')
|
|
||||||
|
@ -33,20 +33,20 @@ void define_cluster_finder_bindings(py::module &m) {
|
|||||||
return clusters;
|
return clusters;
|
||||||
});
|
});
|
||||||
|
|
||||||
py::class_<Cluster>(m, "Cluster", py::buffer_protocol())
|
py::class_<DynamicCluster>(m, "DynamicCluster", py::buffer_protocol())
|
||||||
.def(py::init<int, int, Dtype>())
|
.def(py::init<int, int, Dtype>())
|
||||||
.def("size", &Cluster::size)
|
.def("size", &DynamicCluster::size)
|
||||||
.def("begin", &Cluster::begin)
|
.def("begin", &DynamicCluster::begin)
|
||||||
.def("end", &Cluster::end)
|
.def("end", &DynamicCluster::end)
|
||||||
.def_readwrite("x", &Cluster::x)
|
.def_readwrite("x", &DynamicCluster::x)
|
||||||
.def_readwrite("y", &Cluster::y)
|
.def_readwrite("y", &DynamicCluster::y)
|
||||||
.def_buffer([](Cluster &c) -> py::buffer_info {
|
.def_buffer([](DynamicCluster &c) -> py::buffer_info {
|
||||||
return py::buffer_info(c.data(), c.dt.bytes(), c.dt.format_descr(),
|
return py::buffer_info(c.data(), c.dt.bytes(), c.dt.format_descr(),
|
||||||
1, {c.size()}, {c.dt.bytes()});
|
1, {c.size()}, {c.dt.bytes()});
|
||||||
})
|
})
|
||||||
|
|
||||||
.def("__repr__", [](const Cluster &a) {
|
.def("__repr__", [](const DynamicCluster &a) {
|
||||||
return "<Cluster: x: " + std::to_string(a.x) +
|
return "<DynamicCluster: x: " + std::to_string(a.x) +
|
||||||
", y: " + std::to_string(a.y) + ">";
|
", y: " + std::to_string(a.y) + ">";
|
||||||
});
|
});
|
||||||
}
|
}
|
34
python/src/cluster_file.hpp
Normal file
34
python/src/cluster_file.hpp
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#include "aare/ClusterFile.hpp"
|
||||||
|
#include "aare/defs.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <pybind11/iostream.h>
|
||||||
|
#include <pybind11/numpy.h>
|
||||||
|
#include <pybind11/pybind11.h>
|
||||||
|
#include <pybind11/stl.h>
|
||||||
|
#include <pybind11/stl/filesystem.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace py = pybind11;
|
||||||
|
using namespace ::aare;
|
||||||
|
|
||||||
|
void define_cluster_file_io_bindings(py::module &m) {
|
||||||
|
PYBIND11_NUMPY_DTYPE(Cluster, x, y, data);
|
||||||
|
|
||||||
|
py::class_<ClusterFile>(m, "ClusterFile")
|
||||||
|
.def(py::init<const std::filesystem::path &>())
|
||||||
|
.def("read_clusters",
|
||||||
|
[](ClusterFile &self, size_t n_clusters) {
|
||||||
|
auto* vec = new std::vector<Cluster>(self.read_clusters(n_clusters));
|
||||||
|
return return_vector(vec);
|
||||||
|
})
|
||||||
|
.def("read_cluster_with_cut",
|
||||||
|
[](ClusterFile &self, size_t n_clusters, py::array_t<double> noise_map, int nx, int ny) {
|
||||||
|
auto view = make_view_2d(noise_map);
|
||||||
|
auto* vec = new std::vector<Cluster>(self.read_cluster_with_cut(n_clusters, view.data(), nx, ny));
|
||||||
|
return return_vector(vec);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
@ -264,7 +264,7 @@ void define_file_io_bindings(py::module &m) {
|
|||||||
|
|
||||||
// .def("close", &ClusterFileV2::close);
|
// .def("close", &ClusterFileV2::close);
|
||||||
|
|
||||||
// m.def("to_clustV2", [](std::vector<Cluster> &clusters, const int
|
// m.def("to_clustV2", [](std::vector<DynamicCluster> &clusters, const int
|
||||||
// frame_number) {
|
// frame_number) {
|
||||||
// std::vector<ClusterV2> clusters_;
|
// std::vector<ClusterV2> clusters_;
|
||||||
// for (auto &c : clusters) {
|
// for (auto &c : clusters) {
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include "pixel_map.hpp"
|
#include "pixel_map.hpp"
|
||||||
#include "pedestal.hpp"
|
#include "pedestal.hpp"
|
||||||
#include "cluster.hpp"
|
#include "cluster.hpp"
|
||||||
|
#include "cluster_file.hpp"
|
||||||
|
|
||||||
//Pybind stuff
|
//Pybind stuff
|
||||||
#include <pybind11/pybind11.h>
|
#include <pybind11/pybind11.h>
|
||||||
@ -22,4 +23,5 @@ PYBIND11_MODULE(_aare, m) {
|
|||||||
define_pedestal_bindings<double>(m, "Pedestal");
|
define_pedestal_bindings<double>(m, "Pedestal");
|
||||||
define_pedestal_bindings<float>(m, "Pedestal_float32");
|
define_pedestal_bindings<float>(m, "Pedestal_float32");
|
||||||
define_cluster_finder_bindings(m);
|
define_cluster_finder_bindings(m);
|
||||||
|
define_cluster_file_io_bindings(m);
|
||||||
}
|
}
|
295
src/ClusterFile.cpp
Normal file
295
src/ClusterFile.cpp
Normal file
@ -0,0 +1,295 @@
|
|||||||
|
#include "aare/ClusterFile.hpp"
|
||||||
|
|
||||||
|
namespace aare {
|
||||||
|
|
||||||
|
ClusterFile::ClusterFile(const std::filesystem::path &fname) {
|
||||||
|
fp = fopen(fname.c_str(), "rb");
|
||||||
|
if (!fp) {
|
||||||
|
throw std::runtime_error("Could not open file: " + fname.string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Cluster> ClusterFile::read_clusters(size_t n_clusters) {
|
||||||
|
std::vector<Cluster> clusters(n_clusters);
|
||||||
|
|
||||||
|
int32_t iframe = 0; // frame number needs to be 4 bytes!
|
||||||
|
|
||||||
|
size_t nph_read = 0;
|
||||||
|
|
||||||
|
// uint32_t nn = *n_left;
|
||||||
|
uint32_t nn = m_num_left;
|
||||||
|
// uint32_t nph = *n_left; // number of clusters in frame needs to be 4
|
||||||
|
// bytes!
|
||||||
|
uint32_t nph = m_num_left;
|
||||||
|
|
||||||
|
auto buf = reinterpret_cast<Cluster *>(clusters.data());
|
||||||
|
// if there are photons left from previous frame read them first
|
||||||
|
if (nph) {
|
||||||
|
if (nph > n_clusters) {
|
||||||
|
// if we have more photons left in the frame then photons to read we
|
||||||
|
// read directly the requested number
|
||||||
|
nn = n_clusters;
|
||||||
|
} else {
|
||||||
|
nn = nph;
|
||||||
|
}
|
||||||
|
nph_read += fread((void *)(buf + nph_read), sizeof(Cluster), nn, fp);
|
||||||
|
m_num_left = nph - nn; // write back the number of photons left
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nph_read < n_clusters) {
|
||||||
|
// keep on reading frames and photons until reaching n_clusters
|
||||||
|
while (fread(&iframe, sizeof(iframe), 1, fp)) {
|
||||||
|
// read number of clusters in frame
|
||||||
|
if (fread(&nph, sizeof(nph), 1, fp)) {
|
||||||
|
if (nph > (n_clusters - nph_read))
|
||||||
|
nn = n_clusters - nph_read;
|
||||||
|
else
|
||||||
|
nn = nph;
|
||||||
|
|
||||||
|
nph_read +=
|
||||||
|
fread((void *)(buf + nph_read), sizeof(Cluster), nn, fp);
|
||||||
|
m_num_left = nph - nn;
|
||||||
|
}
|
||||||
|
if (nph_read >= n_clusters)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resize the vector to the number of clusters.
|
||||||
|
// No new allocation, only change bounds.
|
||||||
|
clusters.resize(nph_read);
|
||||||
|
return clusters;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Cluster> ClusterFile::read_cluster_with_cut(size_t n_clusters,
|
||||||
|
double *noise_map,
|
||||||
|
int nx, int ny) {
|
||||||
|
|
||||||
|
std::vector<Cluster> clusters(n_clusters);
|
||||||
|
// size_t read_clusters_with_cut(FILE *fp, size_t n_clusters, Cluster *buf,
|
||||||
|
// uint32_t *n_left, double *noise_map, int
|
||||||
|
// nx, int ny) {
|
||||||
|
int iframe = 0;
|
||||||
|
// uint32_t nph = *n_left;
|
||||||
|
uint32_t nph = m_num_left;
|
||||||
|
// uint32_t nn = *n_left;
|
||||||
|
uint32_t nn = m_num_left;
|
||||||
|
size_t nph_read = 0;
|
||||||
|
|
||||||
|
int32_t t2max, tot1;
|
||||||
|
int32_t tot3;
|
||||||
|
// Cluster *ptr = buf;
|
||||||
|
Cluster *ptr = clusters.data();
|
||||||
|
int good = 1;
|
||||||
|
double noise;
|
||||||
|
// read photons left from previous frame
|
||||||
|
if (noise_map)
|
||||||
|
printf("Using noise map\n");
|
||||||
|
|
||||||
|
if (nph) {
|
||||||
|
if (nph > n_clusters) {
|
||||||
|
// if we have more photons left in the frame then photons to
|
||||||
|
// read we read directly the requested number
|
||||||
|
nn = n_clusters;
|
||||||
|
} else {
|
||||||
|
nn = nph;
|
||||||
|
}
|
||||||
|
for (size_t iph = 0; iph < nn; iph++) {
|
||||||
|
// read photons 1 by 1
|
||||||
|
size_t n_read = fread((void *)(ptr), sizeof(Cluster), 1, fp);
|
||||||
|
if (n_read != 1) {
|
||||||
|
clusters.resize(nph_read);
|
||||||
|
return clusters;
|
||||||
|
}
|
||||||
|
// TODO! error handling on read
|
||||||
|
good = 1;
|
||||||
|
if (noise_map) {
|
||||||
|
if (ptr->x >= 0 && ptr->x < nx && ptr->y >= 0 && ptr->y < ny) {
|
||||||
|
tot1 = ptr->data[4];
|
||||||
|
analyze_cluster(*ptr, &t2max, &tot3, NULL, NULL, NULL, NULL,
|
||||||
|
NULL);
|
||||||
|
noise = noise_map[ptr->y * nx + ptr->x];
|
||||||
|
if (tot1 > noise || t2max > 2 * noise || tot3 > 3 * noise) {
|
||||||
|
;
|
||||||
|
} else {
|
||||||
|
good = 0;
|
||||||
|
printf("%d %d %f %d %d %d\n", ptr->x, ptr->y, noise,
|
||||||
|
tot1, t2max, tot3);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printf("Bad pixel number %d %d\n", ptr->x, ptr->y);
|
||||||
|
good = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (good) {
|
||||||
|
ptr++;
|
||||||
|
nph_read++;
|
||||||
|
}
|
||||||
|
(m_num_left)--;
|
||||||
|
if (nph_read >= n_clusters)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nph_read < n_clusters) {
|
||||||
|
// // keep on reading frames and photons until reaching n_clusters
|
||||||
|
while (fread(&iframe, sizeof(iframe), 1, fp)) {
|
||||||
|
// // printf("%d\n",nph_read);
|
||||||
|
|
||||||
|
if (fread(&nph, sizeof(nph), 1, fp)) {
|
||||||
|
// // printf("** %d\n",nph);
|
||||||
|
m_num_left = nph;
|
||||||
|
for (size_t iph = 0; iph < nph; iph++) {
|
||||||
|
// // read photons 1 by 1
|
||||||
|
size_t n_read =
|
||||||
|
fread((void *)(ptr), sizeof(Cluster), 1, fp);
|
||||||
|
if (n_read != 1) {
|
||||||
|
clusters.resize(nph_read);
|
||||||
|
return clusters;
|
||||||
|
// return nph_read;
|
||||||
|
}
|
||||||
|
good = 1;
|
||||||
|
if (noise_map) {
|
||||||
|
if (ptr->x >= 0 && ptr->x < nx && ptr->y >= 0 &&
|
||||||
|
ptr->y < ny) {
|
||||||
|
tot1 = ptr->data[4];
|
||||||
|
analyze_cluster(*ptr, &t2max, &tot3, NULL,
|
||||||
|
NULL,
|
||||||
|
NULL, NULL, NULL);
|
||||||
|
// noise = noise_map[ptr->y * nx + ptr->x];
|
||||||
|
noise = noise_map[ptr->y + ny * ptr->x];
|
||||||
|
if (tot1 > noise || t2max > 2 * noise ||
|
||||||
|
tot3 > 3 * noise) {
|
||||||
|
;
|
||||||
|
} else
|
||||||
|
good = 0;
|
||||||
|
} else {
|
||||||
|
printf("Bad pixel number %d %d\n", ptr->x,
|
||||||
|
ptr->y); good = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (good) {
|
||||||
|
ptr++;
|
||||||
|
nph_read++;
|
||||||
|
}
|
||||||
|
(m_num_left)--;
|
||||||
|
if (nph_read >= n_clusters)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nph_read >= n_clusters)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// printf("%d\n",nph_read);
|
||||||
|
clusters.resize(nph_read);
|
||||||
|
return clusters;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int ClusterFile::analyze_cluster(Cluster cl, int32_t *t2, int32_t *t3, char *quad,
|
||||||
|
double *eta2x, double *eta2y, double *eta3x,
|
||||||
|
double *eta3y) {
|
||||||
|
|
||||||
|
return analyze_data(cl.data, t2, t3, quad, eta2x, eta2y, eta3x, eta3y);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ClusterFile::analyze_data(int32_t *data, int32_t *t2, int32_t *t3, char *quad,
|
||||||
|
double *eta2x, double *eta2y, double *eta3x, double *eta3y) {
|
||||||
|
|
||||||
|
int ok = 1;
|
||||||
|
|
||||||
|
int32_t tot2[4];
|
||||||
|
int32_t t2max = 0;
|
||||||
|
char c = 0;
|
||||||
|
int32_t val, tot3;
|
||||||
|
|
||||||
|
tot3 = 0;
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
tot2[i] = 0;
|
||||||
|
|
||||||
|
for (int ix = 0; ix < 3; ix++) {
|
||||||
|
for (int iy = 0; iy < 3; iy++) {
|
||||||
|
val = data[iy * 3 + ix];
|
||||||
|
// printf ("%d ",data[iy * 3 + ix]);
|
||||||
|
tot3 += val;
|
||||||
|
if (ix <= 1 && iy <= 1)
|
||||||
|
tot2[cBottomLeft] += val;
|
||||||
|
if (ix >= 1 && iy <= 1)
|
||||||
|
tot2[cBottomRight] += val;
|
||||||
|
if (ix <= 1 && iy >= 1)
|
||||||
|
tot2[cTopLeft] += val;
|
||||||
|
if (ix >= 1 && iy >= 1)
|
||||||
|
tot2[cTopRight] += val;
|
||||||
|
}
|
||||||
|
// printf ("\n");
|
||||||
|
}
|
||||||
|
// printf ("\n");
|
||||||
|
|
||||||
|
if (t2 || quad) {
|
||||||
|
|
||||||
|
t2max = tot2[0];
|
||||||
|
c = cBottomLeft;
|
||||||
|
for (int i = 1; i < 4; i++) {
|
||||||
|
if (tot2[i] > t2max) {
|
||||||
|
t2max = tot2[i];
|
||||||
|
c = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//printf("*** %d %d %d %d -- %d\n",tot2[0],tot2[1],tot2[2],tot2[3],t2max);
|
||||||
|
if (quad)
|
||||||
|
*quad = c;
|
||||||
|
if (t2)
|
||||||
|
*t2 = t2max;
|
||||||
|
}
|
||||||
|
if (t3)
|
||||||
|
*t3 = tot3;
|
||||||
|
|
||||||
|
if (eta2x || eta2y) {
|
||||||
|
if (eta2x)
|
||||||
|
*eta2x = 0;
|
||||||
|
if (eta2y)
|
||||||
|
*eta2y = 0;
|
||||||
|
switch (c) {
|
||||||
|
case cBottomLeft:
|
||||||
|
if (eta2x && (data[3] + data[4]) != 0)
|
||||||
|
*eta2x = (double)(data[4]) / (data[3] + data[4]);
|
||||||
|
if (eta2y && (data[1] + data[4]) != 0)
|
||||||
|
*eta2y = (double)(data[4]) / (data[1] + data[4]);
|
||||||
|
break;
|
||||||
|
case cBottomRight:
|
||||||
|
if (eta2x && (data[2] + data[5]) != 0)
|
||||||
|
*eta2x = (double)(data[5]) / (data[4] + data[5]);
|
||||||
|
if (eta2y && (data[1] + data[4]) != 0)
|
||||||
|
*eta2y = (double)(data[4]) / (data[1] + data[4]);
|
||||||
|
break;
|
||||||
|
case cTopLeft:
|
||||||
|
if (eta2x && (data[7] + data[4]) != 0)
|
||||||
|
*eta2x = (double)(data[4]) / (data[3] + data[4]);
|
||||||
|
if (eta2y && (data[7] + data[4]) != 0)
|
||||||
|
*eta2y = (double)(data[7]) / (data[7] + data[4]);
|
||||||
|
break;
|
||||||
|
case cTopRight:
|
||||||
|
if (eta2x && t2max != 0)
|
||||||
|
*eta2x = (double)(data[5]) / (data[5] + data[4]);
|
||||||
|
if (eta2y && t2max != 0)
|
||||||
|
*eta2y = (double)(data[7]) / (data[7] + data[4]);
|
||||||
|
break;
|
||||||
|
default:;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (eta3x || eta3y) {
|
||||||
|
if (eta3x && (data[3] + data[4] + data[5]) != 0)
|
||||||
|
*eta3x = (double)(-data[3] + data[3 + 2]) /
|
||||||
|
(data[3] + data[4] + data[5]);
|
||||||
|
if (eta3y && (data[1] + data[4] + data[7]) != 0)
|
||||||
|
*eta3y = (double)(-data[1] + data[2 * 3 + 1]) /
|
||||||
|
(data[1] + data[4] + data[7]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace aare
|
@ -9,14 +9,14 @@ TEST_CASE("Enum to string conversion") {
|
|||||||
REQUIRE(ToString(aare::DetectorType::Jungfrau) == "Jungfrau");
|
REQUIRE(ToString(aare::DetectorType::Jungfrau) == "Jungfrau");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Cluster creation") {
|
TEST_CASE("DynamicCluster creation") {
|
||||||
aare::Cluster c(13, 15);
|
aare::DynamicCluster c(13, 15);
|
||||||
REQUIRE(c.cluster_sizeX == 13);
|
REQUIRE(c.cluster_sizeX == 13);
|
||||||
REQUIRE(c.cluster_sizeY == 15);
|
REQUIRE(c.cluster_sizeY == 15);
|
||||||
REQUIRE(c.dt == aare::Dtype(typeid(int32_t)));
|
REQUIRE(c.dt == aare::Dtype(typeid(int32_t)));
|
||||||
REQUIRE(c.data() != nullptr);
|
REQUIRE(c.data() != nullptr);
|
||||||
|
|
||||||
aare::Cluster c2(c);
|
aare::DynamicCluster c2(c);
|
||||||
REQUIRE(c2.cluster_sizeX == 13);
|
REQUIRE(c2.cluster_sizeX == 13);
|
||||||
REQUIRE(c2.cluster_sizeY == 15);
|
REQUIRE(c2.cluster_sizeY == 15);
|
||||||
REQUIRE(c2.dt == aare::Dtype(typeid(int32_t)));
|
REQUIRE(c2.dt == aare::Dtype(typeid(int32_t)));
|
||||||
@ -25,7 +25,7 @@ TEST_CASE("Cluster creation") {
|
|||||||
|
|
||||||
// TEST_CASE("cluster set and get data") {
|
// TEST_CASE("cluster set and get data") {
|
||||||
|
|
||||||
// aare::Cluster c2(33, 44, aare::Dtype(typeid(double)));
|
// aare::DynamicCluster c2(33, 44, aare::Dtype(typeid(double)));
|
||||||
// REQUIRE(c2.cluster_sizeX == 33);
|
// REQUIRE(c2.cluster_sizeX == 33);
|
||||||
// REQUIRE(c2.cluster_sizeY == 44);
|
// REQUIRE(c2.cluster_sizeY == 44);
|
||||||
// REQUIRE(c2.dt == aare::Dtype::DOUBLE);
|
// REQUIRE(c2.dt == aare::Dtype::DOUBLE);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user