MarlinMT  0.1.0
Hist.h
Go to the documentation of this file.
1 #pragma once
2 
3 // -- MarlinBook includes
5 #include "marlinmt/book/Types.h"
6 
7 // -- Hist includes
10 
11 namespace marlinmt {
12  namespace book {
13 
14  //--------------------------------------------------------------------------
15 
16  template < typename Config >
18  const std::string_view &title )
19  : _title{title} {}
20 
21  //--------------------------------------------------------------------------
22 
23  template < typename Config >
27  Flags::value(Flags::Book::Single) >( *this ) ;
28  }
29 
30  //--------------------------------------------------------------------------
31 
32  template < typename Config >
34  EntryDataBase< types::HistT<Config> >::multiCopy( std::size_t n ) const {
36  Flags::value(Flags::Book::MultiCopy) >( *this, n ) ;
37  }
38 
39  //--------------------------------------------------------------------------
40 
41  template < typename Config >
43  EntryDataBase< types::HistT<Config> >::multiShared(std::size_t n) const {
44  return EntryData< types::HistT<Config>,
45  Flags::value(Flags::Book::MultiShared) >( *this, n ) ;
46  }
47 
48  //--------------------------------------------------------------------------
49 
50  template<typename Config>
51  EntryData< types::HistT<Config>, 0 >::EntryData(
52  const typename types::HistT<Config>::AxisConfig_t &axis )
54  static_assert(D == 1, "This is no 1D Hist, therefor it can't be constructed with 1 axis.");
55  this->axis(0) = std::make_unique<typename types::HistT<Config>::AxisConfig_t>(axis) ;
56  }
57 
58  //--------------------------------------------------------------------------
59 
60  template < typename Config >
61  EntryData< types::HistT<Config>, 0 >::EntryData(
62  const std::string_view &title, const typename types::HistT<Config>::AxisConfig_t &axis )
64  static_assert(D == 1, "This is no 1D Hist, therefor it can't be constructed with 1 axis.");
65  this->axis(0) = std::make_unique<typename types::HistT<Config>::AxisConfig_t>(axis) ;
66  }
67 
68  //--------------------------------------------------------------------------
69 
70  template < typename Config >
71  EntryData< types::HistT<Config>, 0 >::EntryData(
72  const typename types::HistT<Config>::AxisConfig_t & x_axis,
73  const typename types::HistT<Config>::AxisConfig_t & y_axis )
75  static_assert(D == 2, "This is no 2D Hist, therefor it can't be constructed with 2 axis.");
76  this->axis(0) = std::make_unique<typename types::HistT<Config>::AxisConfig_t>(x_axis) ;
77  this->axis(1) = std::make_unique<typename types::HistT<Config>::AxisConfig_t>(y_axis) ;
78  }
79 
80  //--------------------------------------------------------------------------
81 
82  template < typename Config >
83  EntryData< types::HistT<Config>, 0 >::EntryData(
84  const std::string_view &title,
85  const typename types::HistT<Config>::AxisConfig_t & x_axis,
86  const typename types::HistT<Config>::AxisConfig_t & y_axis )
88  static_assert(D == 2, "This is no 2D Hist, therefor it can't be constructed with 2 axis.");
89  this->axis(0) = std::make_unique<typename types::HistT<Config>::AxisConfig_t>(x_axis) ;
90  this->axis(1) = std::make_unique<typename types::HistT<Config>::AxisConfig_t>(y_axis) ;
91  }
92 
93  //--------------------------------------------------------------------------
94 
95  template < typename Config >
96  EntryData< types::HistT<Config>, 0 >::EntryData(
97  const typename types::HistT<Config>::AxisConfig_t &x_axis,
98  const typename types::HistT<Config>::AxisConfig_t &y_axis,
99  const typename types::HistT<Config>::AxisConfig_t &z_axis )
101  static_assert(D == 3, "This is no 3D Hist, therefor it can't be constructed with 3 axis.");
102  this->axis(0) = std::make_unique<typename types::HistT<Config>::AxisConfig_t>(x_axis) ;
103  this->axis(1) = std::make_unique<typename types::HistT<Config>::AxisConfig_t>(y_axis) ;
104  this->axis(2) = std::make_unique<typename types::HistT<Config>::AxisConfig_t>(z_axis) ;
105  }
106 
107  //--------------------------------------------------------------------------
108 
109  template < typename Config >
110  EntryData< types::HistT<Config>, 0 >::EntryData(
111  const std::string_view &title,
112  const typename types::HistT<Config>::AxisConfig_t & x_axis,
113  const typename types::HistT<Config>::AxisConfig_t & y_axis,
114  const typename types::HistT<Config>::AxisConfig_t & z_axis )
116  static_assert(D == 3, "This is no 3D Hist, therefor it can't be constructed with 3 axis.");
117  this->axis(0) = std::make_unique<typename types::HistT<Config>::AxisConfig_t>(x_axis) ;
118  this->axis(1) = std::make_unique<typename types::HistT<Config>::AxisConfig_t>(y_axis) ;
119  this->axis(2) = std::make_unique<typename types::HistT<Config>::AxisConfig_t>(z_axis) ;
120  }
121 
122  //--------------------------------------------------------------------------
123 
124  template < typename Config >
125  EntryData< types::HistT<Config>, 0 >::EntryData(
126  const std::string_view& title,
127  const std::array<
129  Config::Dimension>& axes)
130  : EntryDataBase<types::HistT<Config>>(title){
131  for (int i = 0; i < D; ++i) {
132  this->axis(i) = std::make_unique<typename Type::AxisConfig_t>(*(axes[i]));
133  }
134  }
135 
136  //--------------------------------------------------------------------------
137 
138  template < typename Config >
140  const std::shared_ptr< MemLayout > & mem,
141  const std::shared_ptr< types::HistT<Config> > & obj,
142  std::shared_ptr<void> data,
143  Flag_t type,
144  typename Handle< types::HistT<Config> >::FinalizeFn_t finalFn )
145  : BaseHandle< types::HistT<Config> >{mem, obj},
146  _finalFn{std::move(finalFn)},
147  _data{std::move(data)},
148  _type{std::move(type)}
149  {}
150 
151  //--------------------------------------------------------------------------
152 
153 
154  template < typename Config >
155  void Handle< types::HistT<Config> >::fill(
156  const typename Handle< types::HistT<Config> >::Point_t &x,
157  const typename Handle< types::HistT<Config> >::Weight_t & w ) {
158  fillImp(x,w);
159  }
160 
161  //--------------------------------------------------------------------------
162 
163  template < typename Config >
164  template < typename PointContainer, typename WeightContainer>
165  void Handle< types::HistT<Config> >::fillN(
166  const PointContainer &points,
167  const WeightContainer &weights ) {
168  // FIXME: only for arrays and vectors
169  fillNImp(
170  &(*points.begin()), &(*points.end()),
171  &(*weights.begin()), &(*weights.end()));
172  }
173 
174  //--------------------------------------------------------------------------
175 
176  template < typename Config >
178  _finalFn() ;
180  }
181 
182  //--------------------------------------------------------------------------
183 
184  template < typename Config >
186  : _context{std::move(context)} {}
187 
188  template < typename Config >
190  auto hist = _context.mem->at< Type >( 0 ) ;
191  return Handle< Type >(
192  _context.mem,
193  hist,
194  hist,
196  []() {} ) ;
197  }
198 
199  //--------------------------------------------------------------------------
200 
201  template < typename Config >
203  Context context )
204  : _context{std::move(context)} {}
205 
206  template < typename Config >
208  EntryMultiCopy< types::HistT<Config> >::handle( std::size_t idx ) {
209  using Hnd_t = Handle< Type > ;
210  auto pHist = _context.mem->at< Type >( idx ) ;
211  return Hnd_t(
212  _context.mem,
213  pHist,
214  pHist,
216  []() {} ) ;
217  }
218 
219  //--------------------------------------------------------------------------
220 
221  template < typename Config >
223  for ( auto &filler : _fillers ) {
224  if ( auto ptr = filler.lock() ) {
225  ptr->Flush() ;
226  }
227  }
228  }
229 
230 
231  //--------------------------------------------------------------------------
232 
233  template < typename Config >
235  Context context )
236  : _context{std::move(context)},
238  std::make_shared< types::HistConcurrentFillManager< Config > >(
239  *_context.mem->at< Type >( 0 ) )},
240  _fillers( 10 )
241  {
242  for ( std::size_t i = 0; i < context.nInstances; ++i) {
243  _staticFiller.push_back(
244  std::make_shared<
246  *_fillMgr));
247  _fillers.push_back(_staticFiller.back());
248  }
249  }
250 
251  //--------------------------------------------------------------------------
252 
253  template < typename Config >
254  EntryMultiShared< types::HistT<Config> >::~EntryMultiShared() {
255  flush() ;
256  }
257 
258  //--------------------------------------------------------------------------
259 
260  template < typename Config >
263  using Hnd_t = Handle<Type> ;
264  auto pFiller
265  = std::make_shared< types::HistConcurrentFiller< Config > >(
266  *_fillMgr ) ;
267  {
268  std::lock_guard<std::mutex> lock(_fillersExtend);
269  std::size_t capacity = _fillers.capacity();
270  if (_fillers.size() < capacity) {
271  _fillers.emplace_back(pFiller);
272  } else {
273  _fillers.erase(
274  std::remove_if(
275  _fillers.begin(), _fillers.end(),
276  [](auto& ptr){ return ptr.expired();}),
277  _fillers.end());
278  if (_fillers.size() > capacity * 0.8f) {
279  _fillers.reserve(_fillers.size() * 1.5f);
280  }
281  }
282  }
283 
284  return Hnd_t(
285  _context.mem,
286  _context.mem->at< Type >( 0 ),
287  pFiller,
289  [this]() { this->flush(); } ) ;
290  }
291 
292  //--------------------------------------------------------------------------
293 
294  template < typename Config >
297  std::size_t idx) {
298  return Handle<Type>(
299  _context.mem,
300  _context.mem->at< Type > ( 0 ),
301  _staticFiller[idx],
303  [this](){this->flush();});
304  }
305 
306  //--------------------------------------------------------------------------
307 
308  template < typename Config >
309  template < typename... Args_t, int d >
310  std::enable_if_t< d == 1, std::shared_ptr<details::Entry> >
312  ::book( BookStore &store, const Args_t &... args ) const {
313  return store.bookSingle< Object_t,
314  const std::string_view &,
315  const typename types::HistT<Config>::AxisConfig_t & >(
316  args..., _data.title(), *_data.axis(0) ) ;
317  }
318 
319  //--------------------------------------------------------------------------
320 
321  template < typename Config >
322  template < typename... Args_t, int d >
323  std::enable_if_t< d == 2, std::shared_ptr<details::Entry>>
324  EntryData<types::HistT<Config>, Flags::value(Flags::Book::Single)>
325  ::book( BookStore &store, const Args_t &... args ) const {
326  return store.bookSingle< Object_t,
327  const std::string_view &,
328  const typename types::HistT<Config>::AxisConfig_t &,
329  const typename types::HistT<Config>::AxisConfig_t & >(
330  args..., _data.title(), *_data.axis(0), *_data.axis(1) ) ;
331  }
332 
333  //--------------------------------------------------------------------------
334 
335  template < typename Config >
336  template < typename... Args_t, int d >
337  std::enable_if_t< d == 3, std::shared_ptr<details::Entry> >
338  EntryData<types::HistT<Config>, Flags::value(Flags::Book::Single)>
339  ::book( BookStore &store, const Args_t &... args ) const {
340  return store.bookSingle< Object_t,
341  const std::string_view &,
342  const typename types::HistT<Config>::AxisConfig_t &,
343  const typename types::HistT<Config>::AxisConfig_t &,
344  const typename types::HistT<Config>::AxisConfig_t & >(
345  args...,
346  _data.title(),
347  *_data.axis(0),
348  *_data.axis(1),
349  *_data.axis(2)) ;
350  }
351 
352  //--------------------------------------------------------------------------
353 
354  template < typename Config >
355  template < typename... Args_t, int d >
356  std::enable_if_t< d == 1, std::shared_ptr<details::Entry> >
357  EntryData<types::HistT<Config>, Flags::value(Flags::Book::MultiCopy)>
358  ::book( BookStore &store, const Args_t &... args ) const {
359  return store.bookMultiCopy< Object_t,
360  &types::add,
361  const std::string_view &,
362  const typename types::HistT<Config>::AxisConfig_t & >(
363  _n, args..., _data.title(), *_data.axis(0) ) ;
364  }
365 
366  //--------------------------------------------------------------------------
367 
368  template < typename Config >
369  template < typename... Args_t, int d >
370  std::enable_if_t< d == 2, std::shared_ptr<details::Entry> >
371  EntryData<types::HistT<Config>, Flags::value(Flags::Book::MultiCopy)>
372  ::book( BookStore &store, const Args_t &... args ) const {
373  return store.bookMultiCopy<Object_t,
374  &types::add,
375  const std::string_view &,
376  const typename types::HistT<Config>::AxisConfig_t &,
377  const typename types::HistT<Config>::AxisConfig_t & >(
378  _n, args..., _data.title(), *_data.axis(0), *_data.axis(1) ) ;
379  }
380 
381  //--------------------------------------------------------------------------
382 
383  template < typename Config >
384  template < typename... Args_t, int d >
385  std::enable_if_t< d == 3, std::shared_ptr<details::Entry> >
386  EntryData<types::HistT<Config>, Flags::value(Flags::Book::MultiCopy)>
387  ::book( BookStore &store, const Args_t &... args ) const {
388  return store.bookMultiCopy< Object_t,
389  &types::add,
390  const std::string_view &,
391  const typename types::HistT<Config>::AxisConfig_t &,
392  const typename types::HistT<Config>::AxisConfig_t &,
393  const typename types::HistT<Config>::AxisConfig_t & >(
394  _n,
395  args...,
396  _data.title(),
397  *_data.axis(0),
398  *_data.axis(1),
399  *_data.axis(2) ) ;
400  }
401 
402  //--------------------------------------------------------------------------
403 
404  template < typename Config >
405  template < typename... Args_t, int d >
406  std::enable_if_t< d == 1, std::shared_ptr<details::Entry> >
407  EntryData<types::HistT<Config>, Flags::value(Flags::Book::MultiShared)>
408  ::book( BookStore &store, const Args_t &... args ) const {
409  return store.bookMultiShared< Object_t,
410  const std::string_view &,
411  const typename types::HistT<Config>::AxisConfig_t & >(
412  _n, args..., _data.title(), *_data.axis(0) ) ;
413  }
414 
415  //--------------------------------------------------------------------------
416 
417  template < typename Config >
418  template < typename... Args_t, int d >
419  std::enable_if_t< d == 2, std::shared_ptr<details::Entry> >
420  EntryData<types::HistT<Config>, Flags::value(Flags::Book::MultiShared)>
421  ::book( BookStore &store, const Args_t &... args ) const {
422  return store.bookMultiShared< Object_t,
423  const std::string_view &,
424  const typename types::HistT<Config>::AxisConfig_t &,
425  const typename types::HistT<Config>::AxisConfig_t & >(
426  _n, args..., _data.title(), *_data.axis(0), *_data.axis(1) ) ;
427  }
428 
429  //--------------------------------------------------------------------------
430 
431  template < typename Config >
432  template < typename... Args_t, int d >
433  std::enable_if_t< d == 3, std::shared_ptr<details::Entry> >
434  EntryData<types::HistT<Config>, Flags::value(Flags::Book::MultiShared)>
435  ::book( BookStore &store, const Args_t &... args ) const {
436  return store.bookMultiShared< Object_t,
437  const std::string_view &,
438  const typename types::HistT<Config>::AxisConfig_t &,
439  const typename types::HistT<Config>::AxisConfig_t &,
440  const typename types::HistT<Config>::AxisConfig_t & >(
441  _n,
442  args...,
443  _data.title(),
444  *_data.axis(0),
445  *_data.axis(1),
446  *_data.axis(2) ) ;
447  }
448 
449  //--------------------------------------------------------------------------
450 
451  template < typename Config >
452  template < std::size_t I >
453  inline void Handle<types::HistT<Config>>::fillImp(
454  const typename types::HistT<Config>::Point_t& x,
455  const typename types::HistT<Config>::Weight_t& w
456  ) {
457  using EntryType = std::tuple_element_t<I, EntryTypes<Type>>;
458  if( _type == EntryType::Flag) {
459  EntryType::fill(_data, x, w);
460  } else if constexpr ( I + 1 < (std::tuple_size_v<EntryTypes<Type>>)){
461  fillImp<I + 1>(x,w);
462  }
463  }
464 
465  //--------------------------------------------------------------------------
466 
467  template < typename Config >
468  template < std::size_t I >
469  inline void Handle<types::HistT<Config>>::fillNImp (
470  const typename types::HistT<Config>::Point_t* pFirst,
471  const typename types::HistT<Config>::Point_t* pLast,
472  const typename types::HistT<Config>::Weight_t* wFirst,
473  const typename types::HistT<Config>::Weight_t* wLast
474  ) {
475  using EntryType = std::tuple_element_t<I, EntryTypes<Type>>;
476  if(_type == EntryType::Flag) {
477  EntryType::fillN(_data, pFirst, pLast, wFirst, wLast);
478  }
479  else if constexpr (I + 1 < (std::tuple_size_v<EntryTypes<Type>>)) {
480  fillNImp<I + 1>(pFirst, pLast, wFirst, wLast);
481  }
482  }
483 
484  //--------------------------------------------------------------------------
485 
486  template < typename Config >
488  const std::shared_ptr<void>& data,
489  const typename types::HistT<Config>::Point_t& x,
490  const typename types::HistT<Config>::Weight_t& w
491  ) {
492  static_cast<Type*>(data.get())->Fill(x,w);
493  }
494 
495  //--------------------------------------------------------------------------
496 
497  template < typename Config >
498  inline void EntrySingle<types::HistT<Config>>::fillN(
499  const std::shared_ptr<void>& data,
500  const typename types::HistT<Config>::Point_t* pFirst,
501  const typename types::HistT<Config>::Point_t* pLast,
502  const typename types::HistT<Config>::Weight_t* wFirst,
503  const typename types::HistT<Config>::Weight_t* wLast
504  ) {
505  static_cast<Type*>(data.get())->FillN(
506  pFirst, pLast, wFirst, wLast);
507  }
508 
509  //--------------------------------------------------------------------------
510 
511  template < typename Config >
513  const std::shared_ptr<void>& data,
514  const typename types::HistT<Config>::Point_t& x,
515  const typename types::HistT<Config>::Weight_t& w
516  ) {
517  static_cast<Type*>(data.get())->Fill(x,w);
518  }
519 
520  //--------------------------------------------------------------------------
521 
522  template < typename Config >
524  const std::shared_ptr<void>& data,
525  const typename types::HistT<Config>::Point_t* pFirst,
526  const typename types::HistT<Config>::Point_t* pLast,
527  const typename types::HistT<Config>::Weight_t* wFirst,
528  const typename types::HistT<Config>::Weight_t* wLast
529  ) {
530  static_cast<Type*>(data.get())->FillN(
531  pFirst, pLast, wFirst, wLast);
532  }
533 
534  //--------------------------------------------------------------------------
535 
536  template < typename Config >
538  const std::shared_ptr<void>& data,
539  const typename types::HistT<Config>::Point_t& x,
540  const typename types::HistT<Config>::Weight_t& w
541  ) {
542  static_cast<types::HistConcurrentFiller<Config>*>(data.get())->Fill(x,w);
543  }
544 
545  //--------------------------------------------------------------------------
546 
547  template < typename Config >
549  const std::shared_ptr<void>& data,
550  const typename types::HistT<Config>::Point_t* pFirst,
551  const typename types::HistT<Config>::Point_t* pLast,
552  const typename types::HistT<Config>::Weight_t* wFirst,
553  const typename types::HistT<Config>::Weight_t* wLast
554  ) {
555  static_cast<types::HistConcurrentFiller<Config>*>(data.get())->FillN(
556  pFirst, pLast, wFirst, wLast);
557  }
558 
559  } // end namespace book
560 } // end namespace marlinmt
std::tuple< EntrySingle< Type >, EntryMultiCopy< Type >, EntryMultiShared< Type > > EntryTypes
Definition: Entry.h:117
Managed Access and creation of Objects.
Definition: BookStore.h:96
Container for data to construct and setup booked object.
Definition: EntryData.h:66
entry for object to be used Multithreaded.
Definition: Entry.h:87
constexpr unsigned long long value(const Flag_t &flag)
Definition: Flags.h:106
std::shared_ptr< details::Entry > bookMultiShared(std::size_t n, std::filesystem::path path, Args_t... ctor_p)
creates an Entry for parallel access.
Definition: BookStore.h:370
std::shared_ptr< details::Entry > bookSingle(std::filesystem::path path, Args_t... ctor_p)
creates an Entry for a default Object.
Definition: BookStore.h:332
std::function< void() > FinalizeFn_t
Function signature called when close Handle.
Definition: HistEntry.h:34
Flag type for flags in marlinmt::book.
Definition: Flags.h:15
Generalized histogram class.
Definition: Base.h:151
constexpr Flag_t MultiShared(1U<< 1U)
create one instance witch concurrent access.
Base Class for Entry Data, for similar behavior.
Definition: EntryData.h:45
collection for Axis Description
Definition: Base.h:23
entry for object to be used Multithreaded.
Definition: Entry.h:55
minimal entry for Object.
Definition: Entry.h:27
constexpr Flag_t MultiCopy(1U<< 2U)
create multiple instances of booked object (if possible) to avoid sync points
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::shared_ptr< details::Entry > bookMultiCopy(std::size_t n, std::filesystem::path path, Args_t... ctor_p)
creates an Entry for parallel access.
Definition: BookStore.h:352
_fillers(10)
Definition: Hist.h:240
const T & merged()
get final object.
Definition: Handle.h:40
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
Data selection for the Entry to work properly.
Definition: EntryData.h:71
std::size_t nInstances
Definition: EntryData.h:84
typename Config::Weight_t Weight_t
type used for bin weight
Definition: Base.h:183
constexpr Flag_t Single(1U<< 0U)
vanilla object.