Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FasTrackConnector.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FasTrackConnector.cpp
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 // TODO: update to C++17 style
11 
12 #include <fstream>
13 #include <iostream>
14 #include <list>
15 #include <set>
16 #include <unordered_map>
17 
18 namespace Acts {
19 
20 FasTrackConnection::FasTrackConnection(unsigned int s, unsigned int d)
21  : m_src(s), m_dst(d) {}
22 
23 FasTrackConnector::FasTrackConnector(std::ifstream &inFile) {
24  m_layerGroups.clear();
25 
26  int nLinks{};
27 
28  inFile >> nLinks >> m_etaBin;
29 
30  for (int l = 0; l < nLinks; l++) {
31  unsigned int stage{}, lIdx{}, src{}, dst{}, nEntries{};
32  int height{}, width{};
33 
34  inFile >> lIdx >> stage >> src >> dst >> height >> width >> nEntries;
35 
36  FasTrackConnection *pC = new FasTrackConnection(src, dst);
37 
38  int dummy{};
39 
40  for (int i = 0; i < height; i++) {
41  for (int j = 0; j < width; j++) {
42  inFile >> dummy; // pC->m_binTable[j+i*width];
43  }
44  }
45 
46  int vol_id = src / 1000;
47 
48  if (vol_id == 13 || vol_id == 12 || vol_id == 14) {
49  delete pC;
50  continue;
51  }
52 
53  vol_id = dst / 1000;
54 
55  if (vol_id == 13 || vol_id == 12 || vol_id == 14) {
56  delete pC;
57  continue;
58  }
59 
60  std::map<int, std::vector<FasTrackConnection *>>::iterator it =
61  m_connMap.find(stage);
62 
63  if (it == m_connMap.end()) {
64  std::vector<FasTrackConnection *> v(1, pC);
65  m_connMap.insert(std::make_pair(stage, v));
66  } else {
67  (*it).second.push_back(pC);
68  }
69  }
70 
71  // re-arrange the connection stages
72 
73  std::list<const FasTrackConnection *> lConns;
74 
75  std::map<int, std::vector<const FasTrackConnection *>> newConnMap;
76 
77  for (const auto &conn : m_connMap) {
78  std::copy(conn.second.begin(), conn.second.end(),
79  std::back_inserter(lConns));
80  }
81 
82  int stageCounter = 0;
83 
84  while (!lConns.empty()) {
85  std::unordered_map<unsigned int, std::pair<int, int>>
86  mCounter; // layerKey, nDst, nSrc
87 
88  for (const auto &conn : lConns) {
89  auto entryIt = mCounter.find(conn->m_dst);
90  if (entryIt != mCounter.end()) {
91  (*entryIt).second.first++;
92  } else {
93  int nDst = 1;
94  int nSrc = 0;
95  mCounter.insert(
96  std::make_pair(conn->m_dst, std::make_pair(nDst, nSrc)));
97  }
98 
99  entryIt = mCounter.find(conn->m_src);
100  if (entryIt != mCounter.end()) {
101  (*entryIt).second.second++;
102  } else {
103  int nDst = 0;
104  int nSrc = 1;
105  mCounter.insert(
106  std::make_pair(conn->m_src, std::make_pair(nDst, nSrc)));
107  }
108  }
109 
110  // find layers with nSrc = 0
111 
112  std::set<unsigned int> zeroLayers;
113 
114  for (const auto &layerCounts : mCounter) {
115  if (layerCounts.second.second != 0) {
116  continue;
117  }
118 
119  zeroLayers.insert(layerCounts.first);
120  }
121 
122  // remove connections which use zeroLayer as destination
123 
124  std::vector<const FasTrackConnection *> theStage;
125 
126  std::list<const FasTrackConnection *>::iterator cIt = lConns.begin();
127 
128  while (cIt != lConns.end()) {
129  if (zeroLayers.find((*cIt)->m_dst) !=
130  zeroLayers.end()) { // check if contains
131  theStage.push_back(*cIt);
132  cIt = lConns.erase(cIt);
133  continue;
134  }
135  cIt++;
136  }
137  newConnMap.insert(std::make_pair(stageCounter, theStage));
138  stageCounter++;
139  }
140 
141  // create layer groups
142 
143  int currentStage = 0;
144 
145  // the doublet making is done using "outside-in" approach hence the reverse
146  // iterations
147 
148  for (std::map<int, std::vector<const FasTrackConnection *>>::reverse_iterator
149  it = newConnMap.rbegin();
150  it != newConnMap.rend(); ++it, currentStage++) {
151  const std::vector<const FasTrackConnection *> &vConn = (*it).second;
152 
153  // loop over links, extract all connections for the stage, group sources by
154  // L1 (dst) index
155 
156  std::map<unsigned int, std::vector<const FasTrackConnection *>> l1ConnMap;
157 
158  for (const auto *conn : vConn) {
159  unsigned int dst = conn->m_dst;
160 
161  std::map<unsigned int, std::vector<const FasTrackConnection *>>::iterator
162  l1MapIt = l1ConnMap.find(dst);
163  if (l1MapIt != l1ConnMap.end()) {
164  (*l1MapIt).second.push_back(conn);
165  } else {
166  std::vector<const FasTrackConnection *> v = {conn};
167  l1ConnMap.insert(std::make_pair(dst, v));
168  }
169  }
170 
171  std::vector<LayerGroup> lgv;
172 
173  lgv.reserve(l1ConnMap.size());
174 
175  for (const auto &l1Group : l1ConnMap) {
176  lgv.push_back(LayerGroup(l1Group.first, l1Group.second));
177  }
178 
179  m_layerGroups.insert(std::make_pair(currentStage, lgv));
180  }
181 
182  newConnMap.clear();
183 }
184 
186  m_layerGroups.clear();
187  for (std::map<int, std::vector<FasTrackConnection *>>::iterator it =
188  m_connMap.begin();
189  it != m_connMap.end(); ++it) {
190  for (std::vector<FasTrackConnection *>::iterator cIt = (*it).second.begin();
191  cIt != (*it).second.end(); ++cIt) {
192  delete (*cIt);
193  }
194  }
195 }
196 
197 } // namespace Acts