Skip to content

Commit 5132df0

Browse files
authored
Merge pull request #44904 from VinInn/SmallTempTrajectory
Speedup (grouped)limitedCandidates by changing the implementation of the candidates' number limitation.
2 parents 067f78d + 194b3ed commit 5132df0

File tree

12 files changed

+210
-374
lines changed

12 files changed

+210
-374
lines changed

DataFormats/TrackCandidate/interface/TrajectoryStopReasons.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#ifndef TRAJECTORYSTOPREASONS_H
22
#define TRAJECTORYSTOPREASONS_H
33

4+
#include <cstdint>
45
#include <string>
56

6-
enum class StopReason {
7+
enum class StopReason : uint8_t {
78
UNINITIALIZED = 0,
89
MAX_HITS = 1,
910
MAX_LOST_HITS = 2,

RecoTracker/CkfPattern/interface/BaseCkfTrajectoryBuilder.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,14 @@ class BaseCkfTrajectoryBuilder : public TrajectoryBuilder {
7777

7878
static void fillPSetDescription(edm::ParameterSetDescription& iDesc);
7979

80-
// new interface returning the start Trajectory...
81-
virtual TempTrajectory buildTrajectories(const TrajectorySeed& seed,
82-
TrajectoryContainer& ret,
83-
unsigned int& nCandPerSeed,
84-
const TrajectoryFilter*) const {
80+
virtual void buildTrajectories(const TrajectorySeed& seed,
81+
TrajectoryContainer& ret,
82+
unsigned int& nCandPerSeed,
83+
const TrajectoryFilter*) const {
8584
assert(0 == 1);
86-
return TempTrajectory();
8785
}
8886

89-
virtual void rebuildTrajectories(TempTrajectory const& startingTraj,
90-
const TrajectorySeed& seed,
91-
TrajectoryContainer& result) const {
92-
assert(0 == 1);
93-
}
87+
virtual void rebuildTrajectories(const TrajectorySeed& seed, TrajectoryContainer& result) const { assert(0 == 1); }
9488

9589
void setNavigationSchool(NavigationSchool const* nv) { theNavigationSchool = nv; }
9690

RecoTracker/CkfPattern/interface/CkfTrajectoryBuilder.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,12 @@ class CkfTrajectoryBuilder : public BaseCkfTrajectoryBuilder {
4949
/// trajectories building starting from a seed
5050
void trajectories(const TrajectorySeed& seed, TrajectoryContainer& ret) const override;
5151

52-
// new interface returning the start Trajectory...
53-
TempTrajectory buildTrajectories(const TrajectorySeed&,
54-
TrajectoryContainer& ret,
55-
unsigned int& nCandPerSeed,
56-
const TrajectoryFilter*) const override;
57-
58-
void rebuildTrajectories(TempTrajectory const& startingTraj,
59-
const TrajectorySeed&,
60-
TrajectoryContainer& result) const override {}
52+
void buildTrajectories(const TrajectorySeed&,
53+
TrajectoryContainer& ret,
54+
unsigned int& nCandPerSeed,
55+
const TrajectoryFilter*) const override;
56+
57+
void rebuildTrajectories(const TrajectorySeed&, TrajectoryContainer& result) const override {}
6158

6259
/// set Event for the internal MeasurementTracker data member
6360
// virtual void setEvent(const edm::Event& event) const;

RecoTracker/CkfPattern/plugins/GroupedCkfTrajectoryBuilder.cc

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,10 @@ void GroupedCkfTrajectoryBuilder::trajectories(const TrajectorySeed& seed,
219219
}
220220

221221
void GroupedCkfTrajectoryBuilder::rebuildSeedingRegion(const TrajectorySeed& seed, TrajectoryContainer& result) const {
222-
TempTrajectory const& startingTraj = createStartingTrajectory(seed);
223-
rebuildTrajectories(startingTraj, seed, result);
222+
rebuildTrajectories(seed, result);
224223
}
225224

226-
void GroupedCkfTrajectoryBuilder::rebuildTrajectories(TempTrajectory const& startingTraj,
227-
const TrajectorySeed& seed,
228-
TrajectoryContainer& result) const {
225+
void GroupedCkfTrajectoryBuilder::rebuildTrajectories(const TrajectorySeed& seed, TrajectoryContainer& result) const {
229226
TempTrajectoryContainer work;
230227

231228
TrajectoryContainer final;
@@ -242,7 +239,7 @@ void GroupedCkfTrajectoryBuilder::rebuildTrajectories(TempTrajectory const& star
242239
if (traj.isValid())
243240
work.emplace_back(std::move(traj));
244241

245-
rebuildSeedingRegion(seed, startingTraj, work);
242+
rebuildSeedingRegion(seed, work);
246243

247244
// we clean here now
248245
FastTrajectoryCleaner cleaner(theFoundHitBonus, theLostHitPenalty, false);
@@ -259,10 +256,10 @@ void GroupedCkfTrajectoryBuilder::rebuildTrajectories(TempTrajectory const& star
259256
statCount.rebuilt(result.size());
260257
}
261258

262-
TempTrajectory GroupedCkfTrajectoryBuilder::buildTrajectories(const TrajectorySeed& seed,
263-
GroupedCkfTrajectoryBuilder::TrajectoryContainer& result,
264-
unsigned int& nCandPerSeed,
265-
const TrajectoryFilter* regionalCondition) const {
259+
void GroupedCkfTrajectoryBuilder::buildTrajectories(const TrajectorySeed& seed,
260+
GroupedCkfTrajectoryBuilder::TrajectoryContainer& result,
261+
unsigned int& nCandPerSeed,
262+
const TrajectoryFilter* regionalCondition) const {
266263
if (theMeasurementTracker == nullptr) {
267264
throw cms::Exception("LogicError")
268265
<< "Asking to create trajectories to an un-initialized GroupedCkfTrajectoryBuilder.\nYou have to call "
@@ -282,7 +279,7 @@ TempTrajectory GroupedCkfTrajectoryBuilder::buildTrajectories(const TrajectorySe
282279
const bool inOut = true;
283280
nCandPerSeed = groupedLimitedCandidates(seed, startingTraj, regionalCondition, forwardPropagator(seed), inOut, work_);
284281
if (work_.empty())
285-
return startingTraj;
282+
return;
286283

287284
// cleaning now done here...
288285
FastTrajectoryCleaner cleaner(theFoundHitBonus, theLostHitPenalty);
@@ -340,8 +337,6 @@ TempTrajectory GroupedCkfTrajectoryBuilder::buildTrajectories(const TrajectorySe
340337
std::cout << std::endl;
341338
}
342339
#endif
343-
344-
return startingTraj;
345340
}
346341

347342
unsigned int GroupedCkfTrajectoryBuilder::groupedLimitedCandidates(const TrajectorySeed& seed,
@@ -355,6 +350,7 @@ unsigned int GroupedCkfTrajectoryBuilder::groupedLimitedCandidates(const Traject
355350
unsigned int prevNewCandSize = 0;
356351
TempTrajectoryContainer candidates;
357352
TempTrajectoryContainer newCand;
353+
newCand.reserve(theMaxCand);
358354
candidates.push_back(startingTraj);
359355

360356
while (!candidates.empty()) {
@@ -372,28 +368,11 @@ unsigned int GroupedCkfTrajectoryBuilder::groupedLimitedCandidates(const Traject
372368
nCands += newCand.size() - prevNewCandSize;
373369
prevNewCandSize = newCand.size();
374370

375-
if ((int)newCand.size() > theMaxCand) {
376-
//ShowCand()(newCand);
377-
378-
std::nth_element(newCand.begin(),
379-
newCand.begin() + theMaxCand,
380-
newCand.end(),
381-
GroupedTrajCandLess(theLostHitPenalty, theFoundHitBonus));
382-
newCand.erase(newCand.begin() + theMaxCand, newCand.end());
383-
}
384-
LogDebug("CkfPattern") << "newCand(2): after removing extra candidates.\n"
385-
<< PrintoutHelper::dumpCandidates(newCand);
371+
assert((int)newCand.size() <= theMaxCand);
386372
}
387373

388374
LogDebug("CkfPattern") << "newCand.size() at end = " << newCand.size();
389-
/*
390-
if (theIntermediateCleaning) {
391-
candidates.clear();
392-
candidates = groupedIntermediaryClean(newCand);
393-
} else {
394-
candidates.swap(newCand);
395-
}
396-
*/
375+
397376
if (theIntermediateCleaning) {
398377
#ifdef STANDARD_INTERMEDIARYCLEAN
399378
IntermediateTrajectoryCleaner::clean(newCand);
@@ -483,6 +462,9 @@ bool GroupedCkfTrajectoryBuilder::advanceOneLayer(const TrajectorySeed& seed,
483462
TempTrajectoryContainer& result) const {
484463
std::pair<TSOS, std::vector<const DetLayer*> >&& stateAndLayers = findStateAndLayers(seed, traj);
485464

465+
bool full = (int)newCand.size() == theMaxCand;
466+
auto lessTraj = GroupedTrajCandLess(theLostHitPenalty, theFoundHitBonus);
467+
486468
if (maxPt2ForLooperReconstruction > 0) {
487469
if (
488470
//stateAndLayers.second.size()==0 &&
@@ -660,8 +642,22 @@ bool GroupedCkfTrajectoryBuilder::advanceOneLayer(const TrajectorySeed& seed,
660642
<< " hits=" << newTraj.foundHits();
661643

662644
newTraj.setStopReason(StopReason::NOT_STOPPED);
663-
newCand.push_back(std::move(newTraj));
664-
foundNewCandidates = true;
645+
if (full) {
646+
bool better = lessTraj(newTraj, newCand.front());
647+
if (better) {
648+
// replace worst
649+
foundNewCandidates = true;
650+
std::pop_heap(newCand.begin(), newCand.end(), lessTraj);
651+
newCand.back().swap(newTraj);
652+
std::push_heap(newCand.begin(), newCand.end(), lessTraj);
653+
} // else? no need to add it just to remove it later!
654+
} else {
655+
newCand.push_back(std::move(newTraj));
656+
foundNewCandidates = true;
657+
full = (int)newCand.size() == theMaxCand;
658+
if (full)
659+
std::make_heap(newCand.begin(), newCand.end(), lessTraj);
660+
}
665661
} else {
666662
// Have finished building this track. Check if it passes cuts.
667663

@@ -845,7 +841,6 @@ void GroupedCkfTrajectoryBuilder::groupedIntermediaryClean(TempTrajectoryContain
845841
}
846842

847843
void GroupedCkfTrajectoryBuilder::rebuildSeedingRegion(const TrajectorySeed& seed,
848-
TempTrajectory const& startingTraj,
849844
TempTrajectoryContainer& result) const {
850845
//
851846
// Rebuilding of trajectories. Candidates are taken from result,
@@ -870,7 +865,7 @@ void GroupedCkfTrajectoryBuilder::rebuildSeedingRegion(const TrajectorySeed& see
870865
// Refit - keep existing trajectory in case fit is not possible
871866
// or fails
872867
//
873-
868+
assert(it->isValid());
874869
auto&& reFitted = backwardFit(*it, nSeed, fitter, seedHits);
875870
if UNLIKELY (!reFitted.isValid()) {
876871
rebuiltTrajectories.push_back(std::move(*it));

RecoTracker/CkfPattern/plugins/GroupedCkfTrajectoryBuilder.h

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ class dso_internal GroupedCkfTrajectoryBuilder final : public BaseCkfTrajectoryB
4545
void trajectories(const TrajectorySeed&, TrajectoryContainer& ret, const TrackingRegion&) const;
4646

4747
/// common part of both public trajectory building methods
48-
// also new interface returning the start Trajectory...
49-
TempTrajectory buildTrajectories(const TrajectorySeed& seed,
50-
TrajectoryContainer& ret,
51-
unsigned int& nCandPerSeed,
52-
const TrajectoryFilter*) const override;
48+
void buildTrajectories(const TrajectorySeed& seed,
49+
TrajectoryContainer& ret,
50+
unsigned int& nCandPerSeed,
51+
const TrajectoryFilter*) const override;
5352

5453
/** trajectories re-building in the seeding region.
5554
It looks for additional measurements in the seeding region of the
@@ -58,11 +57,8 @@ class dso_internal GroupedCkfTrajectoryBuilder final : public BaseCkfTrajectoryB
5857
collection.
5958
**/
6059
void rebuildSeedingRegion(const TrajectorySeed&, TrajectoryContainer& result) const override;
61-
62-
// same as above using the precomputed startingTraj..
63-
void rebuildTrajectories(TempTrajectory const& startingTraj,
64-
const TrajectorySeed&,
65-
TrajectoryContainer& result) const override;
60+
// same as above FIXME
61+
void rebuildTrajectories(const TrajectorySeed&, TrajectoryContainer& result) const override;
6662

6763
// Access to lower level components
6864
const TrajectoryStateUpdator& updator() const { return *theUpdator; }
@@ -122,12 +118,10 @@ class dso_internal GroupedCkfTrajectoryBuilder final : public BaseCkfTrajectoryB
122118
TempTrajectoryContainer& result) const dso_internal;
123119

124120
/// try to find additional hits in seeding region
125-
void rebuildSeedingRegion(const TrajectorySeed& seed,
126-
TempTrajectory const& startingTraj,
127-
TempTrajectoryContainer& result) const dso_internal;
121+
void rebuildSeedingRegion(const TrajectorySeed& seed, TempTrajectoryContainer& result) const dso_internal;
128122

129-
//** try to find additional hits in seeding region for a candidate
130-
//* (returns number of trajectories added) *
123+
// ** try to find additional hits in seeding region for a candidate
124+
// * (returns number of trajectories added) *
131125
int rebuildSeedingRegion(const TrajectorySeed& seed,
132126
const std::vector<const TrackingRecHit*>& seedHits,
133127
TempTrajectory& candidate,

RecoTracker/CkfPattern/plugins/TrajectorySegmentBuilder.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ void TrajectorySegmentBuilder::cleanCandidates(vector<TempTrajectory>& candidate
527527
if (allFound) {
528528
candidates[*i1].invalidate();
529529
statCount.invalid();
530+
break;
530531
}
531532
}
532533
}

RecoTracker/CkfPattern/src/CkfTrackCandidateMakerBase.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ namespace cms {
281281
// Build trajectory from seed outwards
282282
theTmpTrajectories.clear();
283283
unsigned int nCandPerSeed = 0;
284-
auto const& startTraj =
285-
theTrajectoryBuilder->buildTrajectories((*collseed)[j], theTmpTrajectories, nCandPerSeed, nullptr);
284+
theTrajectoryBuilder->buildTrajectories((*collseed)[j], theTmpTrajectories, nCandPerSeed, nullptr);
286285
{
287286
Lock lock(theMutex);
288287
(*outputSeedStopInfos)[j].setCandidatesPerSeed(nCandPerSeed);
@@ -309,7 +308,7 @@ namespace cms {
309308
// seed and if possible further inwards.
310309

311310
if (doSeedingRegionRebuilding) {
312-
theTrajectoryBuilder->rebuildTrajectories(startTraj, (*collseed)[j], theTmpTrajectories);
311+
theTrajectoryBuilder->rebuildTrajectories((*collseed)[j], theTmpTrajectories);
313312

314313
LogDebug("CkfPattern") << "======== Out-in trajectory building found " << theTmpTrajectories.size()
315314
<< " valid/invalid trajectories from seed " << j << " ========\n"

0 commit comments

Comments
 (0)