Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AlignmentAlgorithm.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file AlignmentAlgorithm.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 
18 
21  : ActsExamples::IAlgorithm("AlignmentAlgorithm", lvl),
22  m_cfg(std::move(cfg)) {
23  if (m_cfg.inputMeasurements.empty()) {
24  throw std::invalid_argument("Missing input measurement collection");
25  }
26  if (m_cfg.inputSourceLinks.empty()) {
27  throw std::invalid_argument("Missing input source links collection");
28  }
29  if (m_cfg.inputProtoTracks.empty()) {
30  throw std::invalid_argument("Missing input proto tracks collection");
31  }
32  if (m_cfg.inputInitialTrackParameters.empty()) {
33  throw std::invalid_argument(
34  "Missing input initial track parameters collection");
35  }
36  if (m_cfg.outputAlignmentParameters.empty()) {
37  throw std::invalid_argument(
38  "Missing output alignment parameters collection");
39  }
40 
46 }
47 
49  const ActsExamples::AlgorithmContext& ctx) const {
50  // Read input data
51  const auto& measurements = m_inputMeasurements(ctx);
52  const auto& sourceLinks = m_inputSourceLinks(ctx);
53  const auto& protoTracks = m_inputProtoTracks(ctx);
54  const auto& initialParameters = m_inputInitialTrackParameters(ctx);
55 
56  // Consistency cross checks
57  if (protoTracks.size() != initialParameters.size()) {
58  ACTS_FATAL("Inconsistent number of proto tracks and parameters "
59  << protoTracks.size() << " vs " << initialParameters.size());
60  return ProcessCode::ABORT;
61  }
62 
63  size_t numTracksUsed = protoTracks.size();
64  if (m_cfg.maxNumTracks > 0 and
65  m_cfg.maxNumTracks < static_cast<int>(protoTracks.size())) {
66  numTracksUsed = m_cfg.maxNumTracks;
67  }
68 
69  // Prepare the input track collection
70  std::vector<std::vector<IndexSourceLink>> sourceLinkTrackContainer;
71  sourceLinkTrackContainer.reserve(numTracksUsed);
72  std::vector<IndexSourceLink> trackSourceLinks;
73  for (std::size_t itrack = 0; itrack < numTracksUsed; ++itrack) {
74  // The list of hits and the initial start parameters
75  const auto& protoTrack = protoTracks[itrack];
76 
77  // Clear & reserve the right size
78  trackSourceLinks.clear();
79  trackSourceLinks.reserve(protoTrack.size());
80 
81  // Fill the source links via their indices from the container
82  for (auto hitIndex : protoTrack) {
83  auto sourceLink = sourceLinks.nth(hitIndex);
84  if (sourceLink == sourceLinks.end()) {
85  ACTS_FATAL("Proto track " << itrack << " contains invalid hit index"
86  << hitIndex);
87  return ProcessCode::ABORT;
88  }
89  trackSourceLinks.push_back(*sourceLink);
90  }
91  sourceLinkTrackContainer.push_back(trackSourceLinks);
92  }
93 
94  // Prepare the output for alignment parameters
95  AlignmentParameters alignedParameters;
96 
97  // Construct a perigee surface as the target surface for the fitter
98  auto pSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(
99  Acts::Vector3{0., 0., 0.});
100 
102  PassThroughCalibrator pcalibrator;
103  MeasurementCalibratorAdapter calibrator(pcalibrator, measurements);
104  extensions.calibrator.connect<&MeasurementCalibratorAdapter::calibrate>(
105  &calibrator);
106  Acts::GainMatrixUpdater kfUpdater;
107  Acts::GainMatrixSmoother kfSmoother;
108  extensions.updater.connect<
109  &Acts::GainMatrixUpdater::operator()<Acts::VectorMultiTrajectory>>(
110  &kfUpdater);
111  extensions.smoother.connect<
112  &Acts::GainMatrixSmoother::operator()<Acts::VectorMultiTrajectory>>(
113  &kfSmoother);
114 
115  // Set the KalmanFitter options
116  TrackFitterOptions kfOptions(ctx.geoContext, ctx.magFieldContext,
117  ctx.calibContext, extensions,
119 
120  // Set the alignment options
122  kfOptions, m_cfg.alignedTransformUpdater, m_cfg.alignedDetElements,
123  m_cfg.chi2ONdfCutOff, m_cfg.deltaChi2ONdfCutOff, m_cfg.maxNumIterations);
124 
125  ACTS_DEBUG("Invoke track-based alignment with " << numTracksUsed
126  << " input tracks");
127  auto result =
128  (*m_cfg.align)(sourceLinkTrackContainer, initialParameters, alignOptions);
129  if (result.ok()) {
130  const auto& alignOutput = result.value();
131  alignedParameters = alignOutput.alignedParameters;
132  ACTS_VERBOSE(
133  "Alignment finished with deltaChi2 = " << result.value().deltaChi2);
134  } else {
135  ACTS_WARNING("Alignment failed with " << result.error());
136  }
137 
138  // add alignment parameters to event store
139  m_outputAlignmentParameters(ctx, std::move(alignedParameters));
141 }