@@ -219,13 +219,10 @@ void GroupedCkfTrajectoryBuilder::trajectories(const TrajectorySeed& seed,
219219}
220220
221221void 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.\n You 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
347342unsigned 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
847843void 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));
0 commit comments