EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackSelector.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TrackSelector.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 
13 
14 #include <cmath>
15 #include <cstdint>
16 #include <stdexcept>
17 #include <vector>
18 
21  : BareAlgorithm("TrackSelector", lvl), m_cfg(cfg) {
22  if (m_cfg.inputTrackParameters.empty()) {
23  throw std::invalid_argument("Missing input track parameters collection");
24  }
25  if (m_cfg.outputTrackParameters.empty()) {
26  throw std::invalid_argument("Missing output track parameters collection");
27  }
28  if (m_cfg.outputTrackIndices.empty()) {
29  throw std::invalid_argument("Missing output track indices collection");
30  }
31 }
32 
34  const ActsExamples::AlgorithmContext& ctx) const {
35  // helper functions to select tracks
36  auto within = [](double x, double min, double max) {
37  return (min <= x) and (x < max);
38  };
39  auto isValidTrack = [&](const auto& trk) {
40  const auto theta = trk.template get<Acts::eBoundTheta>();
41  const auto eta = -std::log(std::tan(theta / 2));
42  // define charge selection
43  const bool validNeutral = (trk.charge() == 0) and not m_cfg.removeNeutral;
44  const bool validCharged = (trk.charge() != 0) and not m_cfg.removeCharged;
45  const bool validCharge = validNeutral or validCharged;
46  return validCharge and
47  within(trk.transverseMomentum(), m_cfg.ptMin, m_cfg.ptMax) and
48  within(std::abs(theta), m_cfg.absEtaMin, m_cfg.absEtaMax) and
49  within(eta, m_cfg.etaMin, m_cfg.etaMax) and
50  within(trk.template get<Acts::eBoundPhi>(), m_cfg.phiMin,
51  m_cfg.phiMax) and
52  within(trk.template get<Acts::eBoundLoc0>(), m_cfg.loc0Min,
53  m_cfg.loc0Max) and
54  within(trk.template get<Acts::eBoundLoc1>(), m_cfg.loc1Min,
55  m_cfg.loc1Max) and
56  within(trk.template get<Acts::eBoundTime>(), m_cfg.timeMin,
57  m_cfg.timeMax);
58  };
59 
60  // prepare input and output containers
61  const auto& inputTrackParameters =
62  ctx.eventStore.get<TrackParametersContainer>(m_cfg.inputTrackParameters);
63  TrackParametersContainer outputTrackParameters;
64  std::vector<uint32_t> outputTrackIndices;
65  outputTrackParameters.reserve(inputTrackParameters.size());
66  outputTrackIndices.reserve(inputTrackParameters.size());
67 
68  // copy selected tracks and record initial track index
69  for (uint32_t i = 0; i < inputTrackParameters.size(); ++i) {
70  const auto& trk = inputTrackParameters[i];
71  if (isValidTrack(trk)) {
72  outputTrackParameters.push_back(trk);
73  outputTrackIndices.push_back(i);
74  }
75  }
76  outputTrackParameters.shrink_to_fit();
77  outputTrackIndices.shrink_to_fit();
78 
79  ACTS_DEBUG("event " << ctx.eventNumber << " selected "
80  << outputTrackParameters.size() << " from "
81  << inputTrackParameters.size() << " tracks");
82 
83  ctx.eventStore.add(m_cfg.outputTrackParameters,
84  std::move(outputTrackParameters));
85  ctx.eventStore.add(m_cfg.outputTrackIndices, std::move(outputTrackIndices));
86  return ProcessCode::SUCCESS;
87 }