Logo ROOT   6.12/07
Reference Guide
THistBufferedFill.hxx
Go to the documentation of this file.
1 /// \file ROOT/THistBufferedFill.h
2 /// \ingroup Hist ROOT7
3 /// \author Axel Naumann <axel@cern.ch>
4 /// \date 2015-07-03
5 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6 /// is welcome!
7 
8 /*************************************************************************
9  * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
10  * All rights reserved. *
11  * *
12  * For the licensing terms see $ROOTSYS/LICENSE. *
13  * For the list of contributors see $ROOTSYS/README/CREDITS. *
14  *************************************************************************/
15 
16 #ifndef ROOT7_THistBufferedFill
17 #define ROOT7_THistBufferedFill
18 
19 #include "ROOT/RArrayView.hxx"
20 
21 namespace ROOT {
22 namespace Experimental {
23 
24 namespace Internal {
25 template <class DERIVED, class HIST, int SIZE>
27 public:
28  using CoordArray_t = typename HIST::CoordArray_t;
29  using Weight_t = typename HIST::Weight_t;
30 
31 private:
32  size_t fCursor = 0;
33  std::array<CoordArray_t, SIZE> fXBuf;
34  std::array<Weight_t, SIZE> fWBuf;
35 
36 public:
39 
40  DERIVED &toDerived() { return *static_cast<DERIVED *>(this); }
41  const DERIVED &toDerived() const { return *static_cast<const DERIVED *>(this); }
42 
43  std::array_view<CoordArray_t> GetCoords() const
44  {
45  return std::array_view<CoordArray_t>(fXBuf.begin(), fXBuf.begin() + fCursor);
46  }
47  std::array_view<Weight_t> GetWeights() const
48  {
49  return std::array_view<Weight_t>(fWBuf.begin(), fWBuf.begin() + fCursor);
50  }
51 
52  void Fill(const CoordArray_t &x, Weight_t weight = 1.)
53  {
54  fXBuf[fCursor] = x;
55  fWBuf[fCursor++] = weight;
56  if (fCursor == SIZE) {
57  toDerived().Flush();
58  fCursor = 0;
59  }
60  }
61 };
62 
63 } // namespace Internal
64 
65 /** \class THistBufferedFill
66  Buffers calls to Fill().
67 
68  Once the buffer is full, on destruction of when calling Flush(), it sends the
69  buffers off as an ideally vectorizable FillN() operation. It also serves as a
70  multi-threaded way of filling the same histogram, reducing the locking
71  frequency.
72 
73  The HIST template can be either a THist instance, a THistImpl instance, or
74  a THistLockedFill instance.
75  **/
76 
77 template <class HIST, int SIZE = 1024>
78 class THistBufferedFill: public Internal::THistBufferedFillBase<THistBufferedFill<HIST, SIZE>, HIST, SIZE> {
79 public:
80  using Hist_t = HIST;
81  using CoordArray_t = typename HIST::CoordArray_t;
82  using Weight_t = typename HIST::Weight_t;
83 
84 private:
85  HIST &fHist;
86  size_t fCursor = 0;
87  std::array<CoordArray_t, SIZE> fXBuf;
88  std::array<Weight_t, SIZE> fWBuf;
89 
90 public:
91  THistBufferedFill(Hist_t &hist): fHist{hist} {}
92 
93  void FillN(const std::array_view<CoordArray_t> xN, const std::array_view<Weight_t> weightN)
94  {
95  fHist.FillN(xN, weightN);
96  }
97 
98  void FillN(const std::array_view<CoordArray_t> xN) { fHist.FillN(xN); }
99 
100  void Flush() { fHist.FillN(this->GetCoords(), this->GetWeights()); }
101 
102  HIST &GetHist()
103  {
104  Flush(); // synchronize!
105  return fHist;
106  }
107  operator HIST &() { return GetHist(); }
108 
109  static constexpr int GetNDim() { return HIST::GetNDim(); }
110 };
111 } // namespace Experimental
112 } // namespace ROOT
113 
114 #endif
void Fill(const CoordArray_t &x, Weight_t weight=1.)
const char * GetHist()
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
std::array< CoordArray_t, SIZE > fXBuf
typename HIST::CoordArray_t CoordArray_t
Double_t x[n]
Definition: legend1.C:17
void FillN(const std::array_view< CoordArray_t > xN, const std::array_view< Weight_t > weightN)
void FillN(const std::array_view< CoordArray_t > xN)
TCoordArray< DIMENSIONS > CoordArray_t
Definition: THistUtils.hxx:49
std::array< Weight_t, SIZE > fWBuf
std::array_view< CoordArray_t > GetCoords() const
std::array_view< Weight_t > GetWeights() const