ATLAS Offline Software
BucketGraphUtils.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN
3  for the benefit of the ATLAS collaboration
4 */
5 #ifndef MUONINFERENCE_BUCKETGRAPHUTILS_H
6 #define MUONINFERENCE_BUCKETGRAPHUTILS_H
7 
8 #include <vector>
9 #include <cstdint>
10 #include <algorithm>
11 #include <cmath>
12 #include <unordered_set>
13 
18 
20 
21 struct NodeAux {
22  double x{0.f};
23  double y{0.f};
24  double z{0.f};
25  int layers{0};
26  int nSp{0};
27  double bucketSize{0.f};
28  int sector{0};
29  int chamber{0};
30 };
31 
32 inline double bucket_size_mm(const MuonR4::SpacePointBucket& b) {
33  return b.coveredMax() - b.coveredMin();
34 }
35 
41  const ActsTrk::GeometryContext& gctx,
42  std::vector<NodeAux>& nodes,
43  std::vector<float>& featuresLeaves,
44  std::vector<int64_t>& spInBucket) // << int64_t!
45 {
47  nodes.clear();
48  featuresLeaves.clear();
49  spInBucket.clear();
50  nodes.reserve(buckets.size());
51  featuresLeaves.reserve(6u * buckets.size()); // preallocate
52  spInBucket.reserve(buckets.size());
53 
55 
56  for (const MuonR4::SpacePointBucket* bucket : buckets) {
57  const double bsize = bucket_size_mm(*bucket);
58  // Don't skip buckets with 0 size - this was causing 0 nodes with new gctx implementation
59  // The new gctx may result in buckets with exactly 0.0 size that should still be processed
60 
61  NodeAux n;
62 
63  const double midY = 0.5 * (bucket->coveredMin() + bucket->coveredMax());
64  const Amg::Vector3D glob = bucket->msSector()->localToGlobalTrans(gctx) * (midY * Amg::Vector3D::UnitY());
65  n.x = glob.x();
66  n.y = glob.y();
67  n.z = glob.z();
68 
69  std::unordered_set<unsigned int> laySet;
70  laySet.reserve(bucket->size());
71  for (const auto& spPtr : *bucket) {
72  laySet.insert(layerSorter.sectorLayerNum(*spPtr));
73  }
74  n.layers = static_cast<int>(laySet.size());
75  n.nSp = static_cast<int>(bucket->size());
76  n.bucketSize = bsize;
77  n.sector = bucket->msSector()->sector();
78  n.chamber = Acts::toUnderlying(bucket->msSector()->chamberIndex());
79 
80  nodes.push_back(n);
81 
82  featuresLeaves.push_back(static_cast<float>(n.x));
83  featuresLeaves.push_back(static_cast<float>(n.y));
84  featuresLeaves.push_back(static_cast<float>(n.z));
85  featuresLeaves.push_back(static_cast<float>(n.layers));
86  featuresLeaves.push_back(static_cast<float>(n.nSp));
87  featuresLeaves.push_back(static_cast<float>(n.bucketSize));
88 
89  spInBucket.emplace_back(static_cast<int64_t>(n.nSp)); // store as int64_t
90  }
91 }
92 
93 inline void buildSparseEdges(const std::vector<NodeAux>& nodes,
94  int minLayers,
95  int maxChamberDelta,
96  int maxSectorDelta,
97  double maxDistXY,
98  double maxAbsDz,
99  std::vector<int64_t>& srcEdges,
100  std::vector<int64_t>& dstEdges)
101 {
103  srcEdges.clear();
104  dstEdges.clear();
105 
106  std::vector<size_t> validIdx;
107  validIdx.reserve(nodes.size());
108  for (size_t i = 0; i < nodes.size(); ++i) {
109  if (nodes[i].layers >= minLayers) validIdx.push_back(i);
110  }
111 
112  if (validIdx.size() < 2) {
113  if (!nodes.empty()) {
114  srcEdges.push_back(0);
115  dstEdges.push_back(0);
116  }
117  return;
118  }
119 
120  const unsigned int secMax = Muon::MuonStationIndex::numberOfSectors();
121 
122  for (size_t a = 0; a < validIdx.size(); ++a) {
123  const size_t i = validIdx[a];
124  const auto& ni = nodes[i];
125  for (size_t b = a + 1; b < validIdx.size(); ++b) {
126  const size_t j = validIdx[b];
127  const auto& nj = nodes[j];
128 
129  const double dx = static_cast<double>(ni.x) - static_cast<double>(nj.x);
130  const double dy = static_cast<double>(ni.y) - static_cast<double>(nj.y);
131  const double dz = static_cast<double>(ni.z) - static_cast<double>(nj.z);
132 
133  const double distXY = Acts::fastHypot(dx, dy);
134  const int secDiffLin = std::abs(ni.sector - nj.sector) % static_cast<int>(secMax);
135  const int d_sec = std::min(secDiffLin, static_cast<int>(secMax) - secDiffLin);
136  const int d_ch = std::abs(ni.chamber - nj.chamber);
137 
138  const bool mask =
139  (d_ch > 0) &&
140  (d_sec <= maxSectorDelta) &&
141  (distXY < maxDistXY) &&
142  (std::abs(dz) < maxAbsDz) &&
143  (d_ch <= maxChamberDelta);
144 
145  if (mask) {
146  srcEdges.push_back(static_cast<int64_t>(i));
147  dstEdges.push_back(static_cast<int64_t>(j));
148  srcEdges.push_back(static_cast<int64_t>(j));
149  dstEdges.push_back(static_cast<int64_t>(i));
150  }
151  }
152  }
153 
154  if (srcEdges.empty() && !nodes.empty()) {
155  srcEdges.push_back(0);
156  dstEdges.push_back(0);
157  }
158 }
159 
160 inline size_t packEdgeIndex(const std::vector<int64_t>& srcEdges,
161  const std::vector<int64_t>& dstEdges,
162  std::vector<int64_t>& edgeIndexPacked)
163 {
164  const size_t E = srcEdges.size();
165  edgeIndexPacked = srcEdges;
166  edgeIndexPacked.insert(edgeIndexPacked.end(), dstEdges.begin(), dstEdges.end());
167  return E;
168 }
169 
170 } // namespace MuonML::BucketGraphUtils
171 
172 #endif
173 
MuonR4::minLayers
constexpr unsigned int minLayers
Definition: NswSegmentFinderAlg.cxx:58
MuonML::BucketGraphUtils::NodeAux::nSp
int nSp
Definition: BucketGraphUtils.h:26
MuonR4::SpacePointBucket
: The muon space point bucket represents a collection of points that will bre processed together in t...
Definition: MuonSpectrometer/MuonPhaseII/Event/MuonSpacePoint/MuonSpacePoint/SpacePointContainer.h:21
MuonR4::SpacePointPerLayerSorter
The SpacePointPerLayerSorter sort two given space points by their layer Identifier.
Definition: SpacePointPerLayerSorter.h:15
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
module_driven_slicing.layers
layers
Definition: module_driven_slicing.py:113
MuonML::BucketGraphUtils::NodeAux::sector
int sector
Definition: BucketGraphUtils.h:28
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
Muon::MuonStationIndex::numberOfSectors
constexpr unsigned numberOfSectors()
return total number of sectors
Definition: MuonStationIndex.h:118
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:459
GeometryContext.h
GeoPrimitives.h
MuonML::BucketGraphUtils::NodeAux::y
double y
Definition: BucketGraphUtils.h:23
MuonML::BucketGraphUtils::NodeAux::x
double x
Definition: BucketGraphUtils.h:22
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:727
MuonML::BucketGraphUtils::bucket_size_mm
double bucket_size_mm(const MuonR4::SpacePointBucket &b)
Definition: BucketGraphUtils.h:32
MuonML::BucketGraphUtils::NodeAux::chamber
int chamber
Definition: BucketGraphUtils.h:29
ActsTrk::GeometryContext
Definition: GeometryContext.h:28
DataVector
Derived DataVector<T>.
Definition: DataVector.h:795
MuonML::BucketGraphUtils
Definition: BucketGraphUtils.h:19
MuonML::BucketGraphUtils::buildSparseEdges
void buildSparseEdges(const std::vector< NodeAux > &nodes, int minLayers, int maxChamberDelta, int maxSectorDelta, double maxDistXY, double maxAbsDz, std::vector< int64_t > &srcEdges, std::vector< int64_t > &dstEdges)
Definition: BucketGraphUtils.h:93
VP1PartSpect::E
@ E
Definition: VP1PartSpectFlags.h:21
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:76
MuonML::BucketGraphUtils::packEdgeIndex
size_t packEdgeIndex(const std::vector< int64_t > &srcEdges, const std::vector< int64_t > &dstEdges, std::vector< int64_t > &edgeIndexPacked)
Definition: BucketGraphUtils.h:160
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
MuonML::BucketGraphUtils::NodeAux
Definition: BucketGraphUtils.h:21
makeTRTBarrelCans.dy
tuple dy
Definition: makeTRTBarrelCans.py:21
a
TList * a
Definition: liststreamerinfos.cxx:10
SpacePointContainer.h
makeTRTBarrelCans.dx
tuple dx
Definition: makeTRTBarrelCans.py:20
MuonML::BucketGraphUtils::buildNodesAndFeatures
void buildNodesAndFeatures(const MuonR4::SpacePointContainer &buckets, const ActsTrk::GeometryContext &gctx, std::vector< NodeAux > &nodes, std::vector< float > &featuresLeaves, std::vector< int64_t > &spInBucket)
Build nodes + flat features (N,6) and number of SPs per kept bucket.
Definition: BucketGraphUtils.h:40
MuonML::BucketGraphUtils::NodeAux::layers
int layers
Definition: BucketGraphUtils.h:25
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
MuonML::BucketGraphUtils::NodeAux::bucketSize
double bucketSize
Definition: BucketGraphUtils.h:27
SpacePointPerLayerSorter.h
MuonML::BucketGraphUtils::NodeAux::z
double z
Definition: BucketGraphUtils.h:24