14 #include "RtypesCore.h"
17 UShort_t
residesIn(Float_t raw, std::vector<Float_t>* dict)
19 for (
size_t i = 0;
i < dict->size(); ++
i)
21 if (raw <= dict->
at(
i))
25 else if ((dict->at(
i) - raw) < (raw - dict->at(
i - 1)))
31 return dict->size() - 1;
38 std::vector<UShort_t>* order,
39 std::vector<Float_t>* dict,
40 std::vector<size_t>* cnt,
43 std::normal_distribution<double>& distribution,
44 size_t maxNumClusters);
46 Int_t
newLoc(std::vector<Int_t>* loc_vec, std::vector<std::vector<Int_t>>* loc_vec_vec);
47 void removeDiff(Float_t
distance, Float_t
min, std::map<Float_t, std::set<Float_t>>* distance_min_set_map);
48 void addDiff(Float_t
distance, Float_t
min, std::map<Float_t, std::set<Float_t>>* distance_min_set_map);
50 Float_t
approx(std::vector<UShort_t>* order, std::vector<Float_t>* dict, std::vector<size_t>* cnt, Int_t n_entries, std::default_random_engine&
generator, std::normal_distribution<double>& distribution,
size_t maxNumClusters)
52 Float_t maxAbsErrorDoubled = (Float_t) 0;
54 std::map<Float_t, std::pair<Float_t, Int_t>> min_max_loc_map;
55 std::vector<std::vector<Int_t>> loc_vec_vec;
56 std::vector<Int_t> loc_vec;
57 std::map<Float_t, std::set<Float_t>> distance_min_set_map;
59 for (Int_t
j = 0;
j < n_entries;
j++)
61 Float_t number = distribution(generator);
62 Float_t* gen_ = &number;
64 std::map<Float_t, std::pair<Float_t, Int_t>>::iterator mmlm = min_max_loc_map.find(*gen_);
66 if (mmlm != min_max_loc_map.end())
67 loc_vec_vec[mmlm->second.second].push_back(
j);
70 Int_t
loc =
newLoc(&loc_vec, &loc_vec_vec);
72 loc_vec_vec[
loc].push_back(
j);
74 min_max_loc_map[*gen_] = std::pair<Float_t, Int_t>(*gen_,
loc);
76 mmlm = min_max_loc_map.find(*gen_);
77 if (mmlm != min_max_loc_map.begin() && *gen_ <= prev(mmlm)->second.first)
79 loc_vec_vec[prev(mmlm)->second.second].push_back(
j);
80 loc_vec_vec[mmlm->second.second].clear();
81 loc_vec.push_back(mmlm->second.second);
83 min_max_loc_map.erase(mmlm);
85 else if (min_max_loc_map.size() >= 2)
87 if (mmlm != min_max_loc_map.begin() && mmlm != prev(min_max_loc_map.end()))
89 removeDiff(
next(mmlm)->second.first - prev(mmlm)->first, prev(mmlm)->first, &distance_min_set_map);
92 if (mmlm != min_max_loc_map.begin())
93 addDiff(mmlm->second.first - prev(mmlm)->first, prev(mmlm)->first, &distance_min_set_map);
95 if (mmlm != prev(min_max_loc_map.end()))
96 addDiff(
next(mmlm)->second.first - mmlm->first, mmlm->first, &distance_min_set_map);
100 if (min_max_loc_map.size() <= maxNumClusters)
103 std::map<Float_t, std::set<Float_t>>::iterator dmsm = distance_min_set_map.begin();
104 Float_t
min = *(dmsm->second.begin());
106 dmsm->second.erase(min);
107 if (dmsm->second.empty())
108 distance_min_set_map.erase(dmsm);
110 mmlm = min_max_loc_map.find(min);
111 if (mmlm != min_max_loc_map.begin())
112 removeDiff(mmlm->second.first - prev(mmlm)->first, prev(mmlm)->first, &distance_min_set_map);
114 if (
next(mmlm) != prev(min_max_loc_map.end()))
117 std::vector<Int_t>*
s = &(loc_vec_vec[
next(mmlm)->second.second]);
118 loc_vec_vec[mmlm->second.second].insert(loc_vec_vec[mmlm->second.second].end(), s->begin(), s->end());
119 mmlm->second.first =
next(mmlm)->second.first;
120 min_max_loc_map.erase(
next(mmlm));
121 mmlm = min_max_loc_map.find(min);
122 maxAbsErrorDoubled = std::max(maxAbsErrorDoubled, mmlm->second.first - mmlm->first);
123 if (mmlm != min_max_loc_map.begin())
124 addDiff(mmlm->second.first - prev(mmlm)->first, prev(mmlm)->first, &distance_min_set_map);
126 if (mmlm != prev(min_max_loc_map.end()))
127 addDiff(
next(mmlm)->second.first - mmlm->first, mmlm->first, &distance_min_set_map);
130 order->resize(n_entries);
131 for (
const auto& mmlm : min_max_loc_map)
133 Double_t estimate = (Double_t) (mmlm.first + mmlm.second.first) / (Double_t) 2;
135 for (
const auto&
index : loc_vec_vec[mmlm.second.second])
137 (*order)[
index] = dict->size();
140 dict->push_back(estimate);
142 cnt->push_back(loc_vec_vec[mmlm.second.second].size());
145 return maxAbsErrorDoubled / (
double) 2;
148 Int_t
newLoc(std::vector<Int_t>* loc_vec, std::vector<std::vector<Int_t>>* loc_vec_vec)
150 if (!loc_vec->empty())
152 Int_t
loc = loc_vec->back();
157 Int_t
loc = loc_vec_vec->size();
158 loc_vec_vec->push_back({});
162 void removeDiff(Float_t
distance, Float_t
min, std::map<Float_t, std::set<Float_t>>* distance_min_set_map)
164 std::map<Float_t, std::set<Float_t>>::iterator dmsm = distance_min_set_map->find(distance);
165 dmsm->second.erase(min);
167 if (dmsm->second.empty())
168 distance_min_set_map->erase(dmsm);
171 void addDiff(Float_t distance, Float_t min, std::map<Float_t, std::set<Float_t>>* distance_min_set_map)
173 std::map<Float_t, std::set<Float_t>>::iterator dmsm = distance_min_set_map->find(distance);
174 if (dmsm == distance_min_set_map->end())
176 (*distance_min_set_map)[
distance] = {min};
180 dmsm->second.insert(min);