TimeDistributionAlgorithm.cc
Go to the documentation of this file.
1 
9 #include <utl/TimeDistributionAlgorithm.h>
10 #include <utl/TimeDistribution.h>
11 
12 #include <utl/ErrorLogger.h>
13 
14 #include <cmath>
15 
16 using namespace utl;
17 using namespace std;
18 
19 
20 template<typename T>
21 double
22 TimeDistributionAlgorithm::Sum(const TimeDistribution<T>& td, const int bin1, const int bin2)
23 {
24  const int rangeStart = max(td.GetStart(), min(bin1, bin2));
25  const int rangeStop = min(td.GetStop(), max(bin1, bin2));
26 
27  if (td.fData.find(rangeStart) == td.fData.end() ||
28  td.fData.find(rangeStop) == td.fData.end())
29  return 0; // completely outside range containing valid data
30 
31  double sum = 0.0;
32  typedef typename TimeDistribution<T>::InternalConstIterator TDCIt;
33  const TDCIt mStop = ++(td.fData.find(rangeStop));
34  for (TDCIt mIt = td.fData.find(rangeStart); mIt != mStop; ++mIt)
35  sum += mIt->second;
36 
37  return sum;
38 }
39 
40 
41 template<typename T>
42 double
43 TimeDistributionAlgorithm::Mean(const TimeDistribution<T>& td, const int bin1, const int bin2)
44 {
45  return Sum(td, bin1, bin2) / (abs(bin2 - bin1) + 1);
46 }
47 
48 
49 template<typename T>
50 double
51 TimeDistributionAlgorithm::RMS(const TimeDistribution<T>& td, const int bin1, const int bin2)
52 {
53  const int rangeStart = max(td.GetStart(), min(bin1, bin2));
54  const int rangeStop = min(td.GetStop(), max(bin1, bin2));
55 
56  if (td.fData.find(rangeStart) == td.fData.end() ||
57  td.fData.find(rangeStop) == td.fData.end())
58  return 0; // completely outside range containing valid data
59 
60  double v = 0;
61  double v2 = 0;
62  // ATTENTION use of td.fData can be replaced with use of SlotIterator once that is ready (I think)
63  for (int i = rangeStart; i <= rangeStop; ++i) {
64  const double slotContents = td.At(i);
65  v += slotContents;
66  v2 += slotContents * slotContents;
67  }
68  const int n = abs(bin2 - bin1) + 1;
69  v /= n;
70  v2 /= n;
71 
72  return sqrt(v2 - v*v);
73 }
74 
75 
76 template<typename T>
78 TimeDistributionAlgorithm::MeanRMS(const TimeDistribution<T>& td, const int bin1, const int bin2)
79 {
80  const int rangeStart = max(td.GetStart(), min(bin1, bin2));
81  const int rangeStop = min(td.GetStop(), max(bin1, bin2));
82 
83  if (td.fData.find(rangeStart) == td.fData.end() ||
84  td.fData.find(rangeStop) == td.fData.end())
85  return TupleD(0, 0); // completely outside range containing valid data
86 
87  double v = 0;
88  double v2 = 0;
89  // ATTENTION use of td.fData can be replaced with use of SlotIterator once that is ready (I think)
90  for (int i = rangeStart; i <= rangeStop; ++i) {
91  const double slotContents = td.At(i);
92  v += slotContents;
93  v2 += slotContents * slotContents;
94  }
95  const int n = abs(bin2 - bin1) + 1;
96  v /= n;
97  v2 /= n;
98 
99  return TupleD(v, sqrt(v2 - v*v));
100 }
101 
102 
103 template<typename T>
104 double
105 TimeDistributionAlgorithm::Centroid(const TimeDistribution<T>& td, const int bin1, const int bin2)
106 {
107  const int rangeStart = max(td.GetStart(), min(bin1, bin2));
108  const int rangeStop = min(td.GetStop(), max(bin1, bin2));
109  if (td.fData.find(rangeStart) == td.fData.end() ||
110  td.fData.find(rangeStop) == td.fData.end())
111  return 0; // completely outside range containing valid data
112 
113  double sum = 0;
114  double val = 0;
115  for (int i = rangeStart; i <= rangeStop; ++i) {
116  const double slotContent = td.At(i);
117  sum += slotContent;
118  val += slotContent * i;
119  }
120 
121  return val / sum;
122 }
123 
124 
125 // instantations
126 #define INST(T...) \
127 template double TimeDistributionAlgorithm::Sum(const TimeDistribution<T>& td, const int bin1, const int bin2); \
128 template double TimeDistributionAlgorithm::Mean(const TimeDistribution<T>& td, const int bin1, const int bin2); \
129 template double TimeDistributionAlgorithm::RMS(const TimeDistribution<T>& td, const int bin1, const int bin2); \
130 template TimeDistributionAlgorithm::TupleD TimeDistributionAlgorithm::MeanRMS(const TimeDistribution<T>& td, const int bin1, const int bin2); \
131 template double TimeDistributionAlgorithm::Centroid(const TimeDistribution<T>& td, const int bin1, const int bin2)
132 
133 INST(int);
134 INST(double);
135 
136 #undef INST
137 
138 
139 // Configure (x)emacs for this file ...
140 // Local Variables:
141 // mode: c++
142 // compile-command: "make -C .. -k"
143 // End:
T At(const double time) const
int GetStart() const
First slot with data.
static double RMS(const TimeDistribution< T > &td, const int bin1, const int bin2)
Evalueate RMS of TimeDistribution between bin1 and bin2.
#define INST(T...)
boost::tuple< double, double > TupleD
Histogram class for time distributions with suppressed empty bins.
InternalMap::const_iterator InternalConstIterator
#define max(a, b)
double abs(const SVector< n, T > &v)
int GetStop() const
Last slot with data (1 less than First slot if no data)
static TupleD MeanRMS(const TimeDistribution< T > &td, const int bin1, const int bin2)
Evalueate both mean and RMS of TimeDistribution between bin1 and bin2.
static double Centroid(const TimeDistribution< T > &td, const int bin1, const int bin2)
Evaluate the centroid in slot number between bin1 and bin2.
static double Sum(const TimeDistribution< T > &td, const int bin1, const int bin2)
Evaluate the sum of bin1 through bin2.
static double Mean(const TimeDistribution< T > &td, const int bin1, const int bin2)
Evaluate mean of TimeDistribution between bin1 and bin2.

, generated on Tue Sep 26 2023.