fire v0.19.0
Framework for sImulation and Reconstruction of Events
AbstractData.h
1#ifndef FIRE_IO_ABSTRACTDATA_H
2#define FIRE_IO_ABSTRACTDATA_H
3
4#include <string>
5
6#include <boost/core/demangle.hpp> // for demangling
7
8#include "fire/version/Version.h"
10
11namespace fire::io {
12
13class Writer;
14class Reader;
15namespace h5 { class Reader; }
16#ifdef fire_USE_ROOT
17namespace root { class Reader; }
18#endif
19
30class BaseData {
31 public:
37 explicit BaseData(const std::string& path) : path_{path} {}
38
42 virtual ~BaseData() = default;
43
49 virtual void load(h5::Reader& f) = 0;
50
51#ifdef fire_USE_ROOT
57 virtual void load(root::Reader& f) = 0;
58#endif
59
65 virtual void save(Writer& f) = 0;
66
73 virtual void structure(Writer& f) = 0;
74
78 virtual void clear() = 0;
79
80 protected:
83
84};
85
95template <typename DataType>
96class AbstractData : public BaseData {
97 public:
115 explicit AbstractData(const std::string& path, Reader* input_file = nullptr,
116 DataType* handle = nullptr);
117
124 virtual ~AbstractData() {
125 if (owner_) delete handle_;
126 }
127
133 virtual void load(h5::Reader& f) = 0;
134
135#ifdef fire_USE_ROOT
141 virtual void load(root::Reader& f) = 0;
142#endif
143
149 virtual void save(Writer& f) = 0;
150
155 virtual void structure(Writer& f) = 0;
156
177 virtual void clear() {
178 if (owner_) {
181 } else {
182 handle_->clear();
183 }
184 }
185 }
186
195 virtual const DataType& get() const { return *handle_; }
196
205 int version() const {
206 return load_type_.value_or(save_type_).second;
207 }
208
226 virtual void update(const DataType& val) {
227 *handle_ = val;
228 }
229
230 protected:
232 std::optional<std::pair<std::string,int>> load_type_;
235
237 DataType* handle_;
239 bool owner_;
240}; // AbstractData
241
242}
243
244#endif
Serialization class version deduction.
Type-specific base class to hold common data methods.
Definition: AbstractData.h:96
std::pair< std::string, int > save_type_
type this data that is being used to write
Definition: AbstractData.h:234
std::optional< std::pair< std::string, int > > load_type_
type this data is loading from
Definition: AbstractData.h:232
AbstractData(const std::string &path, Reader *input_file=nullptr, DataType *handle=nullptr)
Define the dataset path and provide an optional handle.
Definition: Data.h:38
virtual void clear()
Define the clear function here to handle the most common cases.
Definition: AbstractData.h:177
virtual void structure(Writer &f)=0
pure virtual method for saving structure
virtual void update(const DataType &val)
Update the in-memory data object with the passed value.
Definition: AbstractData.h:226
virtual void load(h5::Reader &f)=0
pure virtual method for loading data
virtual ~AbstractData()
Delete our object if we own it, otherwise do nothing.
Definition: AbstractData.h:124
int version() const
Get the version number for the type we are loading from.
Definition: AbstractData.h:205
virtual void save(Writer &f)=0
pure virtual method for saving data
bool owner_
we own the object in memory
Definition: AbstractData.h:239
virtual const DataType & get() const
Get the current in-memory data.
Definition: AbstractData.h:195
DataType * handle_
handle on current object in memory
Definition: AbstractData.h:237
Empty data base allowing recursion.
Definition: AbstractData.h:30
virtual void clear()=0
pure virtual method for resetting the current data to a blank state
virtual void load(h5::Reader &f)=0
pure virtual method for loading data from the input file
virtual void save(Writer &f)=0
pure virtual method for saving the current data
BaseData(const std::string &path)
Define the full in-file path to the data set of this data.
Definition: AbstractData.h:37
std::string path_
path of data set
Definition: AbstractData.h:82
virtual ~BaseData()=default
virtual destructor so inherited classes can be properly destructed.
virtual void structure(Writer &f)=0
we should persist our hierarchy into the output file
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