Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CugraphTrackBuilding.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CugraphTrackBuilding.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 <map>
12 
13 #include <torch/script.h>
14 
16 
17 namespace Acts {
18 
19 std::vector<std::vector<int>> CugraphTrackBuilding::operator()(
20  std::any, std::any edges, std::any edge_weights,
21  std::vector<int> &spacepointIDs, int) {
22  auto numSpacepoints = spacepointIDs.size();
23  auto edgesAfterFiltering = std::any_cast<std::vector<int64_t>>(edges);
24  auto numEdgesAfterF = edgesAfterFiltering.size() / 2;
25  auto gOutputCTen = std::any_cast<at::Tensor>(edge_weights);
26 
27  if (numEdgesAfterF == 0) {
28  return {};
29  }
30 
31  // ************
32  // Track Labeling with cugraph::connected_components
33  // ************
34  std::vector<int32_t> rowIndices;
35  std::vector<int32_t> colIndices;
36  std::vector<float> edgeWeights;
37  std::vector<int32_t> trackLabels(numSpacepoints);
38  std::copy(edgesAfterFiltering.begin(),
39  edgesAfterFiltering.begin() + numEdgesAfterF,
40  std::back_insert_iterator(rowIndices));
41  std::copy(edgesAfterFiltering.begin() + numEdgesAfterF,
42  edgesAfterFiltering.end(), std::back_insert_iterator(colIndices));
43  std::copy(gOutputCTen.data_ptr<float>(),
44  gOutputCTen.data_ptr<float>() + numEdgesAfterF,
45  std::back_insert_iterator(edgeWeights));
46 
47  ACTS_VERBOSE("run weaklyConnectedComponents");
48  weaklyConnectedComponents<int32_t, int32_t, float>(
49  rowIndices, colIndices, edgeWeights, trackLabels, logger());
50 
51  ACTS_DEBUG("size of components: " << trackLabels.size());
52  if (trackLabels.size() == 0) {
53  return {};
54  }
55 
56  std::vector<std::vector<int>> trackCandidates;
57  trackCandidates.clear();
58 
59  int existTrkIdx = 0;
60  // map labeling from MCC to customized track id.
61  std::map<int, int> trackLableToIds;
62 
63  for (auto idx = 0ul; idx < numSpacepoints; ++idx) {
64  int trackLabel = trackLabels[idx];
65  int spacepointID = spacepointIDs[idx];
66 
67  int trkId;
68  if (trackLableToIds.find(trackLabel) != trackLableToIds.end()) {
69  trkId = trackLableToIds[trackLabel];
70  trackCandidates[trkId].push_back(spacepointID);
71  } else {
72  // a new track, assign the track id
73  // and create a vector
74  trkId = existTrkIdx;
75  trackCandidates.push_back(std::vector<int>{trkId});
76  trackLableToIds[trackLabel] = trkId;
77  existTrkIdx++;
78  }
79  }
80 
81  return trackCandidates;
82 }
83 
84 } // namespace Acts