Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackParamsEstimationAlgorithm.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TrackParamsEstimationAlgorithm.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 
10 
18 #include "Acts/Seeding/Seed.hpp"
25 
26 #include <cmath>
27 #include <cstddef>
28 #include <optional>
29 #include <ostream>
30 #include <stdexcept>
31 #include <system_error>
32 #include <utility>
33 #include <vector>
34 
38  : ActsExamples::IAlgorithm("TrackParamsEstimationAlgorithm", lvl),
39  m_cfg(std::move(cfg)) {
40  if (m_cfg.inputSeeds.empty()) {
41  throw std::invalid_argument("Missing seeds input collection");
42  }
43  if (m_cfg.outputTrackParameters.empty()) {
44  throw std::invalid_argument("Missing track parameters output collection");
45  }
46  if (not m_cfg.trackingGeometry) {
47  throw std::invalid_argument("Missing tracking geometry");
48  }
49  if (not m_cfg.magneticField) {
50  throw std::invalid_argument("Missing magnetic field");
51  }
52 
55 
59 
60  // Set up the track parameters covariance (the same for all tracks)
61  for (std::size_t i = Acts::eBoundLoc0; i < Acts::eBoundSize; ++i) {
64  }
65 }
66 
68  const ActsExamples::AlgorithmContext& ctx) const {
69  auto const& seeds = m_inputSeeds(ctx);
70  ACTS_VERBOSE("Read " << seeds.size() << " seeds");
71 
72  TrackParametersContainer trackParameters;
73  trackParameters.reserve(seeds.size());
74 
75  std::optional<SimSeedContainer> outputSeeds;
76  if (m_outputSeeds.isInitialized()) {
77  outputSeeds->reserve(seeds.size());
78  }
79 
80  const ProtoTrackContainer* inputTracks = nullptr;
81  std::optional<ProtoTrackContainer> outputTracks;
82  if (m_inputTracks.isInitialized() && m_outputTracks.isInitialized()) {
83  const auto& inputTracksRef = m_inputTracks(ctx);
84  if (seeds.size() != inputTracksRef.size()) {
85  ACTS_FATAL("Inconsistent number of seeds and prototracks");
86  return ProcessCode::ABORT;
87  }
88  inputTracks = &inputTracksRef;
89  outputTracks.emplace();
90  outputTracks->reserve(seeds.size());
91  }
92 
93  auto bCache = m_cfg.magneticField->makeCache(ctx.magFieldContext);
94 
95  IndexSourceLink::SurfaceAccessor surfaceAccessor{*m_cfg.trackingGeometry};
96 
97  // Loop over all found seeds to estimate track parameters
98  for (size_t iseed = 0; iseed < seeds.size(); ++iseed) {
99  const auto& seed = seeds[iseed];
100  // Get the bottom space point and its reference surface
101  const auto bottomSP = seed.sp().front();
102  if (bottomSP->sourceLinks().empty()) {
103  ACTS_WARNING("Missing source link in the space point")
104  continue;
105  }
106  const auto& sourceLink = bottomSP->sourceLinks()[0];
107  const Acts::Surface* surface = surfaceAccessor(sourceLink);
108 
109  if (surface == nullptr) {
110  ACTS_WARNING(
111  "Surface from source link is not found in the tracking geometry");
112  continue;
113  }
114 
115  // Get the magnetic field at the bottom space point
116  auto fieldRes = m_cfg.magneticField->getField(
117  {bottomSP->x(), bottomSP->y(), bottomSP->z()}, bCache);
118  if (!fieldRes.ok()) {
119  ACTS_ERROR("Field lookup error: " << fieldRes.error());
120  return ProcessCode::ABORT;
121  }
122  Acts::Vector3 field = *fieldRes;
123 
124  // Estimate the track parameters from seed
125  auto optParams = Acts::estimateTrackParamsFromSeed(
126  ctx.geoContext, seed.sp().begin(), seed.sp().end(), *surface, field,
127  m_cfg.bFieldMin, logger());
128  if (not optParams.has_value()) {
129  ACTS_WARNING("Estimation of track parameters for seed " << iseed
130  << " failed.");
131  continue;
132  } else {
133  const auto& params = optParams.value();
134  trackParameters.emplace_back(surface->getSharedPtr(), params,
135  m_covariance, m_cfg.particleHypothesis);
136  if (outputSeeds) {
137  outputSeeds->push_back(seed);
138  }
139  if (outputTracks && inputTracks != nullptr) {
140  outputTracks->push_back(inputTracks->at(iseed));
141  }
142  }
143  }
144 
145  ACTS_VERBOSE("Estimated " << trackParameters.size() << " track parameters");
146 
147  m_outputTrackParameters(ctx, std::move(trackParameters));
148  if (m_outputSeeds.isInitialized()) {
149  m_outputSeeds(ctx, std::move(*outputSeeds));
150  }
151 
152  if (m_outputTracks.isInitialized()) {
153  m_outputTracks(ctx, std::move(*outputTracks));
154  }
155 
156  return ProcessCode::SUCCESS;
157 }