LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
TruncMean Class Reference

#include "TruncMean.h"

Public Member Functions

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 pair-wise (nth entry in rr_v corresponds to nth entry in dq_v vector). Input rr_v values are also assumed to be ordered: monotonically increasing or decreasing. For every dq value a truncated linear dq value is calculated as follows: 0) all dq values within a rr range set by the class variable _rad are selected. 1) the median and rms of these values is calculated. 2) the subset of local dq values within the range [median-rms, median+rms] is selected. 3) the resulting local truncated dq is the average of this truncated subset. std::vector<float> rr_v -> vector of x-axis coordinates (i.e. position for track profile) std::vector<float> dq_v -> vector of measured values for which truncated profile is requested (i.e. charge profile of a track) std::vector<float> dq_trunc_v -> passed by reference -> output stored here float nsigma -> optional parameter, number of sigma to keep around RMS for TM calculation. More...
 
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 for which truncated mean is asked size_t nmin -> minimum number of iterations to converge on truncated mean size_t nmax -> maximum number of iterations to converge on truncated mean size_t lmin -> minimum number of entries in vector before exiting and returning current value size_t currentiteration -> current iteration float convergencelimit -> fractional difference between successive iterations under which the iteration is completed, provided nmin iterations have occurred. nsigma -> number of sigma around the median value to keep when the distribution is trimmed. More...
 
void setRadius (const float &rad)
 Set the smearing radius over which to take hits for truncated mean computaton. More...
 

Private Member Functions

float Mean (const std::vector< float > &v)
 
float Median (const std::vector< float > &v)
 
float RMS (const std::vector< float > &v)
 

Private Attributes

double _rad
 

Detailed Description

The truncated mean class allows to compute the following quantities 1) the truncated mean profile of an ordered vector of values, such as the charge profile along a particle's track. To create such a profile use the function CalcTruncMeanProfile() 2) Get the truncated mean value of a distribution. This function iteratively hones in on the truncated mean of a distribution by updating the mean and cutting the tails after each iteration. For this functionality use CalcIterativeTruncMean() doxygen documentation!

Definition at line 34 of file TruncMean.h.

Member Function Documentation

float TruncMean::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 for which truncated mean is asked size_t nmin -> minimum number of iterations to converge on truncated mean size_t nmax -> maximum number of iterations to converge on truncated mean size_t lmin -> minimum number of entries in vector before exiting and returning current value size_t currentiteration -> current iteration float convergencelimit -> fractional difference between successive iterations under which the iteration is completed, provided nmin iterations have occurred. nsigma -> number of sigma around the median value to keep when the distribution is trimmed.

Definition at line 7 of file TruncMean.cxx.

References pmtana::mean(), Mean(), Median(), RMS(), and x.

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 }
Float_t x
Definition: compare.C:6
float Median(const std::vector< float > &v)
Definition: TruncMean.cxx:121
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 TruncMean::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 pair-wise (nth entry in rr_v corresponds to nth entry in dq_v vector). Input rr_v values are also assumed to be ordered: monotonically increasing or decreasing. For every dq value a truncated linear dq value is calculated as follows: 0) all dq values within a rr range set by the class variable _rad are selected. 1) the median and rms of these values is calculated. 2) the subset of local dq values within the range [median-rms, median+rms] is selected. 3) the resulting local truncated dq is the average of this truncated subset. std::vector<float> rr_v -> vector of x-axis coordinates (i.e. position for track profile) std::vector<float> dq_v -> vector of measured values for which truncated profile is requested (i.e. charge profile of a track) std::vector<float> dq_trunc_v -> passed by reference -> output stored here float nsigma -> optional parameter, number of sigma to keep around RMS for TM calculation.

Definition at line 47 of file TruncMean.cxx.

References _rad, Median(), n, and RMS().

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 }
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
Char_t n[5]
float TruncMean::Mean ( const std::vector< float > &  v)
private

Definition at line 110 of file TruncMean.cxx.

References pmtana::mean(), and n.

Referenced by CalcIterativeTruncMean(), and setRadius().

111 {
112 
113  float mean = 0.;
114  for (auto const& n : v)
115  mean += n;
116  mean /= v.size();
117 
118  return mean;
119 }
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
Char_t n[5]
float TruncMean::Median ( const std::vector< float > &  v)
private

Definition at line 121 of file TruncMean.cxx.

Referenced by CalcIterativeTruncMean(), CalcTruncMeanProfile(), and setRadius().

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 }
float TruncMean::RMS ( const std::vector< float > &  v)
private

Definition at line 135 of file TruncMean.cxx.

Referenced by CalcIterativeTruncMean(), CalcTruncMeanProfile(), and setRadius().

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 }
void TruncMean::setRadius ( const float &  rad)
inline

Set the smearing radius over which to take hits for truncated mean computaton.

Definition at line 82 of file TruncMean.h.

References _rad, Mean(), Median(), and RMS().

82 { _rad = rad; }
double _rad
Definition: TruncMean.h:93

Member Data Documentation

double TruncMean::_rad
private

Smearing radius over which charge from neighboring hits is scanned to calculate local truncated mean

Definition at line 93 of file TruncMean.h.

Referenced by CalcTruncMeanProfile(), and setRadius().


The documentation for this class was generated from the following files: