Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SeedFinderTest.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SeedFinderTest.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 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 
11 #include "Acts/Geometry/Extent.hpp"
14 #include "Acts/Seeding/Seed.hpp"
22 
23 #include <algorithm>
24 #include <chrono>
25 #include <cmath>
26 #include <cstdlib>
27 #include <fstream>
28 #include <iostream>
29 #include <iterator>
30 #include <memory>
31 #include <string>
32 #include <tuple>
33 #include <utility>
34 #include <vector>
35 
36 #include "ATLASCuts.hpp"
37 #include "SpacePoint.hpp"
38 
39 using namespace Acts::UnitLiterals;
40 
41 std::vector<const SpacePoint*> readFile(const std::string& filename) {
43  int layer = 0;
44  std::vector<const SpacePoint*> readSP;
45 
46  std::ifstream spFile(filename);
47  if (spFile.is_open()) {
48  while (!spFile.eof()) {
49  std::getline(spFile, line);
50  std::stringstream ss(line);
51  std::string linetype;
52  ss >> linetype;
53  float x = 0, y = 0, z = 0, r = 0, varianceR = 0, varianceZ = 0;
54  if (linetype == "lxyz") {
55  ss >> layer >> x >> y >> z >> varianceR >> varianceZ;
56  r = std::hypot(x, y);
57  float f22 = varianceR;
58  float wid = varianceZ;
59  float cov = wid * wid * .08333;
60  if (cov < f22) {
61  cov = f22;
62  }
63  if (std::abs(z) > 450.) {
64  varianceZ = 9. * cov;
65  varianceR = .06;
66  } else {
67  varianceR = 9. * cov;
68  varianceZ = .06;
69  }
70  SpacePoint* sp =
71  new SpacePoint{x, y, z, r, layer, varianceR, varianceZ};
72  // if(r < 200.){
73  // sp->setClusterList(1,0);
74  // }
75  readSP.push_back(sp);
76  }
77  }
78  }
79  return readSP;
80 }
81 
82 int main(int argc, char** argv) {
83  std::string file{"sp.txt"};
84  bool help(false);
85  bool quiet(false);
86 
87  int opt = -1;
88  while ((opt = getopt(argc, argv, "hf:q")) != -1) {
89  switch (opt) {
90  case 'f':
91  file = optarg;
92  break;
93  case 'q':
94  quiet = true;
95  break;
96  case 'h':
97  help = true;
98  [[fallthrough]];
99  default: /* '?' */
100  std::cerr << "Usage: " << argv[0] << " [-hq] [-f FILENAME]\n";
101  if (help) {
102  std::cout << " -h : this help" << std::endl;
103  std::cout
104  << " -f FILE : read spacepoints from FILE. Default is \""
105  << file << "\"" << std::endl;
106  std::cout << " -q : don't print out all found seeds"
107  << std::endl;
108  }
109 
110  exit(EXIT_FAILURE);
111  }
112  }
113 
114  std::ifstream f(file);
115  if (!f.good()) {
116  std::cerr << "input file \"" << file << "\" does not exist\n";
117  exit(EXIT_FAILURE);
118  }
119 
120  auto start_read = std::chrono::system_clock::now();
121  std::vector<const SpacePoint*> spVec = readFile(file);
122  auto end_read = std::chrono::system_clock::now();
123  std::chrono::duration<double> elapsed_read = end_read - start_read;
124 
125  std::cout << "read " << spVec.size() << " SP from file " << file << " in "
126  << elapsed_read.count() << "s" << std::endl;
127 
129  // silicon detector max
130  config.rMax = 160._mm;
131  config.deltaRMin = 5._mm;
132  config.deltaRMax = 160._mm;
133  config.deltaRMinTopSP = config.deltaRMin;
134  config.deltaRMinBottomSP = config.deltaRMin;
135  config.deltaRMaxTopSP = config.deltaRMax;
136  config.deltaRMaxBottomSP = config.deltaRMax;
137  config.collisionRegionMin = -250._mm;
138  config.collisionRegionMax = 250._mm;
139  config.zMin = -2800._mm;
140  config.zMax = 2800._mm;
141  config.maxSeedsPerSpM = 5;
142  // 2.7 eta
143  config.cotThetaMax = 7.40627;
144  config.sigmaScattering = 1.00000;
145 
146  config.minPt = 500._MeV;
147 
148  config.impactMax = 10._mm;
149 
150  config.useVariableMiddleSPRange = false;
151 
153  options.beamPos = {-.5_mm, -.5_mm};
154  options.bFieldInZ = 1.99724_T;
155 
156  int numPhiNeighbors = 1;
157 
158  // extent used to store r range for middle spacepoint
159  Acts::Extent rRangeSPExtent;
160 
161  config.useVariableMiddleSPRange = false;
162  const Acts::Range1D<float> rMiddleSPRange;
163 
164  std::vector<std::pair<int, int>> zBinNeighborsTop;
165  std::vector<std::pair<int, int>> zBinNeighborsBottom;
166 
167  auto bottomBinFinder = std::make_shared<Acts::BinFinder<SpacePoint>>(
168  Acts::BinFinder<SpacePoint>(zBinNeighborsBottom, numPhiNeighbors));
169  auto topBinFinder = std::make_shared<Acts::BinFinder<SpacePoint>>(
170  Acts::BinFinder<SpacePoint>(zBinNeighborsTop, numPhiNeighbors));
171  Acts::SeedFilterConfig sfconf;
173  config.seedFilter = std::make_unique<Acts::SeedFilter<SpacePoint>>(
174  Acts::SeedFilter<SpacePoint>(sfconf, &atlasCuts));
175  Acts::SeedFinder<SpacePoint> a; // test creation of unconfigured finder
177 
178  // covariance tool, sets covariances per spacepoint as required
179  auto ct = [=](const SpacePoint& sp, float, float,
180  float) -> std::pair<Acts::Vector3, Acts::Vector2> {
181  Acts::Vector3 position(sp.x(), sp.y(), sp.z());
183  return std::make_pair(position, covariance);
184  };
185 
186  // setup spacepoint grid config
188  gridConf.minPt = config.minPt;
189  gridConf.rMax = config.rMax;
190  gridConf.zMax = config.zMax;
191  gridConf.zMin = config.zMin;
192  gridConf.deltaRMax = config.deltaRMax;
193  gridConf.cotThetaMax = config.cotThetaMax;
194  // setup spacepoint grid options
196  gridOpts.bFieldInZ = options.bFieldInZ;
197  // create grid with bin sizes according to the configured geometry
198  std::unique_ptr<Acts::SpacePointGrid<SpacePoint>> grid =
199  Acts::SpacePointGridCreator::createGrid<SpacePoint>(gridConf, gridOpts);
200  auto spGroup = Acts::BinnedSPGroup<SpacePoint>(
201  spVec.begin(), spVec.end(), ct, bottomBinFinder, topBinFinder,
202  std::move(grid), rRangeSPExtent, config, options);
203 
204  std::vector<std::vector<Acts::Seed<SpacePoint>>> seedVector;
205  decltype(a)::SeedingState state;
206  auto start = std::chrono::system_clock::now();
207  for (auto [bottom, middle, top] : spGroup) {
208  auto& v = seedVector.emplace_back();
209  a.createSeedsForGroup(options, state, spGroup.grid(), std::back_inserter(v),
210  bottom, middle, top, rMiddleSPRange);
211  }
212  auto end = std::chrono::system_clock::now();
213  std::chrono::duration<double> elapsed_seconds = end - start;
214  std::cout << "time to create seeds: " << elapsed_seconds.count() << std::endl;
215  std::cout << "Number of regions: " << seedVector.size() << std::endl;
216  int numSeeds = 0;
217  for (auto& outVec : seedVector) {
218  numSeeds += outVec.size();
219  }
220  std::cout << "Number of seeds generated: " << numSeeds << std::endl;
221  if (!quiet) {
222  for (auto& regionVec : seedVector) {
223  for (size_t i = 0; i < regionVec.size(); i++) {
224  const Acts::Seed<SpacePoint>* seed = &regionVec[i];
225  const SpacePoint* sp = seed->sp()[0];
226  std::cout << " (" << sp->x() << ", " << sp->y() << ", " << sp->z()
227  << ") ";
228  sp = seed->sp()[1];
229  std::cout << sp->layer << " (" << sp->x() << ", " << sp->y() << ", "
230  << sp->z() << ") ";
231  sp = seed->sp()[2];
232  std::cout << sp->layer << " (" << sp->x() << ", " << sp->y() << ", "
233  << sp->z() << ") ";
234  std::cout << std::endl;
235  }
236  }
237  }
238  return 0;
239 }