Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GNN_Geometry.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GNN_Geometry.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
13 
14 #include <algorithm>
15 #include <cmath>
16 #include <map>
17 #include <memory>
18 #include <vector>
19 
20 namespace Acts {
22  public:
23  int m_subdet; // combined ID
24  int m_type; // 0: barrel, +/-n : endcap
25  float m_refCoord;
27 
28  TrigInDetSiLayer(int subdet, short int type, float center, float min,
29  float max)
30  : m_subdet(subdet),
31  m_type(type),
32  m_refCoord(center),
33  m_minBound(min),
34  m_maxBound(max) {}
35 };
36 
37 template <typename space_point_t>
39  public:
40  TrigFTF_GNN_Layer(const TrigInDetSiLayer &ls, float ew, int bin0)
41  : m_layer(ls), m_etaBinWidth(ew) {
42  if (m_layer.m_type == 0) { // barrel
47  } else { // endcap
52  }
53 
54  float t1 = m_z1 / m_r1;
55  float eta1 = -std::log(sqrt(1 + t1 * t1) - t1);
56 
57  float t2 = m_z2 / m_r2;
58  float eta2 = -std::log(sqrt(1 + t2 * t2) - t2);
59 
60  m_minEta = eta1;
61  m_maxEta = eta2;
62 
63  if (m_maxEta < m_minEta) {
64  m_minEta = eta2;
65  m_maxEta = eta1;
66  }
67 
68  m_maxEta += 1e-6; // increasing them slightly to avoid range_check
69  // exceptions
70  m_minEta -= 1e-6;
71 
72  float deltaEta = m_maxEta - m_minEta;
73 
74  int binCounter = bin0;
75 
76  if (deltaEta < m_etaBinWidth) {
77  m_nBins = 1;
78  m_bins.push_back(binCounter++);
79  m_etaBin = deltaEta;
80  if (m_layer.m_type == 0) { // barrel
81  m_minRadius.push_back(m_layer.m_refCoord - 2.0);
82  m_maxRadius.push_back(m_layer.m_refCoord + 2.0);
83  m_minBinCoord.push_back(m_layer.m_minBound);
84  m_maxBinCoord.push_back(m_layer.m_maxBound);
85  } else { // endcap
86  m_minRadius.push_back(m_layer.m_minBound - 2.0);
87  m_maxRadius.push_back(m_layer.m_maxBound + 2.0);
88  m_minBinCoord.push_back(m_layer.m_minBound);
89  m_maxBinCoord.push_back(m_layer.m_maxBound);
90  }
91  } else {
92  float nB = static_cast<int>(deltaEta / m_etaBinWidth);
93  m_nBins = nB;
94  if (deltaEta - m_etaBinWidth * nB > 0.5 * m_etaBinWidth) {
95  m_nBins++;
96  }
97  m_etaBin = deltaEta / m_nBins;
98 
99  if (m_nBins == 1) {
100  m_bins.push_back(binCounter++);
101  if (m_layer.m_type == 0) { // barrel
102  m_minRadius.push_back(m_layer.m_refCoord - 2.0);
103  m_maxRadius.push_back(m_layer.m_refCoord + 2.0);
104  m_minBinCoord.push_back(m_layer.m_minBound);
105  m_maxBinCoord.push_back(m_layer.m_maxBound);
106  } else { // endcap
107  m_minRadius.push_back(m_layer.m_minBound - 2.0);
108  m_maxRadius.push_back(m_layer.m_maxBound + 2.0);
109  m_minBinCoord.push_back(m_layer.m_minBound);
110  m_maxBinCoord.push_back(m_layer.m_maxBound);
111  }
112  } else {
113  float eta = m_minEta + 0.5 * m_etaBin;
114 
115  for (int i = 1; i <= m_nBins; i++) {
116  m_bins.push_back(binCounter++);
117 
118  float e1 = eta - 0.5 * m_etaBin;
119  float e2 = eta + 0.5 * m_etaBin;
120 
121  if (m_layer.m_type == 0) { // barrel
122  m_minRadius.push_back(m_layer.m_refCoord - 2.0);
123  m_maxRadius.push_back(m_layer.m_refCoord + 2.0);
124  float z1 = m_layer.m_refCoord * std::sinh(e1);
125  m_minBinCoord.push_back(z1);
126  float z2 = m_layer.m_refCoord * std::sinh(e2);
127  m_maxBinCoord.push_back(z2);
128  } else { // endcap
129  float r = m_layer.m_refCoord / std::sinh(e1);
130  m_minBinCoord.push_back(r);
131  m_minRadius.push_back(r - 2.0);
132  r = m_layer.m_refCoord / std::sinh(e2);
133  m_maxBinCoord.push_back(r);
134  m_maxRadius.push_back(r + 2.0);
135  }
136 
137  eta += m_etaBin;
138  }
139  }
140  }
141  }
142 
143  int getEtaBin(float zh, float rh) const {
144  if (m_bins.size() == 1) {
145  return m_bins.at(0);
146  }
147  float t1 = zh / rh;
148  float eta = -std::log(std::sqrt(1 + t1 * t1) - t1);
149 
150  int idx = static_cast<int>((eta - m_minEta) / m_etaBin);
151 
152  if (idx < 0) {
153  idx = 0;
154  }
155  if (idx >= static_cast<int>(m_bins.size())) {
156  idx = static_cast<int>(m_bins.size()) - 1;
157  }
158  return m_bins.at(idx); // index in the global storage
159  }
160 
161  float getMinBinRadius(int idx) const {
162  if (idx >= static_cast<int>(m_minRadius.size())) {
163  idx = idx - 1;
164  }
165  if (idx < 0) {
166  idx = 0;
167  }
168  return m_minRadius.at(idx);
169  }
170 
171  float getMaxBinRadius(int idx) const {
172  if (idx >= static_cast<int>(m_maxRadius.size())) {
173  idx = idx - 1;
174  }
175  if (idx < 0) {
176  idx = 0;
177  }
178  return m_maxRadius.at(idx);
179  }
180 
181  int num_bins() const { return m_bins.size(); }
182 
183  bool verifyBin(const TrigFTF_GNN_Layer<space_point_t> *pL, int b1, int b2,
184  float min_z0, float max_z0) const {
185  float z1min = m_minBinCoord.at(b1);
186  float z1max = m_maxBinCoord.at(b1);
187  float r1 = m_layer.m_refCoord;
188 
189  if (m_layer.m_type == 0 && pL->m_layer.m_type == 0) { // barrel -> barrel
190 
191  const float tol = 5.0;
192 
193  float min_b2 = pL->m_minBinCoord.at(b2);
194  float max_b2 = pL->m_maxBinCoord.at(b2);
195 
196  float r2 = pL->m_layer.m_refCoord;
197 
198  float A = r2 / (r2 - r1);
199  float B = r1 / (r2 - r1);
200 
201  float z0_min = z1min * A - max_b2 * B;
202  float z0_max = z1max * A - min_b2 * B;
203 
204  if (z0_max < min_z0 - tol || z0_min > max_z0 + tol) {
205  return false;
206  }
207  return true;
208  }
209 
210  if (m_layer.m_type == 0 && pL->m_layer.m_type != 0) { // barrel -> endcap
211 
212  const float tol = 10.0;
213 
214  float z2 = pL->m_layer.m_refCoord;
215  float r2max = pL->m_maxBinCoord.at(b2);
216  float r2min = pL->m_minBinCoord.at(b2);
217 
218  if (r2max <= r1) {
219  return false;
220  }
221  if (r2min <= r1) {
222  r2min = r1 + 1e-3;
223  }
224 
225  float z0_max = 0.0;
226  float z0_min = 0.0;
227 
228  if (z2 > 0) {
229  z0_max = (z1max * r2max - z2 * r1) / (r2max - r1);
230  z0_min = (z1min * r2min - z2 * r1) / (r2min - r1);
231  } else {
232  z0_max = (z1max * r2min - z2 * r1) / (r2min - r1);
233  z0_min = (z1min * r2max - z2 * r1) / (r2max - r1);
234  }
235 
236  if (z0_max < min_z0 - tol || z0_min > max_z0 + tol) {
237  return false;
238  }
239  return true;
240  }
241 
242  return true;
243  }
244 
246  std::vector<int> m_bins; // eta-bin indices
247  std::vector<float> m_minRadius;
248  std::vector<float> m_maxRadius;
249  std::vector<float> m_minBinCoord;
250  std::vector<float> m_maxBinCoord;
251 
252  float m_minEta{}, m_maxEta{};
253 
254  protected:
256 
257  float m_r1{}, m_z1{}, m_r2{}, m_z2{};
258  float m_nBins{};
259  float m_etaBin{};
260 };
261 
262 template <typename space_point_t>
264  public:
265  TrigFTF_GNN_Geometry(const std::vector<TrigInDetSiLayer> &layers,
266  std::unique_ptr<Acts::FasTrackConnector> &conn)
267 
268  : m_fastrack(std::move(conn)) {
269  const float min_z0 = -168.0;
270  const float max_z0 = 168.0;
271 
272  m_etaBinWidth = m_fastrack->m_etaBin;
273  for (const auto &layer : layers) {
276  m_nEtaBins += pL->num_bins();
277  }
278 
279  // calculating bin tables in the connector...
280 
281  for (std::map<int, std::vector<FasTrackConnection *>>::const_iterator it =
282  m_fastrack->m_connMap.begin();
283  it != m_fastrack->m_connMap.end(); ++it) {
284  const std::vector<FasTrackConnection *> &vConn = (*it).second;
285 
286  for (std::vector<FasTrackConnection *>::const_iterator cIt =
287  vConn.begin();
288  cIt != vConn.end(); ++cIt) {
289  unsigned int src = (*cIt)->m_src; // n2 : the new connectors
290  unsigned int dst = (*cIt)->m_dst; // n1
291 
296 
297  if (pL1 == nullptr) {
298  std::cout << " skipping invalid dst layer " << dst << std::endl;
299  continue;
300  }
301  if (pL2 == nullptr) {
302  std::cout << " skipping invalid src layer " << src << std::endl;
303  continue;
304  }
305  int nSrcBins = pL2->m_bins.size();
306  int nDstBins = pL1->m_bins.size();
307 
308  (*cIt)->m_binTable.resize(nSrcBins * nDstBins, 0);
309 
310  for (int b1 = 0; b1 < nDstBins; b1++) { // loop over bins in Layer 1
311  for (int b2 = 0; b2 < nSrcBins; b2++) { // loop over bins in Layer 2
312  if (!pL1->verifyBin(pL2, b1, b2, min_z0, max_z0)) {
313  continue;
314  }
315  int address = b1 + b2 * nDstBins;
316  (*cIt)->m_binTable.at(address) = 1;
317  }
318  }
319  }
320  }
321  }
322 
323  TrigFTF_GNN_Geometry() = default;
324 
325  // for safety to prevent passing as copy
326  TrigFTF_GNN_Geometry(const TrigFTF_GNN_Geometry &) = delete;
328 
330  for (typename std::vector<TrigFTF_GNN_Layer<space_point_t> *>::iterator it =
331  m_layArray.begin();
332  it != m_layArray.end(); ++it) {
333  delete (*it);
334  }
335 
336  m_layMap.clear();
337  m_layArray.clear();
338  }
339 
341  unsigned int key) const {
342  typename std::map<unsigned int,
343  TrigFTF_GNN_Layer<space_point_t> *>::const_iterator it =
344  m_layMap.find(key);
345  if (it == m_layMap.end()) {
346  return nullptr;
347  }
348 
349  return (*it).second;
350  }
351 
353  int idx) const {
354  return m_layArray.at(idx);
355  }
356 
357  int num_bins() const { return m_nEtaBins; }
358 
359  Acts::FasTrackConnector *fastrack() const { return m_fastrack.get(); }
360 
361  protected:
363  int bin0) {
364  unsigned int layerKey = l.m_subdet; // this should be combined ID
365  float ew = m_etaBinWidth;
366 
368  new TrigFTF_GNN_Layer<space_point_t>(l, ew, bin0);
369 
370  m_layMap.insert(std::pair<unsigned int, TrigFTF_GNN_Layer<space_point_t> *>(
371  layerKey, pHL));
372  m_layArray.push_back(pHL);
373  return pHL;
374  }
375 
376  float m_etaBinWidth{};
377 
378  std::map<unsigned int, TrigFTF_GNN_Layer<space_point_t> *> m_layMap;
379  std::vector<TrigFTF_GNN_Layer<space_point_t> *> m_layArray;
380 
381  int m_nEtaBins{0};
382 
383  std::unique_ptr<Acts::FasTrackConnector> m_fastrack;
384 };
385 
386 } // namespace Acts