artdaq_demo_hdf5  v1_03_00
highFiveDatasetHelper.hh
1 #ifndef artdaq_demo_hdf5_HDF5_highFive_highFiveDatasetHelper_hh
2 #define artdaq_demo_hdf5_HDF5_highFive_highFiveDatasetHelper_hh 1
3 
4 #include <artdaq-demo-hdf5/HDF5/highFive/HighFive/include/highfive/H5DataSet.hpp>
5 
6 namespace artdaq {
7 namespace hdf5 {
8 
15 {
16 public:
22  HighFiveDatasetHelper(HighFive::DataSet const& dataset, size_t chunk_size = 128)
23  : dataset_(dataset)
24  , current_row_(0)
25  , current_size_(dataset.getDimensions()[0])
26  , chunk_size_(chunk_size)
27  {
28  // Zero-size chunks are not allowed
29  if (chunk_size_ == 0)
30  {
31  chunk_size_ = 10;
32  }
33  }
34 
39  template<typename T>
40  void write(T const& data)
41  {
42  if (current_row_ >= current_size_) resize();
43 
44  dataset_.select({current_row_, 0}, {1, 1}).write(data);
45  current_row_++;
46  }
47 
53  template<typename T>
54  void write(T const& data, size_t width)
55  {
56  if (current_row_ >= current_size_) resize();
57 
58  dataset_.select({current_row_, 0}, {1, width}).write(data);
59  current_row_++;
60  }
61 
67  template<typename T>
68  std::vector<T> read(size_t row)
69  {
70  if (row >= current_size_)
71  {
72  TLOG_ERROR("HighFiveDatasetHelper") << "Requested row " << row << " is outside the bounds of this dataset! dataset sz=" << current_size_;
73  return std::vector<T>();
74  }
75 
76  std::vector<T> readBuf;
77  dataset_.select({row, 0}, {1, dataset_.getDimensions()[1]}).read(readBuf);
78  return readBuf;
79  }
80 
86  template<typename T>
87  T readOne(size_t row)
88  {
89  auto res = read<T>(row);
90  if (!res.empty()) return res[0];
91 
92  return T();
93  }
94 
99  size_t getDatasetSize() { return current_size_; }
104  size_t getRowSize() { return dataset_.getDimensions()[1]; }
105 
106 private:
107  void resize()
108  {
109  TLOG(TLVL_TRACE) << "HighFiveDatasetHelper::resize: Growing dataset by one chunk";
110  // Ideally, grow by one chunk at a time
111  if (current_size_ % chunk_size_ == 0)
112  {
113  dataset_.resize({current_size_ + chunk_size_, dataset_.getDimensions()[1]});
114  current_size_ += chunk_size_;
115  }
116  else
117  {
118  auto size_add = chunk_size_ - (current_size_ % chunk_size_);
119  if (size_add / static_cast<double>(chunk_size_) < 0.5)
120  {
121  size_add += chunk_size_;
122  }
123 
124  dataset_.resize({current_size_ + size_add, dataset_.getDimensions()[1]});
125  current_size_ += size_add;
126  }
127  }
128 
129 private:
130  HighFive::DataSet dataset_;
131  size_t current_row_;
132  size_t current_size_;
133  size_t chunk_size_;
134 };
135 } // namespace hdf5
136 } // namespace artdaq
137 
138 #endif //artdaq_demo_hdf5_HDF5_highFive_highFiveDatasetHelper_hh
T readOne(size_t row)
Read a single value from the column.
void write(T const &data)
Write a value to the column, resizing if necessary.
std::vector< T > read(size_t row)
Read a set of value from a row of the column.
void write(T const &data, size_t width)
Write a value to the column, resizing if necessary.
HighFiveDatasetHelper(HighFive::DataSet const &dataset, size_t chunk_size=128)
HighFiveDatasetHelper Constructor.
size_t getDatasetSize()
Get the number of rows in the column.
size_t getRowSize()
Get the number of entries in each row.
Helper class for HighFiveNtupleDataset.