Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Generators.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Generators.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 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 
19 
20 #include <array>
21 #include <cassert>
22 #include <cmath>
23 #include <cstddef>
24 #include <memory>
25 #include <optional>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 #include <pybind11/pybind11.h>
31 #include <pybind11/stl.h>
32 
33 namespace ActsExamples {
34 class IReader;
35 } // namespace ActsExamples
36 
37 namespace py = pybind11;
38 
39 namespace {
40 double thetaToEta(double theta) {
41  assert(theta != 0);
42  return -1 * std::log(std::tan(theta / 2.));
43 }
44 double etaToTheta(double eta) {
45  return 2 * std::atan(std::exp(-eta));
46 }
47 } // namespace
48 
49 namespace Acts::Python {
50 
51 void addGenerators(Context& ctx) {
52  auto mex = ctx.get("examples");
53  {
56  std::shared_ptr<ActsExamples::EventGenerator>>(
57  mex, "EventGenerator")
58  .def(py::init<const Config&, Acts::Logging::Level>(),
59  py::arg("config"), py::arg("level"))
60  .def_property_readonly(
62 
64  std::shared_ptr<ActsExamples::EventGenerator::VertexGenerator>>(
65  gen, "VertexGenerator");
66  py::class_<
68  std::shared_ptr<ActsExamples::EventGenerator::ParticlesGenerator>>(
69  gen, "ParticlesGenerator");
70  py::class_<
72  std::shared_ptr<ActsExamples::EventGenerator::MultiplicityGenerator>>(
73  gen, "MultiplicityGenerator");
74 
75  using EventGenerator = ActsExamples::EventGenerator;
77  py::class_<Generator>(gen, "Generator")
78  .def(py::init<>())
79  .def(py::init<std::shared_ptr<EventGenerator::MultiplicityGenerator>,
80  std::shared_ptr<EventGenerator::VertexGenerator>,
81  std::shared_ptr<EventGenerator::ParticlesGenerator>>(),
82  py::arg("multiplicity"), py::arg("vertex"), py::arg("particles"))
83  .def_readwrite("multiplicity", &Generator::multiplicity)
84  .def_readwrite("vertex", &Generator::vertex)
85  .def_readwrite("particles", &Generator::particles);
86 
87  py::class_<Config>(gen, "Config")
88  .def(py::init<>())
89  .def_readwrite("outputParticles", &Config::outputParticles)
90  .def_readwrite("generators", &Config::generators)
91  .def_readwrite("randomNumbers", &Config::randomNumbers);
92  }
93 
96  std::shared_ptr<ActsExamples::GaussianVertexGenerator>>(
97  mex, "GaussianVertexGenerator")
98  .def(py::init<>())
99  .def(py::init([](const Acts::Vector4& stddev, const Acts::Vector4& mean) {
100  ActsExamples::GaussianVertexGenerator g;
101  g.stddev = stddev;
102  g.mean = mean;
103  return g;
104  }),
105  py::arg("stddev"), py::arg("mean"))
106  .def_readwrite("stddev", &ActsExamples::GaussianVertexGenerator::stddev)
107  .def_readwrite("mean", &ActsExamples::GaussianVertexGenerator::mean);
108 
110  ActsExamples::EventGenerator::VertexGenerator,
111  std::shared_ptr<ActsExamples::FixedVertexGenerator>>(
112  mex, "FixedVertexGenerator")
113  .def(py::init<>())
114  .def(py::init([](const Acts::Vector4& v) {
115  ActsExamples::FixedVertexGenerator g;
116  g.fixed = v;
117  return g;
118  }),
119  py::arg("fixed"))
120  .def_readwrite("fixed", &ActsExamples::FixedVertexGenerator::fixed);
121 
122  py::class_<ActsExamples::SimParticle>(mex, "SimParticle");
123  py::class_<ActsExamples::SimParticleContainer>(mex, "SimParticleContainer");
124 
125  {
127  auto gen =
130  std::shared_ptr<ActsExamples::ParametricParticleGenerator>>(
131  mex, "ParametricParticleGenerator")
132  .def(py::init<const Config&>());
133 
134  py::class_<Config>(gen, "Config")
135  .def(py::init<>())
136  .def_readwrite("phiMin", &Config::phiMin)
137  .def_readwrite("phiMax", &Config::phiMax)
138  .def_readwrite("thetaMin", &Config::thetaMin)
139  .def_readwrite("thetaMax", &Config::thetaMax)
140  .def_readwrite("etaUniform", &Config::etaUniform)
141  .def_readwrite("pMin", &Config::pMin)
142  .def_readwrite("pMax", &Config::pMax)
143  .def_readwrite("pTransverse", &Config::pTransverse)
144  .def_readwrite("pdg", &Config::pdg)
145  .def_readwrite("randomizeCharge", &Config::randomizeCharge)
146  .def_readwrite("numParticles", &Config::numParticles)
147  .def_readwrite("mass", &Config::mass)
148  .def_readwrite("charge", &Config::charge)
149  .def_property(
150  "p",
151  [](Config& cfg) {
152  return std::pair{cfg.pMin, cfg.pMax};
153  },
154  [](Config& cfg, std::pair<double, double> value) {
155  cfg.pMin = value.first;
156  cfg.pMax = value.second;
157  })
158  .def_property(
159  "phi",
160  [](Config& cfg) {
161  return std::pair{cfg.phiMin, cfg.phiMax};
162  },
163  [](Config& cfg, std::pair<double, double> value) {
164  cfg.phiMin = value.first;
165  cfg.phiMax = value.second;
166  })
167  .def_property(
168  "theta",
169  [](Config& cfg) {
170  return std::pair{cfg.thetaMin, cfg.thetaMax};
171  },
172  [](Config& cfg, std::pair<double, double> value) {
173  cfg.thetaMin = value.first;
174  cfg.thetaMax = value.second;
175  })
176  .def_property(
177  "eta",
178  [](Config& cfg) {
179  return std::pair{thetaToEta(cfg.thetaMin),
180  thetaToEta(cfg.thetaMax)};
181  },
182  [](Config& cfg, std::pair<double, double> value) {
183  cfg.thetaMin = etaToTheta(value.first);
184  cfg.thetaMax = etaToTheta(value.second);
185  });
186  }
187 
190  std::shared_ptr<ActsExamples::FixedMultiplicityGenerator>>(
191  mex, "FixedMultiplicityGenerator")
192  .def(py::init<>())
193  .def(py::init([](size_t n) {
194  ActsExamples::FixedMultiplicityGenerator g;
195  g.n = n;
196  return g;
197  }),
198  py::arg("n"))
199  .def_readwrite("n", &ActsExamples::FixedMultiplicityGenerator::n);
200 
202  ActsExamples::EventGenerator::MultiplicityGenerator,
203  std::shared_ptr<ActsExamples::PoissonMultiplicityGenerator>>(
204  mex, "PoissonMultiplicityGenerator")
205  .def(py::init<>())
206  .def(py::init([](double mean) {
207  ActsExamples::PoissonMultiplicityGenerator g;
208  g.mean = mean;
209  return g;
210  }),
211  py::arg("mean"))
212  .def_readwrite("mean", &ActsExamples::PoissonMultiplicityGenerator::mean);
213 }
214 } // namespace Acts::Python