fire v0.19.0
Framework for sImulation and Reconstruction of Events
ParameterStorage.h
1#ifndef FIRE_IO_H5_PARAMETERSTORAGE_H
2#define FIRE_IO_H5_PARAMETERSTORAGE_H
3
4// STL
5#include <boost/core/demangle.hpp>
6#include <iostream>
7#include <map>
8#include <string>
9#include <variant>
10
11#include "fire/io/Data.h"
12#include "fire/io/Reader.h"
13#include "fire/exception/Exception.h"
14
15namespace fire::io {
16
41 public:
59 template <typename ParameterType>
60 const ParameterType& get(const std::string& name) const {
61 try {
62 return std::get<ParameterType>(parameters_.at(name));
63 } catch (const std::bad_variant_access&) {
64 throw Exception("BadType",
65 "Parameter named " + name + " is not type " +
66 boost::core::demangle(typeid(ParameterType).name()));
67 } catch (const std::out_of_range&) {
68 throw Exception("NotFound","Parameter named " + name + " not found.");
69 }
70 }
71
94 template <typename ParameterType>
95 void set(const std::string& name, const ParameterType& val) {
96 static_assert(
97 std::is_same_v<ParameterType, int> ||
98 std::is_same_v<ParameterType, float> ||
99 std::is_same_v<ParameterType, std::string>,
100 "Parameters are only allowed to be float, int, or std::string.");
101 parameters_[name] = val;
102 }
103
113 void clear();
114
115 private:
117 friend class Data<ParameterStorage>;
118
124};
125
138template <>
139class Data<ParameterStorage> : public AbstractData<ParameterStorage> {
140 public:
152 Data(const std::string& path, Reader* input_file, ParameterStorage* handle);
153
175 void load(h5::Reader& r) final override;
176
177#ifdef fire_USE_ROOT
185 void load(root::Reader&) final override {
186 throw Exception("BadCode","This function should never be called.");
187 }
188#endif
189
203 void save(Writer& w) final override;
204
205 void structure(Writer& w) final override;
206
207 private:
224 template <typename ParameterType>
225 void attach(const std::string& name) {
226 parameters_[name] = std::make_unique<Data<ParameterType>>(
227 this->path_ + "/" + name,
229 std::get_if<ParameterType>(&(this->handle_->parameters_[name])));
230 }
231
232 private:
237};
238
239}
240
241#endif
Standard base exception class with some useful output information.
Definition: Exception.h:18
Type-specific base class to hold common data methods.
Definition: AbstractData.h:96
DataType * handle_
handle on current object in memory
Definition: AbstractData.h:237
std::string path_
path of data set
Definition: AbstractData.h:82
std::unordered_map< std::string, std::unique_ptr< BaseData > > parameters_
the dynamic parameter listing (parallel to parameters_ member variable)
Definition: ParameterStorage.h:234
void attach(const std::string &name)
Attach the input parameter name as a new dataset in our own parameter map using the entry in the hand...
Definition: ParameterStorage.h:225
Reader * input_file_
the input file we are reading from
Definition: ParameterStorage.h:236
General data set.
Definition: Data.h:90
void save(Writer &f) final override
pure virtual method for saving data
Definition: Data.h:166
Reader * input_file_
pointer to the input file (if there is one)
Definition: Data.h:236
void load(h5::Reader &f) final override
Loading this dataset from the file involves simply loading all of the members of the data type.
Definition: Data.h:135
Data(const std::string &path, Reader *input_file=nullptr, DataType *handle=nullptr)
Attach ourselves to the input type after construction.
Definition: Data.h:116
void structure(Writer &f) final override
pure virtual method for saving structure
Definition: Data.h:170
Provides dynamic parameter storage by interfacing between a map to variants storing parameters and a ...
Definition: ParameterStorage.h:40
std::unordered_map< std::string, std::variant< int, float, std::string > > parameters_
three types of parameters are allowed: int, float, string
Definition: ParameterStorage.h:123
void clear()
clear the parameters
Definition: ParameterStorage.cxx:6
void set(const std::string &name, const ParameterType &val)
Set a parameter to be a specific value.
Definition: ParameterStorage.h:95
const ParameterType & get(const std::string &name) const
Get a parameter corresponding to the input name.
Definition: ParameterStorage.h:60
Prototype for reading files within fire.
Definition: Reader.h:29
Write the fire DataSets into a deterministic structure in the output HDF5 data file.
Definition: Writer.h:19
Reading a file generated by fire.
Definition: Reader.h:22
Reading ROOT files into our data structures.
Definition: Reader.h:29
Geant4 does a GLOBAL definition of the keyword TRUE.
Definition: AbstractData.h:11