Belle II Software development
ECLLocalRunCalibAcc.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <math.h>
9// ECL
10#include <ecl/modules/eclLocalRunCalibration/ECLLocalRunCalibAcc.h>
11// STL
12#include <algorithm>
13// BOOST
14#include <boost/accumulators/accumulators.hpp>
15#include <boost/accumulators/statistics.hpp>
16using namespace Belle2;
17// Constructor.
19 const float& min_value,
20 const float& max_value,
21 const int* const ndevs):
22 m_leftLimit(min_value),
23 m_rightLimit(max_value),
24 c_ndevs(ndevs),
25 m_nevents(0),
26 m_count(0),
27 m_mean(0.),
28 m_stddev(0.)
29{
30 m_data.reserve(1000);
31}
32// Destructor.
34{
35}
36// Check value.
37bool ECLLocalRunCalibAcc::isValueInRange(const float& value) const
38{
39 return (value > m_leftLimit) &&
40 (value < m_rightLimit);
41}
42// Add value.
43void ECLLocalRunCalibAcc::add(const float& value)
44{
45 m_data.push_back(value);
46}
47// Calculate standard deviation
48// using variance and number of
49// accepted events.
50float ECLLocalRunCalibAcc::calcStdDev(const float& variance,
51 const int& count) const
52{
53 return sqrt((variance * count) / (count - 1));
54}
55// Update value lowe and upper limits.
56void ECLLocalRunCalibAcc::updateLimits(const float& mean,
57 const float& stddev)
58{
59 m_leftLimit = std::max(m_leftLimit,
60 mean - (*c_ndevs) * stddev);
61 m_rightLimit = std::min(m_rightLimit,
62 mean + (*c_ndevs) * stddev);
63}
64// Calculate accumulated features.
66{
67 if (m_data.size() == 0) {
68 m_nevents = 0;
69 m_count = 0;
70 m_mean = 0;
71 m_stddev = 0;
72 } else {
73 namespace bacc = boost::accumulators;
74 bacc::accumulator_set <
75 float,
76 bacc::features <
77 bacc::tag::count,
78 bacc::tag::median,
79 bacc::tag::variance >> acc;
80 for (const auto& value : m_data) {
81 if (isValueInRange(value)) {
82 acc(value);
83 }
84 }
85 float tcount = bacc::count(acc);
86 float tmedian = bacc::median(acc);
87 float tvariance = bacc::variance(acc);
88 float tstddev = calcStdDev(tvariance,
89 tcount);
90 updateLimits(tmedian, tstddev);
91 bacc::accumulator_set <
92 float,
93 bacc::features <
94 bacc::tag::count,
95 bacc::tag::mean,
96 bacc::tag::variance >> acc_final;
97 for (const auto& value : m_data) {
98 if (isValueInRange(value)) {
99 acc_final(value);
100 }
101 }
102 m_nevents = static_cast<int>(m_data.size());
103 m_data.clear();
104 m_count = bacc::count(acc_final);
105 m_mean = bacc::mean(acc_final);
106 tvariance = bacc::variance(acc_final);
107 m_stddev = calcStdDev(tvariance, m_count);
108 }
109}
110// Get total number of events.
112{
113 return m_nevents;
114}
115// Get number of accepted events.
117{
118 return m_count;
119}
120// Get mean value.
122{
123 return m_mean;
124}
125// Get standard deviation.
127{
128 return m_stddev;
129}
void calc()
Calculate mean value, standard deviation and number of accepted events.
float calcStdDev(const float &variance, const int &count) const
Calculate standard deviation using variance and number of accepted events.
float m_leftLimit
Lower value limit.
int getCount() const
Get number of accepted events.
float getMean() const
Get mean value.
ECLLocalRunCalibAcc(const float &min_value, const float &max_value, const int *const ndevs)
Constructor.
float m_rightLimit
Upper value limit.
void updateLimits(const float &mean, const float &stddev)
Update value limits.
bool isValueInRange(const float &value) const
Check value.
std::vector< int > m_data
Vector of accepted values.
int m_nevents
Total number of events.
int getNOfEvents() const
Get total number of events.
void add(const float &value)
Add value.
const int *const c_ndevs
Number of standard deviations used to update value limits.
float getStdDev() const
Get standard deviation.
int m_count
Number of accepted events.
float m_stddev
Standard deviation.
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
Abstract base class for different kinds of events.