forked from cms-patatrack/pixeltrack-standalone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunningAverage.h
53 lines (45 loc) · 1.33 KB
/
RunningAverage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef FWCore_Utilities_RunningAverage_H
#define FWCore_Utilities_RunningAverage_H
#include <atomic>
#include <algorithm>
#include <array>
// Function for testing RunningAverage
namespace test_average {
namespace running_average {
int test();
}
} // namespace test_average
namespace edm {
// keeps the running average of the last N entries
// thread safe, fast: does not garantee precise update in case of collision
class RunningAverage {
// For tests
friend int ::test_average::running_average::test();
public:
static constexpr int N = 16; // better be a power of 2
explicit RunningAverage(unsigned int k = 4) : m_mean(N * k), m_curr(0) {
for (auto& i : m_buffer)
i = k;
}
int mean() const { return m_mean / N; }
int upper() const {
auto lm = mean();
return lm += (std::abs(m_buffer[0] - lm) + std::abs(m_buffer[N / 2] - lm));
} // about 2 sigma
void update(unsigned int q) {
int e = m_curr;
while (!m_curr.compare_exchange_weak(e, e + 1))
;
int k = (N - 1) & e;
int old = m_buffer[k];
if (!m_buffer[k].compare_exchange_strong(old, q))
return;
m_mean += (q - old);
}
private:
std::array<std::atomic<int>, N> m_buffer;
std::atomic<int> m_mean;
std::atomic<int> m_curr;
};
} // namespace edm
#endif