All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sbncode/sbncode/SinglePhotonAnalysis/Libraries/TruncMean.h
Go to the documentation of this file.
1 /**
2  * \file TruncMean.h
3  *
4  * \ingroup 3DMichel
5  *
6  * \brief Class def header for a class TruncMean
7  *
8  * @author david caratelli [davidc@fnal.gov]
9  * Written 08/02/2018.
10  *
11  */
12 
13 #ifndef SBNCODE_SINGLEPHOTONANALYSIS_TRUNCMEAN_H
14 #define SBNCODE_SINGLEPHOTONANALYSIS_TRUNCMEAN_H
15 
16 #include <iostream>
17 #include <algorithm>
18 #include <cmath>
19 #include <vector>
20 #include <climits>
21 #include <limits>
22 
23 /**
24  \class TruncMean
25  The truncated mean class allows to compute the following quantities
26  1) the truncated mean profile of an ordered vector of values, such as
27  the charge profile along a particle's track.
28  To create such a profile use the function CalcTruncMeanProfile()
29  2) Get the truncated mean value of a distribution. This function
30  iteratively hones in on the truncated mean of a distribution by
31  updating the mean and cutting the tails after each iteration.
32  For this functionality use CalcIterativeTruncMean()
33  doxygen documentation!
34 */
35 
36 namespace single_photon
37 {
38  static const double kINVALID_FLOAT = std::numeric_limits<double>::max();
39 
40  class TruncMean{
41 
42  public:
43 
44  /// Default constructor
46 
47  /// Default destructor
49 
50  /**
51  @brief Given residual range and dq vectors return truncated local dq.
52  Input vectors are assumed to be match pair-wise (nth entry in rr_v
53  corresponds to nth entry in dq_v vector).
54  Input rr_v values are also assumed to be ordered: monotonically increasing
55  or decreasing.
56  For every dq value a truncated linear dq value is calculated as follows:
57  0) all dq values within a rr range set by the class variable _rad are selected.
58  1) the median and rms of these values is calculated.
59  2) the subset of local dq values within the range [median-rms, median+rms] is selected.
60  3) the resulting local truncated dq is the average of this truncated subset.
61  @input std::vector<double> rr_v -> vector of x-axis coordinates (i.e. position for track profile)
62  @input std::vector<double> dq_v -> vector of measured values for which truncated profile is requested
63  (i.e. charge profile of a track)
64  @input std::vector<double> dq_trunc_v -> passed by reference -> output stored here
65  @input double nsigma -> optional parameter, number of sigma to keep around RMS for TM calculation
66  */
67  void CalcTruncMeanProfile(const std::vector<double>& rr_v, const std::vector<double>& dq_v,
68  std::vector<double>& dq_trunc_v, const double& nsigma = 1);
69 
70  /**
71  @brief Iteratively calculate the truncated mean of a distribution
72  @brief: mean is returned if vecter's size is too small, or reach the max iteration, or median has converged
73  @input std::vector<double> v -> vector of values for which truncated mean is asked
74  @input size_t nmin -> minimum number of iterations to converge on truncated mean
75  @input size_t nmax -> maximum number of iterations to converge on truncated mean
76  @input size_t lmin -> minimum number of entries in vector before exiting and returning current value
77  @input size_t currentiteration -> current iteration
78  @input double convergencelimit -> fractional difference between successive iterations
79  under which the iteration is completed, provided nmin iterations have occurred.
80  @input nsigma -> number of sigma around the median value to keep when the distribution is trimmed.
81  */
82  double CalcIterativeTruncMean(std::vector<double> v, const size_t& nmin,
83  const size_t& nmax, const size_t& currentiteration,
84  const size_t& lmin,
85  const double& convergencelimit,
86  const double& nsigma, const double& oldmed = kINVALID_FLOAT);
87 
88  /**
89  @brief Set the smearing radius over which to take hits for truncated mean computaton.
90  */
91  void setRadius(const double& rad) { _rad = rad; }
92 
93  private:
94 
95  double Mean (const std::vector<double>& v);
96  double Median(const std::vector<double>& v);
97  double RMS (const std::vector<double>& v);
98 
99  /**
100  Smearing radius over which charge from neighboring hits is scanned to calculate local
101  truncated mean
102  */
103  double _rad;
104 
105  };
106 
107 
108 }
109 #endif // SBNCODE_SINGLEPHOTONANALYSIS_TRUNCMEAN_H
void setRadius(const double &rad)
Set the smearing radius over which to take hits for truncated mean computaton.
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 ...
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.