MarlinMT  0.1.0
Base.h
Go to the documentation of this file.
1 #pragma once
2 
3 // -- std includes
4 #include <array>
5 #include <memory>
6 #include <optional>
7 #include <stdexcept>
8 #include <string>
9 #include <string_view>
10 #include <type_traits>
11 #include <vector>
12 
13 namespace marlinmt {
14  namespace book {
16  namespace types {
17 
18  // TODO: check for minimal amount of copies
22  template<typename Precision_t>
23  class AxisConfig {
24  public:
25 
33  AxisConfig( const std::string_view& title,
34  std::size_t bins,
35  Precision_t min,
36  Precision_t max)
37  : _title(title), _bins{bins}, _min{min}, _max{max}{}
38 
39 
46  AxisConfig( std::size_t bins,
47  Precision_t min,
48  Precision_t max)
49  : AxisConfig( "", bins, min, max ){}
50 
60  template<typename Itr>
61  AxisConfig( const std::string_view& title,
62  Itr begin, Itr end)
63 
64  : _title(title),
65  _bins{(end - begin) - 1},
66  _iregularBorder(begin, end),
67  _min{_iregularBorder.front()},
68  _max{_iregularBorder.back()}
69  {}
70 
79  template<typename Itr>
80  explicit AxisConfig( Itr begin, Itr end )
81  : AxisConfig<Itr>( "", begin, end ){}
82 
89  template<typename Container>
91  const std::string_view& title,
92  const Container& container)
93  : AxisConfig(title, container.begin(), container.end()) {}
94 
96  [[nodiscard]]
97  std::string_view title() const { return _title; }
98 
100  [[nodiscard]]
101  std::size_t bins() const { return _bins; }
102 
104  [[nodiscard]]
105  Precision_t min() const { return _min; }
106 
108  [[nodiscard]]
109  Precision_t max() const { return _max; }
110 
112  [[nodiscard]]
113  bool isRegular() const { return !_iregularBorder.has_value(); }
114 
119  const std::vector<Precision_t>& iregularBorder() const {
120  return _iregularBorder.value_or(std::vector<Precision_t>{});
121  }
122  private:
123  std::string _title;
124  std::size_t _bins;
125  Precision_t _min;
126  Precision_t _max;
127  std::optional<std::vector<Precision_t>> _iregularBorder{std::nullopt};
128  };
129 
130 
135  template<typename P, typename W, std::size_t D>
136  struct HistConfig {
138  using Weight_t = W;
140  using Precision_t = P;
142  using Impl_t = void*;
144  using ConcurrentFiller_t = void*;
146  using ConcurrentManager_t = void*;
147  static constexpr std::size_t Dimension = static_cast<std::size_t>(D);
148  };
149 
150  template<typename>
151  class HistT;
152 
154  template<typename Config>
155  HistT<Config>& add(HistT<Config>& to, const HistT<Config>& from);
156 
158  template<typename Config>
159  void add(
160  const std::shared_ptr<HistT<Config>>& to,
161  const std::shared_ptr<HistT<Config>>& from);
162 
163  template<typename>
165 
166  template<typename>
168 
172  template<typename Config>
173  class HistT {
174  typename Config::Impl_t& impl() { return _impl; }
175  [[nodiscard]]
176  const typename Config::Impl_t& impl() const { return _impl; }
177  friend HistT<Config>& add<Config>(HistT<Config>&,const HistT<Config>&);
178  friend void add<Config>(const std::shared_ptr<HistT<Config>>&,const std::shared_ptr<HistT<Config>>&);
179  friend class HistConcurrentFillManager<Config>;
180 
181  public:
183  using Weight_t = typename Config::Weight_t;
185  using Precision_t = typename Config::Precision_t;
187  static constexpr std::size_t Dimension = Config::Dimension;
189  using Point_t = std::array<Precision_t, Dimension>;
192 
196  explicit HistT(const AxisConfig_t& axis) : HistT("",axis) {}
197 
201  HistT(const AxisConfig_t& axisA,
202  const AxisConfig_t& axisB) : HistT("",axisA, axisB) {}
203 
207  HistT(const AxisConfig_t& axisA,
208  const AxisConfig_t& axisB,
209  const AxisConfig_t& axisC) : HistT("",axisA, axisB, axisC) {}
210 
214  HistT( const std::string_view& title, const AxisConfig_t& axis);
215 
219  HistT(
220  const std::string_view& title,
221  const AxisConfig_t& axisA,
222  const AxisConfig_t& axisB);
223 
227  HistT( const std::string_view& title,
228  const AxisConfig_t& axisA,
229  const AxisConfig_t& axisB,
230  const AxisConfig_t& axisC);
231 
235  void Fill(const Point_t& point, const Weight_t& weight);
236 
237  // TODO: used generic iterator
246  void FillN(const Point_t *pFirst, const Point_t *pLast,
247  const Weight_t *wFirst, const Weight_t *wLast);
248 
249 
256  void FillN(const Point_t *first, const Point_t *last);
257 
261  [[nodiscard]]
262  const typename Config::Impl_t& get() const { return _impl; }
263 
267  constexpr bool hasImpl() {
268  return !std::is_same_v<Config::Impl_t, void*>;
269  }
270 
271  private:
272  typename Config::Impl_t _impl{};
273  };
274 
278  template<typename Config>
280  friend class HistConcurrentFiller<Config>;
281  typename Config::ConcurrentManager_t& impl() { return _impl; }
282  public:
284  private:
285  typename Config::ConcurrentManager_t _impl;
286  };
287 
291  template<typename Config>
292  class HistConcurrentFiller {
293  public:
297  using Point_t = typename Type::Point_t;
299  using Weight_t = typename Type::Weight_t;
300 
302 
304  void Fill(const Point_t& p, const Weight_t& w);
305 
307  void FillN(const Point_t *pFirst, const Point_t *pLast,
308  const Weight_t *wFirst, const Weight_t *wLast);
309 
311  void FillN(const Point_t *first, const Point_t *last);
312 
314  void Flush();
315 
316  private:
317  typename Config::ConcurrentFiller_t _impl;
318  };
319 
325  template<typename Config>
326  auto toRoot6(const HistT<Config>& hist, const std::string_view& name);
327 
328 
329  } // end namespace types
330 
331  namespace details {
338  template<typename I, typename O>
339  constexpr O safe_cast(const I& input) {
340  static_assert((true == std::is_integral_v<I>) == std::is_integral_v<O>
341  , "safe_cast only available for integral types!");
342  if (
343  static_cast<O>(input) >= std::numeric_limits<O>::min()
344  && static_cast<O>(input) <= std::numeric_limits<O>::max()) {
345  return static_cast<O>(input);
346  }
347  throw std::domain_error("Input number is outside of target type domain!");
348  }
349 
350  } // end namespace details
351 
352  // -- MarlinBook forward declarations
353  class BookStore ;
354  template < typename T >
355  class Handle ;
356  template < typename T >
357  class BaseHandle ;
358 
359  } // end namespace book
360 } // end namespace marlinmt
HistT(const AxisConfig_t &axis)
non-title 1D-histogram constructor.
Definition: Base.h:196
void * Impl_t
math type used to handle histogram
Definition: Base.h:142
Managed Access and creation of Objects.
Definition: BookStore.h:96
Config::Impl_t & impl()
Definition: Base.h:174
book::types::HistT< HistConfig< P, W, D > > HistT
W Weight_t
type used for bin weight
Definition: Base.h:138
HistT(const AxisConfig_t &axisA, const AxisConfig_t &axisB)
non-title 2D-histogram constructor.
Definition: Base.h:201
const Config::Impl_t & impl() const
Definition: Base.h:176
AxisConfig(const std::string_view &title, Itr begin, Itr end)
Axis with irregular borders.
Definition: Base.h:61
void * ConcurrentManager_t
class to create ConcurrentFiller for one Histogram.
Definition: Base.h:146
AxisConfig(const std::string_view &title, const Container &container)
Axis with irregular borders.
Definition: Base.h:90
constexpr bool hasImpl()
check if histogram type implemented.
Definition: Base.h:267
type trait for Histograms.
Definition: Base.h:136
HistT(const AxisConfig_t &axisA, const AxisConfig_t &axisB, const AxisConfig_t &axisC)
non-title 3D-histogram constructor.
Definition: Base.h:207
Config::ConcurrentManager_t _impl
Definition: Base.h:285
Config::ConcurrentFiller_t _impl
Definition: Base.h:317
bool isRegular() const
check if bins are equal sized.
Definition: Base.h:113
const std::vector< Precision_t > & iregularBorder() const
get borders, when borders are irregular.
Definition: Base.h:119
Generalized histogram class.
Definition: Base.h:151
typename Config::Precision_t Precision_t
type used for bin borders
Definition: Base.h:185
AxisConfig(std::size_t bins, Precision_t min, Precision_t max)
Axis with equal sized bins.
Definition: Base.h:46
P Precision_t
type used for bin borders
Definition: Base.h:140
AxisConfig(const std::string_view &title, std::size_t bins, Precision_t min, Precision_t max)
Axis with equal sized bins.
Definition: Base.h:33
collection for Axis Description
Definition: Base.h:23
std::optional< std::vector< Precision_t > > _iregularBorder
Definition: Base.h:127
std::size_t bins() const
Get amount of bins.
Definition: Base.h:101
constexpr O safe_cast(const I &input)
caste integral types with domain check.
Definition: Base.h:339
Config::ConcurrentManager_t & impl()
Definition: Base.h:281
auto toRoot6(const HistT< Config > &hist, const std::string_view &name)
convert histogram to Root-6 Object for serialization
Definition: Dummy.h:71
AxisConfig(Itr begin, Itr end)
Axis with irregular borders.
Definition: Base.h:80
HistT< Config > & add(HistT< Config > &to, const HistT< Config > &from)
add weights from one Histogram to an other.
Definition: Dummy.h:14
class which basic functionality for every handle.
Definition: Handle.h:24
vanilla Handle.
Definition: Handle.h:54
std::string_view title() const
Get Axis Title.
Definition: Base.h:97
Precision_t max() const
Get upper bound.
Definition: Base.h:109
std::array< Precision_t, Dimension > Point_t
type used for Entry Points
Definition: Base.h:189
class managing parallel filling to one histogram.
Definition: Base.h:164
class managing HistConcurrentFiller creation for one histogram.
Definition: Base.h:167
typename Type::Weight_t Weight_t
Type used for bin weight.
Definition: Base.h:299
void * ConcurrentFiller_t
class to access single instance across multiple threads.
Definition: Base.h:144
typename Config::Weight_t Weight_t
type used for bin weight
Definition: Base.h:183
Precision_t min() const
Get lower bound.
Definition: Base.h:105
typename Type::Point_t Point_t
Type used for points.
Definition: Base.h:297