forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUtilities.cpp
More file actions
413 lines (366 loc) · 15.4 KB
/
Utilities.cpp
File metadata and controls
413 lines (366 loc) · 15.4 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
// 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/Utilities/Any.hpp"
#include "Acts/Utilities/AxisDefinitions.hpp"
#include "Acts/Utilities/BinningData.hpp"
#include "Acts/Utilities/CalibrationContext.hpp"
#include "Acts/Utilities/Histogram.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "Acts/Utilities/RangeXD.hpp"
#include <cmath>
#include <random>
#include <sstream>
#include <type_traits>
#include <unordered_map>
#include <boost/histogram.hpp>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
using namespace pybind11::literals;
using namespace Acts;
namespace ActsPython {
namespace bh = boost::histogram;
using namespace Acts::Experimental;
/// Copy inner bin values (no flow) from a boost::histogram into a numpy array
/// in C (row-major) order. ValueFn extracts a double from the indexed element.
template <typename Hist, typename ValueFn>
static py::array_t<double> copyBins(const Hist& h, ValueFn fn) {
const auto n = h.rank();
std::vector<py::ssize_t> shape(n);
for (std::size_t i = 0; i < n; ++i) {
shape[i] = static_cast<py::ssize_t>(h.axis(i).size());
}
py::array_t<double> result(shape);
double* ptr = result.mutable_data();
for (auto&& x : bh::indexed(h, bh::coverage::inner)) {
py::ssize_t flat = 0;
py::ssize_t stride = 1;
for (int i = static_cast<int>(n) - 1; i >= 0; --i) {
flat += static_cast<py::ssize_t>(x.index(i)) * stride;
stride *= shape[i];
}
ptr[flat] = fn(x);
}
return result;
}
template <std::size_t Dim>
static void bindHistogram(py::module_& m) {
using H = Histogram<Dim>;
py::classh<H>(m, ("Histogram" + std::to_string(Dim)).c_str())
.def_property_readonly("name", &H::name)
.def_property_readonly("title", &H::title)
.def_property_readonly("rank", [](const H&) { return Dim; })
.def_property_readonly("histogram", &H::histogram,
py::return_value_policy::reference_internal);
}
template <std::size_t Dim>
static void bindEfficiency(py::module_& m) {
using E = Efficiency<Dim>;
py::classh<E>(m, ("Efficiency" + std::to_string(Dim)).c_str())
.def_property_readonly("name", &E::name)
.def_property_readonly("title", &E::title)
.def_property_readonly("rank", [](const E&) { return Dim; })
.def_property_readonly("accepted", &E::acceptedHistogram,
py::return_value_policy::reference_internal)
.def_property_readonly("total", &E::totalHistogram,
py::return_value_policy::reference_internal);
}
/// @brief This adds the classes from Core/Utilities to the python module
/// @param m the pybind11 core module
void addUtilities(py::module_& m) {
{
py::class_<AnyBase<512>>(m, "AnyBase512").def(py::init<>());
}
{
py::class_<CalibrationContext>(m, "CalibrationContext").def(py::init<>());
}
// Add l ogging infrastructure
{
auto logging = m.def_submodule("logging", "");
auto levelEnum = py::enum_<Logging::Level>(logging, "Level")
.value("VERBOSE", Logging::VERBOSE)
.value("DEBUG", Logging::DEBUG)
.value("INFO", Logging::INFO)
.value("WARNING", Logging::WARNING)
.value("ERROR", Logging::ERROR)
.value("FATAL", Logging::FATAL)
.value("MAX", Logging::MAX)
.export_values();
levelEnum
.def("__lt__", [](Logging::Level self,
Logging::Level other) { return self < other; })
.def("__gt__", [](Logging::Level self,
Logging::Level other) { return self > other; })
.def("__le__", [](Logging::Level self,
Logging::Level other) { return self <= other; })
.def("__ge__", [](Logging::Level self, Logging::Level other) {
return self >= other;
});
{
auto makeLogFunction = [](Logging::Level level) {
return
[level](const Logger& logger, const py::str& fmt, py::args args) {
if (!logger.doPrint(level)) {
return;
}
logger.log(level, fmt.format(*args));
};
};
auto logger =
py::class_<Logger, py::smart_holder>(m, "Logger")
.def("log", &Logger::log)
.def_property_readonly("level", &Logger::level)
.def_property_readonly("name", &Logger::name)
.def("verbose", makeLogFunction(Logging::VERBOSE))
.def("debug", makeLogFunction(Logging::DEBUG))
.def("info", makeLogFunction(Logging::INFO))
.def("warning", makeLogFunction(Logging::WARNING))
.def("error", makeLogFunction(Logging::ERROR))
.def("fatal", makeLogFunction(Logging::FATAL))
.def("clone",
py::overload_cast<const std::optional<std::string>&,
const std::optional<Logging::Level>&>(
&Logger::clone, py::const_),
py::arg("name") = py::none(), py::arg("level") = py::none())
.def(
"clone",
py::overload_cast<Logging::Level>(&Logger::clone, py::const_),
py::arg("level"))
.def("cloneWithSuffix", &Logger::cloneWithSuffix,
py::arg("suffix"), py::arg("level") = py::none());
}
static std::unordered_map<std::string, std::unique_ptr<const Logger>>
loggerCache;
m.def(
"getDefaultLogger",
[](const std::string& name, Logging::Level level) {
return getDefaultLogger(name, level);
},
py::arg("name"), py::arg("level") = Logging::INFO,
py::return_value_policy::take_ownership);
logging.def("setFailureThreshold", &Logging::setFailureThreshold);
logging.def("getFailureThreshold", &Logging::getFailureThreshold);
struct ScopedFailureThresholdContextManager {
std::optional<Logging::ScopedFailureThreshold> m_scopedFailureThreshold =
std::nullopt;
Logging::Level m_level;
explicit ScopedFailureThresholdContextManager(Logging::Level level)
: m_level(level) {}
void enter() { m_scopedFailureThreshold.emplace(m_level); }
void exit(const py::object& /*exc_type*/, const py::object& /*exc_value*/,
const py::object& /*traceback*/) {
m_scopedFailureThreshold.reset();
}
};
py::class_<ScopedFailureThresholdContextManager>(logging,
"ScopedFailureThreshold")
.def(py::init<Logging::Level>(), "level"_a)
.def("__enter__", &ScopedFailureThresholdContextManager::enter)
.def("__exit__", &ScopedFailureThresholdContextManager::exit);
static py::exception<Logging::ThresholdFailure> exc(
logging, "ThresholdFailure", PyExc_RuntimeError);
// NOLINTNEXTLINE(performance-unnecessary-value-param)
py::register_exception_translator([](std::exception_ptr p) {
try {
if (p) {
std::rethrow_exception(p);
}
} catch (const std::exception& e) {
std::string what = e.what();
if (what.find("ACTS_LOG_FAILURE_THRESHOLD") != std::string::npos) {
py::set_error(exc, e.what());
} else {
std::rethrow_exception(p);
}
}
});
logging.def("_consumeLoggerFunction",
[](std::unique_ptr<const Logger> logger) {
logger->log(Logging::VERBOSE, "consumed logger logs");
});
struct ConfigWithLogger {
std::unique_ptr<const Logger> logger;
};
py::class_<ConfigWithLogger>(logging, "_ConfigWithLogger")
.def(py::init<std::unique_ptr<const Logger>>(), "logger"_a)
.def_property_readonly(
"logger", [](ConfigWithLogger& self) { return self.logger.get(); });
}
// Add axis related classes
{
auto binningValue = py::enum_<AxisDirection>(m, "AxisDirection")
.value("AxisX", AxisDirection::AxisX)
.value("AxisY", AxisDirection::AxisY)
.value("AxisZ", AxisDirection::AxisZ)
.value("AxisR", AxisDirection::AxisR)
.value("AxisPhi", AxisDirection::AxisPhi)
.value("AxisRPhi", AxisDirection::AxisRPhi)
.value("AxisTheta", AxisDirection::AxisTheta)
.value("AxisEta", AxisDirection::AxisEta)
.value("AxisMag", AxisDirection::AxisMag);
auto boundaryType = py::enum_<AxisBoundaryType>(m, "AxisBoundaryType")
.value("Bound", AxisBoundaryType::Bound)
.value("Closed", AxisBoundaryType::Closed)
.value("Open", AxisBoundaryType::Open);
auto axisType = py::enum_<AxisType>(m, "AxisType")
.value("equidistant", AxisType::Equidistant)
.value("variable", AxisType::Variable);
}
{
// Be able to construct a proto binning
py::class_<ProtoAxis>(m, "ProtoAxis")
.def(py::init<AxisBoundaryType, const std::vector<double>&>(),
"bType"_a, "e"_a)
.def(py::init<AxisBoundaryType, double, double, std::size_t>(),
"bType"_a, "minE"_a, "maxE"_a, "nbins"_a)
.def(py::init<AxisBoundaryType, std::size_t>(), "bType"_a, "nbins"_a);
py::class_<DirectedProtoAxis>(m, "DirectedProtoAxis")
.def(py::init<AxisDirection, AxisBoundaryType,
const std::vector<double>&>(),
"bValue"_a, "bType"_a, "e"_a)
.def(py::init<AxisDirection, AxisBoundaryType, double, double,
std::size_t>(),
"bValue"_a, "bType"_a, "minE"_a, "maxE"_a, "nbins"_a)
.def(py::init<AxisDirection, AxisBoundaryType, std::size_t>(),
"bValue"_a, "bType"_a, "nbins"_a);
}
{
using RangeXDDim3 = RangeXD<3u, double>;
py::class_<RangeXDDim3>(m, "RangeXDDim3")
.def(py::init([](const std::array<double, 2u>& range0,
const std::array<double, 2u>& range1,
const std::array<double, 2u>& range2) {
RangeXDDim3 range;
range[0].shrink(range0[0], range0[1]);
range[1].shrink(range1[0], range1[1]);
range[2].shrink(range2[0], range2[1]);
return range;
}));
}
// Histogram bindings
{
// Single axis type covering regular, variable, and log axes
py::class_<AxisVariant>(m, "Axis")
.def_static(
"regular",
[](unsigned bins, double low, double high,
const std::string& title) {
return AxisVariant(BoostRegularAxis(bins, low, high, title));
},
"bins"_a, "low"_a, "high"_a, "title"_a = "")
.def_static(
"log",
[](unsigned bins, double low, double high,
const std::string& title) {
return AxisVariant(BoostLogAxis(bins, low, high, title));
},
"bins"_a, "low"_a, "high"_a, "title"_a = "")
.def_static(
"variable",
[](const std::vector<double>& edges, const std::string& title) {
return AxisVariant(BoostVariableAxis(edges, title));
},
"edges"_a, "title"_a = "")
.def_property_readonly("size", &AxisVariant::size)
.def_property_readonly(
"label",
[](const AxisVariant& ax) -> std::string { return ax.metadata(); })
.def_property_readonly(
"edges", [](const AxisVariant& ax) { return extractBinEdges(ax); });
// Core dense histogram (BoostHist — values as copied numpy array)
py::class_<BoostHist>(m, "BoostHistogram")
.def_property_readonly("rank", &BoostHist::rank)
.def(
"axis",
[](const BoostHist& h, std::size_t i) -> AxisVariant {
return h.axis(i);
},
"i"_a)
.def("values", [](const BoostHist& h) {
return copyBins(h, [](auto& x) { return static_cast<double>(*x); });
});
// Profile histogram (BoostProfileHist — means/variances as numpy arrays)
py::class_<BoostProfileHist>(m, "BoostProfileHistogram")
.def_property_readonly("rank", &BoostProfileHist::rank)
.def(
"axis",
[](const BoostProfileHist& h, std::size_t i) -> AxisVariant {
return h.axis(i);
},
"i"_a)
.def("counts",
[](const BoostProfileHist& h) {
return copyBins(h, [](auto& x) {
return static_cast<double>((*x).count());
});
})
.def("means",
[](const BoostProfileHist& h) {
return copyBins(h, [](auto& x) { return (*x).value(); });
})
.def("sum_of_deltas_squared", [](const BoostProfileHist& h) {
return copyBins(h, [](auto& x) {
const auto n = (*x).count();
return n > 1.0 ? (*x).variance() * (n - 1.0) : 0.0;
});
});
bindHistogram<1>(m);
bindHistogram<2>(m);
bindHistogram<3>(m);
{
using P = ProfileHistogram<1>;
py::classh<P>(m, "ProfileHistogram1")
.def_property_readonly("name", &P::name)
.def_property_readonly("title", &P::title)
.def_property_readonly("rank", [](const P&) { return 1u; })
.def_property_readonly("sampleAxisTitle", &P::sampleAxisTitle)
.def_property_readonly("histogram", &P::histogram,
py::return_value_policy::reference_internal);
}
bindEfficiency<1>(m);
bindEfficiency<2>(m);
// Demo factory functions for testing and examples — not public API
m.def("_demo_histogram1", []() -> Histogram1 {
BoostRegularAxis ax(10, 0.0, 10.0, "x [a.u.]");
Histogram1 h("demo", "Demo Histogram", {AxisVariant(ax)});
std::mt19937 rng(42);
std::uniform_real_distribution<double> x(0.0, 10.0);
for (int i = 0; i < 1000; ++i) {
h.fill({x(rng)});
}
return h;
});
m.def("_demo_profile1", []() -> ProfileHistogram1 {
BoostRegularAxis ax(10, 0.0, 10.0, "x [a.u.]");
ProfileHistogram1 h("demo", "Demo Profile", {AxisVariant(ax)},
"y [a.u.]");
std::mt19937 rng(42);
std::uniform_real_distribution<double> x(0.0, 10.0);
std::normal_distribution<double> noise(0.0, 0.1);
for (int i = 0; i < 1000; ++i) {
double xi = x(rng);
h.fill({xi}, std::sin(xi) + noise(rng));
}
return h;
});
m.def("_demo_efficiency1", []() -> Efficiency1 {
BoostRegularAxis ax(10, 0.0, 10.0, "x [a.u.]");
Efficiency1 h("demo", "Demo Efficiency", {AxisVariant(ax)});
std::mt19937 rng(42);
std::uniform_real_distribution<double> x(0.0, 10.0);
for (int i = 0; i < 1000; ++i) {
double xi = x(rng);
std::bernoulli_distribution accepted(1.0 - std::exp(-xi));
h.fill({xi}, accepted(rng));
}
return h;
});
}
}
} // namespace ActsPython