19 std::vector<UShort_t>* order,
20 std::vector<Float_t>* dict,
21 std::vector<size_t>* cnt,
28 Int_t
newLoc(std::vector<Int_t>* loc_vec, std::vector<std::set<Int_t>>* loc_set_vec);
29 void removeDiff(Float_t
distance, Float_t
min, std::map<Float_t, std::set<Float_t>>* distance_min_set_map);
30 void addDiff(Float_t
distance, Float_t
min, std::map<Float_t, std::set<Float_t>>* distance_min_set_map);
32 Float_t
approx(std::vector<UShort_t>* order, std::vector<Float_t>* dict, std::vector<size_t>* cnt, Int_t n_entries, TTree*
t, Float_t* gen_,
size_t maxNumClusters)
34 Float_t maxAbsErrorDoubled = (Float_t) 0;
36 std::map<Float_t, std::pair<Float_t, Int_t>> min_max_loc_map;
37 std::vector<std::set<Int_t>> loc_set_vec;
38 std::vector<Int_t> loc_vec;
39 std::map<Float_t, std::set<Float_t>> distance_min_set_map;
41 for (Int_t
j = 0 ;
j < n_entries;
j++){
44 std::map<Float_t, std::pair<Float_t, Int_t>>::iterator mmlm = min_max_loc_map.find(*gen_);
46 if (mmlm != min_max_loc_map.end())
47 loc_set_vec[mmlm->second.second].insert(
j);
49 Int_t
loc =
newLoc(&loc_vec, &loc_set_vec);
51 loc_set_vec[
loc].insert(
j);
53 min_max_loc_map[*gen_] = std::pair<Float_t, Int_t>(*gen_,
loc);
55 mmlm = min_max_loc_map.find(*gen_);
56 if (mmlm != min_max_loc_map.begin() && *gen_ <= prev(mmlm)->second.first) {
57 loc_set_vec[prev(mmlm)->second.second].insert(
j);
58 loc_set_vec[mmlm->second.second].clear();
59 loc_vec.push_back(mmlm->second.second);
61 min_max_loc_map.erase(mmlm);
63 }
else if (min_max_loc_map.size() >= 2) {
64 if (mmlm != min_max_loc_map.begin() && mmlm != prev(min_max_loc_map.end())) {
66 removeDiff(
next(mmlm)->second.first - prev(mmlm)->first, prev(mmlm)->first, &distance_min_set_map);
69 if (mmlm != min_max_loc_map.begin())
70 addDiff(mmlm->second.first - prev(mmlm)->first, prev(mmlm)->first, &distance_min_set_map);
72 if (mmlm != prev(min_max_loc_map.end()))
73 addDiff(
next(mmlm)->second.first - mmlm->first, mmlm->first, &distance_min_set_map);
77 if (min_max_loc_map.size() <= maxNumClusters)
80 std::map<Float_t, std::set<Float_t>>::iterator dmsm = distance_min_set_map.begin();
81 Float_t
min = *(dmsm->second.begin());
83 dmsm->second.erase(min);
84 if (dmsm->second.empty())
85 distance_min_set_map.erase(dmsm);
87 mmlm = min_max_loc_map.find(min);
88 if (mmlm != min_max_loc_map.begin())
89 removeDiff(mmlm->second.first - prev(mmlm)->first, prev(mmlm)->first, &distance_min_set_map);
91 if (
next(mmlm) != prev(min_max_loc_map.end()))
94 std::set<Int_t>*
s = &(loc_set_vec[
next(mmlm)->second.second]);
95 loc_set_vec[mmlm->second.second].insert(s->begin(), s->end());
96 mmlm->second.first =
next(mmlm)->second.first;
97 min_max_loc_map.erase(
next(mmlm));
98 mmlm = min_max_loc_map.find(min);
99 maxAbsErrorDoubled = std::max(maxAbsErrorDoubled, mmlm->second.first - mmlm->first);
100 if (mmlm != min_max_loc_map.begin())
101 addDiff(mmlm->second.first - prev(mmlm)->first, prev(mmlm)->first, &distance_min_set_map);
103 if (mmlm != prev(min_max_loc_map.end()))
104 addDiff(
next(mmlm)->second.first - mmlm->first, mmlm->first, &distance_min_set_map);
108 Double_t squaredSum = 0;
111 order->resize(n_entries);
112 for (
const auto &mmlm : min_max_loc_map) {
113 Double_t estimate = (Double_t) (mmlm.first + mmlm.second.first) / (Double_t) 2;
116 for (
const auto &
index : loc_set_vec[mmlm.second.second]) {
117 (*order)[
index] = dict->size();
120 Double_t
delta = std::fabs(*gen_ - estimate);
121 squaredSum += (delta *
delta);
125 dict->push_back(estimate);
127 cnt->push_back(loc_set_vec[mmlm.second.second].size());
130 Double_t avg = sum / (Double_t) n_entries;
131 return sqrt((squaredSum / (Double_t) n_entries) - avg * avg);
134 Int_t
newLoc(std::vector<Int_t>* loc_vec, std::vector<std::set<Int_t>>* loc_set_vec)
136 if (!loc_vec->empty()) {
137 Int_t
loc = loc_vec->back();
142 Int_t
loc = loc_set_vec->size();
143 loc_set_vec->push_back({});
150 std::map<Float_t, std::set<Float_t>>::iterator dmsm = distance_min_set_map->find(distance);
151 dmsm->second.erase(min);
153 if (dmsm->second.empty())
154 distance_min_set_map->erase(dmsm);
157 void addDiff(Float_t
distance, Float_t
min, std::map<Float_t, std::set<Float_t>>* distance_min_set_map)
159 std::map<Float_t, std::set<Float_t>>::iterator dmsm = distance_min_set_map->find(distance);
160 if (dmsm == distance_min_set_map->end()) {
161 (*distance_min_set_map)[
distance] = {min};
163 dmsm->second.insert(min);