Skip to content

Commit 51b4137

Browse files
authored
Merge pull request #48266 from fwyzard/fix_CTPPS_crash_151x
Improve sorting of `DetSetVector<CTPPSPixelRecHit>`
2 parents 0455dbf + de14258 commit 51b4137

File tree

12 files changed

+679
-244
lines changed

12 files changed

+679
-244
lines changed

CondFormats/PPSObjects/interface/CTPPSPixelIndices.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace rpixValues {
6565
constexpr int ROCSizeInY = 52; // ROC col size in pixels
6666
// Default DET barrel size
6767
constexpr int defaultDetSizeInX = 160; // Det row size in pixels (2 ROCs)
68-
constexpr int defaultDetSizeInY = 156; // Det col size in pixels (3 ROCs)
68+
constexpr int defaultDetSizeInY = 156; // Det col size in pixels (3 ROCs)
6969

7070
// Check the limits
7171
constexpr bool CTPPS_CHECK_LIMITS = true;
@@ -168,13 +168,13 @@ class CTPPSPixelIndices {
168168
// The transformation depends on the ROC-ID
169169
if (rocId >= 0 && rocId < 3) {
170170
row = 159 - rowROC;
171-
172171
col = (rocId + 1) * rpixValues::ROCSizeInY - colROC - 1;
173172
} else if (rocId >= 3 && rocId < 6) {
174173
row = rowROC;
175-
176174
col = (5 - rocId) * rpixValues::ROCSizeInY + colROC;
177175
} else {
176+
row = -1;
177+
col = -1;
178178
edm::LogError("RPix") << "CTPPSPixelIndices: wrong ROC ID " << rocId;
179179
return -1;
180180
}
@@ -188,6 +188,7 @@ class CTPPSPixelIndices {
188188

189189
return 0;
190190
}
191+
191192
//**************************************************************************
192193
// Transform from the module indixes to the ROC indices.
193194
// col, row - indices in the Module

DataFormats/CTPPSReco/interface/CTPPSPixelRecHit.h

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef DataFormats_CTPPSReco_interface_CTPPSPixelRecHit_h
2+
#define DataFormats_CTPPSReco_interface_CTPPSPixelRecHit_h
3+
14
/*
25
*
36
* This is a part of CTPPS offline software.
@@ -6,11 +9,11 @@
69
*
710
*/
811

9-
#ifndef DataFormats_CTPPSReco_CTPPSPixelRecHit_H
10-
#define DataFormats_CTPPSReco_CTPPSPixelRecHit_H
12+
#include <cassert>
1113

1214
#include "DataFormats/GeometrySurface/interface/LocalError.h"
1315
#include "DataFormats/GeometryVector/interface/LocalPoint.h"
16+
#include "FWCore/Utilities/interface/isFinite.h"
1417

1518
// Reconstructed hits in CTPPS Pixel detector
1619

@@ -37,19 +40,21 @@ class CTPPSPixelRecHit {
3740
clusterSizeRow_(rowsize),
3841
clusterSizeCol_(colsize) {}
3942

40-
inline LocalPoint point() const { return thePoint_; }
41-
inline LocalError error() const { return theError_; }
43+
LocalPoint point() const { return thePoint_; }
44+
LocalError error() const { return theError_; }
45+
46+
bool isOnEdge() const { return isOnEdge_; }
47+
bool hasBadPixels() const { return hasBadPixels_; }
48+
bool spanTwoRocs() const { return spanTwoRocs_; }
4249

43-
inline bool isOnEdge() const { return isOnEdge_; }
44-
inline bool hasBadPixels() const { return hasBadPixels_; }
45-
inline bool spanTwoRocs() const { return spanTwoRocs_; }
50+
unsigned int minPixelRow() const { return minPixelRow_; }
51+
unsigned int minPixelCol() const { return minPixelCol_; }
4652

47-
inline unsigned int minPixelRow() const { return minPixelRow_; }
48-
inline unsigned int minPixelCol() const { return minPixelCol_; }
53+
unsigned int clusterSize() const { return clusterSize_; }
54+
unsigned int clusterSizeRow() const { return clusterSizeRow_; }
55+
unsigned int clusterSizeCol() const { return clusterSizeCol_; }
4956

50-
inline unsigned int clusterSize() const { return clusterSize_; }
51-
inline unsigned int clusterSizeRow() const { return clusterSizeRow_; }
52-
inline unsigned int clusterSizeCol() const { return clusterSizeCol_; }
57+
float sort_key() const { return thePoint_.mag2(); }
5358

5459
private:
5560
LocalPoint thePoint_;
@@ -67,6 +72,12 @@ class CTPPSPixelRecHit {
6772
unsigned int clusterSizeCol_;
6873
};
6974

70-
inline bool operator<(CTPPSPixelRecHit& a, CTPPSPixelRecHit& b) { return (a.point().mag() < b.point().mag()); };
75+
inline bool operator<(CTPPSPixelRecHit const& a, CTPPSPixelRecHit const& b) {
76+
float a_key = a.sort_key();
77+
float b_key = b.sort_key();
78+
assert(edm::isFinite(a_key));
79+
assert(edm::isFinite(b_key));
80+
return a_key < b_key;
81+
}
7182

72-
#endif
83+
#endif // DataFormats_CTPPSReco_interface_CTPPSPixelRecHit_h

DataFormats/Common/interface/DetSetVector.h

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,71 @@ behavior (usually a core dump).
3737

3838
#include <algorithm>
3939
#include <iterator>
40+
#include <numeric>
41+
#include <type_traits>
4042
#include <vector>
4143

42-
#include <type_traits>
43-
#include "boost/concept_check.hpp"
44+
#include <boost/concept_check.hpp>
4445

46+
#include "DataFormats/Common/interface/BoolCache.h"
4547
#include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
4648
#include "DataFormats/Common/interface/DetSet.h"
4749
#include "DataFormats/Common/interface/FillView.h"
4850
#include "DataFormats/Common/interface/Ref.h"
4951
#include "DataFormats/Common/interface/traits.h"
50-
5152
#include "FWCore/Utilities/interface/EDMException.h"
5253

53-
#include "DataFormats/Common/interface/BoolCache.h"
54-
5554
namespace edm {
55+
56+
namespace detail {
57+
58+
// from https://devblogs.microsoft.com/oldnewthing/20170104-00/?p=95115
59+
60+
template <typename Iter1, typename Iter2>
61+
void apply_permutation(Iter1 first, Iter1 last, Iter2 indices) {
62+
using T = std::iterator_traits<Iter1>::value_type;
63+
using Diff = std::iterator_traits<Iter2>::value_type;
64+
Diff length = last - first;
65+
for (Diff i = 0; i < length; i++) {
66+
Diff current = i;
67+
if (i != indices[current]) {
68+
T t{std::move(first[i])};
69+
while (i != indices[current]) {
70+
Diff next = indices[current];
71+
first[current] = std::move(first[next]);
72+
indices[current] = current;
73+
current = next;
74+
}
75+
first[current] = std::move(t);
76+
indices[current] = current;
77+
}
78+
}
79+
}
80+
81+
// from https://devblogs.microsoft.com/oldnewthing/20170106-00/?p=95135
82+
83+
template <typename Iter, typename UnaryOperation, typename Compare>
84+
void sort_by_key(Iter first, Iter last, UnaryOperation op, Compare comp) {
85+
using Diff = std::iterator_traits<Iter>::difference_type;
86+
using T = std::iterator_traits<Iter>::value_type;
87+
using Key = decltype(op(std::declval<T>()));
88+
Diff length = std::distance(first, last);
89+
std::vector<Key> keys;
90+
keys.reserve(length);
91+
std::transform(first, last, std::back_inserter(keys), [&](T& t) { return op(t); });
92+
std::vector<Diff> indices(length);
93+
std::iota(indices.begin(), indices.end(), static_cast<Diff>(0));
94+
std::sort(indices.begin(), indices.end(), [&](Diff a, Diff b) { return comp(keys[a], keys[b]); });
95+
apply_permutation(first, last, indices.begin());
96+
}
97+
98+
template <typename Iter, typename UnaryOperation>
99+
void sort_by_key(Iter first, Iter last, UnaryOperation op) {
100+
sort_by_key(first, last, op, std::less<>());
101+
}
102+
103+
} // namespace detail
104+
56105
class ProductID;
57106

58107
//------------------------------------------------------------
@@ -340,7 +389,11 @@ namespace edm {
340389
for (; i != e; ++i) {
341390
i->data.shrink_to_fit();
342391
// sort the Detset pointed to by
343-
std::sort(i->data.begin(), i->data.end());
392+
if constexpr (requires(const T& t) { t.sort_key(); }) {
393+
edm::detail::sort_by_key(i->data.begin(), i->data.end(), [](const T& t) { return t.sort_key(); });
394+
} else {
395+
std::sort(i->data.begin(), i->data.end());
396+
}
344397
}
345398
}
346399

DataFormats/Common/test/BuildFile.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
<bin name="testDataFormatsCommon" file="testRunner.cpp,testOwnVector.cc,testOneToOneAssociation.cc,testValueMap.cc,testOneToManyAssociation.cc,testAssociationVector.cc,testAssociationNew.cc,testValueMapNew.cc,testSortedCollection.cc,testRangeMap.cc,ref_t.cppunit.cc,DetSetRefVector_t.cppunit.cc,reftobasevector_t.cppunit.cc,cloningptr_t.cppunit.cc,ptr_t.cppunit.cc,ptrvector_t.cppunit.cc,containermask_t.cppunit.cc,reftobaseprod_t.cppunit.cc,handle_t.cppunit.cc">
1515
</bin>
1616

17-
<bin name="testDataFormatsCommonCatch2" file="DetSetVector_t.cpp,exDSTV.cpp,exTrie.cpp,OwnVector_t.cpp,Wrapper_t.cpp,testBoostRange.cpp,reftobase_t.cpp,">
18-
<use name="catch2"/>
17+
<bin name="testDataFormatsCommonCatch2" file="DetSetVector_t.cpp,DetSetVectorWithKey_t.cpp,exDSTV.cpp,exTrie.cpp,OwnVector_t.cpp,Wrapper_t.cpp,testBoostRange.cpp,reftobase_t.cpp">
18+
<use name="catch2"/>
1919
</bin>
2020

2121
<bin file="DictionaryTools_t.cpp">

0 commit comments

Comments
 (0)