LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
TruncMean.cxx
Go to the documentation of this file.
1 #include "TruncMean.h"
2 
3 #include <algorithm>
4 #include <cmath>
5 #include <vector>
6 
7 float TruncMean::CalcIterativeTruncMean(std::vector<float> v,
8  const size_t& nmin,
9  const size_t& nmax,
10  const size_t& currentiteration,
11  const size_t& lmin,
12  const float& convergencelimit,
13  const float& nsigma,
14  const float& oldmed)
15 {
16 
17  auto const& mean = Mean(v);
18  auto const& med = Median(v);
19  auto const& rms = RMS(v);
20 
21  // if the vector length is below the lower limit -> return
22  if (v.size() < lmin) return mean;
23 
24  // if we have passed the maximum number of iterations -> return
25  if (currentiteration >= nmax) return mean;
26 
27  // if we passed the minimum number of iterations and the mean is close enough to the old value
28  float fracdiff = fabs(med - oldmed) / oldmed;
29  if ((currentiteration >= nmin) && (fracdiff < convergencelimit)) return mean;
30 
31  // if reached here it means we have to go on for another iteration
32 
33  // cutoff tails of distribution surrounding the mean
34  // use erase-remove : https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
35  // https://stackoverflow.com/questions/17270837/stdvector-removing-elements-which-fulfill-some-conditions
36  v.erase(std::remove_if(v.begin(),
37  v.end(),
38  [med, nsigma, rms](const float& x) {
39  return ((x < (med - nsigma * rms)) || (x > (med + nsigma * rms)));
40  }), // lamdda condition for events to be removed
41  v.end());
42 
44  v, nmin, nmax, lmin, currentiteration + 1, convergencelimit, nsigma, med);
45 }
46 
47 void TruncMean::CalcTruncMeanProfile(const std::vector<float>& rr_v,
48  const std::vector<float>& dq_v,
49  std::vector<float>& dq_trunc_v,
50  const float& nsigma)
51 {
52 
53  // how many points to sample
54  int Nneighbor = (int)(_rad * 3 * 2);
55 
56  dq_trunc_v.clear();
57  dq_trunc_v.reserve(rr_v.size());
58 
59  int Nmax = dq_v.size() - 1;
60 
61  for (size_t n = 0; n < dq_v.size(); n++) {
62 
63  // current residual range
64  float rr = rr_v.at(n);
65 
66  int nmin = n - Nneighbor;
67  int nmax = n + Nneighbor;
68 
69  if (nmin < 0) nmin = 0;
70  if (nmax > Nmax) nmax = Nmax;
71 
72  // vector for local dq values
73  std::vector<float> dq_local_v;
74 
75  for (int i = nmin; i < nmax; i++) {
76 
77  float dr = rr - rr_v[i];
78  if (dr < 0) dr *= -1;
79 
80  if (dr > _rad) continue;
81 
82  dq_local_v.push_back(dq_v[i]);
83 
84  } // for all ticks we want to scan
85 
86  if (dq_local_v.size() == 0) {
87  dq_trunc_v.push_back(dq_v.at(n));
88  continue;
89  }
90 
91  // calculate median and rms
92  float median = Median(dq_local_v);
93  float rms = RMS(dq_local_v);
94 
95  float truncated_dq = 0.;
96  int npts = 0;
97  for (auto const& dq : dq_local_v) {
98  if ((dq < (median + rms * nsigma)) && (dq > (median - rms * nsigma))) {
99  truncated_dq += dq;
100  npts += 1;
101  }
102  }
103 
104  dq_trunc_v.push_back(truncated_dq / npts);
105  } // for all values
106 
107  return;
108 }
109 
110 float TruncMean::Mean(const std::vector<float>& v)
111 {
112 
113  float mean = 0.;
114  for (auto const& n : v)
115  mean += n;
116  mean /= v.size();
117 
118  return mean;
119 }
120 
121 float TruncMean::Median(const std::vector<float>& v)
122 {
123 
124  if (v.size() == 1) return v[0];
125 
126  std::vector<float> vcpy = v;
127 
128  std::sort(vcpy.begin(), vcpy.end());
129 
130  float median = vcpy[vcpy.size() / 2];
131 
132  return median;
133 }
134 
135 float TruncMean::RMS(const std::vector<float>& v)
136 {
137 
138  float avg = 0.;
139  for (auto const& val : v)
140  avg += val;
141  avg /= v.size();
142  float rms = 0.;
143  for (auto const& val : v)
144  rms += (val - avg) * (val - avg);
145  rms = sqrt(rms / (v.size() - 1));
146 
147  return rms;
148 }
Float_t x
Definition: compare.C:6
Class def header for a class TruncMean.
float Median(const std::vector< float > &v)
Definition: TruncMean.cxx:121
double _rad
Definition: TruncMean.h:93
float RMS(const std::vector< float > &v)
Definition: TruncMean.cxx:135
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
float CalcIterativeTruncMean(std::vector< float > v, const size_t &nmin, const size_t &nmax, const size_t &currentiteration, const size_t &lmin, const float &convergencelimit, const float &nsigma, const float &oldmed=kINVALID_FLOAT)
Iteratively calculate the truncated mean of a distribution std::vector<float> v -> vector of values ...
Definition: TruncMean.cxx:7
float Mean(const std::vector< float > &v)
Definition: TruncMean.cxx:110
void CalcTruncMeanProfile(const std::vector< float > &rr_v, const std::vector< float > &dq_v, std::vector< float > &dq_trunc_v, const float &nsigma=1)
Given residual range and dq vectors return truncated local dq. Input vectors are assumed to be match ...
Definition: TruncMean.cxx:47
Char_t n[5]