File indexing completed on 2024-04-06 12:15:28
0001 #ifndef Geometry_TrackerNumberingBuilder_trackerStablePhiSort_H
0002 #define Geometry_TrackerNumberingBuilder_trackerStablePhiSort_H
0003
0004 #include <vector>
0005 #include <algorithm>
0006 #include <cmath>
0007
0008 template <class RandomAccessIterator, class Extractor>
0009 void trackerStablePhiSort(RandomAccessIterator begin, RandomAccessIterator end, const Extractor& extr) {
0010 using Scalar = decltype(extr(*begin));
0011 using Value = typename std::iterator_traits<RandomAccessIterator>::value_type;
0012 using Element = std::pair<Scalar, Value*>;
0013
0014 std::vector<Element> tmpvec(end - begin);
0015 std::transform(begin, end, tmpvec.begin(), [&extr](Value& it) { return Element(extr(it), &it); });
0016
0017 std::sort(tmpvec.begin(), tmpvec.end());
0018
0019
0020
0021
0022
0023 constexpr unsigned int nMaxModulesPerRing = 5;
0024 constexpr double phiMin = M_PI_4;
0025 constexpr double phiMax = 2 * M_PI - phiMin;
0026 constexpr double tolerance = 0.000001;
0027
0028 const unsigned int n = tmpvec.size();
0029
0030 if (n > nMaxModulesPerRing) {
0031
0032
0033 if (std::abs(tmpvec.back().first - 0) < tolerance
0034 || std::abs(tmpvec.back().first - 2 * M_PI) < tolerance)
0035 {
0036
0037 tmpvec.insert(tmpvec.begin(), tmpvec.back());
0038 tmpvec.pop_back();
0039 }
0040 } else {
0041
0042
0043
0044
0045 auto p = std::find_if(tmpvec.begin(), tmpvec.end(), [&phiMin](auto const& x) { return x.first > phiMin; });
0046
0047
0048
0049 if (p == tmpvec.end() || p->first >= phiMax) {
0050
0051
0052
0053 if (p != tmpvec.end()) {
0054 tmpvec.insert(tmpvec.begin(), p, tmpvec.end());
0055 tmpvec.resize(n);
0056 }
0057 }
0058 }
0059
0060
0061
0062 std::vector<Value> tmpvecy(n);
0063 std::transform(tmpvec.begin(), tmpvec.end(), tmpvecy.begin(), [](auto& x) { return *x.second; });
0064 std::copy(tmpvecy.begin(), tmpvecy.end(), begin);
0065 }
0066
0067 #endif