16 #ifndef ROOT7_RHistData
17 #define ROOT7_RHistData
25 namespace Experimental {
27 template <
int DIMENSIONS,
class PRECISION,
template <
int D_,
class P_>
class... STAT>
35 template <
int DIMENSIONS,
class PRECISION>
36 class RHistStatContent {
39 using CoordArray_t = Hist::CoordArray_t<DIMENSIONS>;
41 using Weight_t = PRECISION;
43 using Content_t = std::vector<PRECISION>;
51 RConstBinStat(
const RHistStatContent &stat,
int index): fContent(stat.GetBinContent(index)) {}
52 PRECISION GetContent()
const {
return fContent; }
64 RBinStat(RHistStatContent &stat,
int index): fContent(stat.GetBinContent(index)) {}
65 PRECISION &GetContent()
const {
return fContent; }
71 using ConstBinStat_t = RConstBinStat;
72 using BinStat_t = RBinStat;
79 Content_t fBinContent;
82 RHistStatContent() =
default;
83 RHistStatContent(
size_t in_size): fBinContent(in_size) {}
86 void Fill(
const CoordArray_t & ,
int binidx, Weight_t weight = 1.)
88 fBinContent[binidx] += weight;
94 int64_t GetEntries()
const {
return fEntries; }
97 size_t size() const noexcept {
return fBinContent.size(); }
100 Weight_t operator[](
int idx)
const {
return fBinContent[idx]; }
102 Weight_t &operator[](
int idx) {
return fBinContent[idx]; }
105 Weight_t GetBinContent(
int idx)
const {
return fBinContent[idx]; }
107 Weight_t &GetBinContent(
int idx) {
return fBinContent[idx]; }
110 const Content_t &GetContentArray()
const {
return fBinContent; }
112 Content_t &GetContentArray() {
return fBinContent; }
119 template <
int DIMENSIONS,
class PRECISION>
120 class RHistStatTotalSumOfWeights {
123 using CoordArray_t = Hist::CoordArray_t<DIMENSIONS>;
125 using Weight_t = PRECISION;
133 RBinStat(
const RHistStatTotalSumOfWeights &,
int) {}
136 using ConstBinStat_t = RBinStat;
137 using BinStat_t = RBinStat;
141 PRECISION fSumWeights = 0;
144 RHistStatTotalSumOfWeights() =
default;
145 RHistStatTotalSumOfWeights(
size_t) {}
148 void Fill(
const CoordArray_t & ,
int, Weight_t weight = 1.) { fSumWeights += weight; }
151 Weight_t GetSumOfWeights()
const {
return fSumWeights; }
158 template <
int DIMENSIONS,
class PRECISION>
159 class RHistStatTotalSumOfSquaredWeights {
162 using CoordArray_t = Hist::CoordArray_t<DIMENSIONS>;
164 using Weight_t = PRECISION;
172 RBinStat(
const RHistStatTotalSumOfSquaredWeights &,
int) {}
175 using ConstBinStat_t = RBinStat;
176 using BinStat_t = RBinStat;
180 PRECISION fSumWeights2 = 0;
183 RHistStatTotalSumOfSquaredWeights() =
default;
184 RHistStatTotalSumOfSquaredWeights(
size_t) {}
187 void Fill(
const CoordArray_t & ,
int , Weight_t weight = 1.) { fSumWeights2 += weight * weight; }
190 Weight_t GetSumOfSquaredWeights()
const {
return fSumWeights2; }
197 template <
int DIMENSIONS,
class PRECISION>
198 class RHistStatUncertainty {
202 using CoordArray_t = Hist::CoordArray_t<DIMENSIONS>;
204 using Weight_t = PRECISION;
206 using Content_t = std::vector<PRECISION>;
212 class RConstBinStat {
214 RConstBinStat(
const RHistStatUncertainty &stat,
int index): fSumW2(stat.GetSumOfSquaredWeights(index)) {}
215 PRECISION GetSumW2()
const {
return fSumW2; }
217 double GetUncertaintyImpl()
const {
return std::sqrt(std::abs(fSumW2)); }
229 RBinStat(RHistStatUncertainty &stat,
int index): fSumW2(stat.GetSumOfSquaredWeights(index)) {}
230 PRECISION &GetSumW2()
const {
return fSumW2; }
232 double GetUncertaintyImpl()
const {
return std::sqrt(std::abs(fSumW2)); }
238 using ConstBinStat_t = RConstBinStat;
239 using BinStat_t = RBinStat;
243 Content_t fSumWeightsSquared;
246 RHistStatUncertainty() =
default;
247 RHistStatUncertainty(
size_t size): fSumWeightsSquared(size) {}
250 void Fill(
const CoordArray_t & ,
int binidx, Weight_t weight = 1.)
252 fSumWeightsSquared[binidx] += weight * weight;
257 double GetBinUncertaintyImpl(
int binidx)
const {
return std::sqrt(fSumWeightsSquared[binidx]); }
260 Weight_t GetSumOfSquaredWeights(
int binidx)
const {
return fSumWeightsSquared[binidx]; }
263 Weight_t &GetSumOfSquaredWeights(
int binidx) {
return fSumWeightsSquared[binidx]; }
266 const std::vector<double> &GetSumOfSquaredWeights()
const {
return fSumWeightsSquared; }
268 std::vector<double> &GetSumOfSquaredWeights() {
return fSumWeightsSquared; }
274 template <
int DIMENSIONS,
class PRECISION>
275 class RHistDataMomentUncert {
278 using CoordArray_t = Hist::CoordArray_t<DIMENSIONS>;
280 using Weight_t = PRECISION;
282 using Content_t = std::vector<PRECISION>;
290 RBinStat(
const RHistDataMomentUncert &,
int) {}
293 using ConstBinStat_t = RBinStat;
294 using BinStat_t = RBinStat;
297 std::array<Weight_t, DIMENSIONS> fMomentXW;
298 std::array<Weight_t, DIMENSIONS> fMomentX2W;
301 RHistDataMomentUncert() =
default;
302 RHistDataMomentUncert(
size_t) {}
305 void Fill(
const CoordArray_t &x,
int , Weight_t weight = 1.)
307 for (
int idim = 0; idim < DIMENSIONS; ++idim) {
308 const PRECISION xw = x[idim] * weight;
309 fMomentXW[idim] += xw;
310 fMomentX2W[idim] += x[idim] * xw;
318 template <
int DIMENSIONS,
class PRECISION>
319 class RHistStatRuntime {
322 using CoordArray_t = Hist::CoordArray_t<DIMENSIONS>;
324 using Weight_t = PRECISION;
326 using Content_t = std::vector<PRECISION>;
334 RBinStat(
const RHistStatRuntime &,
int) {}
337 using ConstBinStat_t = RBinStat;
338 using BinStat_t = RBinStat;
340 RHistStatRuntime() =
default;
341 RHistStatRuntime(
size_t) {}
342 virtual ~RHistStatRuntime() =
default;
344 virtual void DoFill(
const CoordArray_t &x,
int binidx, Weight_t weightN) = 0;
345 void Fill(
const CoordArray_t &x,
int binidx, Weight_t weight = 1.) { DoFill(x, binidx, weight); }
353 template <
class DATA,
class... BASES>
354 class RHistBinStat:
public BASES... {
358 static auto HaveUncertainty(
const T *This) -> decltype(This->GetUncertaintyImpl());
361 static char HaveUncertainty(...);
364 RHistBinStat(DATA &data,
int index): BASES(data, index)... {}
368 static constexpr
bool HasBinUncertainty()
370 struct AllYourBaseAreBelongToUs:
public BASES... {
372 return sizeof(HaveUncertainty<AllYourBaseAreBelongToUs>(
nullptr)) ==
sizeof(double);
376 template <bool B = true, class = typename std::enable_if<B && HasBinUncertainty()>::type>
377 double GetUncertainty()
const
379 return this->GetUncertaintyImpl();
384 template <bool B = true, class = typename std::enable_if<B && !HasBinUncertainty()>::type>
385 double GetUncertainty(...)
const
387 auto content = this->GetContent();
388 return std::sqrt(std::fabs(content));
395 template <
int DIMENSIONS,
class PRECISION,
class STORAGE,
template <
int D_,
class P_>
class... STAT>
396 class RHistData:
public STAT<DIMENSIONS, PRECISION>... {
400 static auto HaveUncertainty(
const T *This) -> decltype(This->GetBinUncertaintyImpl(12));
403 static char HaveUncertainty(...);
407 using Hist_t = RHist<DIMENSIONS, PRECISION, STAT...>;
410 using Weight_t = PRECISION;
413 using CoordArray_t = Hist::CoordArray_t<DIMENSIONS>;
416 using ConstHistBinStat_t =
417 RHistBinStat<const RHistData, typename STAT<DIMENSIONS, PRECISION>::ConstBinStat_t...>;
420 using HistBinStat_t = RHistBinStat<RHistData, typename STAT<DIMENSIONS, PRECISION>::BinStat_t...>;
423 static constexpr
int GetNDim() noexcept {
return DIMENSIONS; }
425 RHistData() =
default;
429 RHistData(
size_t size): STAT<DIMENSIONS, PRECISION>(size)... {}
432 void Fill(
const CoordArray_t &x,
int binidx, Weight_t weight = 1.)
448 using trigger_base_fill =
int[];
449 (void)trigger_base_fill{(STAT<DIMENSIONS, PRECISION>::Fill(x, binidx, weight), 0)...};
454 static constexpr
bool HasBinUncertainty()
456 struct AllYourBaseAreBelongToUs:
public STAT<DIMENSIONS, PRECISION>... {
458 return sizeof(HaveUncertainty<AllYourBaseAreBelongToUs>(
nullptr)) ==
sizeof(double);
463 template <bool B = true, class = typename std::enable_if<B && HasBinUncertainty()>::type>
464 double GetBinUncertainty(
int binidx)
const
466 return this->GetBinUncertaintyImpl(binidx);
471 template <bool B = true, class = typename std::enable_if<B && !HasBinUncertainty()>::type>
472 double GetBinUncertainty(
int binidx, ...)
const
474 auto content = this->GetBinContent(binidx);
475 return std::sqrt(std::fabs(content));
479 ConstHistBinStat_t GetView(
int idx)
const {
return ConstHistBinStat_t(*
this, idx); }
481 HistBinStat_t GetView(
int idx) {
return HistBinStat_t(*
this, idx); }