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