ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParticleSelector.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ParticleSelector.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 <boost/program_options.hpp>
13 #include <stdexcept>
14 #include <vector>
15 
20 #include "Acts/Utilities/Units.hpp"
21 
23  using boost::program_options::bool_switch;
25  using Options::Interval;
26 
27  auto opt = desc.add_options();
28  opt("select-rho-mm", value<Interval>()->value_name("MIN:MAX"),
29  "Select particle transverse distance to the origin in mm");
30  opt("select-absz-mm", value<Interval>()->value_name("MIN:MAX"),
31  "Select particle absolute longitudinal distance to the origin in mm");
32  opt("select-phi-degree", value<Interval>()->value_name("MIN:MAX"),
33  "Select particle direction angle in the transverse plane in degree");
34  opt("select-eta", value<Interval>()->value_name("MIN:MAX"),
35  "Select particle pseudo-rapidity");
36  opt("select-abseta", value<Interval>()->value_name("MIN:MAX"),
37  "Select particle absolute pseudo-rapidity");
38  opt("select-pt-gev", value<Interval>()->value_name("MIN:MAX"),
39  "Select particle transverse momentum in GeV");
40  opt("remove-charged", bool_switch(), "Remove charged particles");
41  opt("remove-neutral", bool_switch(), "Remove neutral particles");
42 }
43 
45  const FW::Options::Variables& vars) {
46  using namespace Acts::UnitLiterals;
47 
48  // Set boundary values if the given config exists
49  auto extractInterval = [&](const char* name, auto unit, auto& lower,
50  auto& upper) {
51  if (vars[name].empty()) {
52  return;
53  }
54  auto interval = vars[name].as<Options::Interval>();
55  lower = interval.lower.value_or(lower) * unit;
56  upper = interval.upper.value_or(upper) * unit;
57  };
58 
59  Config cfg;
60  extractInterval("select-rho-mm", 1_mm, cfg.rhoMin, cfg.rhoMax);
61  extractInterval("select-absz-mm", 1_mm, cfg.absZMin, cfg.absZMax);
62  extractInterval("select-phi-degree", 1_degree, cfg.phiMin, cfg.phiMax);
63  extractInterval("select-eta", 1.0, cfg.etaMin, cfg.etaMax);
64  extractInterval("select-abseta", 1.0, cfg.absEtaMin, cfg.absEtaMax);
65  extractInterval("select-pt-gev", 1_GeV, cfg.ptMin, cfg.ptMax);
66  cfg.removeCharged = vars["remove-charged"].as<bool>();
67  cfg.removeNeutral = vars["remove-neutral"].as<bool>();
68  return cfg;
69 }
70 
73  : FW::BareAlgorithm("ParticleSelector", lvl), m_cfg(cfg) {
74  if (m_cfg.inputEvent.empty()) {
75  throw std::invalid_argument("Missing input event collection");
76  }
77  if (m_cfg.outputEvent.empty()) {
78  throw std::invalid_argument("Missing output event collection");
79  }
80  ACTS_DEBUG("selection particle rho [" << m_cfg.rhoMin << "," << m_cfg.rhoMax
81  << "]");
82  ACTS_DEBUG("selection particle |z| [" << m_cfg.absZMin << "," << m_cfg.absZMax
83  << "]");
84  ACTS_DEBUG("selection particle phi [" << m_cfg.phiMin << "," << m_cfg.phiMax
85  << "]");
86  ACTS_DEBUG("selection particle eta [" << m_cfg.etaMin << "," << m_cfg.etaMax
87  << "]");
88  ACTS_DEBUG("selection particle |eta| [" << m_cfg.absEtaMin << ","
89  << m_cfg.absEtaMax << "]");
90  ACTS_DEBUG("selection particle pt [" << m_cfg.ptMin << "," << m_cfg.ptMax
91  << "]");
92  ACTS_DEBUG("remove charged particles " << m_cfg.removeCharged);
93  ACTS_DEBUG("remove neutral particles " << m_cfg.removeNeutral);
94 }
95 
97  const FW::AlgorithmContext& ctx) const {
98  using SimEvent = std::vector<SimVertex>;
99 
100  // prepare input/ output types
101  const auto& input = ctx.eventStore.get<SimEvent>(m_cfg.inputEvent);
102  SimEvent selected;
103 
104  auto within = [](double x, double min, double max) {
105  return (min <= x) and (x < max);
106  };
107  auto isValidParticle = [&](const ActsFatras::Particle& p) {
108  const auto eta = Acts::VectorHelpers::eta(p.unitDirection());
109  const auto phi = Acts::VectorHelpers::phi(p.unitDirection());
110  const auto rho = Acts::VectorHelpers::perp(p.position());
111  // defined charge selection
112  const bool validNeutral = (p.charge() == 0) and not m_cfg.removeNeutral;
113  const bool validCharged = (p.charge() != 0) and not m_cfg.removeCharged;
114  const bool validCharge = validNeutral or validCharged;
115  return validCharge and
116  within(p.transverseMomentum(), m_cfg.ptMin, m_cfg.ptMax) and
117  within(std::abs(eta), m_cfg.absEtaMin, m_cfg.absEtaMax) and
118  within(eta, m_cfg.etaMin, m_cfg.etaMax) and
119  within(phi, m_cfg.phiMin, m_cfg.phiMax) and
120  within(std::abs(p.position().z()), m_cfg.absZMin, m_cfg.absZMax) and
121  within(rho, m_cfg.rhoMin, m_cfg.rhoMax);
122  };
123 
124  std::size_t allParticles = 0;
125  std::size_t selectedParticles = 0;
126 
127  selected.reserve(input.size());
128  for (const auto& inputVertex : input) {
129  allParticles += inputVertex.incoming.size();
130  allParticles += inputVertex.outgoing.size();
131 
132  SimVertex vertex(inputVertex.position4, inputVertex.process);
133  // copy selected particles over
134  std::copy_if(inputVertex.incoming.begin(), inputVertex.incoming.end(),
135  std::back_inserter(vertex.incoming), isValidParticle);
136  std::copy_if(inputVertex.outgoing.begin(), inputVertex.outgoing.end(),
137  std::back_inserter(vertex.outgoing), isValidParticle);
138 
139  // only retain vertex if it still contains particles
140  if (vertex.incoming.empty() and vertex.outgoing.empty()) {
141  continue;
142  }
143 
144  selectedParticles += vertex.incoming.size();
145  selectedParticles += vertex.outgoing.size();
146  selected.push_back(std::move(vertex));
147  }
148 
149  ACTS_DEBUG("event " << ctx.eventNumber << " selected " << selectedParticles
150  << " from " << allParticles << " particles");
151 
152  ctx.eventStore.add(m_cfg.outputEvent, std::move(selected));
153  return ProcessCode::SUCCESS;
154 }