diff --git a/GNNTrackFinding/README.md b/GNNTrackFinding/README.md index 1655ef7..a89326b 100644 --- a/GNNTrackFinding/README.md +++ b/GNNTrackFinding/README.md @@ -359,6 +359,8 @@ overlaps. | `TrackBuilding` | `"connected-components"` | Track building algorithm, see below | | `WalkAddScore` | `0.6` | `"cc-and-walk"` only: score above which a neighbour is always followed | | `WalkMinScore` | `0.1` | `"cc-and-walk"` only: score below which the walk stops | +| `WalkBestPredecessor` | `false` | `"cc-and-walk"` only: follow u -> v only if it is v's best edge from an unused hit | +| `MinMeanEdgeScore` | `0` | `"cc-and-walk"` only: drop candidates whose mean internal edge score is below this | | `MinHitsPerTrack` | `3` | Minimum number of hits for a candidate to be fitted | | `PropagateBackward` | `false` | Extrapolate the fitted tracks towards the beamline | | `ExtrapolateToCalo` | `true` | Extrapolate the fitted tracks to the calorimeter face and add an `AtCalorimeter` track state | diff --git a/GNNTrackFinding/src/CCAndWalkTrackBuilding.cpp b/GNNTrackFinding/src/CCAndWalkTrackBuilding.cpp index 5b1bcb8..223b37e 100644 --- a/GNNTrackFinding/src/CCAndWalkTrackBuilding.cpp +++ b/GNNTrackFinding/src/CCAndWalkTrackBuilding.cpp @@ -82,6 +82,7 @@ CCAndWalkTrackBuilding::CCAndWalkTrackBuilding(const Config& cfg, std::unique_pt } std::vector CCAndWalkTrackBuilding::longestPathFrom(int start, const std::vector>& outEdges, + const std::vector>& inEdges, const std::vector& used) const { std::vector best{}; std::vector path{}; @@ -116,6 +117,25 @@ std::vector CCAndWalkTrackBuilding::longestPathFrom(int start, const std::v if (next.empty() && bestEdge != nullptr && bestEdge->score > m_cfg.minScore) { next.push_back(bestEdge->target); } + if (m_cfg.bestPredecessor) { + // Keep a target only if no other free hit has a better edge into it. + // The path is a handful of hits, so a linear scan of it is cheapest. + const auto scoreTo = [&](int target) { + for (const auto& edge : outEdges[node]) { + if (edge.target == target) { + return edge.score; + } + } + return 0.f; + }; + std::erase_if(next, [&](int target) { + const float score = scoreTo(target); + return std::ranges::any_of(inEdges[target], [&](const OutEdge& in) { + return in.target != node && !used[in.target] && std::ranges::find(path, in.target) == path.end() && + in.score > score; + }); + }); + } for (const int target : next) { self(self, target); @@ -202,11 +222,15 @@ std::vector> CCAndWalkTrackBuilding::operator()(PipelineTensors assert(eachPairOnce() && "CCAndWalkTrackBuilding got the same pair of hits twice"); std::vector> outEdges(numNodes); + std::vector> inEdges(m_cfg.bestPredecessor ? numNodes : 0); std::vector inDegree(numNodes, 0); UnionFind components(numNodes); for (const auto& [edge, score] : directed) { const auto [u, v] = edge; outEdges[u].push_back({v, score}); + if (m_cfg.bestPredecessor) { + inEdges[v].push_back({u, score}); + } ++inDegree[v]; components.unite(u, v); } @@ -235,6 +259,42 @@ std::vector> CCAndWalkTrackBuilding::operator()(PipelineTensors std::vector used(numNodes, false); std::size_t numSimple = 0; std::size_t numWalked = 0; + std::size_t numLowScore = 0; + + // Retires the hits of @p nodes and emits them as a candidate, unless the mean + // score of the edges between them is below Config::minMeanScore. + std::vector candidateOf(m_cfg.minMeanScore > 0.f ? numNodes : 0, -1); + const auto emit = [&](const std::vector& nodes) { + for (const int node : nodes) { + used[node] = true; + } + if (m_cfg.minMeanScore > 0.f) { + const int id = static_cast(numSimple + numWalked + numLowScore); + for (const int node : nodes) { + candidateOf[node] = id; + } + float sum = 0.f; + std::size_t count = 0; + for (const int node : nodes) { + for (const auto& edge : outEdges[node]) { + if (candidateOf[edge.target] == id) { + sum += edge.score; + ++count; + } + } + } + if (count == 0 || sum < m_cfg.minMeanScore * count) { + ++numLowScore; + return false; + } + } + candidates.emplace_back(); + candidates.back().reserve(nodes.size()); + for (const int node : nodes) { + candidates.back().push_back(spacePointIDs[node]); + } + return true; + }; for (auto& nodes : componentNodes) { if (nodes.size() < m_cfg.minCandidateSize) { @@ -246,13 +306,7 @@ std::vector> CCAndWalkTrackBuilding::operator()(PipelineTensors const bool isSimplePath = std::ranges::all_of(nodes, [&](int node) { return inDegree[node] <= 1 && outEdges[node].size() <= 1; }); if (isSimplePath) { - candidates.emplace_back(); - candidates.back().reserve(nodes.size()); - for (const int node : nodes) { - candidates.back().push_back(spacePointIDs[node]); - used[node] = true; - } - ++numSimple; + numSimple += emit(nodes); continue; } @@ -262,26 +316,21 @@ std::vector> CCAndWalkTrackBuilding::operator()(PipelineTensors if (used[start]) { continue; } - const std::vector path = longestPathFrom(start, outEdges, used); + const std::vector path = longestPathFrom(start, outEdges, inEdges, used); if (path.size() < m_cfg.minCandidateSize) { // Nothing worth keeping starts here. Only the starting hit is retired, // so the hits it reached stay available to a later, longer path. used[start] = true; continue; } - candidates.emplace_back(); - candidates.back().reserve(path.size()); - for (const int node : path) { - candidates.back().push_back(spacePointIDs[node]); - used[node] = true; - } - ++numWalked; + numWalked += emit(path); } } ACTS_DEBUG(fmt::format("Built {} track candidates from {} hits and {} edges: {} components were already paths, {} " - "came out of walking the rest", - candidates.size(), numNodes, directed.size(), numSimple, numWalked)); + "came out of walking the rest, {} dropped for a mean edge score below {}", + candidates.size(), numNodes, directed.size(), numSimple, numWalked, numLowScore, + m_cfg.minMeanScore)); return candidates; } diff --git a/GNNTrackFinding/src/CCAndWalkTrackBuilding.h b/GNNTrackFinding/src/CCAndWalkTrackBuilding.h index ec181f0..f1a1b0e 100644 --- a/GNNTrackFinding/src/CCAndWalkTrackBuilding.h +++ b/GNNTrackFinding/src/CCAndWalkTrackBuilding.h @@ -73,6 +73,14 @@ public: /// branching, so a pathological component would otherwise stall the job; /// hitting the bound returns the longest path found so far. std::size_t maxWalkSteps{10000}; + /// Best-predecessor rule: the walk moves from hit u to hit v only if u -> v + /// scores at least as high as every other edge into v from a hit that is + /// neither used nor on the current path. Without it a branch that joins + /// another track's chain later takes that track's hits. + bool bestPredecessor{false}; + /// Candidates whose mean edge score (over the edges between their own hits) + /// is below this are dropped. Their hits stay used. 0 keeps every candidate. + float minMeanScore{0.f}; }; CCAndWalkTrackBuilding(const Config& cfg, std::unique_ptr logger); @@ -83,7 +91,7 @@ public: const Config& config() const { return m_cfg; } private: - /// One outgoing edge of a node, pointing at the hit further out + /// One edge of a node: the hit at its other end and its score struct OutEdge { int target{}; float score{}; @@ -91,7 +99,10 @@ private: /// The longest path reachable from @p start, following the rules described in /// the class documentation. Returns the node indices in walking order. + /// @p inEdges lists, per node, the edges into it (OutEdge::target is then + /// the source hit); used only with Config::bestPredecessor. std::vector longestPathFrom(int start, const std::vector>& outEdges, + const std::vector>& inEdges, const std::vector& used) const; Config m_cfg; diff --git a/GNNTrackFinding/src/GNNTrackFinder.cpp b/GNNTrackFinding/src/GNNTrackFinder.cpp index 47b6354..5543d1b 100644 --- a/GNNTrackFinding/src/GNNTrackFinder.cpp +++ b/GNNTrackFinding/src/GNNTrackFinder.cpp @@ -512,7 +512,9 @@ void GNNTrackFinder::buildPipeline(const std::vector& embeddingScales, CCAndWalkTrackBuilding::Config{.distanceFeatureIndices = m_distanceFeatureIndices, .addScore = m_walkAddScore.value(), .minScore = m_walkMinScore.value(), - .minCandidateSize = m_minHitsPerTrk.value()}, + .minCandidateSize = m_minHitsPerTrk.value(), + .bestPredecessor = m_walkBestPredecessor.value(), + .minMeanScore = m_minMeanEdgeScore.value()}, m_logger->clone(name() + ".TrackBuilder")); } else { trackBuilder = std::make_shared(ActsPlugins::BoostTrackBuilding::Config{}, diff --git a/GNNTrackFinding/src/GNNTrackFinder.h b/GNNTrackFinding/src/GNNTrackFinder.h index af8429e..17649fe 100644 --- a/GNNTrackFinding/src/GNNTrackFinder.h +++ b/GNNTrackFinding/src/GNNTrackFinder.h @@ -153,6 +153,14 @@ struct GNNTrackFinder : public k4FWCore::Transformer m_walkBestPredecessor{ + this, "WalkBestPredecessor", false, + "\"cc-and-walk\" only: the walk moves from hit u to hit v only if u -> v is v's best-scoring incoming edge among " + "the hits not yet used (acorn's bestpred_walk). Stops a branch from taking the hits of another track."}; + Gaudi::Property m_minMeanEdgeScore{ + this, "MinMeanEdgeScore", 0.f, + "\"cc-and-walk\" only: drop candidates whose mean edge score, over the edges between their own hits, is below " + "this (acorn's min_track_score). 0 keeps every candidate."}; /// @name Kalman-fit configuration ///@{