formatted my changes

This commit is contained in:
2025-06-11 15:13:35 +02:00
parent d89530ed22
commit cc57cc7c27
11 changed files with 203 additions and 214 deletions

View File

@ -21,20 +21,15 @@ class H5Handles {
std::vector<hsize_t> offset;
public:
H5Handles(const std::string& fname, const std::string& dname):
file_name(fname),
dataset_name(dname),
file(fname, H5F_ACC_RDONLY),
dataset(file.openDataSet(dname)),
dataspace(dataset.getSpace()),
H5Handles(const std::string &fname, const std::string &dname)
: file_name(fname), dataset_name(dname), file(fname, H5F_ACC_RDONLY),
dataset(file.openDataSet(dname)), dataspace(dataset.getSpace()),
datatype(dataset.getDataType()) {
intialize_dimensions();
initialize_memspace();
}
std::vector<hsize_t> get_dims() const {
return dims;
}
std::vector<hsize_t> get_dims() const { return dims; }
void seek(size_t frame_index) {
if (frame_index >= dims[0]) {
@ -43,7 +38,8 @@ class H5Handles {
offset[0] = static_cast<hsize_t>(frame_index);
}
void get_data_into(size_t frame_index, std::byte *frame_buffer, size_t n_frames = 1) {
void get_data_into(size_t frame_index, std::byte *frame_buffer,
size_t n_frames = 1) {
seek(frame_index);
count[0] = static_cast<hsize_t>(n_frames);
// std::cout << "offset:" << ToString(offset) << " count:" <<
@ -52,7 +48,8 @@ class H5Handles {
dataset.read(frame_buffer, datatype, *memspace, dataspace);
}
void get_header_into(size_t frame_index, int part_index, std::byte *header_buffer) {
void get_header_into(size_t frame_index, int part_index,
std::byte *header_buffer) {
seek(frame_index);
offset[1] = static_cast<hsize_t>(part_index);
// std::cout << "offset:" << ToString(offset) << " count:" <<
@ -85,13 +82,12 @@ class H5Handles {
hsize_t dims_image[2] = {dims[1], dims[2]};
memspace = std::make_unique<H5::DataSpace>(2, dims_image);
} else {
throw std::runtime_error(LOCATION + "Invalid rank for dataset: " + std::to_string(rank));
throw std::runtime_error(
LOCATION + "Invalid rank for dataset: " + std::to_string(rank));
}
}
};
template <typename Fn>
void read_hdf5_header_fields(DetectorHeader *header, Fn &&fn_read_field) {
fn_read_field(0, reinterpret_cast<std::byte *>(&(header->frameNumber)));

View File

@ -6,8 +6,6 @@
#include <fstream>
#include <optional>
namespace aare {
using ns = std::chrono::nanoseconds;
@ -40,7 +38,6 @@ class Hdf5FileNameComponents {
void set_old_scheme(bool old_scheme);
};
/**
* @brief Class for parsing a master file either in our .json format or the old
* .Hdf5 format
@ -89,17 +86,12 @@ class Hdf5MasterFile {
std::optional<std::vector<ns>> m_exptime_array{};
std::optional<std::vector<ns>> m_gate_delay_array{};
std::optional<int> m_gates{};
std::optional<std::map<std::string, std::string>> m_additional_json_header{};
std::optional<std::map<std::string, std::string>>
m_additional_json_header{};
size_t m_frames_in_file{};
// TODO! should these be bool?
public:
Hdf5MasterFile(const std::filesystem::path &fpath);
@ -147,11 +139,11 @@ class Hdf5MasterFile {
std::optional<std::vector<ns>> exptime_array() const;
std::optional<std::vector<ns>> gate_delay_array() const;
std::optional<int> gates() const;
std::optional<std::map<std::string, std::string>> additional_json_header() const;
std::optional<std::map<std::string, std::string>>
additional_json_header() const;
size_t frames_in_file() const;
size_t n_modules() const;
private:
static const std::string metadata_group_name;
void parse_acquisition_metadata(const std::filesystem::path &fpath);

View File

@ -39,7 +39,6 @@ class RawFileNameComponents {
void set_old_scheme(bool old_scheme);
};
/**
* @brief Class for parsing a master file either in our .json format or the old
* .raw format

View File

@ -224,7 +224,6 @@ struct ROI {
using dynamic_shape = std::vector<ssize_t>;
class ScanParameters {
bool m_enabled = false;
std::string m_dac;
@ -292,28 +291,37 @@ enum class DetectorType {
enum class TimingMode { Auto, Trigger };
enum class FrameDiscardPolicy { NoDiscard, Discard, DiscardPartial };
enum class BurstMode { Burst_Interal, Burst_External, Continuous_Internal,
Continuous_External };
enum class BurstMode {
Burst_Interal,
Burst_External,
Continuous_Internal,
Continuous_External
};
/** ToString and StringTo Conversions */
// generic
template <class T, typename = std::enable_if_t<!is_duration<T>::value>>
std::string ToString(T arg) { return T(arg); }
std::string ToString(T arg) {
return T(arg);
}
template <typename T, std::enable_if_t<
!is_duration<T>::value && !is_container<T>::value, int> = 0 >
T StringTo(const std::string &arg) { return T(arg); }
template <typename T,
std::enable_if_t<!is_duration<T>::value && !is_container<T>::value,
int> = 0>
T StringTo(const std::string &arg) {
return T(arg);
}
// time
std::string RemoveUnit(std::string &str);
inline void TrimWhiteSpaces(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
[](unsigned char c) { return !std::isspace(c); }));
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char c) {
return !std::isspace(c);
}));
s.erase(std::find_if(s.rbegin(), s.rend(),
[](unsigned char c) { return !std::isspace(c); }).base(),
[](unsigned char c) { return !std::isspace(c); })
.base(),
s.end());
}
@ -342,11 +350,11 @@ template <typename From>
typename std::enable_if<is_duration<From>::value, std::string>::type
ToString(From t) {
using std::chrono::duration_cast;
using std::chrono::abs;
using std::chrono::nanoseconds;
using std::chrono::duration_cast;
using std::chrono::microseconds;
using std::chrono::milliseconds;
using std::chrono::nanoseconds;
auto tns = duration_cast<nanoseconds>(t);
if (abs(tns) < microseconds(1)) {
return ToString(tns, "ns");
@ -384,7 +392,8 @@ T StringTo(const std::string &t, const std::string &unit) {
} else if (unit == "s" || unit.empty()) {
return duration_cast<T>(std::chrono::duration<double>(tval));
} else {
throw std::invalid_argument("[ERROR] Invalid unit in conversion from string to std::chrono::duration");
throw std::invalid_argument("[ERROR] Invalid unit in conversion from "
"string to std::chrono::duration");
}
}
@ -449,8 +458,6 @@ template <> inline int StringTo(const std::string &s) {
return std::stoull(s, nullptr, base);
}*/
// vector
template <typename T> std::string ToString(const std::vector<T> &vec) {
std::ostringstream oss;
@ -469,19 +476,21 @@ std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
return os << ToString(v);
}
template <
typename Container,
template <typename Container,
std::enable_if_t<is_container<Container>::value &&
!is_std_string_v<Container> /*&&
!is_map_v<Container>*/,
!is_map_v<Container>*/
,
int> = 0>
Container StringTo(const std::string &s) {
using Value = typename Container::value_type;
// strip outer brackets
std::string str = s;
str.erase(std::remove_if(str.begin(), str.end(),
[](unsigned char c){ return c=='[' || c==']'; }), str.end());
str.erase(
std::remove_if(str.begin(), str.end(),
[](unsigned char c) { return c == '[' || c == ']'; }),
str.end());
std::stringstream ss(str);
std::string item;
@ -496,7 +505,6 @@ Container StringTo(const std::string& s) {
return result;
}
// map
template <typename KeyType, typename ValueType>
std::string ToString(const std::map<KeyType, ValueType> &m) {
@ -515,7 +523,8 @@ std::string ToString(const std::map<KeyType, ValueType> &m) {
return os.str();
}
template <> inline std::map<std::string, std::string> StringTo(const std::string &s) {
template <>
inline std::map<std::string, std::string> StringTo(const std::string &s) {
std::map<std::string, std::string> result;
std::string str = s;
@ -543,7 +552,6 @@ template <> inline std::map<std::string, std::string> StringTo(const std::string
return result;
}
// optional
template <class T> std::string ToString(const std::optional<T> &opt) {
return opt ? ToString(*opt) : "nullopt";
@ -571,14 +579,12 @@ template <> std::string ToString(FrameDiscardPolicy arg);
template <> BurstMode StringTo(const std::string & /*mode*/);
template <> std::string ToString(BurstMode arg);
std::ostream &operator<<(std::ostream &os,
const ScanParameters &r);
std::ostream &operator<<(std::ostream &os, const ScanParameters &r);
template <> std::string ToString(ScanParameters arg);
std::ostream &operator<<(std::ostream &os, const ROI &roi);
template <> std::string ToString(ROI arg);
using DataTypeVariants = std::variant<uint16_t, uint32_t>;
} // namespace aare

View File

@ -49,7 +49,6 @@ struct is_container<
decltype(std::declval<T>().empty())>,
void>::type> : public std::true_type {};
/**
* Type trait to evaluate if template parameter is
* complying with a std::string
@ -62,8 +61,7 @@ inline constexpr bool is_std_string_v =
* Type trait to evaluate if template parameter is
* complying with std::map
*/
template <typename T>
struct is_map : std::false_type {};
template <typename T> struct is_map : std::false_type {};
template <typename K, typename V, typename... Args>
struct is_map<std::map<K, V, Args...>> : std::true_type {};
@ -71,4 +69,4 @@ struct is_map<std::map<K, V, Args...>> : std::true_type {};
template <typename T>
inline constexpr bool is_map_v = is_map<std::decay_t<T>>::value;
} // namsespace aare
} // namespace aare

View File

@ -129,10 +129,10 @@ void Hdf5File::get_data_into(size_t frame_index, std::byte *frame_buffer,
void Hdf5File::get_header_into(size_t frame_index, int part_index,
DetectorHeader *header) {
try {
read_hdf5_header_fields(header,
[&](size_t iParameter, std::byte *dest) {
m_header_datasets[iParameter]->get_header_into(
frame_index, part_index, dest);
read_hdf5_header_fields(header, [&](size_t iParameter,
std::byte *dest) {
m_header_datasets[iParameter]->get_header_into(frame_index,
part_index, dest);
});
LOG(logDEBUG5) << "Read 1D header for frame " << frame_index;
} catch (const H5::Exception &e) {
@ -183,20 +183,21 @@ const std::vector<std::string> Hdf5File::header_dataset_names = {
"detector specific 4",
"detector type",
"detector header version",
"packets caught bit mask"
};
"packets caught bit mask"};
void Hdf5File::open_data_file() {
if (m_mode != "r")
throw std::runtime_error(LOCATION +
"Unsupported mode. Can only read Hdf5 files.");
try {
m_data_dataset = std::make_unique<H5Handles>(m_master.master_fname().string(), metadata_group_name + "/data");
m_data_dataset = std::make_unique<H5Handles>(
m_master.master_fname().string(), metadata_group_name + "/data");
m_total_frames = m_data_dataset->get_dims()[0];
m_rows = m_data_dataset->get_dims()[1];
m_cols = m_data_dataset->get_dims()[2];
//fmt::print("Data Dataset dimensions: frames = {}, rows = {}, cols = {}\n",
// fmt::print("Data Dataset dimensions: frames = {}, rows = {}, cols =
// {}\n",
// m_total_frames, m_rows, m_cols);
} catch (const H5::Exception &e) {
m_data_dataset.reset();
@ -213,8 +214,12 @@ void Hdf5File::open_header_files() {
"Unsupported mode. Can only read Hdf5 files.");
try {
for (size_t i = 0; i != header_dataset_names.size(); ++i) {
m_header_datasets.push_back(std::make_unique<H5Handles>(m_master.master_fname().string(), metadata_group_name + header_dataset_names[i]));
LOG(logDEBUG) << header_dataset_names[i] << " Dataset dimensions: size = " << m_header_datasets[i]->get_dims()[0];
m_header_datasets.push_back(std::make_unique<H5Handles>(
m_master.master_fname().string(),
metadata_group_name + header_dataset_names[i]));
LOG(logDEBUG) << header_dataset_names[i]
<< " Dataset dimensions: size = "
<< m_header_datasets[i]->get_dims()[0];
}
} catch (const H5::Exception &e) {
m_header_datasets.clear();

View File

@ -1,7 +1,7 @@
#include "aare/Hdf5MasterFile.hpp"
#include "aare/logger.hpp"
#include <sstream>
#include <iomanip>
#include <sstream>
namespace aare {
Hdf5FileNameComponents::Hdf5FileNameComponents(
@ -106,12 +106,8 @@ std::optional<ScanParameters> Hdf5MasterFile::scan_parameters() const {
size_t Hdf5MasterFile::total_frames_expected() const {
return m_total_frames_expected;
}
std::optional<ns> Hdf5MasterFile::exptime() const {
return m_exptime;
}
std::optional<ns> Hdf5MasterFile::period() const {
return m_period;
}
std::optional<ns> Hdf5MasterFile::exptime() const { return m_exptime; }
std::optional<ns> Hdf5MasterFile::period() const { return m_period; }
std::optional<BurstMode> Hdf5MasterFile::burst_mode() const {
return m_burst_mode;
}
@ -119,22 +115,15 @@ std::optional<int> Hdf5MasterFile::number_of_udp_interfaces() const {
return m_number_of_udp_interfaces;
}
int Hdf5MasterFile::bitdepth() const { return m_bitdepth; }
std::optional<bool> Hdf5MasterFile::ten_giga() const {
return m_ten_giga;
}
std::optional<bool> Hdf5MasterFile::ten_giga() const { return m_ten_giga; }
std::optional<int> Hdf5MasterFile::threshold_energy() const {
return m_threshold_energy;
}
std::optional<std::vector<int>>
Hdf5MasterFile::threshold_energy_all() const {
std::optional<std::vector<int>> Hdf5MasterFile::threshold_energy_all() const {
return m_threshold_energy_all;
}
std::optional<ns> Hdf5MasterFile::subexptime() const {
return m_subexptime;
}
std::optional<ns> Hdf5MasterFile::subperiod() const {
return m_subperiod;
}
std::optional<ns> Hdf5MasterFile::subexptime() const { return m_subexptime; }
std::optional<ns> Hdf5MasterFile::subperiod() const { return m_subperiod; }
std::optional<bool> Hdf5MasterFile::quad() const { return m_quad; }
std::optional<int> Hdf5MasterFile::number_of_rows() const {
return m_number_of_rows;
@ -151,9 +140,7 @@ bool Hdf5MasterFile::digital_flag() const { return m_digital_flag; }
std::optional<int> Hdf5MasterFile::digital_samples() const {
return m_digital_samples;
}
std::optional<int> Hdf5MasterFile::dbit_offset() const {
return m_dbit_offset;
}
std::optional<int> Hdf5MasterFile::dbit_offset() const { return m_dbit_offset; }
std::optional<size_t> Hdf5MasterFile::dbit_list() const { return m_dbit_list; }
std::optional<int> Hdf5MasterFile::transceiver_mask() const {
return m_transceiver_mask;
@ -164,7 +151,9 @@ std::optional<int> Hdf5MasterFile::transceiver_samples() const {
}
// g1 roi
std::optional<ROI> Hdf5MasterFile::roi() const { return m_roi; }
std::optional<int> Hdf5MasterFile::counter_mask() const { return m_counter_mask; }
std::optional<int> Hdf5MasterFile::counter_mask() const {
return m_counter_mask;
}
std::optional<std::vector<ns>> Hdf5MasterFile::exptime_array() const {
return m_exptime_array;
}
@ -172,7 +161,8 @@ std::optional<std::vector<ns>> Hdf5MasterFile::gate_delay_array() const {
return m_gate_delay_array;
}
std::optional<int> Hdf5MasterFile::gates() const { return m_gates; }
std::optional<std::map<std::string, std::string>> Hdf5MasterFile::additional_json_header() const {
std::optional<std::map<std::string, std::string>>
Hdf5MasterFile::additional_json_header() const {
return m_additional_json_header;
}
size_t Hdf5MasterFile::frames_in_file() const { return m_frames_in_file; }
@ -253,7 +243,6 @@ void Hdf5MasterFile::parse_acquisition_metadata(
file, std::string(metadata_group_name + "Geometry in x axis"));
LOG(logDEBUG) << "Geometry: " << m_geometry.to_string();
// Image Size
m_image_size_in_bytes = h5_get_scalar_dataset<int>(
file, std::string(metadata_group_name + "Image Size"));
@ -281,7 +270,8 @@ void Hdf5MasterFile::parse_acquisition_metadata(
StringTo<FrameDiscardPolicy>(h5_get_scalar_dataset<std::string>(
file,
std::string(metadata_group_name + "Frame Discard Policy")));
LOG(logDEBUG) << "Frame Discard Policy: " << ToString(m_frame_discard_policy);
LOG(logDEBUG) << "Frame Discard Policy: "
<< ToString(m_frame_discard_policy);
// Frame Padding
m_frame_padding = h5_get_scalar_dataset<int>(
@ -378,15 +368,15 @@ void Hdf5MasterFile::parse_acquisition_metadata(
// Threshold All Energy
try {
m_threshold_energy_all = StringTo<std::vector<int>>(
h5_get_scalar_dataset<std::string>(
m_threshold_energy_all =
StringTo<std::vector<int>>(h5_get_scalar_dataset<std::string>(
file,
std::string(metadata_group_name + "Threshold Energies")));
LOG(logDEBUG) << "Threshold Energies: "
<< ToString(m_threshold_energy_all);
} catch (H5::FileIException &e) {
std::cout << "No Threshold Energies found in file: "
<< fpath << std::endl;
std::cout << "No Threshold Energies found in file: " << fpath
<< std::endl;
// keep the optional empty
}
@ -545,7 +535,8 @@ void Hdf5MasterFile::parse_acquisition_metadata(
file, std::string(metadata_group_name + "receiver roi ymax"));
// if any of the values are set update the roi
if (tmp_roi.xmin != 4294967295 || tmp_roi.xmax != 4294967295 || tmp_roi.ymin != 4294967295 || tmp_roi.ymax != 4294967295) {
if (tmp_roi.xmin != 4294967295 || tmp_roi.xmax != 4294967295 ||
tmp_roi.ymin != 4294967295 || tmp_roi.ymax != 4294967295) {
// why?? TODO
if (dVersion < 6.61) {
tmp_roi.xmax++;
@ -588,8 +579,8 @@ void Hdf5MasterFile::parse_acquisition_metadata(
// Exposure Time Array
try {
m_exptime_array = StringTo<std::vector<ns>>(
h5_get_scalar_dataset<std::string>(
m_exptime_array =
StringTo<std::vector<ns>>(h5_get_scalar_dataset<std::string>(
file, std::string(metadata_group_name + "Exposure Times")));
LOG(logDEBUG) << "Exposure Times: " << ToString(m_exptime_array);
} catch (H5::FileIException &e) {
@ -598,8 +589,8 @@ void Hdf5MasterFile::parse_acquisition_metadata(
// Gate Delay Array
try {
m_gate_delay_array = StringTo<std::vector<ns>>(
h5_get_scalar_dataset<std::string>(
m_gate_delay_array =
StringTo<std::vector<ns>>(h5_get_scalar_dataset<std::string>(
file, std::string(metadata_group_name + "Gate Delays")));
LOG(logDEBUG) << "Gate Delays: " << ToString(m_gate_delay_array);
} catch (H5::FileIException &e) {
@ -617,9 +608,13 @@ void Hdf5MasterFile::parse_acquisition_metadata(
// Additional Json Header
try {
m_additional_json_header = StringTo<std::map<std::string, std::string>>(h5_get_scalar_dataset<std::string>(
file, std::string(metadata_group_name + "Additional JSON Header")));
LOG(logDEBUG) << "Additional JSON Header: " << ToString(m_additional_json_header);
m_additional_json_header =
StringTo<std::map<std::string, std::string>>(
h5_get_scalar_dataset<std::string>(
file, std::string(metadata_group_name +
"Additional JSON Header")));
LOG(logDEBUG) << "Additional JSON Header: "
<< ToString(m_additional_json_header);
} catch (H5::FileIException &e) {
// keep the optional empty
}

View File

@ -61,7 +61,6 @@ const std::string &RawFileNameComponents::base_name() const {
const std::string &RawFileNameComponents::ext() const { return m_ext; }
int RawFileNameComponents::file_index() const { return m_file_index; }
RawMasterFile::RawMasterFile(const std::filesystem::path &fpath)
: m_fnc(fpath) {
if (!std::filesystem::exists(fpath)) {

View File

@ -1,7 +1,7 @@
#include "aare/defs.hpp"
#include <chrono>
#include <stdexcept>
#include <string>
#include <chrono>
#include <fmt/core.h>
namespace aare {
@ -180,7 +180,8 @@ template <> BurstMode StringTo(const std::string &arg) {
return BurstMode::Continuous_Internal;
if (arg == "continuous_external")
return BurstMode::Continuous_External;
throw std::runtime_error("Could not decode burst mode from: \"" + arg + "\"");
throw std::runtime_error("Could not decode burst mode from: \"" + arg +
"\"");
}
/**
@ -196,7 +197,8 @@ template <> std::string ToString(ScanParameters arg) {
<< "dac " << arg.dac() << std::endl
<< "start " << arg.start() << std::endl
<< "stop " << arg.stop() << std::endl
<< "step " << arg.step() << std::endl
<< "step " << arg.step()
<< std::endl
//<< "settleTime "
// << ToString(std::chrono::nanoseconds{arg.dacSettleTime_ns})
<< std::endl;
@ -207,12 +209,10 @@ template <> std::string ToString(ScanParameters arg) {
return oss.str();
}
std::ostream &operator<<(std::ostream &os,
const ScanParameters &r) {
std::ostream &operator<<(std::ostream &os, const ScanParameters &r) {
return os << ToString(r);
}
/**
* @brief Convert a ROI to a string
* @param type ROI
@ -245,7 +245,6 @@ std::string RemoveUnit(std::string &str) {
return unit;
}
// template <> TimingMode StringTo<TimingMode>(std::string mode);
} // namespace aare