fire v0.19.0
Framework for sImulation and Reconstruction of Events
Reader.h
1#ifndef FIRE_IO_H5_READER_H
2#define FIRE_IO_H5_READER_H
3
4// using HighFive
5#include <highfive/H5File.hpp>
6
7#include "fire/io/Reader.h"
8#include "fire/io/Atomic.h"
9
10namespace fire::io::h5 {
11
22class Reader : public ::fire::io::Reader {
23 public:
49 Reader(const std::string& name);
50
60 virtual void load_into(BaseData& d) final override;
61
73
86 virtual std::pair<std::string,int> type(const std::string& path) final override;
87
92 virtual std::string name() const final override;
93
104 std::vector<std::string> list(const std::string& group_path) const;
105
115 HighFive::DataType getDataSetType(const std::string& dataset) const;
116
122 HighFive::ObjectType getH5ObjectType(const std::string& path) const;
123
136 std::string getTypeName(const std::string& obj_name) const;
137
145 inline std::size_t entries() const final override { return entries_; }
146
154 inline std::size_t runs() const final override { return runs_; }
155
160 virtual bool canCopy() const final override { return true; }
161
174 virtual void copy(unsigned long int i_entry, const std::string& path,
175 Writer& output) final override;
176
194 template <typename AtomicType>
195 void load(const std::string& path, AtomicType& val) {
196 static_assert(
197 is_atomic_v<AtomicType>,
198 "Type not supported by HighFive atomic made its way to Reader::load");
199 if (buffers_.find(path) == buffers_.end()) {
200 // first load attempt, we will find out if dataset exists in file here
201 buffers_.emplace(path, std::make_unique<Buffer<AtomicType>>(
202 rows_per_chunk_, file_.getDataSet(path)));
203 }
204
205 dynamic_cast<Buffer<AtomicType>&>(*buffers_[path]).read(val);
206 }
207
209 Reader(const Reader&) = delete;
211 void operator=(const Reader&) = delete;
212
213 private:
220 void mirror(const std::string& path, Writer& output);
221
222 private:
231 protected:
235 HighFive::DataSet set_;
236 public:
243 explicit BufferHandle(std::size_t max, HighFive::DataSet s)
244 : max_len_{max}, set_{s} {}
246 virtual ~BufferHandle() = default;
254 virtual void load() = 0;
255 };
256
262 template <typename AtomicType>
263 class Buffer : public BufferHandle {
272 public:
284 explicit Buffer(std::size_t max, HighFive::DataSet s)
285 : BufferHandle(max, s), buffer_{}, i_file_{0}, i_memory_{0} {
286 // get the number of entries for later checking
287 entries_ = this->set_.getDimensions().at(0);
288 // do first load upon creation
289 this->load();
290 }
292 virtual ~Buffer() = default;
293
303 void read(AtomicType& out) {
304 if (i_memory_ == buffer_.size()) this->load();
305 out = buffer_[i_memory_];
306 i_memory_++;
307 }
308
332 virtual void load() final override {
333 // determine the length we want to request depending
334 // on the number of entries left in the file
335 std::size_t request_len = this->max_len_;
336 if (request_len + i_file_ > entries_) {
337 request_len = entries_ - i_file_;
338 assert(request_len >= 0);
339 }
340 // load the next chunk into memory
341 if constexpr (std::is_same_v<AtomicType,bool>) {
348 buff.resize(request_len);
349 this->set_.select({i_file_}, {request_len})
350 .read(buff.data(),create_enum_bool());
351 buffer_.reserve(buff.size());
352 for (const auto& v : buff) buffer_.push_back(v == Bool::TRUE);
353 } else {
354 this->set_.select({i_file_}, {request_len}).read(buffer_);
355 }
356 // update indices
357 i_file_ += buffer_.size();
358 i_memory_ = 0;
359 }
360 };
361
362 private:
383 unsigned long int last_entry_{0};
384
385 public:
389 MirrorObject(const std::string& path, Reader& reader);
390
394 void copy(unsigned long int i_entry, unsigned long int n, Writer& output);
395 };
396
397 private:
399 HighFive::File file_;
410}; // Reader
411
412} // namespace fire::io::h5
413
414#endif // FIRE_IO_H5_READER_H
Empty data base allowing recursion.
Definition: AbstractData.h:30
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
Type-less handle to the Buffer.
Definition: Reader.h:230
HighFive::DataSet set_
the HDF5 dataset we are reading from
Definition: Reader.h:235
virtual ~BufferHandle()=default
virtual destructor to pass on to derived types
BufferHandle(std::size_t max, HighFive::DataSet s)
Define the size of the in-memory buffer and the set we are reading from.
Definition: Reader.h:243
std::size_t max_len_
the maximum length of the buffer
Definition: Reader.h:233
virtual void load()=0
pure virtual load function to be defined when we know the type
Read buffer allowing individual element access from a dataset.
Definition: Reader.h:263
void read(AtomicType &out)
Read the next entry from the dataset into the input variable.
Definition: Reader.h:303
std::vector< AtomicType > buffer_
the actual buffer of in-memory elements
Definition: Reader.h:265
virtual void load() final override
Load the next chunk of data into memory.
Definition: Reader.h:332
virtual ~Buffer()=default
nothing fancy, just clearing in-memory objects
std::size_t i_file_
the current index of data-set elements in the file
Definition: Reader.h:267
std::size_t i_memory_
the current index of data-set elements in-memory
Definition: Reader.h:269
Buffer(std::size_t max, HighFive::DataSet s)
Define the size of the buffer and provide the dataset to read from.
Definition: Reader.h:284
std::size_t entries_
the number of entries in the entire dataset
Definition: Reader.h:271
A mirror event object.
Definition: Reader.h:373
std::unique_ptr< BaseData > data_
handle to the atomic data type once we get down to that point
Definition: Reader.h:377
MirrorObject(const std::string &path, Reader &reader)
Construct this mirror object and any of its (data or object) children.
Definition: Reader.cxx:108
unsigned long int last_entry_
the last entry that was copied
Definition: Reader.h:383
Reader & reader_
handle to the reader we are reading from
Definition: Reader.h:375
std::vector< std::unique_ptr< MirrorObject > > obj_members_
list of sub-objects within this object
Definition: Reader.h:381
void copy(unsigned long int i_entry, unsigned long int n, Writer &output)
Copy the n entries starting from i_entry.
Definition: Reader.cxx:155
std::unique_ptr< BaseData > size_member_
handle to the size member of this object (if it exists)
Definition: Reader.h:379
Reading a file generated by fire.
Definition: Reader.h:22
void operator=(const Reader &)=delete
never want to copy a reader
HighFive::DataType getDataSetType(const std::string &dataset) const
Deduce the type of the dataset requested.
Definition: Reader.cxx:32
virtual std::vector< std::pair< std::string, std::string > > availableObjects() final override
Get the event objects available in the file.
Definition: Reader.cxx:41
std::size_t entries() const final override
Get the number of entries in the file.
Definition: Reader.h:145
std::unordered_map< std::string, std::unique_ptr< MirrorObject > > mirror_objects_
our in-memory mirror objects for data being copied to the output file without processing
Definition: Reader.h:409
std::unordered_map< std::string, std::unique_ptr< BufferHandle > > buffers_
our in-memory buffers for the data to be read in from disk
Definition: Reader.h:407
std::string getTypeName(const std::string &obj_name) const
Get the 'type' attribute from the input group name.
virtual void load_into(BaseData &d) final override
Load the next event into the passed data.
Definition: Reader.cxx:20
const std::size_t entries_
the number of entries in this file, set in constructor
Definition: Reader.h:401
void load(const std::string &path, AtomicType &val)
Try to load a single value of an atomic type into the input handle.
Definition: Reader.h:195
std::size_t rows_per_chunk_
the number of rows to keep in each chunk, read from DataSet?
Definition: Reader.h:405
virtual std::string name() const final override
Get the name of this file.
Definition: Reader.cxx:24
const std::size_t runs_
the number of runs in this file, set in constructor
Definition: Reader.h:403
virtual void copy(unsigned long int i_entry, const std::string &path, Writer &output) final override
Copy the input data set to the output file.
std::size_t runs() const final override
Get the number of runs in the file.
Definition: Reader.h:154
HighFive::File file_
our highfive file
Definition: Reader.h:399
Reader(const std::string &name)
Open the file in read mode.
Definition: Reader.cxx:8
std::vector< std::string > list(const std::string &group_path) const
List the entries inside of a group.
Definition: Reader.cxx:26
HighFive::ObjectType getH5ObjectType(const std::string &path) const
Get the H5 type of object at the input path.
Definition: Reader.cxx:37
virtual std::pair< std::string, int > type(const std::string &path) final override
Get the type of the input object.
Definition: Reader.cxx:56
void mirror(const std::string &path, Writer &output)
Mirror the structure of the passed path from us into the output file.
Definition: Reader.cxx:74
Reader(const Reader &)=delete
never want to copy a reader
virtual bool canCopy() const final override
We can copy.
Definition: Reader.h:160
T data(T... args)
HighFive::EnumType< Bool > create_enum_bool()
HighFive method for creating the enum data type.
Definition: Atomic.cxx:5
T resize(T... args)
T size(T... args)