Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BoostTrackBuilding.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file BoostTrackBuilding.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
11 #include "Acts/Utilities/Zip.hpp"
12 
13 #include <map>
14 
15 #include <boost/beast/core/span.hpp>
16 #include <boost/graph/adjacency_list.hpp>
17 #include <boost/graph/connected_components.hpp>
18 #include <torch/torch.h>
19 
20 namespace {
21 template <typename vertex_t, typename weight_t>
22 auto weaklyConnectedComponents(vertex_t numNodes,
23  boost::beast::span<vertex_t>& rowIndices,
24  boost::beast::span<vertex_t>& colIndices,
25  boost::beast::span<weight_t>& edgeWeights,
26  std::vector<vertex_t>& trackLabels) {
27  using Graph =
28  boost::adjacency_list<boost::vecS, // edge list
29  boost::vecS, // vertex list
30  boost::undirectedS, // directedness
31  boost::no_property, // property of vertices
32  weight_t // property of edges
33  >;
34 
35  Graph g(numNodes);
36 
37  for (const auto [row, col, weight] :
38  Acts::zip(rowIndices, colIndices, edgeWeights)) {
39  boost::add_edge(row, col, weight, g);
40  }
41 
42  return boost::connected_components(g, &trackLabels[0]);
43 }
44 } // namespace
45 
46 namespace Acts {
47 
48 std::vector<std::vector<int>> BoostTrackBuilding::operator()(
49  std::any nodes, std::any edges, std::any weights,
50  std::vector<int>& spacepointIDs, int) {
51  ACTS_DEBUG("Start track building");
52  const auto edgeTensor = std::any_cast<torch::Tensor>(edges).to(torch::kCPU);
53  const auto edgeWeightTensor =
54  std::any_cast<torch::Tensor>(weights).to(torch::kCPU);
55 
56  assert(edgeTensor.size(0) == 2);
57  assert(edgeTensor.size(1) == edgeWeightTensor.size(0));
58 
59  const auto numSpacepoints = spacepointIDs.size();
60  const auto numEdges = static_cast<std::size_t>(edgeWeightTensor.size(0));
61 
62  if (numEdges == 0) {
63  ACTS_WARNING("No edges remained after edge classification");
64  return {};
65  }
66 
67  using vertex_t = int64_t;
68  using weight_t = float;
69 
70  boost::beast::span<vertex_t> rowIndices(edgeTensor.data_ptr<vertex_t>(),
71  numEdges);
72  boost::beast::span<vertex_t> colIndices(
73  edgeTensor.data_ptr<vertex_t>() + numEdges, numEdges);
74  boost::beast::span<weight_t> edgeWeights(edgeWeightTensor.data_ptr<float>(),
75  numEdges);
76 
77  std::vector<vertex_t> trackLabels(numSpacepoints);
78 
79  auto numberLabels = weaklyConnectedComponents<vertex_t, weight_t>(
80  numSpacepoints, rowIndices, colIndices, edgeWeights, trackLabels);
81 
82  ACTS_VERBOSE("Number of track labels: " << trackLabels.size());
83  ACTS_VERBOSE("Number of unique track labels: " << [&]() {
84  std::vector<vertex_t> sorted(trackLabels);
85  std::sort(sorted.begin(), sorted.end());
86  sorted.erase(std::unique(sorted.begin(), sorted.end()), sorted.end());
87  return sorted.size();
88  }());
89 
90  if (trackLabels.size() == 0) {
91  return {};
92  }
93 
94  std::vector<std::vector<int>> trackCandidates(numberLabels);
95 
96  for (const auto [label, id] : Acts::zip(trackLabels, spacepointIDs)) {
97  trackCandidates[label].push_back(id);
98  }
99 
100  return trackCandidates;
101 }
102 
103 } // namespace Acts