Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GNN_DataStorage.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GNN_DataStorage.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 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 
9 #pragma once
10 
11 // TODO: update to C++17 style
14 
15 #include <algorithm>
16 #include <map>
17 #include <vector>
18 
19 namespace Acts {
20 
21 constexpr size_t MAX_SEG_PER_NODE = 1000; // was 30
22 constexpr size_t N_SEG_CONNS = 6; // was 6
23 
24 // new sp struct
25 template <typename space_point_t>
26 struct FTF_SP {
27  const space_point_t *SP; // want inside to have pointer
28  int FTF_ID;
30  FTF_SP(const space_point_t *sp, int id, int combined_id)
31  : SP(sp), FTF_ID(id), combined_ID{combined_id} {
32  if (SP->sourceLinks().size() == 1) { // pixels have 1 SL
33  m_isPixel = true;
34  } else {
35  m_isPixel = false;
36  }
37  m_phi = std::atan(SP->x() / SP->y());
38  };
39  bool isPixel() const { return m_isPixel; }
40  bool isSCT() const { return !m_isPixel; }
41  float phi() const { return m_phi; }
42  bool m_isPixel;
43  float m_phi;
44 };
45 
46 template <typename space_point_t>
48  public:
49  struct CompareByPhi {
52  return (n1->m_sp_FTF.phi() < n2->m_sp_FTF.phi());
53  }
54  };
55 
56  TrigFTF_GNN_Node(const FTF_SP<space_point_t> &FTF_sp, float minT = -100.0,
57  float maxT = 100.0)
58  : m_sp_FTF(FTF_sp), m_minCutOnTau(minT), m_maxCutOnTau(maxT) {}
59 
60  inline void addIn(int i) {
61  if (m_in.size() < MAX_SEG_PER_NODE) {
62  m_in.push_back(i);
63  }
64  }
65 
66  inline void addOut(int i) {
67  if (m_out.size() < MAX_SEG_PER_NODE) {
68  m_out.push_back(i);
69  }
70  }
71 
72  inline bool isConnector() const {
73  if (m_in.empty() || m_out.empty()) {
74  return false;
75  }
76  return true;
77  }
78 
79  inline bool isFull() const {
80  if (m_in.size() == MAX_SEG_PER_NODE && m_out.size() == MAX_SEG_PER_NODE) {
81  return true;
82  } else {
83  return false;
84  }
85  }
86 
88 
89  std::vector<unsigned int> m_in; // indices of the edges in the edge storage
90  std::vector<unsigned int> m_out;
92 };
93 
94 template <typename space_point_t>
96  public:
97  TrigFTF_GNN_EtaBin() { m_vn.clear(); }
98 
100  for (typename std::vector<TrigFTF_GNN_Node<space_point_t> *>::iterator it =
101  m_vn.begin();
102  it != m_vn.end(); ++it) {
103  delete (*it);
104  }
105  }
106 
107  void sortByPhi() {
108  std::sort(m_vn.begin(), m_vn.end(),
110  }
111 
112  bool empty() const { return m_vn.empty(); }
113 
114  void generatePhiIndexing(float dphi) {
115  for (unsigned int nIdx = 0; nIdx < m_vn.size(); nIdx++) {
116  TrigFTF_GNN_Node<space_point_t> *pN = m_vn.at(nIdx);
117  // float phi = pN->m_sp.phi();
118  // float phi = (std::atan(pN->m_sp.x() / pN->m_sp.y()));
119  float phi = pN->m_sp_FTF.phi();
120  if (phi <= M_PI - dphi) {
121  continue;
122  }
123 
124  m_vPhiNodes.push_back(
125  std::pair<float, unsigned int>(phi - 2 * M_PI, nIdx));
126  }
127 
128  for (unsigned int nIdx = 0; nIdx < m_vn.size(); nIdx++) {
129  TrigFTF_GNN_Node<space_point_t> *pN = m_vn.at(nIdx);
130  float phi = pN->m_sp_FTF.phi();
131  m_vPhiNodes.push_back(std::pair<float, unsigned int>(phi, nIdx));
132  }
133 
134  for (unsigned int nIdx = 0; nIdx < m_vn.size(); nIdx++) {
135  TrigFTF_GNN_Node<space_point_t> *pN = m_vn.at(nIdx);
136  float phi = pN->m_sp_FTF.phi();
137  if (phi >= -M_PI + dphi) {
138  break;
139  }
140  m_vPhiNodes.push_back(
141  std::pair<float, unsigned int>(phi + 2 * M_PI, nIdx));
142  }
143  }
144 
145  std::vector<TrigFTF_GNN_Node<space_point_t> *> m_vn;
146  // TODO change to
147  // std::vector<std::unique_ptr<TrigFTF_GNN_Node<space_point_t>>> m_vn;
148  std::vector<std::pair<float, unsigned int>> m_vPhiNodes;
149 };
150 
151 template <typename space_point_t>
153  public:
155  : m_geo(g) {
156  m_etaBins.reserve(g.num_bins());
157  for (int k = 0; k < g.num_bins(); k++) {
159  }
160  }
161 
162  int addSpacePoint(const FTF_SP<space_point_t> &sp, bool useClusterWidth) {
164  m_geo.getTrigFTF_GNN_LayerByKey(sp.combined_ID);
165 
166  if (pL == nullptr) {
167  return -1;
168  }
169 
170  int binIndex = pL->getEtaBin(sp.SP->z(), sp.SP->r());
171 
172  if (binIndex == -1) {
173  return -2;
174  }
175 
176  bool isBarrel = (pL->m_layer.m_type == 0);
177 
178  if (isBarrel) {
179  float min_tau = -100.0;
180  float max_tau = 100.0;
181  // can't do this bit yet as dont have cluster width
182  if (useClusterWidth) {
183  // const Trk::SpacePoint* osp = sp.offlineSpacePoint();
184  // const InDet::PixelCluster* pCL = dynamic_cast<const
185  // InDet::PixelCluster*>(osp->clusterList().first);
186  // float cluster_width = pCL->width().widthPhiRZ().y();
187  float cluster_width = 1; // temporary while cluster width not available
188  min_tau = 6.7 * (cluster_width - 0.2);
189  max_tau =
190  1.6 + 0.15 / (cluster_width + 0.2) + 6.1 * (cluster_width - 0.2);
191  }
192 
193  m_etaBins.at(binIndex).m_vn.push_back(new TrigFTF_GNN_Node<space_point_t>(
194  sp, min_tau, max_tau)); // adding ftf member to nodes
195  } else {
196  if (useClusterWidth) {
197  // const Trk::SpacePoint* osp = sp.offlineSpacePoint();
198  // const InDet::PixelCluster* pCL = dynamic_cast<const
199  // InDet::PixelCluster*>(osp->clusterList().first);
200  // float cluster_width = pCL->width().widthPhiRZ().y();
201  float cluster_width = 1; // temporary while cluster width not available
202  if (cluster_width > 0.2) {
203  return -3;
204  }
205  }
206  m_etaBins.at(binIndex).m_vn.push_back(
208  }
209 
210  return 0;
211  }
212 
213  // for safety to prevent passing as copy
216 
217  unsigned int numberOfNodes() const {
218  unsigned int n = 0;
219 
220  for (auto &b : m_etaBins) {
221  n += b.m_vn.size();
222  }
223  return n;
224  }
225 
227  std::vector<const TrigFTF_GNN_Node<space_point_t> *> &vn) {
228  vn.clear();
229  vn.reserve(numberOfNodes());
230  for (const auto &b : m_etaBins) {
231  for (typename std::vector<
232  TrigFTF_GNN_Node<space_point_t> *>::const_iterator nIt =
233  b.m_vn.begin();
234  nIt != b.m_vn.end(); ++nIt) {
235  if ((*nIt)->m_in.empty()) {
236  continue;
237  }
238  if ((*nIt)->m_out.empty()) {
239  continue;
240  }
241  vn.push_back(*nIt);
242  }
243  }
244  }
245 
246  void sortByPhi() {
247  for (auto &b : m_etaBins) {
248  b.sortByPhi();
249  }
250  }
251 
252  void generatePhiIndexing(float dphi) {
253  for (auto &b : m_etaBins) {
254  b.generatePhiIndexing(dphi);
255  }
256  }
257 
259  if (idx >= static_cast<int>(m_etaBins.size())) {
260  idx = idx - 1;
261  }
262  return m_etaBins.at(idx);
263  }
264 
265  protected:
267 
268  std::vector<TrigFTF_GNN_EtaBin<space_point_t>> m_etaBins;
269 };
270 
271 template <typename space_point_t>
273  public:
274  struct CompareLevel {
275  public:
276  bool operator()(const TrigFTF_GNN_Edge *pS1, const TrigFTF_GNN_Edge *pS2) {
277  return pS1->m_level > pS2->m_level;
278  }
279  };
280 
282  TrigFTF_GNN_Node<space_point_t> *n2, float p1, float p2,
283  float p3, float p4)
284  : m_n1(n1), m_n2(n2), m_level(1), m_next(1) {
285  m_p[0] = p1;
286  m_p[1] = p2;
287  m_p[2] = p3;
288  m_p[3] = p4;
289  }
290 
291  TrigFTF_GNN_Edge() : m_n1(nullptr), m_n2(nullptr), m_level(-1), m_next(-1) {}
292 
293  // TrigFTF_GNN_Edge(const TrigFTF_GNN_Edge<space_point_t> &e)
294  // : m_n1(e.m_n1), m_n2(e.m_n2) {}
295 
296  // inline void initialize(TrigFTF_GNN_Node<space_point_t> *n1,
297  // TrigFTF_GNN_Node<space_point_t> *n2) {
298  // m_n1 = n1;
299  // m_n2 = n2;
300  // m_level = 1;
301  // m_next = 1;
302  // m_nNei = 0;
303  // }
304 
307 
308  signed char m_level{}, m_next{};
309 
310  unsigned char m_nNei{0};
311  float m_p[4]{};
312 
313  unsigned int m_vNei[N_SEG_CONNS]{}; // global indices of the connected edges
314 };
315 
316 } // namespace Acts