Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackFindingAlgorithm.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TrackFindingAlgorithm.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 
10 
30 
31 #include <cmath>
32 #include <functional>
33 #include <memory>
34 #include <optional>
35 #include <ostream>
36 #include <stdexcept>
37 #include <system_error>
38 #include <utility>
39 
40 #include <boost/histogram.hpp>
41 
44  : ActsExamples::IAlgorithm("TrackFindingAlgorithm", level),
45  m_cfg(std::move(config)),
46  m_trackSelector([this]() -> std::optional<Acts::TrackSelector> {
47  if (m_cfg.trackSelectorCfg.has_value()) {
48  return {m_cfg.trackSelectorCfg.value()};
49  } else {
50  return std::nullopt;
51  }
52  }()) {
53  if (m_cfg.inputMeasurements.empty()) {
54  throw std::invalid_argument("Missing measurements input collection");
55  }
56  if (m_cfg.inputSourceLinks.empty()) {
57  throw std::invalid_argument("Missing source links input collection");
58  }
59  if (m_cfg.inputInitialTrackParameters.empty()) {
60  throw std::invalid_argument(
61  "Missing initial track parameters input collection");
62  }
63  if (m_cfg.outputTracks.empty()) {
64  throw std::invalid_argument("Missing tracks output collection");
65  }
66 
67  m_inputMeasurements.initialize(m_cfg.inputMeasurements);
68  m_inputSourceLinks.initialize(m_cfg.inputSourceLinks);
69  m_inputInitialTrackParameters.initialize(m_cfg.inputInitialTrackParameters);
70  m_outputTracks.initialize(m_cfg.outputTracks);
71 }
72 
74  const ActsExamples::AlgorithmContext& ctx) const {
75  // Read input data
76  const auto& measurements = m_inputMeasurements(ctx);
77  const auto& sourceLinks = m_inputSourceLinks(ctx);
78  const auto& initialParameters = m_inputInitialTrackParameters(ctx);
79 
80  // Construct a perigee surface as the target surface
81  auto pSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(
82  Acts::Vector3{0., 0., 0.});
83 
85  pOptions.maxSteps = m_cfg.maxSteps;
86  pOptions.direction =
88 
89  PassThroughCalibrator pcalibrator;
90  MeasurementCalibratorAdapter calibrator(pcalibrator, measurements);
91  Acts::GainMatrixUpdater kfUpdater;
92  Acts::GainMatrixSmoother kfSmoother;
93  Acts::MeasurementSelector measSel{m_cfg.measurementSelectorCfg};
94 
96  extensions;
98  &calibrator);
99  extensions.updater.connect<
100  &Acts::GainMatrixUpdater::operator()<Acts::VectorMultiTrajectory>>(
101  &kfUpdater);
102  extensions.smoother.connect<
103  &Acts::GainMatrixSmoother::operator()<Acts::VectorMultiTrajectory>>(
104  &kfSmoother);
105  extensions.measurementSelector
106  .connect<&Acts::MeasurementSelector::select<Acts::VectorMultiTrajectory>>(
107  &measSel);
108 
109  IndexSourceLinkAccessor slAccessor;
110  slAccessor.container = &sourceLinks;
112  slAccessorDelegate;
113  slAccessorDelegate.connect<&IndexSourceLinkAccessor::range>(&slAccessor);
114 
115  // Set the CombinatorialKalmanFilter options
117  ctx.geoContext, ctx.magFieldContext, ctx.calibContext, slAccessorDelegate,
118  extensions, pOptions, pSurface.get());
119  options.smoothingTargetSurfaceStrategy =
120  Acts::CombinatorialKalmanFilterTargetSurfaceStrategy::first;
121 
122  // Perform the track finding for all initial parameters
123  ACTS_DEBUG("Invoke track finding with " << initialParameters.size()
124  << " seeds.");
125 
126  auto trackContainer = std::make_shared<Acts::VectorTrackContainer>();
127  auto trackStateContainer = std::make_shared<Acts::VectorMultiTrajectory>();
128 
129  auto trackContainerTemp = std::make_shared<Acts::VectorTrackContainer>();
130  auto trackStateContainerTemp =
131  std::make_shared<Acts::VectorMultiTrajectory>();
132 
133  TrackContainer tracks(trackContainer, trackStateContainer);
134  TrackContainer tracksTemp(trackContainerTemp, trackStateContainerTemp);
135 
136  tracks.addColumn<unsigned int>("trackGroup");
137  tracksTemp.addColumn<unsigned int>("trackGroup");
138  Acts::TrackAccessor<unsigned int> seedNumber("trackGroup");
139 
140  unsigned int nSeed = 0;
141 
142  for (std::size_t iseed = 0; iseed < initialParameters.size(); ++iseed) {
143  // Clear trackContainerTemp and trackStateContainerTemp
144  tracksTemp.clear();
145 
146  auto result =
147  (*m_cfg.findTracks)(initialParameters.at(iseed), options, tracksTemp);
148  m_nTotalSeeds++;
149  nSeed++;
150 
151  if (!result.ok()) {
152  m_nFailedSeeds++;
153  ACTS_WARNING("Track finding failed for seed " << iseed << " with error"
154  << result.error());
155  continue;
156  }
157 
158  auto& tracksForSeed = result.value();
159  for (auto& track : tracksForSeed) {
160  seedNumber(track) = nSeed;
161  if (!m_trackSelector.has_value() ||
162  m_trackSelector->isValidTrack(track)) {
163  auto destProxy = tracks.getTrack(tracks.addTrack());
164  destProxy.copyFrom(track, true); // make sure we copy track states!
165  }
166  }
167  }
168 
169  // Compute shared hits from all the reconstructed tracks
170  if (m_cfg.computeSharedHits) {
171  computeSharedHits(sourceLinks, tracks);
172  }
173 
174  ACTS_DEBUG("Finalized track finding with " << tracks.size()
175  << " track candidates.");
176 
177  m_memoryStatistics.local().hist +=
178  tracks.trackStateContainer().statistics().hist;
179 
180  auto constTrackStateContainer =
181  std::make_shared<Acts::ConstVectorMultiTrajectory>(
182  std::move(*trackStateContainer));
183 
184  auto constTrackContainer = std::make_shared<Acts::ConstVectorTrackContainer>(
185  std::move(*trackContainer));
186 
187  ConstTrackContainer constTracks{constTrackContainer,
188  constTrackStateContainer};
189 
190  m_outputTracks(ctx, std::move(constTracks));
192 }
193 
195  ACTS_INFO("TrackFindingAlgorithm statistics:");
196  ACTS_INFO("- total seeds: " << m_nTotalSeeds);
197  ACTS_INFO("- failed seeds: " << m_nFailedSeeds);
198  ACTS_INFO("- failure ratio: " << static_cast<double>(m_nFailedSeeds) /
199  m_nTotalSeeds);
200 
201  auto memoryStatistics =
202  m_memoryStatistics.combine([](const auto& a, const auto& b) {
204  c.hist = a.hist + b.hist;
205  return c;
206  });
207  std::stringstream ss;
208  memoryStatistics.toStream(ss);
209  ACTS_DEBUG("Track State memory statistics (averaged):\n" << ss.str());
210  return ProcessCode::SUCCESS;
211 }