forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDefinitions.cpp
More file actions
459 lines (434 loc) · 17.3 KB
/
Definitions.cpp
File metadata and controls
459 lines (434 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Definitions/PdgParticle.hpp"
#include "Acts/Definitions/TrackParametrization.hpp"
#include "Acts/Definitions/Units.hpp"
#include "Acts/EventData/ParticleHypothesis.hpp"
#include <format>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
using namespace pybind11::literals;
namespace ActsPython {
/// @brief This adds the classes from Core/Definitions to the python module
/// @param m the pybind11 core module
void addDefinitions(py::module_& m) {
using namespace Acts;
// Add algebra classes here
py::class_<Vector2>(m, "Vector2", py::buffer_protocol())
.def(py::init<double, double>())
.def(py::init([](std::array<double, 2> a) {
Vector2 v;
v << a[0], a[1];
return v;
}))
.def_buffer([](const Vector2& self) {
return py::buffer_info(const_cast<double*>(self.data()), sizeof(double),
py::format_descriptor<double>::format(), 1,
{self.size()}, {sizeof(double)});
})
.def("__getitem__",
[](const Vector2& self, Eigen::Index i) { return self[i]; })
.def("__iter__",
[](const Vector2& self) {
return py::make_iterator(self.data(), self.data() + self.size());
})
.def("__str__", [](const Vector2& self) {
std::stringstream ss;
ss << self.transpose();
return ss.str();
});
py::class_<Vector3>(m, "Vector3", py::buffer_protocol())
.def(py::init<double, double, double>())
.def(py::init([](std::array<double, 3> a) {
Vector3 v;
v << a[0], a[1], a[2];
return v;
}))
.def_static("UnitX", []() -> Vector3 { return Vector3::UnitX(); })
.def_static("UnitY", []() -> Vector3 { return Vector3::UnitY(); })
.def_static("UnitZ", []() -> Vector3 { return Vector3::UnitZ(); })
.def("__add__",
[](const Vector3& self, const Vector3& other) {
return (self + other).eval();
})
.def("__sub__",
[](const Vector3& self, const Vector3& other) {
return (self - other).eval();
})
.def("__mul__",
[](const Vector3& self, const Vector3& other) {
return self.cwiseProduct(other).eval();
})
.def_buffer([](const Vector3& self) {
return py::buffer_info(const_cast<double*>(self.data()), sizeof(double),
py::format_descriptor<double>::format(), 1,
{self.size()}, {sizeof(double)});
})
.def("cross",
[](const Vector3& self, const Vector3& other) {
return self.cross(other).eval();
})
.def("__getitem__",
[](const Vector3& self, Eigen::Index i) { return self[i]; })
.def("__iter__",
[](const Vector3& self) {
return py::make_iterator(self.data(), self.data() + self.size());
})
.def("__str__", [](const Vector3& self) {
return std::format("({}, {}, {})", self[0], self[1], self[2]);
});
py::class_<Vector4>(m, "Vector4", py::buffer_protocol())
.def(py::init<double, double, double, double>())
.def(py::init([](std::array<double, 4> a) {
Vector4 v;
v << a[0], a[1], a[2], a[3];
return v;
}))
.def("__add__",
[](const Vector4& self, const Vector4& other) {
return (self + other).eval();
})
.def("__sub__",
[](const Vector4& self, const Vector4& other) {
return (self - other).eval();
})
.def("__mul__",
[](const Vector4& self, const Vector4& other) {
return self.cwiseProduct(other).eval();
})
.def_buffer([](const Vector4& self) {
return py::buffer_info(const_cast<double*>(self.data()), sizeof(double),
py::format_descriptor<double>::format(), 1,
{self.size()}, {sizeof(double)});
})
.def("__getitem__",
[](const Vector4& self, Eigen::Index i) { return self[i]; })
.def("__iter__",
[](const Vector4& self) {
return py::make_iterator(self.data(), self.data() + self.size());
})
.def("__str__", [](const Vector4& self) {
return std::format("({}, {}, {}, {})", self[0], self[1], self[2],
self[3]);
});
py::class_<BoundVector>(m, "BoundVector", py::buffer_protocol())
.def(py::init<double, double, double, double, double, double>())
.def(py::init([](std::array<double, 6> a) {
BoundVector v;
v << a[0], a[1], a[2], a[3], a[4], a[5];
return v;
}))
.def_static("Zero", []() -> BoundVector { return BoundVector::Zero(); })
.def_buffer([](const BoundVector& self) {
return py::buffer_info(const_cast<double*>(self.data()), sizeof(double),
py::format_descriptor<double>::format(), 1,
{self.size()}, {sizeof(double)});
})
.def("__getitem__",
[](const BoundVector& self, Eigen::Index i) { return self[i]; })
.def("__iter__",
[](const BoundVector& self) {
return py::make_iterator(self.data(), self.data() + self.size());
})
.def("__str__", [](const BoundVector& self) {
return std::format("({}, {}, {}, {}, {}, {})", self[0], self[1],
self[2], self[3], self[4], self[5]);
});
py::class_<BoundMatrix>(m, "BoundMatrix")
.def(py::init([]() { return BoundMatrix::Zero(); }))
.def_static("Zero", []() -> BoundMatrix { return BoundMatrix::Zero(); })
.def_static("Identity",
[]() -> BoundMatrix { return BoundMatrix::Identity(); })
.def("__getitem__",
[](const BoundMatrix& self, const py::object& idx) {
py::tuple t = idx.cast<py::tuple>();
if (py::len(t) != 2) {
throw py::index_error("BoundMatrix index must be (i, j)");
}
return self(t[0].cast<Eigen::Index>(), t[1].cast<Eigen::Index>());
})
.def("__str__", [](const BoundMatrix& self) {
std::stringstream ss;
ss << self;
return ss.str();
});
py::class_<FreeVector>(m, "FreeVector", py::buffer_protocol())
.def(py::init<double, double, double, double, double, double, double,
double>())
.def(py::init([](std::array<double, 8> a) {
FreeVector v;
v << a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7];
return v;
}))
.def_static("Zero", []() -> FreeVector { return FreeVector::Zero(); })
.def_buffer([](const FreeVector& self) {
return py::buffer_info(const_cast<double*>(self.data()), sizeof(double),
py::format_descriptor<double>::format(), 1,
{self.size()}, {sizeof(double)});
})
.def("__getitem__",
[](const FreeVector& self, Eigen::Index i) { return self[i]; })
.def("__iter__",
[](const FreeVector& self) {
return py::make_iterator(self.data(), self.data() + self.size());
})
.def("__str__", [](const FreeVector& self) {
return std::format("({}, {}, {}, {}, {}, {}, {}, {})", self[0], self[1],
self[2], self[3], self[4], self[5], self[6],
self[7]);
});
py::class_<FreeMatrix>(m, "FreeMatrix")
.def(py::init([]() { return FreeMatrix::Zero(); }))
.def_static("Zero", []() -> FreeMatrix { return FreeMatrix::Zero(); })
.def_static("Identity",
[]() -> FreeMatrix { return FreeMatrix::Identity(); })
.def("__getitem__",
[](const FreeMatrix& self, const py::object& idx) {
py::tuple t = idx.cast<py::tuple>();
if (py::len(t) != 2) {
throw py::index_error("FreeMatrix index must be (i, j)");
}
return self(t[0].cast<Eigen::Index>(), t[1].cast<Eigen::Index>());
})
.def("__str__", [](const SquareMatrix2& self) {
std::stringstream ss;
ss << self;
return ss.str();
});
py::class_<SquareMatrix2>(m, "SquareMatrix2")
.def(py::init([]() { return SquareMatrix2::Zero(); }))
.def(py::init([](std::array<std::array<double, 2>, 2> a) {
SquareMatrix2 matrix;
matrix << a[0][0], a[0][1], a[1][0], a[1][1];
return matrix;
}))
.def_static("Zero",
[]() -> SquareMatrix2 { return SquareMatrix2::Zero(); })
.def_static("Identity",
[]() -> SquareMatrix2 { return SquareMatrix2::Identity(); })
.def("__getitem__",
[](const SquareMatrix2& self, const py::object& idx) {
py::tuple t = idx.cast<py::tuple>();
if (py::len(t) != 2) {
throw py::index_error("SquareMatrix2 index must be (i, j)");
}
return self(t[0].cast<Eigen::Index>(), t[1].cast<Eigen::Index>());
})
.def("__setitem__",
[](SquareMatrix2& self, const py::object& idx, double value) {
py::tuple t = idx.cast<py::tuple>();
if (py::len(t) != 2) {
throw py::index_error("SquareMatrix2 index must be (i, j)");
}
self(t[0].cast<Eigen::Index>(), t[1].cast<Eigen::Index>()) = value;
})
.def("__str__", [](const SquareMatrix2& self) {
std::stringstream ss;
ss << self;
return ss.str();
});
py::class_<Translation3>(m, "Translation3")
.def(py::init([](const Vector3& a) { return Translation3(a); }))
.def(py::init([](std::array<double, 3> a) {
return Translation3(Vector3(a[0], a[1], a[2]));
}))
.def("__getitem__", [](const Translation3& self,
Eigen::Index i) { return self.translation()[i]; })
.def("__str__", [](const Translation3& self) {
std::stringstream ss;
ss << self.translation().transpose();
return ss.str();
});
py::class_<RotationMatrix3>(m, "RotationMatrix3")
.def(py::init([](const Vector3& u, const Vector3& v, const Vector3& w) {
RotationMatrix3 r;
r.col(0) = u;
r.col(1) = v;
r.col(2) = w;
return r;
}))
.def("__getitem__",
[](const RotationMatrix3& self, std::array<Eigen::Index, 2> ij) {
return self.matrix()(ij[0], ij[1]);
})
.def("__str__", [](const RotationMatrix3& self) {
std::stringstream ss;
ss << self.matrix();
return ss.str();
});
py::class_<AngleAxis3>(m, "AngleAxis3")
.def(py::init([](double angle, const Vector3& axis) {
return AngleAxis3(angle, axis);
}))
.def("__str__", [](const AngleAxis3& self) {
std::stringstream ss;
ss << "Angle: " << self.angle()
<< ", Axis: " << self.axis().transpose();
return ss.str();
});
py::class_<Transform3>(m, "Transform3")
.def(py::init([](const Vector3& translation) -> Transform3 {
return Transform3{Translation3{translation}};
}))
.def(py::init([](const Vector3& translation,
const RotationMatrix3& rotation) -> Transform3 {
Transform3 t;
t.prerotate(rotation);
t.pretranslate(translation);
return t;
}))
.def_property_readonly(
"translation",
[](const Transform3& self) -> Vector3 { return self.translation(); })
.def_property_readonly("rotation",
[](const Transform3& self) -> RotationMatrix3 {
return self.rotation();
})
.def_static("Identity", &Transform3::Identity)
.def("__mul__", [](const Transform3& self,
const Transform3& other) { return self * other; })
.def("__mul__", [](const Transform3& self,
const Translation3& other) { return self * other; })
.def("__mul__", [](const Transform3& self,
const AngleAxis3& other) { return self * other; })
.def("__str__", [](const Transform3& self) {
std::stringstream ss;
ss << self.matrix();
return ss.str();
});
// Add the unit constants
auto u = m.def_submodule("UnitConstants");
#define UNIT(x) u.attr(#x) = UnitConstants::x;
UNIT(fm)
UNIT(pm)
UNIT(um)
UNIT(nm)
UNIT(mm)
UNIT(cm)
UNIT(m)
UNIT(km)
UNIT(mm2)
UNIT(cm2)
UNIT(m2)
UNIT(mm3)
UNIT(cm3)
UNIT(m3)
UNIT(s)
UNIT(fs)
UNIT(ps)
UNIT(ns)
UNIT(us)
UNIT(ms)
UNIT(min)
UNIT(h)
UNIT(mrad)
UNIT(rad)
UNIT(degree)
UNIT(eV)
UNIT(keV)
UNIT(MeV)
UNIT(GeV)
UNIT(TeV)
UNIT(J)
UNIT(u)
UNIT(g)
UNIT(kg)
UNIT(e)
UNIT(T)
UNIT(Gauss)
UNIT(kGauss)
UNIT(mol)
#undef UNIT
// Add the PdgParticle enum
py::enum_<PdgParticle>(m, "PdgParticle")
.value("eInvalid", PdgParticle::eInvalid)
.value("eElectron", PdgParticle::eElectron)
.value("eAntiElectron", PdgParticle::eAntiElectron)
.value("ePositron", PdgParticle::ePositron)
.value("eMuon", PdgParticle::eMuon)
.value("eAntiMuon", PdgParticle::eAntiMuon)
.value("eTau", PdgParticle::eTau)
.value("eAntiTau", PdgParticle::eAntiTau)
.value("eGamma", PdgParticle::eGamma)
.value("ePionZero", PdgParticle::ePionZero)
.value("ePionPlus", PdgParticle::ePionPlus)
.value("ePionMinus", PdgParticle::ePionMinus)
.value("eKaonPlus", PdgParticle::eKaonPlus)
.value("eKaonMinus", PdgParticle::eKaonMinus)
.value("eNeutron", PdgParticle::eNeutron)
.value("eAntiNeutron", PdgParticle::eAntiNeutron)
.value("eProton", PdgParticle::eProton)
.value("eAntiProton", PdgParticle::eAntiProton)
.value("eLead", PdgParticle::eLead)
.value("eJPsi", PdgParticle::eJPsi)
.value("eB0", PdgParticle::eB0)
.value("eBPlus", PdgParticle::eBPlus)
.value("eD0", PdgParticle::eD0)
.value("eDPlus", PdgParticle::eDPlus)
.value("eAntiB0", PdgParticle::eAntiB0)
.value("eAntiD0", PdgParticle::eAntiD0)
.value("eNeutrinoE", PdgParticle::eNeutrinoE)
.value("eNeutrinoMu", PdgParticle::eNeutrinoMu)
.value("eNeutrinoTau", PdgParticle::eNeutrinoTau)
.value("eAntiNeutrinoE", PdgParticle::eAntiNeutrinoE)
.value("eAntiNeutrinoMu", PdgParticle::eAntiNeutrinoMu)
.value("eAntiNeutrinoTau", PdgParticle::eAntiNeutrinoTau);
// Add the parsePdgParticle function
m.def("parsePdgParticle", &parsePdgParticle, py::arg("name"));
py::class_<ParticleHypothesis>(m, "ParticleHypothesis")
.def(py::init([](PdgParticle absPdg, float mass, float absCharge) {
return ParticleHypothesis(absPdg, mass,
ChargeHypothesis{absCharge});
}),
py::arg("pdg"), py::arg("mass"), py::arg("absCharge"))
.def(py::init([](std::underlying_type_t<PdgParticle> absPdg, float mass,
float absCharge) {
return ParticleHypothesis(static_cast<PdgParticle>(absPdg), mass,
ChargeHypothesis{absCharge});
}),
py::arg("absPdg"), py::arg("mass"), py::arg("absCharge"))
.def("__str__",
[](const ParticleHypothesis& particleHypothesis) {
std::stringstream os;
particleHypothesis.toStream(os);
return os.str();
})
.def_property_readonly("absolutePdg", &ParticleHypothesis::absolutePdg)
.def_property_readonly("mass", &ParticleHypothesis::mass)
.def_property_readonly("absoluteCharge",
&ParticleHypothesis::absoluteCharge)
.def_property_readonly_static(
"muon",
[](const py::object& /*self*/) { return ParticleHypothesis::muon(); })
.def_property_readonly_static(
"pion",
[](const py::object& /*self*/) { return ParticleHypothesis::pion(); })
.def_property_readonly_static("electron",
[](const py::object& /*self*/) {
return ParticleHypothesis::electron();
})
.def_property_readonly_static(
"kaon",
[](const py::object& /*self*/) { return ParticleHypothesis::kaon(); })
.def_property_readonly_static("proton",
[](const py::object& /*self*/) {
return ParticleHypothesis::proton();
})
.def_property_readonly_static("geantino",
[](const py::object& /*self*/) {
return ParticleHypothesis::geantino();
})
.def_property_readonly_static(
"chargedGeantino", [](const py::object& /*self*/) {
return ParticleHypothesis::chargedGeantino();
});
}
} // namespace ActsPython