ATLAS Offline Software
FPGATrackSimGNNRoadMakerTool.cxx
Go to the documentation of this file.
1 
2 // Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 
6 
8 // AthAlgTool
9 
10 FPGATrackSimGNNRoadMakerTool::FPGATrackSimGNNRoadMakerTool(const std::string& algname, const std::string &name, const IInterface *ifc)
11  : AthAlgTool(algname, name, ifc) {}
12 
14 {
15  ATH_CHECK(m_FPGATrackSimMapping.retrieve());
16  m_nLayers = m_FPGATrackSimMapping->PlaneMap_1st(0)->getNLogiLayers();
17  ATH_CHECK(m_layerNumberTool.retrieve());
18  m_pix_h2l = m_layerNumberTool->pixelLayers();
19  m_layerGeometry = m_layerNumberTool->layerGeometry();
20 
21  return StatusCode::SUCCESS;
22 }
23 
25 // Functions
26 
27 StatusCode FPGATrackSimGNNRoadMakerTool::makeRoads(const std::vector<std::shared_ptr<const FPGATrackSimHit>> & hits, const std::vector<std::shared_ptr<FPGATrackSimGNNHit>> & gnn_hits, const std::vector<std::shared_ptr<FPGATrackSimGNNEdge>> & edges, std::vector<std::shared_ptr<const FPGATrackSimRoad>> & roads)
28 {
29  m_num_nodes = gnn_hits.size();
30  doScoreCut(edges);
31 
32  if(m_roadMakerTool == "ConnectedComponents") {
34  addRoads(hits, gnn_hits, roads);
35  }
36 
37  resetVectors();
38 
39  return StatusCode::SUCCESS;
40 }
41 
42 void FPGATrackSimGNNRoadMakerTool::doScoreCut(const std::vector<std::shared_ptr<FPGATrackSimGNNEdge>> & edges)
43 {
44  for (const auto& edge : edges) {
45  if(edge->getEdgeScore() > m_edgeScoreCut) {
46  m_pass_edge_index_1.push_back(edge->getEdgeIndex1());
47  m_pass_edge_index_2.push_back(edge->getEdgeIndex2());
48  }
49  }
50 }
51 
53 {
54  // Remove isolated nodes from list of nodes using list of edges and indices
57 
58  int index = 0;
59  for (int node : m_unique_nodes) {
60  m_node_index_map[node] = index++; // Mapping original node index to new graph index
61  }
62 
63  for (const auto& entry : m_node_index_map) {
64  m_unique_indices.push_back(entry.first); // Push the original node index into unique_indices
65  }
66 
67  m_Graph g(m_unique_nodes.size());
68 
69  for (size_t i = 0; i < m_pass_edge_index_1.size(); i++) {
72  add_edge(u, v, g); // Add the edge between the mapped node indices
73  }
74 
75  m_component.resize(num_vertices(g), -1);
76  m_num_components = boost::connected_components(g, &m_component[0]);
77 
78  m_labels.resize(m_num_nodes,-1);
79 
80  for (size_t i = 0; i < m_unique_indices.size(); i++) {
82  }
83 }
84 
85 void FPGATrackSimGNNRoadMakerTool::addRoads(const std::vector<std::shared_ptr<const FPGATrackSimHit>> & hits,
86  const std::vector<std::shared_ptr<FPGATrackSimGNNHit>> & gnn_hits,
87  std::vector<std::shared_ptr<const FPGATrackSimRoad>> & roads)
88 {
89  roads.clear();
90  m_roads.clear();
91 
93  for (size_t i = 0; i < m_labels.size(); i++) {
94  if(m_labels[i] != -1) {
95  m_road_hit_list[m_labels[i]].push_back(gnn_hits[i]->getHitID());
96  }
97  }
98 
99  for (const auto& hit_list : m_road_hit_list) {
100  if(m_doGNNPixelSeeding) { addRoadForPixelSeed(hits, hit_list); }
101  else { addRoad(hits, hit_list); }
102  }
103 
104  roads.reserve(m_roads.size());
105  for (const FPGATrackSimRoad & r : m_roads) roads.emplace_back(std::make_shared<const FPGATrackSimRoad>(r));
106 }
107 
108 void FPGATrackSimGNNRoadMakerTool::addRoad(const std::vector<std::shared_ptr<const FPGATrackSimHit>> & hits, const std::vector<int>& road_hitIDs)
109 {
110  // Take the HitID values and find the correct hit from FPGATrackSimHit, then insert it into a vector of mapped FPGATrackSimHit which are needed for the FPGATrackSimRoad
111  std::vector<std::shared_ptr<const FPGATrackSimHit>> mapped_road_hits;
112  const FPGATrackSimPlaneMap *pmap = m_FPGATrackSimMapping->PlaneMap_1st(0);
113  layer_bitmask_t hitLayers = 0;
114 
115  for (const auto& hitID : road_hitIDs) {
116  if(hitID+1 < static_cast<int>(hits.size()) && hits[hitID]->isStrip() && hits[hitID+1]->isStrip() &&
117  to_string(hits[hitID]->getHitType()) == "spacepoint" && to_string(hits[hitID+1]->getHitType()) == "spacepoint" &&
118  hits[hitID]->getX() == hits[hitID+1]->getX()) {
119  auto &hit1 = hits[hitID];
120  std::shared_ptr<FPGATrackSimHit> hitCopy1 = std::make_shared<FPGATrackSimHit>(*hit1);
121  pmap->map(*hitCopy1);
122  hitLayers |= 1 << hitCopy1->getLayer();
123  mapped_road_hits.push_back(std::move(hitCopy1));
124  //
125  auto &hit2 = hits[hitID+1];
126  std::shared_ptr<FPGATrackSimHit> hitCopy2 = std::make_shared<FPGATrackSimHit>(*hit2);
127  pmap->map(*hitCopy2);
128  hitLayers |= 1 << hitCopy2->getLayer();
129  mapped_road_hits.push_back(std::move(hitCopy2));
130  }
131  else {
132  auto &hit = hits[hitID];
133  std::shared_ptr<FPGATrackSimHit> hitCopy = std::make_shared<FPGATrackSimHit>(*hit);
134  pmap->map(*hitCopy);
135  hitLayers |= 1 << hitCopy->getLayer();
136  mapped_road_hits.push_back(std::move(hitCopy));
137  }
138  }
139 
140  m_roads.emplace_back();
141  FPGATrackSimRoad & r = m_roads.back();
142  auto sorted_hits = ::sortByLayer(mapped_road_hits);
143  sorted_hits.resize(m_nLayers);
144  r.setRoadID(m_roads.size() - 1);
145  r.setHitLayers(hitLayers);
146  r.setHits(std::vector<std::vector<std::shared_ptr<const FPGATrackSimHit>>>(std::move(sorted_hits)));
147  r.setSubRegion(0);
148 }
149 
150 void FPGATrackSimGNNRoadMakerTool::addRoadForPixelSeed(const std::vector<std::shared_ptr<const FPGATrackSimHit>> & hits, const std::vector<int>& road_hitIDs)
151 {
152  std::vector<std::shared_ptr<const FPGATrackSimHit>> all_hits;
153  std::vector<std::shared_ptr<const FPGATrackSimHit>> track_hit_list;
154 
155  for (const auto& hitID : road_hitIDs) {
156  auto &hit = hits[hitID]; //Hit object inside the road candidate
157  all_hits.push_back(hit);
158  }
159 
160  // Sort hits in CC list by rho
161  std::sort(all_hits.begin(), all_hits.end(),
162  [](const std::shared_ptr<const FPGATrackSimHit>& hit1,
163  const std::shared_ptr<const FPGATrackSimHit>& hit2) {
164  double rho1 = std::hypot(hit1->getX(), hit1->getY());
165  double rho2 = std::hypot(hit2->getX(), hit2->getY());
166  return rho1 < rho2;
167  });
168 
169  // From all the hits in the CC list, only accept one per global layer ID, defined by the layer configuration from GBTS
170  std::unordered_set<int> seenLayers;
171  for (const auto &hit : all_hits) {
172  short layer = m_pix_h2l->at(static_cast<int>(hit->getIdentifierHash()));
173  TrigInDetSiLayer layerGeometry = m_layerGeometry->at(layer);
174  int combinedId = layerGeometry.m_subdet;
175 
176  if (seenLayers.count(combinedId) == 0) { // unique layer
177  track_hit_list.push_back(hit);
178  seenLayers.insert(combinedId);
179  }
180  }
181 
182  std::size_t nSp = track_hit_list.size();
183  std::vector<std::vector<std::shared_ptr<const FPGATrackSimHit>>> seed_hit_list;
184 
185  // Use a method to choose which spacepoints to keep for the road, which becomes a seed
186  // Need at least 3 spacepoints to form a seed, so only consider 3 spacepoint lists of hits-per-layer
187  // For >=4 spacepoints, consider a evenly-spaced method instead of first N spacepoints
188  // So the road candidate can have anywhere from 3-5 spacepoints
189  auto spacePointIndicesFun = [](std::size_t nSp, std::size_t nSeeds) -> std::vector<std::size_t> {
190  std::vector<std::size_t> idx;
191 
192  for (std::size_t i = 0; i < nSeeds; ++i) {
193  std::size_t pos = (i * (nSp - 1)) / (nSeeds - 1); // evenly spaced
194  if (idx.empty() || idx.back() != pos) { // avoid duplicates
195  idx.push_back(pos);
196  }
197  }
198  return idx;
199  };
200 
201  // Only keep road candidates with at >= 3 spacepoints in unique layers
202  if (nSp >= 3) {
203  std::size_t nSeeds = std::min<std::size_t>(5, nSp);
204  auto indices = spacePointIndicesFun(nSp, nSeeds);
205 
206  for (auto idx : indices) {
207  seed_hit_list.push_back({track_hit_list[idx]});
208  }
209 
210  m_roads.emplace_back();
211  FPGATrackSimRoad & r = m_roads.back();
212  r.setHits(std::move(seed_hit_list));
213  r.setRoadID(m_roads.size() - 1);
214  }
215 }
216 
218 {
219  m_pass_edge_index_1.clear();
220  m_pass_edge_index_2.clear();
221  m_unique_nodes.clear();
222  m_node_index_map.clear();
223  m_unique_indices.clear();
224  m_component.clear();
225  m_labels.clear();
226  m_road_hit_list.clear();
227 }
FPGATrackSimGNNRoadMakerTool::m_layerNumberTool
ToolHandle< ITrigL2LayerNumberTool > m_layerNumberTool
Definition: FPGATrackSimGNNRoadMakerTool.h:58
beamspotman.r
def r
Definition: beamspotman.py:672
FPGATrackSimGNNRoadMakerTool::m_edgeScoreCut
Gaudi::Property< float > m_edgeScoreCut
Definition: FPGATrackSimGNNRoadMakerTool.h:63
getMenu.algname
algname
Definition: getMenu.py:54
FPGATrackSimGNNRoadMakerTool::m_component
std::vector< int > m_component
Definition: FPGATrackSimGNNRoadMakerTool.h:78
FPGATrackSimGNNRoadMakerTool::addRoads
void addRoads(const std::vector< std::shared_ptr< const FPGATrackSimHit >> &hits, const std::vector< std::shared_ptr< FPGATrackSimGNNHit >> &gnn_hits, std::vector< std::shared_ptr< const FPGATrackSimRoad >> &roads)
Definition: FPGATrackSimGNNRoadMakerTool.cxx:85
TRTCalib_Extractor.hits
hits
Definition: TRTCalib_Extractor.py:35
FPGATrackSimGNNRoadMakerTool::initialize
virtual StatusCode initialize() override
Definition: FPGATrackSimGNNRoadMakerTool.cxx:13
FPGATrackSimGNNRoadMakerTool::m_roadMakerTool
Gaudi::Property< std::string > m_roadMakerTool
Definition: FPGATrackSimGNNRoadMakerTool.h:64
FPGATrackSimGNNRoadMakerTool::m_num_nodes
int m_num_nodes
Definition: FPGATrackSimGNNRoadMakerTool.h:70
FPGATrackSimPlaneMap.h
Maps physical layers to logical layers.
index
Definition: index.py:1
FPGATrackSimGNNRoadMakerTool::addRoadForPixelSeed
void addRoadForPixelSeed(const std::vector< std::shared_ptr< const FPGATrackSimHit >> &hits, const std::vector< int > &road_hitIDs)
Definition: FPGATrackSimGNNRoadMakerTool.cxx:150
FPGATrackSimGNNRoadMakerTool.h
Implements algorithm to construct a road from a list of hits using edge scores.
Trk::indices
std::pair< long int, long int > indices
Definition: AlSymMatBase.h:24
FPGATrackSimGNNRoadMakerTool::doConnectedComponents
void doConnectedComponents()
Definition: FPGATrackSimGNNRoadMakerTool.cxx:52
FPGATrackSimGNNRoadMakerTool::m_pass_edge_index_2
std::vector< int > m_pass_edge_index_2
Definition: FPGATrackSimGNNRoadMakerTool.h:72
FPGATrackSimGNNRoadMakerTool::makeRoads
virtual StatusCode makeRoads(const std::vector< std::shared_ptr< const FPGATrackSimHit >> &hits, const std::vector< std::shared_ptr< FPGATrackSimGNNHit >> &gnn_hits, const std::vector< std::shared_ptr< FPGATrackSimGNNEdge >> &edges, std::vector< std::shared_ptr< const FPGATrackSimRoad >> &roads)
Definition: FPGATrackSimGNNRoadMakerTool.cxx:27
FPGATrackSimGNNRoadMakerTool::m_doGNNPixelSeeding
Gaudi::Property< bool > m_doGNNPixelSeeding
Definition: FPGATrackSimGNNRoadMakerTool.h:65
FPGATrackSimHit::getLayer
int getLayer() const
Definition: FPGATrackSimHit.cxx:87
FPGATrackSimGNNRoadMakerTool::m_unique_nodes
std::set< int > m_unique_nodes
Definition: FPGATrackSimGNNRoadMakerTool.h:74
FPGATrackSimGNNRoadMakerTool::addRoad
void addRoad(const std::vector< std::shared_ptr< const FPGATrackSimHit >> &hits, const std::vector< int > &road_hitIDs)
Definition: FPGATrackSimGNNRoadMakerTool.cxx:108
FPGATrackSimGNNRoadMakerTool::m_node_index_map
std::map< int, int > m_node_index_map
Definition: FPGATrackSimGNNRoadMakerTool.h:75
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
FPGATrackSimGNNRoadMakerTool::resetVectors
void resetVectors()
Definition: FPGATrackSimGNNRoadMakerTool.cxx:217
FPGATrackSimGNNRoadMakerTool::m_nLayers
unsigned m_nLayers
Definition: FPGATrackSimGNNRoadMakerTool.h:73
FPGATrackSimGNNRoadMakerTool::m_labels
std::vector< int > m_labels
Definition: FPGATrackSimGNNRoadMakerTool.h:80
FPGATrackSimPlaneMap::map
void map(FPGATrackSimHit &hit) const
Definition: FPGATrackSimPlaneMap.cxx:234
lumiFormat.i
int i
Definition: lumiFormat.py:85
FPGATrackSimGNNRoadMakerTool::m_pix_h2l
const std::vector< short > * m_pix_h2l
Definition: FPGATrackSimGNNRoadMakerTool.h:82
python.CaloCondTools.g
g
Definition: CaloCondTools.py:15
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
FPGATrackSimGNNRoadMakerTool::m_pass_edge_index_1
std::vector< int > m_pass_edge_index_1
Definition: FPGATrackSimGNNRoadMakerTool.h:71
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
PyPoolBrowser.node
node
Definition: PyPoolBrowser.py:131
FPGATrackSimGNNRoadMakerTool::FPGATrackSimGNNRoadMakerTool
FPGATrackSimGNNRoadMakerTool(const std::string &, const std::string &, const IInterface *)
Definition: FPGATrackSimGNNRoadMakerTool.cxx:10
FPGATrackSimPlaneMap
Definition: FPGATrackSimPlaneMap.h:62
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
FPGATrackSimGNNRoadMakerTool::m_FPGATrackSimMapping
ServiceHandle< IFPGATrackSimMappingSvc > m_FPGATrackSimMapping
Definition: FPGATrackSimGNNRoadMakerTool.h:57
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
FPGATrackSimGNNRoadMakerTool::m_layerGeometry
const std::vector< TrigInDetSiLayer > * m_layerGeometry
Definition: FPGATrackSimGNNRoadMakerTool.h:83
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:16
python.PyAthena.v
v
Definition: PyAthena.py:154
FPGATrackSimGNNRoadMakerTool::m_road_hit_list
std::vector< std::vector< int > > m_road_hit_list
Definition: FPGATrackSimGNNRoadMakerTool.h:81
layer_bitmask_t
uint32_t layer_bitmask_t
Definition: FPGATrackSimTypes.h:22
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
FPGATrackSimGNNRoadMakerTool::m_num_components
int m_num_components
Definition: FPGATrackSimGNNRoadMakerTool.h:79
FPGATrackSimGNNRoadMakerTool::doScoreCut
void doScoreCut(const std::vector< std::shared_ptr< FPGATrackSimGNNEdge >> &edges)
Definition: FPGATrackSimGNNRoadMakerTool.cxx:42
TrigInDetSiLayer
Definition: TrigInDetSiLayer.h:8
FPGATrackSimGNNRoadMakerTool::m_roads
std::vector< FPGATrackSimRoad > m_roads
Definition: FPGATrackSimGNNRoadMakerTool.h:100
AthAlgTool
Definition: AthAlgTool.h:26
FPGATrackSimGNNRoadMakerTool::m_unique_indices
std::vector< int > m_unique_indices
Definition: FPGATrackSimGNNRoadMakerTool.h:76
FPGATrackSimGNNRoadMakerTool::m_Graph
boost::adjacency_list< boost::vecS, boost::vecS, boost::undirectedS > m_Graph
Definition: FPGATrackSimGNNRoadMakerTool.h:77
node
Definition: node.h:21
TrigInDetSiLayer::m_subdet
int m_subdet
Definition: TrigInDetSiLayer.h:10
FPGATrackSimRoad
Definition: FPGATrackSimRoad.h:31
sortByLayer
std::vector< std::vector< std::shared_ptr< const FPGATrackSimHit > > > sortByLayer(Container const &hits)
Definition: FPGATrackSimHit.h:315