Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ReadSeedFile.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ReadSeedFile.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 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 // Local include(s).
10 #include "ReadSeedFile.hpp"
11 
12 // System include(s).
13 #include <cmath>
14 #include <fstream>
15 #include <iostream>
16 #include <limits>
17 #include <sstream>
18 #include <stdexcept>
19 
20 std::vector<std::unique_ptr<TestSpacePoint> > readSeedFile(
21  const std::string& fileName, bool filterDuplicates) {
22  // The result object.
23  std::vector<std::unique_ptr<TestSpacePoint> > result;
24 
25  // Open the input file.
26  std::ifstream spFile(fileName);
27  if (!spFile.is_open()) {
28  throw std::runtime_error("Could not open file: " + fileName);
29  }
30 
31  // Read the file's lines one by one, and create spacepoint objects out of
32  // them.
33  std::size_t duplicatesFound = 0;
34  while (!spFile.eof()) {
36  std::getline(spFile, line);
37  std::stringstream ss(line);
38  std::string linetype;
39 
40  ss >> linetype;
41  if (linetype != "lxyz") {
42  continue;
43  }
44 
45  int layer;
46  float x, y, z, varianceR, varianceZ;
47  ss >> layer >> x >> y >> z >> varianceR >> varianceZ;
48  const float r = std::hypot(x, y);
49  const float f22 = varianceR;
50  const float wid = varianceZ;
51  float cov = wid * wid * .08333;
52 
53  if (cov < f22) {
54  cov = f22;
55  }
56  if (std::abs(z) > 450.) {
57  varianceZ = 9. * cov;
58  varianceR = .06;
59  } else {
60  varianceR = 9. * cov;
61  varianceZ = .06;
62  }
63 
64  // Create a new spacepoint object.
65  std::unique_ptr<TestSpacePoint> sp(
66  new TestSpacePoint{x, y, z, r, layer, varianceR, varianceZ});
67 
68  // Check if we already have another spacepoint with the same coordinates.
69  if (filterDuplicates) {
70  bool discardSP = false;
71  for (const auto& otherSP : result) {
72  if (*sp == *otherSP) {
73  ++duplicatesFound;
74  discardSP = true;
75  break;
76  }
77  }
78  if (discardSP) {
79  continue;
80  }
81  }
82 
83  // Store the new spacepoint.
84  result.push_back(std::move(sp));
85  }
86  // Tell the user how many duplicates were found.
87  if (duplicatesFound) {
88  std::cerr << duplicatesFound << " duplicates found in: " << fileName
89  << std::endl;
90  }
91 
92  return result;
93 }