ECCE @ 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 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 <algorithm>
12 #include <stdexcept>
13 #include <vector>
14 
19 
21  : FW::BareAlgorithm("Selector", level), m_cfg(cfg) {
22  if (m_cfg.input.empty()) {
23  throw std::invalid_argument("Missing input collection");
24  }
25  if (m_cfg.output.empty()) {
26  throw std::invalid_argument("Missing output collection");
27  }
28 }
29 
31  const FW::AlgorithmContext& ctx) const {
32  std::vector<VertexAndTracks> selected;
33 
34  // get input tracks
35  const auto& input =
36  ctx.eventStore.get<std::vector<VertexAndTracks>>(m_cfg.input);
37 
38  auto within = [](double x, double min, double max) {
39  return (min <= x) and (x < max);
40  };
41  auto isValidTrack = [&](const auto& trk) {
42  auto rho = std::hypot(trk.position().x(), trk.position().y());
43  auto phi = std::atan2(trk.momentum().y(), trk.momentum().x());
44  auto eta = std::atanh(trk.momentum().z() / trk.momentum().norm());
45  auto pt = std::hypot(trk.momentum().x(), trk.momentum().y());
46  return within(rho, 0, m_cfg.rhoMax) and
47  within(std::abs(trk.position().z()), 0, m_cfg.absZMax) and
48  within(phi, m_cfg.phiMin, m_cfg.phiMax) and
49  within(eta, m_cfg.etaMin, m_cfg.etaMax) and
50  within(std::abs(eta), m_cfg.absEtaMin, m_cfg.absEtaMax) and
51  within(pt, m_cfg.ptMin, m_cfg.ptMax) and
52  (m_cfg.keepNeutral or (trk.charge() != 0));
53  };
54 
55  for (const auto& vertexAndTracks : input) {
56  VertexAndTracks sel;
57  sel.vertex = vertexAndTracks.vertex;
58 
59  // Copy selected tracks over
60  std::copy_if(vertexAndTracks.tracks.begin(), vertexAndTracks.tracks.end(),
61  std::back_inserter(sel.tracks), isValidTrack);
62  // Only retain vertex if it still contains tracks
63  if (not sel.tracks.empty()) {
64  selected.push_back(std::move(sel));
65  }
66  }
67 
68  ACTS_DEBUG("event " << ctx.eventNumber << " selected " << selected.size()
69  << " from " << input.size() << " vertices.");
70 
71  // write selected tracks
72  ctx.eventStore.add(m_cfg.output, std::move(selected));
73 
74  return ProcessCode::SUCCESS;
75 }