Developer (#102)

This commit is contained in:
Erik Fröjdh 2024-11-21 10:27:26 +01:00 committed by GitHub
commit 8f729fc83e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 57 additions and 47 deletions

View File

@ -241,6 +241,7 @@ target_compile_options(
-Wextra -Wextra
-pedantic -pedantic
-Wshadow -Wshadow
-Wold-style-cast
-Wnon-virtual-dtor -Wnon-virtual-dtor
-Woverloaded-virtual -Woverloaded-virtual
-Wdouble-promotion -Wdouble-promotion
@ -413,7 +414,7 @@ endif()
add_custom_target( add_custom_target(
clang-tidy clang-tidy
COMMAND find \( -path "./src/*" -a -not -path "./src/python/*" -a \( -name "*.cpp" -not -name "*.test.cpp"\) \) -not -name "CircularFifo.hpp" -not -name "ProducerConsumerQueue.hpp" -not -name "VariableSizeClusterFinder.hpp" | xargs -I {} -n 1 -P 10 bash -c "${CLANG_TIDY_COMMAND} --config-file=.clang-tidy -p build {}" COMMAND find \( -path "./src/*" -a -not -path "./src/python/*" -a \( -name "*.cpp" -not -name "*.test.cpp" \) \) -not -name "CircularFifo.hpp" -not -name "ProducerConsumerQueue.hpp" -not -name "VariableSizeClusterFinder.hpp" | xargs -I {} -n 1 -P 10 bash -c "${CLANG_TIDY_COMMAND} --config-file=.clang-tidy -p build {}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "linting with clang-tidy" COMMENT "linting with clang-tidy"
VERBATIM VERBATIM

View File

@ -47,6 +47,7 @@ class ClusterFile {
public: public:
ClusterFile(const std::filesystem::path &fname, size_t chunk_size = 1000); ClusterFile(const std::filesystem::path &fname, size_t chunk_size = 1000);
~ClusterFile();
std::vector<Cluster> read_clusters(size_t n_clusters); std::vector<Cluster> read_clusters(size_t n_clusters);
std::vector<Cluster> read_frame(int32_t &out_fnum); std::vector<Cluster> read_frame(int32_t &out_fnum);
std::vector<Cluster> std::vector<Cluster>
@ -59,6 +60,7 @@ class ClusterFile {
double *eta3y); double *eta3y);
size_t chunk_size() const { return m_chunk_size; } size_t chunk_size() const { return m_chunk_size; }
void close();
}; };

View File

@ -61,14 +61,14 @@ class ScanParameters {
struct ROI{ struct ROI{
size_t xmin{}; int64_t xmin{};
size_t xmax{}; int64_t xmax{};
size_t ymin{}; int64_t ymin{};
size_t ymax{}; int64_t ymax{};
size_t height() const { return ymax - ymin; } int64_t height() const { return ymax - ymin; }
size_t width() const { return xmax - xmin; } int64_t width() const { return xmax - xmin; }
}__attribute__((packed)); };
/** /**
@ -91,7 +91,7 @@ class RawMasterFile {
xy m_geometry; xy m_geometry;
size_t m_max_frames_per_file{}; size_t m_max_frames_per_file{};
uint32_t m_adc_mask{}; // uint32_t m_adc_mask{}; // TODO! implement reading
FrameDiscardPolicy m_frame_discard_policy{}; FrameDiscardPolicy m_frame_discard_policy{};
size_t m_frame_padding{}; size_t m_frame_padding{};

View File

@ -16,6 +16,7 @@ namespace aare {
class RawSubFile { class RawSubFile {
protected: protected:
std::ifstream m_file; std::ifstream m_file;
DetectorType m_detector_type;
size_t m_bitdepth; size_t m_bitdepth;
std::filesystem::path m_fname; std::filesystem::path m_fname;
size_t m_rows{}; size_t m_rows{};
@ -25,7 +26,7 @@ class RawSubFile {
uint32_t m_pos_row{}; uint32_t m_pos_row{};
uint32_t m_pos_col{}; uint32_t m_pos_col{};
DetectorType m_detector_type;
std::optional<NDArray<ssize_t, 2>> m_pixel_map; std::optional<NDArray<ssize_t, 2>> m_pixel_map;
public: public:

View File

@ -226,7 +226,7 @@ template <typename T> void VarClusterFinder<T>::single_pass(NDView<T, 2> img) {
template <typename T> void VarClusterFinder<T>::first_pass() { template <typename T> void VarClusterFinder<T>::first_pass() {
for (int i = 0; i < original_.size(); ++i) { for (size_t i = 0; i < original_.size(); ++i) {
if (use_noise_map) if (use_noise_map)
threshold_ = 5 * noiseMap(i); threshold_ = 5 * noiseMap(i);
binary_(i) = (original_(i) > threshold_); binary_(i) = (original_(i) > threshold_);
@ -250,17 +250,17 @@ template <typename T> void VarClusterFinder<T>::first_pass() {
template <typename T> void VarClusterFinder<T>::second_pass() { template <typename T> void VarClusterFinder<T>::second_pass() {
for (int64_t i = 0; i != labeled_.size(); ++i) { for (size_t i = 0; i != labeled_.size(); ++i) {
auto current_label = labeled_(i); auto cl = labeled_(i);
if (current_label != 0) { if (cl != 0) {
auto it = child.find(current_label); auto it = child.find(cl);
while (it != child.end()) { while (it != child.end()) {
current_label = it->second; cl = it->second;
it = child.find(current_label); it = child.find(cl);
// do this once before doing the second pass? // do this once before doing the second pass?
// all values point to the final one... // all values point to the final one...
} }
labeled_(i) = current_label; labeled_(i) = cl;
} }
} }
} }
@ -271,7 +271,7 @@ template <typename T> void VarClusterFinder<T>::store_clusters() {
// Do we always have monotonic increasing // Do we always have monotonic increasing
// labels? Then vector? // labels? Then vector?
// here the translation is label -> Hit // here the translation is label -> Hit
std::unordered_map<int, Hit> h_size; std::unordered_map<int, Hit> h_map;
for (int i = 0; i < shape_[0]; ++i) { for (int i = 0; i < shape_[0]; ++i) {
for (int j = 0; j < shape_[1]; ++j) { for (int j = 0; j < shape_[1]; ++j) {
if (labeled_(i, j) != 0 || false if (labeled_(i, j) != 0 || false
@ -280,7 +280,7 @@ template <typename T> void VarClusterFinder<T>::store_clusters() {
// (i+1 < shape_[0] and labeled_(i+1, j) != 0) or // (i+1 < shape_[0] and labeled_(i+1, j) != 0) or
// (j+1 < shape_[1] and labeled_(i, j+1) != 0) // (j+1 < shape_[1] and labeled_(i, j+1) != 0)
) { ) {
Hit &record = h_size[labeled_(i, j)]; Hit &record = h_map[labeled_(i, j)];
if (record.size < MAX_CLUSTER_SIZE) { if (record.size < MAX_CLUSTER_SIZE) {
record.rows[record.size] = i; record.rows[record.size] = i;
record.cols[record.size] = j; record.cols[record.size] = j;
@ -300,7 +300,7 @@ template <typename T> void VarClusterFinder<T>::store_clusters() {
} }
} }
for (const auto &h : h_size) for (const auto &h : h_map)
hits.push_back(h.second); hits.push_back(h.second);
} }

View File

@ -93,7 +93,7 @@ class DynamicCluster {
(sizeof(T) == dt.bytes()) (sizeof(T) == dt.bytes())
? 0 ? 0
: 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, dt.bytes());
} }
template <typename T> std::string to_string() const { template <typename T> std::string to_string() const {

View File

@ -37,7 +37,7 @@ void define_cluster_file_io_bindings(py::module &m) {
return return_vector(vec); return return_vector(vec);
}) })
.def("__enter__", [](ClusterFile &self) { return &self; }) .def("__enter__", [](ClusterFile &self) { return &self; })
.def("__exit__", [](ClusterFile &self, py::args args) { return; }) .def("__exit__", [](ClusterFile &self) { self.close();})
.def("__iter__", [](ClusterFile &self) { return &self; }) .def("__iter__", [](ClusterFile &self) { return &self; })
.def("__next__", [](ClusterFile &self) { .def("__next__", [](ClusterFile &self) {
auto vec = new std::vector<Cluster>(self.read_clusters(self.chunk_size())); auto vec = new std::vector<Cluster>(self.read_clusters(self.chunk_size()));

View File

@ -194,7 +194,7 @@ void define_file_io_bindings(py::module &m) {
return fmt::format("<ROI: xmin: {} xmax: {} ymin: {} ymax: {}>", self.xmin, self.xmax, self.ymin, self.ymax); return fmt::format("<ROI: xmin: {} xmax: {} ymin: {} ymax: {}>", self.xmin, self.xmax, self.ymin, self.ymax);
}) })
.def("__iter__", [](const ROI &self) { .def("__iter__", [](const ROI &self) {
return py::make_iterator(&self.xmin, &self.ymax+1); return py::make_iterator(&self.xmin, &self.ymax+1); //NOLINT
}); });

View File

@ -25,7 +25,6 @@ void define_raw_file_io_bindings(py::module &m) {
.def(py::init<const std::filesystem::path &>()) .def(py::init<const std::filesystem::path &>())
.def("read_frame", .def("read_frame",
[](RawFile &self) { [](RawFile &self) {
size_t image_size = self.bytes_per_frame();
py::array image; py::array image;
std::vector<ssize_t> shape; std::vector<ssize_t> shape;
shape.reserve(2); shape.reserve(2);

View File

@ -9,6 +9,17 @@ ClusterFile::ClusterFile(const std::filesystem::path &fname, size_t chunk_size):
} }
} }
ClusterFile::~ClusterFile() {
close();
}
void ClusterFile::close(){
if (fp){
fclose(fp);
fp = nullptr;
}
}
std::vector<Cluster> ClusterFile::read_clusters(size_t n_clusters) { std::vector<Cluster> ClusterFile::read_clusters(size_t n_clusters) {
std::vector<Cluster> clusters(n_clusters); std::vector<Cluster> clusters(n_clusters);
@ -27,7 +38,7 @@ std::vector<Cluster> ClusterFile::read_clusters(size_t n_clusters) {
} else { } else {
nn = nph; nn = nph;
} }
nph_read += fread((void *)(buf + nph_read), sizeof(Cluster), nn, fp); nph_read += fread(reinterpret_cast<void *>(buf + nph_read), sizeof(Cluster), nn, fp);
m_num_left = nph - nn; // write back the number of photons left m_num_left = nph - nn; // write back the number of photons left
} }
@ -42,7 +53,7 @@ std::vector<Cluster> ClusterFile::read_clusters(size_t n_clusters) {
nn = nph; nn = nph;
nph_read += nph_read +=
fread((void *)(buf + nph_read), sizeof(Cluster), nn, fp); fread(reinterpret_cast<void *>(buf + nph_read), sizeof(Cluster), nn, fp);
m_num_left = nph - nn; m_num_left = nph - nn;
} }
if (nph_read >= n_clusters) if (nph_read >= n_clusters)
@ -65,13 +76,13 @@ std::vector<Cluster> ClusterFile::read_frame(int32_t &out_fnum) {
throw std::runtime_error("Could not read frame number"); throw std::runtime_error("Could not read frame number");
} }
int n_clusters; int32_t n_clusters; // Saved as 32bit integer in the cluster file
if (fread(&n_clusters, sizeof(n_clusters), 1, fp) != 1) { if (fread(&n_clusters, sizeof(n_clusters), 1, fp) != 1) {
throw std::runtime_error("Could not read number of clusters"); throw std::runtime_error("Could not read number of clusters");
} }
std::vector<Cluster> clusters(n_clusters); std::vector<Cluster> clusters(n_clusters);
if (fread(clusters.data(), sizeof(Cluster), n_clusters, fp) != n_clusters) { if (fread(clusters.data(), sizeof(Cluster), n_clusters, fp) != static_cast<size_t>(n_clusters)) {
throw std::runtime_error("Could not read clusters"); throw std::runtime_error("Could not read clusters");
} }
return clusters; return clusters;
@ -113,7 +124,7 @@ std::vector<Cluster> ClusterFile::read_cluster_with_cut(size_t n_clusters,
} }
for (size_t iph = 0; iph < nn; iph++) { for (size_t iph = 0; iph < nn; iph++) {
// read photons 1 by 1 // read photons 1 by 1
size_t n_read = fread((void *)(ptr), sizeof(Cluster), 1, fp); size_t n_read = fread(reinterpret_cast<void *>(ptr), sizeof(Cluster), 1, fp);
if (n_read != 1) { if (n_read != 1) {
clusters.resize(nph_read); clusters.resize(nph_read);
return clusters; return clusters;
@ -158,7 +169,7 @@ std::vector<Cluster> ClusterFile::read_cluster_with_cut(size_t n_clusters,
for (size_t iph = 0; iph < nph; iph++) { for (size_t iph = 0; iph < nph; iph++) {
// // read photons 1 by 1 // // read photons 1 by 1
size_t n_read = size_t n_read =
fread((void *)(ptr), sizeof(Cluster), 1, fp); fread(reinterpret_cast<void *>(ptr), sizeof(Cluster), 1, fp);
if (n_read != 1) { if (n_read != 1) {
clusters.resize(nph_read); clusters.resize(nph_read);
return clusters; return clusters;
@ -269,27 +280,27 @@ int ClusterFile::analyze_data(int32_t *data, int32_t *t2, int32_t *t3, char *qua
switch (c) { switch (c) {
case cBottomLeft: case cBottomLeft:
if (eta2x && (data[3] + data[4]) != 0) if (eta2x && (data[3] + data[4]) != 0)
*eta2x = (double)(data[4]) / (data[3] + data[4]); *eta2x = static_cast<double>(data[4]) / (data[3] + data[4]);
if (eta2y && (data[1] + data[4]) != 0) if (eta2y && (data[1] + data[4]) != 0)
*eta2y = (double)(data[4]) / (data[1] + data[4]); *eta2y = static_cast<double>(data[4]) / (data[1] + data[4]);
break; break;
case cBottomRight: case cBottomRight:
if (eta2x && (data[2] + data[5]) != 0) if (eta2x && (data[2] + data[5]) != 0)
*eta2x = (double)(data[5]) / (data[4] + data[5]); *eta2x = static_cast<double>(data[5]) / (data[4] + data[5]);
if (eta2y && (data[1] + data[4]) != 0) if (eta2y && (data[1] + data[4]) != 0)
*eta2y = (double)(data[4]) / (data[1] + data[4]); *eta2y = static_cast<double>(data[4]) / (data[1] + data[4]);
break; break;
case cTopLeft: case cTopLeft:
if (eta2x && (data[7] + data[4]) != 0) if (eta2x && (data[7] + data[4]) != 0)
*eta2x = (double)(data[4]) / (data[3] + data[4]); *eta2x = static_cast<double>(data[4]) / (data[3] + data[4]);
if (eta2y && (data[7] + data[4]) != 0) if (eta2y && (data[7] + data[4]) != 0)
*eta2y = (double)(data[7]) / (data[7] + data[4]); *eta2y = static_cast<double>(data[7]) / (data[7] + data[4]);
break; break;
case cTopRight: case cTopRight:
if (eta2x && t2max != 0) if (eta2x && t2max != 0)
*eta2x = (double)(data[5]) / (data[5] + data[4]); *eta2x = static_cast<double>(data[5]) / (data[5] + data[4]);
if (eta2y && t2max != 0) if (eta2y && t2max != 0)
*eta2y = (double)(data[7]) / (data[7] + data[4]); *eta2y = static_cast<double>(data[7]) / (data[7] + data[4]);
break; break;
default:; default:;
} }
@ -297,10 +308,10 @@ int ClusterFile::analyze_data(int32_t *data, int32_t *t2, int32_t *t3, char *qua
if (eta3x || eta3y) { if (eta3x || eta3y) {
if (eta3x && (data[3] + data[4] + data[5]) != 0) if (eta3x && (data[3] + data[4] + data[5]) != 0)
*eta3x = (double)(-data[3] + data[3 + 2]) / *eta3x = static_cast<double>(-data[3] + data[3 + 2]) /
(data[3] + data[4] + data[5]); (data[3] + data[4] + data[5]);
if (eta3y && (data[1] + data[4] + data[7]) != 0) if (eta3y && (data[1] + data[4] + data[7]) != 0)
*eta3y = (double)(-data[1] + data[2 * 3 + 1]) / *eta3y = static_cast<double>(-data[1] + data[2 * 3 + 1]) /
(data[1] + data[4] + data[7]); (data[1] + data[4] + data[7]);
} }

View File

@ -99,10 +99,7 @@ void RawFile::open_subfiles() {
for (size_t i = 0; i != n_subfiles; ++i) { for (size_t i = 0; i != n_subfiles; ++i) {
auto v = std::vector<RawSubFile *>(n_subfile_parts); auto v = std::vector<RawSubFile *>(n_subfile_parts);
for (size_t j = 0; j != n_subfile_parts; ++j) { for (size_t j = 0; j != n_subfile_parts; ++j) {
fmt::print("{} pos: {},{}\n", j,positions[j].row, positions[j].col);
auto pos = m_module_pixel_0[j]; auto pos = m_module_pixel_0[j];
fmt::print("{} pos: {},{}\n", j,pos.y, pos.x);
v[j] = new RawSubFile(m_master.data_fname(j, i), v[j] = new RawSubFile(m_master.data_fname(j, i),
m_master.detector_type(), pos.height, m_master.detector_type(), pos.height,
pos.width, m_master.bitdepth(), pos.width, m_master.bitdepth(),
@ -349,7 +346,7 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer, Detect
if(header) if(header)
++header; ++header;
for (size_t cur_row = 0; cur_row < (pos.height); for (size_t cur_row = 0; cur_row < static_cast<size_t>(pos.height);
cur_row++) { cur_row++) {
auto irow = (pos.y + cur_row); auto irow = (pos.y + cur_row);

View File

@ -9,8 +9,7 @@ namespace aare {
RawSubFile::RawSubFile(const std::filesystem::path &fname, RawSubFile::RawSubFile(const std::filesystem::path &fname,
DetectorType detector, size_t rows, size_t cols, DetectorType detector, size_t rows, size_t cols,
size_t bitdepth, uint32_t pos_row, uint32_t pos_col) size_t bitdepth, uint32_t pos_row, uint32_t pos_col)
: m_bitdepth(bitdepth), m_fname(fname), m_rows(rows), m_cols(cols), : m_detector_type(detector), m_bitdepth(bitdepth), m_fname(fname), m_rows(rows), m_cols(cols),
m_detector_type(detector),
m_bytes_per_frame((m_bitdepth / 8) * m_rows * m_cols), m_pos_row(pos_row), m_pos_col(pos_col) { m_bytes_per_frame((m_bitdepth / 8) * m_rows * m_cols), m_pos_row(pos_row), m_pos_col(pos_col) {
if (m_detector_type == DetectorType::Moench03_old) { if (m_detector_type == DetectorType::Moench03_old) {
m_pixel_map = GenerateMoench03PixelMap(); m_pixel_map = GenerateMoench03PixelMap();