EIC 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 
25 
26 #include <stdexcept>
27 
29  const Config& cfg, Acts::Logging::Level lvl)
30  : ActsExamples::BareAlgorithm("VertexFit", lvl), m_cfg(cfg) {
31  if (m_cfg.inputTrackParameters.empty()) {
32  throw std::invalid_argument("Missing input track parameters collection");
33  }
34  if (m_cfg.inputProtoVertices.empty()) {
35  throw std::invalid_argument("Missing input proto vertices collection");
36  }
37 }
38 
40  const ActsExamples::AlgorithmContext& ctx) const {
41  using MagneticField = Acts::ConstantBField;
44  using PropagatorOptions = Acts::PropagatorOptions<>;
46  using VertexFitter =
48  using VertexFitterOptions =
50 
51  // Setup the magnetic field
52  MagneticField bField(m_cfg.bField);
53  // Setup the propagator with void navigator
54  auto propagator = std::make_shared<Propagator>(Stepper(bField));
55  PropagatorOptions propagatorOpts(ctx.geoContext, ctx.magFieldContext,
56  Acts::LoggerWrapper{logger()});
57  // Setup the vertex fitter
58  VertexFitter::Config vertexFitterCfg;
59  VertexFitter vertexFitter(vertexFitterCfg);
60  VertexFitter::State state(ctx.magFieldContext);
61  // Setup the linearizer
62  Linearizer::Config ltConfig(bField, propagator);
63  Linearizer linearizer(ltConfig);
64 
65  const auto& trackParameters =
66  ctx.eventStore.get<TrackParametersContainer>(m_cfg.inputTrackParameters);
67  const auto& protoVertices =
68  ctx.eventStore.get<ProtoVertexContainer>(m_cfg.inputProtoVertices);
69  std::vector<const Acts::BoundTrackParameters*> inputTrackPtrCollection;
70 
71  for (const auto& protoVertex : protoVertices) {
72  // un-constrained fit requires at least two tracks
73  if ((not m_cfg.doConstrainedFit) and (protoVertex.size() < 2)) {
75  "Skip un-constrained vertex fit on proto-vertex with less than two "
76  "tracks");
77  continue;
78  }
79 
80  // select input tracks for the input proto vertex
81  inputTrackPtrCollection.clear();
82  inputTrackPtrCollection.reserve(protoVertex.size());
83  for (const auto& trackIdx : protoVertex) {
84  inputTrackPtrCollection.push_back(&trackParameters[trackIdx]);
85  }
86 
88  if (!m_cfg.doConstrainedFit) {
89  VertexFitterOptions vfOptions(ctx.geoContext, ctx.magFieldContext);
90 
91  auto fitRes = vertexFitter.fit(inputTrackPtrCollection, linearizer,
92  vfOptions, state);
93  if (fitRes.ok()) {
94  fittedVertex = *fitRes;
95  } else {
96  ACTS_ERROR("Error in vertex fit.");
97  ACTS_ERROR(fitRes.error().message());
98  }
99  } else {
100  // Vertex constraint
102 
103  theConstraint.setCovariance(m_cfg.constraintCov);
104  theConstraint.setPosition(m_cfg.constraintPos);
105 
106  // Vertex fitter options
107  VertexFitterOptions vfOptionsConstr(ctx.geoContext, ctx.magFieldContext,
108  theConstraint);
109 
110  auto fitRes = vertexFitter.fit(inputTrackPtrCollection, linearizer,
111  vfOptionsConstr, state);
112  if (fitRes.ok()) {
113  fittedVertex = *fitRes;
114  } else {
115  ACTS_ERROR("Error in vertex fit with constraint.");
116  ACTS_ERROR(fitRes.error().message());
117  }
118  }
119 
120  ACTS_INFO("Fitted Vertex " << fittedVertex.fullPosition().transpose());
121  }
122  return ProcessCode::SUCCESS;
123 }