Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RefittingAlgorithm.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RefittingAlgorithm.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 
10 
26 
27 #include <functional>
28 #include <optional>
29 #include <ostream>
30 #include <stdexcept>
31 #include <system_error>
32 #include <utility>
33 #include <vector>
34 
37  : ActsExamples::IAlgorithm("TrackFittingAlgorithm", level),
38  m_cfg(std::move(config)) {
39  if (m_cfg.inputTracks.empty()) {
40  throw std::invalid_argument("Missing input tracks collection");
41  }
42  if (m_cfg.outputTracks.empty()) {
43  throw std::invalid_argument("Missing output tracks collection");
44  }
45 
48 }
49 
51  const ActsExamples::AlgorithmContext& ctx) const {
52  const auto& inputTracks = m_inputTracks(ctx);
53 
54  auto trackContainer = std::make_shared<Acts::VectorTrackContainer>();
55  auto trackStateContainer = std::make_shared<Acts::VectorMultiTrajectory>();
56  TrackContainer tracks(trackContainer, trackStateContainer);
57 
58  // Perform the fit for each input track
59  std::vector<Acts::SourceLink> trackSourceLinks;
60  std::vector<const Acts::Surface*> surfSequence;
61  RefittingCalibrator calibrator;
62 
63  auto itrack = 0ul;
64  for (const auto& track : inputTracks) {
65  // Check if you are not in picking mode
66  if (m_cfg.pickTrack > -1 and
67  m_cfg.pickTrack != static_cast<int>(itrack++)) {
68  continue;
69  }
70 
73  &track.referenceSurface(), Acts::PropagatorPlainOptions()};
74 
75  const Acts::BoundTrackParameters initialParams(
76  track.referenceSurface().getSharedPtr(), track.parameters(),
77  track.covariance(), track.particleHypothesis());
78 
79  trackSourceLinks.clear();
80  surfSequence.clear();
81 
82  for (auto state : track.trackStatesReversed()) {
83  surfSequence.push_back(&state.referenceSurface());
84 
85  if (not state.hasCalibrated()) {
86  continue;
87  }
88 
90  trackSourceLinks.push_back(Acts::SourceLink{sl});
91  }
92 
93  if (surfSequence.empty()) {
94  ACTS_WARNING("Empty track " << itrack << " found.");
95  continue;
96  }
97 
98  ACTS_VERBOSE("Initial parameters: "
99  << initialParams.fourPosition(ctx.geoContext).transpose()
100  << " -> " << initialParams.direction().transpose());
101 
102  ACTS_DEBUG("Invoke direct fitter for track " << itrack);
103  auto result = (*m_cfg.fit)(trackSourceLinks, initialParams, options,
104  calibrator, surfSequence, tracks);
105 
106  if (result.ok()) {
107  // Get the fit output object
108  const auto& refittedTrack = result.value();
109  if (refittedTrack.hasReferenceSurface()) {
110  ACTS_VERBOSE("Refitted parameters for track " << itrack);
111  ACTS_VERBOSE(" " << track.parameters().transpose());
112  } else {
113  ACTS_DEBUG("No refitted parameters for track " << itrack);
114  }
115  } else {
116  ACTS_WARNING("Fit failed for track "
117  << itrack << " with error: " << result.error() << ", "
118  << result.error().message());
119  }
120  }
121 
122  std::stringstream ss;
123  trackStateContainer->statistics().toStream(ss);
124  ACTS_DEBUG(ss.str());
125 
126  ConstTrackContainer constTracks{
127  std::make_shared<Acts::ConstVectorTrackContainer>(
128  std::move(*trackContainer)),
129  std::make_shared<Acts::ConstVectorMultiTrajectory>(
130  std::move(*trackStateContainer))};
131 
132  m_outputTracks(ctx, std::move(constTracks));
134 }