10 #include <tracking/trackFindingCDC/numerics/Median.h>
12 #include <tracking/trackFindingCDC/numerics/WeightComperator.h>
13 #include <tracking/trackFindingCDC/utilities/Algorithms.h>
19 double TrackFindingCDC::median(std::vector<double> values)
21 auto notfinite = [](
double v) {
return not std::isfinite(v); };
22 erase_remove_if(values, notfinite);
24 int n = values.size();
26 if (n == 0)
return NAN;
28 std::nth_element(values.begin(), values.begin() + n / 2, values.end());
31 double upper = values[n / 2];
32 double lower = *std::max_element(values.begin(), values.begin() + n / 2);
33 return (lower + upper) / 2;
39 double TrackFindingCDC::weightedMedian(std::vector<WithWeight<double> > weightedValues)
41 auto notfinite = [](
const WithWeight<double>& weightedValue) {
42 return not std::isfinite(weightedValue) or not std::isfinite(weightedValue.getWeight());
44 erase_remove_if(weightedValues, notfinite);
46 if (weightedValues.empty())
return NAN;
48 std::sort(weightedValues.begin(), weightedValues.end());
51 Weight totalWeight = 0;
52 for (WithWeight<double>& weightedValue : weightedValues) {
53 totalWeight += weightedValue.getWeight();
54 weightedValue.setWeight(totalWeight);
57 auto it = std::partition_point(weightedValues.begin(),
59 GetWeight() < totalWeight / 2.0);
60 return it == weightedValues.end() ? weightedValues.back() : *it;