Project
Loading...
Searching...
No Matches
StatAccumulator.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#ifndef MATHUTILS_INCLUDE_MATHUTILS_DETAIL_STATACCUMULATOR_H_
17#define MATHUTILS_INCLUDE_MATHUTILS_DETAIL_STATACCUMULATOR_H_
18
19#ifndef GPUCA_GPUCODE_DEVICE
20#include <tuple>
21#endif
22
23namespace o2
24{
25namespace math_utils
26{
27namespace detail
28{
29
31 // mean / RMS accumulator
32 double sum = 0.;
33 double sum2 = 0.;
34 double wsum = 0.;
35 int n = 0;
36
37 void add(float v, float w = 1.)
38 {
39 const auto c = v * w;
40 sum += c;
41 sum2 += c * v;
42 wsum += w;
43 n++;
44 }
45 double getMean() const { return wsum > 0. ? sum / wsum : 0.; }
46
47#ifndef GPUCA_GPUCODE_DEVICE
48 template <typename T = float>
49 std::tuple<T, T> getMeanRMS2() const
50 {
51 T mean = 0;
52 T rms2 = 0;
53
54 if (wsum) {
55 const T wi = 1. / wsum;
56 mean = sum * wi;
57 rms2 = sum2 * wi - mean * mean;
58 }
59
60 return {mean, rms2};
61 }
62#endif
63
65 {
66 sum += other.sum;
67 sum2 += other.sum2;
68 wsum += other.wsum;
69 n += other.n;
70 return *this;
71 }
72
74 {
75 StatAccumulator res = *this;
76 res += other;
77 return res;
78 }
79
80 void clear()
81 {
82 sum = sum2 = wsum = 0.;
83 n = 0;
84 }
85};
86
87} // namespace detail
88} // namespace math_utils
89} // namespace o2
90
91#endif /* MATHUTILS_INCLUDE_MATHUTILS_DETAIL_STATACCUMULATOR_H_ */
uint32_t res
Definition RawData.h:0
GLdouble n
Definition glcorearb.h:1982
const GLdouble * v
Definition glcorearb.h:832
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
StatAccumulator operator+(const StatAccumulator &other) const
std::tuple< T, T > getMeanRMS2() const
StatAccumulator & operator+=(const StatAccumulator &other)
VectorOfTObjectPtrs other