Logo ROOT  
Reference Guide
RHistBufferedFill.hxx
Go to the documentation of this file.
1/// \file ROOT/RHistBufferedFill.hxx
2/// \ingroup HistV7
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_RHistBufferedFill
17#define ROOT7_RHistBufferedFill
18
19#include "ROOT/RSpan.hxx"
20#include <array>
21
22namespace ROOT {
23namespace Experimental {
24
25namespace Internal {
26template <class DERIVED, class HIST, int SIZE>
28public:
30 using Weight_t = typename HIST::Weight_t;
31
32private:
33 size_t fCursor = 0;
34 std::array<CoordArray_t, SIZE> fXBuf;
35 std::array<Weight_t, SIZE> fWBuf;
36
37public:
40
41 DERIVED &toDerived() { return *static_cast<DERIVED *>(this); }
42 const DERIVED &toDerived() const { return *static_cast<const DERIVED *>(this); }
43
44 std::span<const CoordArray_t> GetCoords() const
45 {
46 return std::span<const CoordArray_t>(fXBuf.begin(), fXBuf.begin() + fCursor);
47 }
48 std::span<const Weight_t> GetWeights() const
49 {
50 return std::span<const Weight_t>(fWBuf.begin(), fWBuf.begin() + fCursor);
51 }
52
53 void Fill(const CoordArray_t &x, Weight_t weight = 1.)
54 {
55 fXBuf[fCursor] = x;
56 fWBuf[fCursor++] = weight;
57 if (fCursor == SIZE) {
58 Flush();
59 }
60 }
61
62 void Flush() {
63 toDerived().FlushImpl();
64 fCursor = 0;
65 }
66};
67
68} // namespace Internal
69
70/** \class RHistBufferedFill
71 Buffers calls to Fill().
72
73 Once the buffer is full, on destruction of when calling Flush(), it sends the
74 buffers off as an ideally vectorizable FillN() operation. It also serves as a
75 multi-threaded way of filling the same histogram, reducing the locking
76 frequency.
77
78 The HIST template can be either a RHist instance, a RHistImpl instance, or
79 a RHistLockedFill instance.
80 **/
81
82template <class HIST, int SIZE = 1024>
83class RHistBufferedFill: public Internal::RHistBufferedFillBase<RHistBufferedFill<HIST, SIZE>, HIST, SIZE> {
84public:
85 using Hist_t = HIST;
87 using Weight_t = typename HIST::Weight_t;
88
89private:
90 HIST &fHist;
91
92 friend class Internal::RHistBufferedFillBase<RHistBufferedFill<HIST, SIZE>, HIST, SIZE>;
93 void FlushImpl() { fHist.FillN(this->GetCoords(), this->GetWeights()); }
94
95public:
97
98 void FillN(const std::span<const CoordArray_t> xN, const std::span<const Weight_t> weightN)
99 {
100 fHist.FillN(xN, weightN);
101 }
102
103 void FillN(const std::span<const CoordArray_t> xN) { fHist.FillN(xN); }
104
105 HIST &GetHist()
106 {
107 this->Flush(); // synchronize!
108 return fHist;
109 }
110 operator HIST &() { return GetHist(); }
111
112 static constexpr int GetNDim() { return HIST::GetNDim(); }
113};
114} // namespace Experimental
115} // namespace ROOT
116
117#endif
std::span< const Weight_t > GetWeights() const
void Fill(const CoordArray_t &x, Weight_t weight=1.)
std::span< const CoordArray_t > GetCoords() const
void FillN(const std::span< const CoordArray_t > xN)
typename HIST::CoordArray_t CoordArray_t
void FillN(const std::span< const CoordArray_t > xN, const std::span< const Weight_t > weightN)
Double_t x[n]
Definition: legend1.C:17
RCoordArray< DIMENSIONS > CoordArray_t
Definition: RHistUtils.hxx:58
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.