8#include <tracking/trackFindingCDC/numerics/Median.h>
10#include <tracking/trackFindingCDC/numerics/WeightComperator.h>
11#include <tracking/trackFindingCDC/utilities/Algorithms.h>
17double TrackFindingCDC::median(std::vector<double> values)
19 auto notfinite = [](
double v) {
return not std::isfinite(v); };
20 erase_remove_if(values, notfinite);
22 int n = values.size();
24 if (n == 0)
return NAN;
26 std::nth_element(values.begin(), values.begin() + n / 2, values.end());
29 double upper = values[n / 2];
30 double lower = *std::max_element(values.begin(), values.begin() + n / 2);
31 return (lower + upper) / 2;
37double TrackFindingCDC::weightedMedian(std::vector<WithWeight<double> > weightedValues)
39 auto notfinite = [](
const WithWeight<double>& weightedValue) {
40 return not std::isfinite(weightedValue) or not std::isfinite(weightedValue.getWeight());
42 erase_remove_if(weightedValues, notfinite);
44 if (weightedValues.empty())
return NAN;
46 std::sort(weightedValues.begin(), weightedValues.end());
49 Weight totalWeight = 0;
50 for (WithWeight<double>& weightedValue : weightedValues) {
51 totalWeight += weightedValue.getWeight();
52 weightedValue.setWeight(totalWeight);
55 auto it = std::partition_point(weightedValues.begin(),
57 GetWeight() < totalWeight / 2.0);
58 return it == weightedValues.end() ? weightedValues.back() : *it;
Abstract base class for different kinds of events.