All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Member Functions | Private Attributes | List of all members
single_photon::TruncMean Class Reference

#include <TruncMean.h>

Public Member Functions

 TruncMean ()
 Default constructor. More...
 
 ~TruncMean ()
 Default destructor. More...
 
void CalcTruncMeanProfile (const std::vector< double > &rr_v, const std::vector< double > &dq_v, std::vector< double > &dq_trunc_v, const double &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<double> rr_v -> vector of x-axis coordinates (i.e. position for track profile) std::vector<double> dq_v -> vector of measured values for which truncated profile is requested (i.e. charge profile of a track) std::vector<double> dq_trunc_v -> passed by reference -> output stored here double nsigma -> optional parameter, number of sigma to keep around RMS for TM calculation. More...
 
double CalcIterativeTruncMean (std::vector< double > v, const size_t &nmin, const size_t &nmax, const size_t &currentiteration, const size_t &lmin, const double &convergencelimit, const double &nsigma, const double &oldmed=kINVALID_FLOAT)
 Iteratively calculate the truncated mean of a distribution. More...
 
void setRadius (const double &rad)
 Set the smearing radius over which to take hits for truncated mean computaton. More...
 

Private Member Functions

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

Private Attributes

double _rad
 

Detailed Description

Definition at line 40 of file sbncode/sbncode/SinglePhotonAnalysis/Libraries/TruncMean.h.

Constructor & Destructor Documentation

single_photon::TruncMean::TruncMean ( )
inline

Default constructor.

Definition at line 45 of file sbncode/sbncode/SinglePhotonAnalysis/Libraries/TruncMean.h.

45 {}
single_photon::TruncMean::~TruncMean ( )
inline

Default destructor.

Definition at line 48 of file sbncode/sbncode/SinglePhotonAnalysis/Libraries/TruncMean.h.

48 {}

Member Function Documentation

double TruncMean::CalcIterativeTruncMean ( std::vector< double >  v,
const size_t &  nmin,
const size_t &  nmax,
const size_t &  currentiteration,
const size_t &  lmin,
const double &  convergencelimit,
const double &  nsigma,
const double &  oldmed = kINVALID_FLOAT 
)

Iteratively calculate the truncated mean of a distribution.

: mean is returned if vecter's size is too small, or reach the max iteration, or median has converged std::vector<double> 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 double 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 6 of file sbncode/sbncode/SinglePhotonAnalysis/Libraries/TruncMean.cxx.

11  {
12 
13  auto const& mean = Mean(v);
14  auto const& med = Median(v);
15  auto const& rms = RMS(v);
16 
17  // if the vector length is below the lower limit -> return
18  if (v.size() < lmin)
19  return mean;
20 
21  // if we have passed the maximum number of iterations -> return
22  if (currentiteration >= nmax)
23  return mean;
24 
25  // if we passed the minimum number of iterations and the mean is close enough to the old value
26  double fracdiff = fabs(med-oldmed) / oldmed;
27  if ( (currentiteration >= nmin) && (fracdiff < convergencelimit) )
28  return mean;
29 
30  // if reached here it means we have to go on for another iteration
31 
32  // cutoff tails of distribution surrounding the mean
33  // use erase-remove : https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
34  // https://stackoverflow.com/questions/17270837/stdvector-removing-elements-which-fulfill-some-conditions
35  v.erase( std::remove_if( v.begin(), v.end(),
36  [med,nsigma,rms](const double& x) { return ( (x < (med-nsigma*rms)) || (x > (med+nsigma*rms)) ); }), // lamdda condition for events to be removed
37  v.end());
38 
39  return CalcIterativeTruncMean(v, nmin, nmax, lmin, currentiteration+1, convergencelimit, nsigma, med);
40  }
process_name opflash particleana ie x
double CalcIterativeTruncMean(std::vector< double > v, const size_t &nmin, const size_t &nmax, const size_t &currentiteration, const size_t &lmin, const double &convergencelimit, const double &nsigma, const double &oldmed=kINVALID_FLOAT)
Iteratively calculate the truncated mean of a distribution.
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
void TruncMean::CalcTruncMeanProfile ( const std::vector< double > &  rr_v,
const std::vector< double > &  dq_v,
std::vector< double > &  dq_trunc_v,
const double &  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<double> rr_v -> vector of x-axis coordinates (i.e. position for track profile) std::vector<double> dq_v -> vector of measured values for which truncated profile is requested (i.e. charge profile of a track) std::vector<double> dq_trunc_v -> passed by reference -> output stored here double nsigma -> optional parameter, number of sigma to keep around RMS for TM calculation.

Definition at line 42 of file sbncode/sbncode/SinglePhotonAnalysis/Libraries/TruncMean.cxx.

44  {
45 
46  // how many points to sample
47  int Nneighbor = (int)(_rad * 3 * 2);
48 
49  dq_trunc_v.clear();
50  dq_trunc_v.reserve( rr_v.size() );
51 
52  int Nmax = dq_v.size()-1;
53 
54  for (size_t n=0; n < dq_v.size(); n++) { // rr_v and dq_v have the same size
55 
56  // current residual range
57  double rr = rr_v.at(n);
58 
59  int nmin = n - Nneighbor;
60  int nmax = n + Nneighbor;
61 
62  if (nmin < 0) nmin = 0;
63  if (nmax > Nmax) nmax = Nmax;
64 
65  // vector for local dq values
66  std::vector<double> dq_local_v;
67 
68  for (int i=nmin; i < nmax; i++) {
69 
70  double dr = rr - rr_v[i];
71  if (dr < 0) dr *= -1;
72 
73  if (dr > _rad) continue;
74 
75  dq_local_v.push_back( dq_v[i] ); //save for ticks that are close enough
76 
77  }// for all ticks we want to scan
78 
79  if (dq_local_v.size() == 0 ) {
80  dq_trunc_v.push_back( dq_v.at(n) ); // if no neighbours, push back dq of itself
81  continue;
82  }
83 
84  // calculate median and rms
85  double median = Median(dq_local_v);
86  double rms = RMS(dq_local_v);
87 
88  double truncated_dq = 0.;
89  int npts = 0;
90  for (auto const& dq : dq_local_v) {
91  if ( ( dq < (median+rms * nsigma) ) && ( dq > (median-rms * nsigma) ) ){
92  truncated_dq += dq;
93  npts += 1;
94  }
95  }
96 
97  dq_trunc_v.push_back( truncated_dq / npts ); // push back averaged dq for these sitting within nsigma
98 
99  if(dq_trunc_v.back() != dq_trunc_v.back()){
100  std::cout<<"ERROR::TruncMean.cxx || NAN "<<dq_trunc_v.back()<<std::endl;
101  std::cout<<"truncated_dq: "<<truncated_dq<<std::endl;
102  std::cout<<"npts: "<<npts<<std::endl;
103  std::cout<<"median: "<<median<<std::endl;
104  std::cout<<"rms: "<<rms<<std::endl;
105  }
106 
107  }// for all values
108 
109  return;
110  }
BEGIN_PROLOG could also be cout
double TruncMean::Mean ( const std::vector< double > &  v)
private

Definition at line 112 of file sbncode/sbncode/SinglePhotonAnalysis/Libraries/TruncMean.cxx.

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

Definition at line 122 of file sbncode/sbncode/SinglePhotonAnalysis/Libraries/TruncMean.cxx.

123  {
124 
125  if (v.size() == 1) return v[0];
126 
127  std::vector<double> vcpy = v;
128 
129  std::sort(vcpy.begin(), vcpy.end()); //sort to ascending order
130 
131  double median = vcpy[ vcpy.size() / 2 ]; // what if v has even # of elements? there is a choice
132 
133  return median;
134  }
double TruncMean::RMS ( const std::vector< double > &  v)
private

Definition at line 136 of file sbncode/sbncode/SinglePhotonAnalysis/Libraries/TruncMean.cxx.

137  {
138 
139  if(v.size()==1) return v.front();
140 
141  double avg = 0.;
142  for (auto const& val : v) avg += val;
143  avg /= v.size();
144  double rms = 0.;
145  for (auto const& val : v) rms += (val-avg)*(val-avg);
146  rms = sqrt( rms / ( v.size() - 1 ) );
147 
148 
149  if(rms!=rms){
150  std::cout<<"ERROR || TruncMean::RMS || is returning nan."<<std::endl;
151  std::cout<<"ERROR || TruncMean::RMS || "<<rms<<std::endl;
152  std::cout<<"ERROR || TruncMean::RMS || Input is of size "<<v.size()<<std::endl;
153  for(auto const &val : v){
154  std::cout<<"ERROR || TruncMean::RMS || "<<val<<std::endl;
155  }
156  std::cout<<"ERROR || TruncMean::RMS || Most likely because your radius is too small! "<<v.size()<<std::endl;
157  }
158 
159  return rms;
160  }
BEGIN_PROLOG could also be cout
void single_photon::TruncMean::setRadius ( const double &  rad)
inline

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

Definition at line 91 of file sbncode/sbncode/SinglePhotonAnalysis/Libraries/TruncMean.h.

Member Data Documentation

double single_photon::TruncMean::_rad
private

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

Definition at line 103 of file sbncode/sbncode/SinglePhotonAnalysis/Libraries/TruncMean.h.


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