Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VertexFitterAlgorithm.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file VertexFitterAlgorithm.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019-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 
19 
20 #include <ostream>
21 #include <stdexcept>
22 #include <system_error>
23 
24 #include "VertexingHelpers.hpp"
25 
27  const Config& cfg, Acts::Logging::Level lvl)
28  : ActsExamples::IAlgorithm("VertexFit", lvl), m_cfg(cfg) {
29  if (m_cfg.inputTrackParameters.empty() == m_cfg.inputTrajectories.empty()) {
30  throw std::invalid_argument(
31  "You have to either provide track parameters or trajectories");
32  }
33  if (m_cfg.inputProtoVertices.empty()) {
34  throw std::invalid_argument("Missing input proto vertices collection");
35  }
36 
39 
42 }
43 
45  const ActsExamples::AlgorithmContext& ctx) const {
46  // Set up EigenStepper
48 
49  // Setup the propagator with void navigator
50  auto propagator = std::make_shared<Propagator>(
52  PropagatorOptions propagatorOpts(ctx.geoContext, ctx.magFieldContext);
53  // Setup the vertex fitter
54  VertexFitter::Config vertexFitterCfg;
55  VertexFitter vertexFitter(vertexFitterCfg);
56  VertexFitter::State state(m_cfg.bField->makeCache(ctx.magFieldContext));
57  // Setup the linearizer
58  Linearizer::Config ltConfig(m_cfg.bField, propagator);
59  Linearizer linearizer(ltConfig, logger().cloneWithSuffix("HelLin"));
60 
61  ACTS_VERBOSE("Read from '" << m_cfg.inputTrackParameters << "'");
62  ACTS_VERBOSE("Read from '" << m_cfg.inputProtoVertices << "'");
63 
64  auto [inputTrackParameters, inputTrackPointers] =
65  makeParameterContainers(ctx, m_inputTrackParameters, m_inputTrajectories);
66 
67  if (inputTrackParameters.size() != inputTrackPointers.size()) {
68  ACTS_ERROR("Input track containers do not align: "
69  << inputTrackParameters.size()
70  << " != " << inputTrackPointers.size());
71  }
72 
73  ACTS_VERBOSE("Have " << inputTrackParameters.size() << " track parameters");
74  const auto& protoVertices = m_inputProtoVertices(ctx);
75  ACTS_VERBOSE("Have " << protoVertices.size() << " proto vertices");
76 
77  std::vector<const Acts::BoundTrackParameters*> inputTrackPtrCollection;
78 
79  VertexCollection fittedVertices;
80 
81  for (const auto& protoVertex : protoVertices) {
82  // un-constrained fit requires at least two tracks
83  if ((not m_cfg.doConstrainedFit) and (protoVertex.size() < 2)) {
84  ACTS_INFO(
85  "Skip un-constrained vertex fit on proto-vertex with less than two "
86  "tracks");
87  continue;
88  }
89 
90  // select input tracks for the input proto vertex
91  inputTrackPtrCollection.clear();
92  inputTrackPtrCollection.reserve(protoVertex.size());
93  for (const auto& trackIdx : protoVertex) {
94  if (trackIdx >= inputTrackParameters.size()) {
95  ACTS_ERROR("track parameters " << trackIdx << " does not exist");
96  continue;
97  }
98 
99  inputTrackPtrCollection.push_back(inputTrackPointers[trackIdx]);
100  }
101 
102  if (!m_cfg.doConstrainedFit) {
103  VertexFitterOptions vfOptions(ctx.geoContext, ctx.magFieldContext);
104 
105  auto fitRes = vertexFitter.fit(inputTrackPtrCollection, linearizer,
106  vfOptions, state);
107  if (fitRes.ok()) {
108  fittedVertices.push_back(*fitRes);
109  } else {
110  ACTS_ERROR("Error in vertex fitter: " << fitRes.error().message());
111  }
112  } else {
113  // Vertex constraint
115 
116  theConstraint.setFullCovariance(m_cfg.constraintCov);
117  theConstraint.setFullPosition(m_cfg.constraintPos);
118 
119  // Vertex fitter options
120  VertexFitterOptions vfOptionsConstr(ctx.geoContext, ctx.magFieldContext,
121  theConstraint);
122 
123  auto fitRes = vertexFitter.fit(inputTrackPtrCollection, linearizer,
124  vfOptionsConstr, state);
125  if (fitRes.ok()) {
126  fittedVertices.push_back(*fitRes);
127  } else {
128  ACTS_ERROR(
129  "Error in constrained vertex fitter: " << fitRes.error().message());
130  }
131  }
132 
133  if (fittedVertices.empty()) {
134  ACTS_DEBUG("No fitted vertex");
135  } else {
136  ACTS_DEBUG("Fitted Vertex "
137  << fittedVertices.back().fullPosition().transpose());
138  ACTS_DEBUG(
139  "Tracks at fitted Vertex: " << fittedVertices.back().tracks().size());
140  }
141  }
142 
143  m_outputVertices(ctx, std::move(fittedVertices));
144  return ProcessCode::SUCCESS;
145 }