ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FittingAlgorithm.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FittingAlgorithm.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 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 
11 #include <stdexcept>
12 
17 
19  : FW::BareAlgorithm("FittingAlgorithm", level), m_cfg(std::move(cfg)) {
20  if (m_cfg.inputSourceLinks.empty()) {
21  throw std::invalid_argument("Missing input source links collection");
22  }
23  if (m_cfg.inputProtoTracks.empty()) {
24  throw std::invalid_argument("Missing input proto tracks collection");
25  }
26  if (m_cfg.inputInitialTrackParameters.empty()) {
27  throw std::invalid_argument(
28  "Missing input initial track parameters collection");
29  }
30  if (m_cfg.outputTrajectories.empty()) {
31  throw std::invalid_argument("Missing output trajectories collection");
32  }
33 }
34 
36  const FW::AlgorithmContext& ctx) const {
37  // Read input data
38  const auto sourceLinks =
39  ctx.eventStore.get<SimSourceLinkContainer>(m_cfg.inputSourceLinks);
40  const auto protoTracks =
41  ctx.eventStore.get<ProtoTrackContainer>(m_cfg.inputProtoTracks);
42  const auto initialParameters = ctx.eventStore.get<TrackParametersContainer>(
43  m_cfg.inputInitialTrackParameters);
44 
45  // Consistency cross checks
46  if (protoTracks.size() != initialParameters.size()) {
47  ACTS_FATAL("Inconsistent number of proto tracks and parameters");
48  return ProcessCode::ABORT;
49  }
50 
51  // Prepare the output data with MultiTrajectory
52  TrajectoryContainer trajectories;
53  trajectories.reserve(protoTracks.size());
54 
55  // Construct a perigee surface as the target surface
56  auto pSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(
57  Acts::Vector3D{0., 0., 0.});
58 
59  // Perform the fit for each input track
60  std::vector<SimSourceLink> trackSourceLinks;
61  for (std::size_t itrack = 0; itrack < protoTracks.size(); ++itrack) {
62  // The list of hits and the initial start parameters
63  const auto& protoTrack = protoTracks[itrack];
64  const auto& initialParams = initialParameters[itrack];
65 
66  // We can have empty tracks which must give empty fit results
67  if (protoTrack.empty()) {
68  trajectories.push_back(TruthFitTrack());
69  ACTS_WARNING("Empty track " << itrack << " found.");
70  continue;
71  }
72 
73  // Clear & reserve the right size
74  trackSourceLinks.clear();
75  trackSourceLinks.reserve(protoTrack.size());
76 
77  // Fill the source links via their indices from the container
78  for (auto hitIndex : protoTrack) {
79  auto sourceLink = sourceLinks.nth(hitIndex);
80  if (sourceLink == sourceLinks.end()) {
81  ACTS_FATAL("Proto track " << itrack << " contains invalid hit index"
82  << hitIndex);
83  return ProcessCode::ABORT;
84  }
85  trackSourceLinks.push_back(*sourceLink);
86  }
87 
88  // Set the KalmanFitter options
91  Acts::VoidOutlierFinder(), &(*pSurface));
92 
93  ACTS_DEBUG("Invoke fitter");
94  auto result = m_cfg.fit(trackSourceLinks, initialParams, kfOptions);
95  if (result.ok()) {
96  // Get the fit output object
97  const auto& fitOutput = result.value();
98  if (fitOutput.fittedParameters) {
99  const auto& params = fitOutput.fittedParameters.value();
100  ACTS_VERBOSE("Fitted paramemeters for track " << itrack);
101  ACTS_VERBOSE(" position: " << params.position().transpose());
102  ACTS_VERBOSE(" momentum: " << params.momentum().transpose());
103  // Construct a truth fit track using trajectory and
104  // track parameter
105  trajectories.emplace_back(fitOutput.trackTip,
106  std::move(fitOutput.fittedStates),
107  std::move(params));
108  } else {
109  ACTS_DEBUG("No fitted paramemeters for track " << itrack);
110  // Construct a truth fit track using trajectory
111  trajectories.emplace_back(fitOutput.trackTip,
112  std::move(fitOutput.fittedStates));
113  }
114  } else {
115  ACTS_WARNING("Fit failed for track " << itrack << " with error"
116  << result.error());
117  // Fit failed, but still create a empty truth fit track
118  trajectories.push_back(TruthFitTrack());
119  }
120  }
121 
122  ctx.eventStore.add(m_cfg.outputTrajectories, std::move(trajectories));
124 }