35 std::optional<HighFive::File> file;
36 std::optional<HighFive::DataSet> dataset;
37 std::optional<HighFive::DataType> datatype;
38 DetectorType detector{DetectorType::Reserved};
39 ReadoutType readout{ReadoutType::CAEN};
42 RL_API DetectorType detector_type()
const {
return detector;}
43 RL_API
void detector_type(
const DetectorType type) {detector = type;}
44 RL_API ReadoutType readout_type()
const {
return readout;}
45 RL_API
void readout_type(
const ReadoutType type) {readout = type;}
46 RL_API
void verbose(
const int v) {verbosity = v;}
49 const std::string & filename,
50 const DetectorType detectorType,
51 const ReadoutType readoutType,
52 const std::string & dataset_name =
"events"
54 : filename{filename}, detector{detectorType}, readout{readoutType} {
56 file = HighFive::File(filename, HighFive::File::OpenOrCreate);
57 }
catch (HighFive::Exception & ex) {
58 std::cout <<
"Error opening file " << filename <<
" for writing:\n" << ex.what();
64 auto dataspace = HighFive::DataSpace({0}, {HighFive::DataSpace::UNLIMITED});
66 HighFive::DataSetCreateProps props;
67 props.add(HighFive::Chunking(std::vector<hsize_t>{100}));
69 auto hc_type = hdf_compound_type();
71 hc_type.commit(file.value(), readoutType_name(readout));
72 dataset = file.value().createDataSet(dataset_name, dataspace, hc_type, props);
78 auto u8str = [](
const auto * p){
return std::string(
reinterpret_cast<const char *
>(p));};
79 file->createAttribute<std::string>(
"program",
"libreadout");
80 file->createAttribute<std::string>(
"version", u8str(libreadout::version::version_number));
81 file->createAttribute<std::string>(
"revision", u8str(libreadout::version::git_revision));
82 file->createAttribute<std::string>(
"events", dataset_name);
83 dataset->createAttribute(
"detector", detectorType_name(detector));
84 dataset->createAttribute(
"readout", readoutType_name(readout));
88 RL_API
void saveReadout(
const uint8_t Ring,
const uint8_t FEN,
const double tof,
const double weight,
const void * data){
89 if (!dataset.has_value()){
90 if (verbosity > 1) std::cout <<
"No readout saved to file due to no dataset available" << std::endl;
93 switch (readoutType_from_detectorType(detector)) {
101 default:
throw std::runtime_error(
"This readout data type not implemented yet!");
106 if (file.has_value() and dataset.has_value() and datatype.has_value()) {
107 auto & ds = dataset.value();
109 auto pos = ds.getDimensions().back();
112 ds.select({pos}, {1}).write_raw(
reinterpret_cast<const uint8_t *
>(&data), datatype.value());
117 HighFive::CompoundType hdf_compound_type()
const {
118 return ::hdf_compound_type(readout);
RL_API void saveReadout(const uint8_t Ring, const uint8_t FEN, const double tof, const double weight, const void *data)
Append one event; data must point to the readout struct matching the configured ReadoutType.