CbmRoot
Loading...
Searching...
No Matches
Accumulators.h
Go to the documentation of this file.
1/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#pragma once
11
12#include <boost/histogram/fwd.hpp> // for weighted_mean<>
13#include <boost/histogram/make_histogram.hpp>
14#include <boost/serialization/access.hpp>
15
16#include <cmath>
17#include <type_traits>
18
20{
24 template<class ValueType>
26 public:
27 using Value_t = ValueType;
28 using ConstRef_t = const Value_t&;
29 using Weight_t = weight_type<Value_t>;
30
33
35 template<class OtherValueType>
37 : fSumWV(other.fSumWV)
38 , fSumWV2(other.fSumWV2)
39 , fSumW(other.fSumW)
40 , fSumW2(other.fSumW2)
41 {
42 }
43
50 : fSumWV(sumWV)
51 , fSumWV2(sumWV2)
52 , fSumW(sumW)
53 , fSumW2(sumW2)
54 {
55 }
56
59 void operator()(ConstRef_t v) { operator()(weight(1.), v); }
60
65 {
66 fSumWV += w.value * v;
67 fSumWV2 += w.value * v * v;
68 fSumW += w.value;
69 fSumW2 += w.value * w.value;
70 }
71
74 {
75 if (rhs.fSumW == 0) {
76 return *this;
77 }
78 fSumWV += rhs.fSumWV;
79 fSumWV2 += rhs.fSumWV2;
80 fSumW += rhs.fSumW;
81 fSumW2 += rhs.fSumW2;
82 return *this;
83 }
84
87 {
88 fSumWV *= s;
89 fSumWV2 *= s * s;
90 return *this;
91 }
92
94 bool operator==(const RootStyleProfileAccumulator& rhs) const noexcept
95 {
96 return fSumWV == rhs.fSumWV && fSumWV2 == rhs.fSumWV2 && fSumW == rhs.fSumW && fSumW2 == rhs.fSumW2;
97 }
98
100 bool operator!=(const RootStyleProfileAccumulator& rhs) const noexcept { return !operator==(rhs); }
101
103 ConstRef_t GetSumW() const noexcept { return fSumW; }
104
106 ConstRef_t GetSumW2() const noexcept { return fSumW2; }
107
109 ConstRef_t GetSumWV() const noexcept { return fSumWV; }
110
112 ConstRef_t GetSumWV2() const noexcept { return fSumWV2; }
113
115 Value_t GetMean() const noexcept { return fSumW ? (fSumWV / fSumW) : 0; }
116
118 Value_t GetEffCount() const noexcept { return fSumW2 ? (fSumW * fSumW / fSumW2) : 0; }
119
121 Value_t GetVariance() const noexcept { return fSumW ? (fSumWV2 / fSumW - GetMean() * GetMean()) : 0; }
122
124 Value_t GetStdDev() const noexcept { return std::sqrt(GetVariance()); }
125
127 Value_t GetSEM() const noexcept { return fSumW ? GetStdDev() / std::sqrt(GetEffCount()) : 0; }
128
129 private:
132 template<class Archive>
133 void serialize(Archive& ar, const unsigned int /*version*/)
134 {
135 ar& make_nvp("fSumWV", fSumWV);
136 ar& make_nvp("fSumWV2", fSumWV2);
137 ar& make_nvp("fSumW", fSumW);
138 ar& make_nvp("fSumW2", fSumW2);
139 }
140
145 };
146} // namespace boost::histogram
147
148#ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
149namespace std
150{
151 template<class T, class U>
153 struct common_type<boost::histogram::RootStyleProfileAccumulator<T>,
156 };
157} // namespace std
158#endif
159
160namespace boost::histogram
161{
162 template<typename T>
163 using RootStyleProfileStorage = dense_storage<RootStyleProfileAccumulator<T>>;
164
166 template<class Axis, class... Axes, class = detail::requires_axis<Axis>>
167 auto MakeRootStyleProfile(Axis&& axis, Axes&&... axes)
168 {
169 return make_histogram_with(RootStyleProfileStorage<double>(), std::forward<Axis>(axis),
170 std::forward<Axes>(axes)...);
171 }
172} // namespace boost::histogram
fscal v[fmask::Size]
Definition KfSimdPseudo.h:4
A ROOT-style accumulator for the Boost-histogram profiles.
Value_t GetMean() const noexcept
Returns the mean value.
RootStyleProfileAccumulator(const RootStyleProfileAccumulator< OtherValueType > &other)
Copy constructor from the instances with other types.
ConstRef_t GetSumWV() const noexcept
Returns sum of products of weight multiplied by value.
bool operator==(const RootStyleProfileAccumulator &rhs) const noexcept
Equality operator.
void serialize(Archive &ar, const unsigned int)
Serialization rule.
RootStyleProfileAccumulator & operator*=(ConstRef_t s) noexcept
Scales all entries by value.
bool operator!=(const RootStyleProfileAccumulator &rhs) const noexcept
Non-equality operator.
void operator()(ConstRef_t v)
Inserts sample with 1. weight.
ConstRef_t GetSumW2() const noexcept
Returns sum of weight squares.
Value_t fSumW2
Sum of squared weights.
void operator()(const Weight_t &w, ConstRef_t v)
Inserts sample with weight.
Value_t GetSEM() const noexcept
Returns standard error of the mean.
RootStyleProfileAccumulator & operator+=(const RootStyleProfileAccumulator &rhs)
Adds another accumulator.
Value_t fSumWV2
Sum of weight * value * value.
ConstRef_t GetSumW() const noexcept
Returns sum of weights.
RootStyleProfileAccumulator()=default
Default constructor.
ConstRef_t GetSumWV2() const noexcept
Returns sum of products of weight multiplied by squared value.
Value_t GetEffCount() const noexcept
Returns effective sum of values.
Value_t GetStdDev() const noexcept
Returns standard deviation of the sample.
RootStyleProfileAccumulator(ConstRef_t sumWV, ConstRef_t sumWV2, ConstRef_t sumW, ConstRef_t sumW2)
Constructor from the external counters.
Value_t GetVariance() const noexcept
Returns variance.
friend class boost::serialization::access
dense_storage< RootStyleProfileAccumulator< T > > RootStyleProfileStorage
auto MakeRootStyleProfile(Axis &&axis, Axes &&... axes)
Maker for RootStyleProfileAccumulator.
Hash for CbmL1LinkKey.