This commit is contained in:
froejdh_e
2024-12-11 09:54:33 +01:00
parent 7f2a23d5b1
commit 60534add92
8 changed files with 198 additions and 124 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include "aare/defs.hpp"
#include "aare/ClusterVector.hpp"
#include <filesystem>
#include <fstream>
@ -38,30 +39,40 @@ struct ClusterAnalysis {
double etay;
};
/*
Binary cluster file. Expects data to be layed out as:
int32_t frame_number
uint32_t number_of_clusters
int16_t x, int16_t y, int32_t data[9] x number_of_clusters
int32_t frame_number
uint32_t number_of_clusters
....
*/
class ClusterFile {
FILE *fp{};
uint32_t m_num_left{};
size_t m_chunk_size{};
const std::string m_mode;
public:
ClusterFile(const std::filesystem::path &fname, size_t chunk_size = 1000);
ClusterFile(const std::filesystem::path &fname, size_t chunk_size = 1000,
const std::string &mode = "r");
~ClusterFile();
std::vector<Cluster> read_clusters(size_t n_clusters);
std::vector<Cluster> read_frame(int32_t &out_fnum);
void write_frame(int32_t frame_number, const ClusterVector<int32_t>& 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);
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);
double *eta2x, double *eta2y, double *eta3x,
double *eta3y);
size_t chunk_size() const { return m_chunk_size; }
void close();
};
} // namespace aare

View File

@ -6,14 +6,25 @@
#include <fmt/core.h>
template <typename T> class ClusterVector {
int32_t m_cluster_size_x;
int32_t m_cluster_size_y;
using value_type = T;
using coord_t = int16_t;
coord_t m_cluster_size_x;
coord_t m_cluster_size_y;
std::byte *m_data{};
size_t m_size{0};
size_t m_capacity;
/*
Format string used in the python bindings to create a numpy
array from the buffer
= - native byte order
h - short
d - double
i - int
*/
constexpr static char m_fmt_base[] = "=h:x:\nh:y:\n({},{}){}:data:" ;
public:
ClusterVector(int32_t cluster_size_x, int32_t cluster_size_y,
ClusterVector(coord_t cluster_size_x, coord_t cluster_size_y,
size_t capacity = 1024)
: m_cluster_size_x(cluster_size_x), m_cluster_size_y(cluster_size_y),
m_capacity(capacity) {
@ -25,15 +36,8 @@ template <typename T> class ClusterVector {
delete[] m_data;
}
// ClusterVector(const ClusterVector & other){
// m_cluster_size_x = other.m_cluster_size_x;
// m_cluster_size_y = other.m_cluster_size_y;
// m_data = new std::byte[other.m_capacity];
// std::copy(other.m_data, other.m_data + other.m_capacity, m_data);
// m_size = other.m_size;
// m_capacity = other.m_capacity;
// }
//Move constructor
ClusterVector(ClusterVector &&other) noexcept
: m_cluster_size_x(other.m_cluster_size_x),
m_cluster_size_y(other.m_cluster_size_y), m_data(other.m_data),
@ -66,15 +70,15 @@ template <typename T> class ClusterVector {
}
// data better hold data of the right size!
void push_back(int32_t x, int32_t y, const std::byte *data) {
void push_back(coord_t x, coord_t y, const std::byte *data) {
if (m_size == m_capacity) {
allocate_buffer(m_capacity * 2);
}
std::byte *ptr = element_ptr(m_size);
*reinterpret_cast<int32_t *>(ptr) = x;
ptr += sizeof(int32_t);
*reinterpret_cast<int32_t *>(ptr) = y;
ptr += sizeof(int32_t);
*reinterpret_cast<coord_t *>(ptr) = x;
ptr += sizeof(coord_t);
*reinterpret_cast<coord_t *>(ptr) = y;
ptr += sizeof(coord_t);
std::copy(data, data + m_cluster_size_x * m_cluster_size_y * sizeof(T),
ptr);
@ -85,7 +89,7 @@ template <typename T> class ClusterVector {
std::vector<T> sums(m_size);
const size_t stride = element_offset();
const size_t n_pixels = m_cluster_size_x * m_cluster_size_y;
std::byte *ptr = m_data + 2 * sizeof(int32_t); // skip x and y
std::byte *ptr = m_data + 2 * sizeof(coord_t); // skip x and y
for (size_t i = 0; i < m_size; i++) {
sums[i] =
@ -109,6 +113,12 @@ template <typename T> class ClusterVector {
int16_t cluster_size_y() const { return m_cluster_size_y; }
std::byte *data() { return m_data; }
const std::byte *data() const { return m_data; }
const std::string_view fmt_base() const {
//TODO! how do we match on coord_t?
return m_fmt_base;
}
private:
void allocate_buffer(size_t new_capacity) {

View File

@ -44,6 +44,7 @@ class File {
void read_into(std::byte *image_buf);
void read_into(std::byte *image_buf, size_t n_frames);
size_t frame_number(); //!< get the frame number at the current position
size_t frame_number(size_t frame_index); //!< get the frame number at the given frame index
size_t bytes_per_frame() const;
size_t pixels_per_frame() const;