LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
TruncMean Class Reference

#include "TruncMean.h"

Public Member Functions

 TruncMean ()
 Default constructor. More...
 
 ~TruncMean ()
 Default destructor. More...
 
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 37 of file TruncMean.h.

Constructor & Destructor Documentation

TruncMean::TruncMean ( )
inline

Default constructor.

Definition at line 42 of file TruncMean.h.

42 {}
TruncMean::~TruncMean ( )
inline

Default destructor.

Definition at line 45 of file TruncMean.h.

References CalcIterativeTruncMean(), CalcTruncMeanProfile(), and kINVALID_FLOAT.

45 {}

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.

Referenced by ~TruncMean().

12 {
13 
14  auto const& mean = Mean(v);
15  auto const& med = Median(v);
16  auto const& rms = RMS(v);
17 
18  // if the vector length is below the lower limit -> return
19  if (v.size() < lmin)
20  return mean;
21 
22  // if we have passed the maximum number of iterations -> return
23  if (currentiteration >= nmax)
24  return mean;
25 
26  // if we passed the minimum number of iterations and the mean is close enough to the old value
27  float fracdiff = fabs(med-oldmed) / oldmed;
28  if ( (currentiteration >= nmin) && (fracdiff < convergencelimit) )
29  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(), v.end(),
37  [med,nsigma,rms](const float& x) { return ( (x < (med-nsigma*rms)) || (x > (med+nsigma*rms)) ); }), // lamdda condition for events to be removed
38  v.end());
39 
40  return CalcIterativeTruncMean(v, nmin, nmax, lmin, currentiteration+1, convergencelimit, nsigma, med);
41 }
Float_t x
Definition: compare.C:6
float Median(const std::vector< float > &v)
Definition: TruncMean.cxx:114
float RMS(const std::vector< float > &v)
Definition: TruncMean.cxx:128
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:15
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:104
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 43 of file TruncMean.cxx.

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

Referenced by ~TruncMean().

45 {
46 
47  // how many points to sample
48  int Nneighbor = (int)(_rad * 3 * 2);
49 
50  dq_trunc_v.clear();
51  dq_trunc_v.reserve( rr_v.size() );
52 
53  int Nmax = dq_v.size()-1;
54 
55  for (size_t n=0; n < dq_v.size(); n++) {
56 
57  // current residual range
58  float rr = rr_v.at(n);
59 
60  int nmin = n - Nneighbor;
61  int nmax = n + Nneighbor;
62 
63  if (nmin < 0) nmin = 0;
64  if (nmax > Nmax) nmax = Nmax;
65 
66  // vector for local dq values
67  std::vector<float> dq_local_v;
68 
69  for (int i=nmin; i < nmax; i++) {
70 
71  float dr = rr - rr_v[i];
72  if (dr < 0) dr *= -1;
73 
74  if (dr > _rad) continue;
75 
76  dq_local_v.push_back( dq_v[i] );
77 
78  }// for all ticks we want to scan
79 
80  if (dq_local_v.size() == 0) {
81  dq_trunc_v.push_back( dq_v.at(n) );
82  continue;
83  }
84 
85  // calculate median and rms
86  float median = Median(dq_local_v);
87  float rms = RMS(dq_local_v);
88 
89  float truncated_dq = 0.;
90  int npts = 0;
91  for (auto const& dq : dq_local_v) {
92  if ( ( dq < (median+rms * nsigma) ) && ( dq > (median-rms * nsigma) ) ){
93  truncated_dq += dq;
94  npts += 1;
95  }
96  }
97 
98  dq_trunc_v.push_back( truncated_dq / npts );
99  }// for all values
100 
101  return;
102 }
float Median(const std::vector< float > &v)
Definition: TruncMean.cxx:114
double _rad
Definition: TruncMean.h:99
float RMS(const std::vector< float > &v)
Definition: TruncMean.cxx:128
Char_t n[5]
float TruncMean::Mean ( const std::vector< float > &  v)
private

Definition at line 104 of file TruncMean.cxx.

References pmtana::mean(), and n.

Referenced by CalcIterativeTruncMean(), and setRadius().

105 {
106 
107  float mean = 0.;
108  for (auto const& n : v) mean += n;
109  mean /= v.size();
110 
111  return mean;
112 }
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:15
Char_t n[5]
float TruncMean::Median ( const std::vector< float > &  v)
private

Definition at line 114 of file TruncMean.cxx.

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

115 {
116 
117  if (v.size() == 1) return v[0];
118 
119  std::vector<float> vcpy = v;
120 
121  std::sort(vcpy.begin(), vcpy.end());
122 
123  float median = vcpy[ vcpy.size() / 2 ];
124 
125  return median;
126 }
float TruncMean::RMS ( const std::vector< float > &  v)
private

Definition at line 128 of file TruncMean.cxx.

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

129 {
130 
131  float avg = 0.;
132  for (auto const& val : v) avg += val;
133  avg /= v.size();
134  float rms = 0.;
135  for (auto const& val : v) rms += (val-avg)*(val-avg);
136  rms = sqrt( rms / ( v.size() - 1 ) );
137 
138  return rms;
139 }
void TruncMean::setRadius ( const float &  rad)
inline

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

Definition at line 87 of file TruncMean.h.

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

87 { _rad = rad; }
double _rad
Definition: TruncMean.h:99

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 99 of file TruncMean.h.

Referenced by CalcTruncMeanProfile(), and setRadius().


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