@@ -350,6 +350,7 @@ unsigned int GroupedCkfTrajectoryBuilder::groupedLimitedCandidates(const Traject
350350 unsigned int prevNewCandSize = 0 ;
351351 TempTrajectoryContainer candidates;
352352 TempTrajectoryContainer newCand;
353+ newCand.reserve (theMaxCand);
353354 candidates.push_back (startingTraj);
354355
355356 while (!candidates.empty ()) {
@@ -367,28 +368,11 @@ unsigned int GroupedCkfTrajectoryBuilder::groupedLimitedCandidates(const Traject
367368 nCands += newCand.size () - prevNewCandSize;
368369 prevNewCandSize = newCand.size ();
369370
370- if ((int )newCand.size () > theMaxCand) {
371- // ShowCand()(newCand);
372-
373- std::nth_element (newCand.begin (),
374- newCand.begin () + theMaxCand,
375- newCand.end (),
376- GroupedTrajCandLess (theLostHitPenalty, theFoundHitBonus));
377- newCand.erase (newCand.begin () + theMaxCand, newCand.end ());
378- }
379- LogDebug (" CkfPattern" ) << " newCand(2): after removing extra candidates.\n "
380- << PrintoutHelper::dumpCandidates (newCand);
371+ assert ((int )newCand.size () <= theMaxCand);
381372 }
382373
383374 LogDebug (" CkfPattern" ) << " newCand.size() at end = " << newCand.size ();
384- /*
385- if (theIntermediateCleaning) {
386- candidates.clear();
387- candidates = groupedIntermediaryClean(newCand);
388- } else {
389- candidates.swap(newCand);
390- }
391- */
375+
392376 if (theIntermediateCleaning) {
393377#ifdef STANDARD_INTERMEDIARYCLEAN
394378 IntermediateTrajectoryCleaner::clean (newCand);
@@ -478,6 +462,9 @@ bool GroupedCkfTrajectoryBuilder::advanceOneLayer(const TrajectorySeed& seed,
478462 TempTrajectoryContainer& result) const {
479463 std::pair<TSOS, std::vector<const DetLayer*> >&& stateAndLayers = findStateAndLayers (seed, traj);
480464
465+ bool full = (int )newCand.size () == theMaxCand;
466+ auto lessTraj = GroupedTrajCandLess (theLostHitPenalty, theFoundHitBonus);
467+
481468 if (maxPt2ForLooperReconstruction > 0 ) {
482469 if (
483470 // stateAndLayers.second.size()==0 &&
@@ -655,8 +642,22 @@ bool GroupedCkfTrajectoryBuilder::advanceOneLayer(const TrajectorySeed& seed,
655642 << " hits=" << newTraj.foundHits ();
656643
657644 newTraj.setStopReason (StopReason::NOT_STOPPED);
658- newCand.push_back (std::move (newTraj));
659- 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+ }
660661 } else {
661662 // Have finished building this track. Check if it passes cuts.
662663
0 commit comments