File indexing completed on 2025-06-30 08:06:51
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Common.hpp"
0013 #include "Acts/Utilities/AxisFwd.hpp"
0014 #include "Acts/Utilities/BinUtility.hpp"
0015 #include "Acts/Utilities/BinningType.hpp"
0016
0017 #include <sstream>
0018 #include <stdexcept>
0019 #include <string>
0020 #include <vector>
0021
0022 namespace Acts::Experimental {
0023
0024
0025
0026
0027
0028
0029 struct ProtoBinning {
0030
0031 BinningValue binValue;
0032
0033 Acts::AxisType axisType = Acts::AxisType::Equidistant;
0034
0035 Acts::AxisBoundaryType boundaryType = Acts::AxisBoundaryType::Bound;
0036
0037 std::vector<ActsScalar> edges = {};
0038
0039 std::size_t expansion = 0u;
0040
0041 bool autorange = false;
0042
0043
0044
0045
0046
0047
0048
0049 ProtoBinning(BinningValue bValue, Acts::AxisBoundaryType bType,
0050 const std::vector<ActsScalar>& e, std::size_t exp = 0u)
0051 : binValue(bValue),
0052 axisType(Acts::AxisType::Variable),
0053 boundaryType(bType),
0054 edges(e),
0055 expansion(exp) {
0056 if (edges.size() < 2u) {
0057 throw std::invalid_argument(
0058 "ProtoBinning: Invalid binning, at least two edges are needed.");
0059 }
0060 }
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 ProtoBinning(BinningValue bValue, Acts::AxisBoundaryType bType,
0071 ActsScalar minE, ActsScalar maxE, std::size_t nbins,
0072 std::size_t exp = 0u)
0073 : binValue(bValue), boundaryType(bType), expansion(exp) {
0074 if (minE >= maxE) {
0075 std::string msg = "ProtoBinning: Invalid binning for value '";
0076 msg += binningValueName(bValue);
0077 msg += "', min edge (" + std::to_string(minE) + ") ";
0078 msg += " needs to be smaller than max edge (";
0079 msg += std::to_string(maxE) + ").";
0080 throw std::invalid_argument(msg);
0081 }
0082 if (nbins < 1u) {
0083 throw std::invalid_argument(
0084 "ProtoBinning: Invalid binning, at least one bin is needed.");
0085 }
0086
0087 ActsScalar stepE = (maxE - minE) / nbins;
0088 edges.reserve(nbins + 1);
0089 for (std::size_t i = 0; i <= nbins; i++) {
0090 edges.push_back(minE + i * stepE);
0091 }
0092 }
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 ProtoBinning(BinningValue bValue, Acts::AxisBoundaryType bType,
0105 std::size_t nbins, std::size_t exp = 0u)
0106 : binValue(bValue),
0107 boundaryType(bType),
0108 edges(nbins + 1, 0.),
0109 expansion(exp),
0110 autorange(true) {}
0111
0112
0113 std::size_t bins() const { return edges.size() - 1u; }
0114
0115
0116 std::string toString() const {
0117 std::stringstream ss;
0118 ss << "ProtoBinning: " << bins() << " bins in "
0119 << binningValueName(binValue);
0120 ss << (axisType == Acts::AxisType::Variable ? ", variable "
0121 : ", equidistant ");
0122 if (!autorange) {
0123 ss << "within [" << edges.front() << ", " << edges.back() << "] ";
0124 } else {
0125 ss << "within automatic range";
0126 }
0127 return ss.str();
0128 }
0129 };
0130
0131
0132 struct BinningDescription {
0133
0134
0135
0136 static BinningDescription fromBinUtility(const BinUtility& binUtility) {
0137 BinningDescription bDesc;
0138 for (const auto& bData : binUtility.binningData()) {
0139
0140 Acts::AxisBoundaryType boundaryType =
0141 bData.option == open ? Acts::AxisBoundaryType::Bound
0142 : Acts::AxisBoundaryType::Closed;
0143 std::vector<ActsScalar> edges;
0144 if (bData.type == equidistant) {
0145 bDesc.binning.push_back(ProtoBinning(bData.binvalue, boundaryType,
0146 bData.min, bData.max, bData.bins(),
0147 0u));
0148
0149 } else {
0150 std::for_each(bData.boundaries().begin(), bData.boundaries().end(),
0151 [&](ActsScalar edge) { edges.push_back(edge); });
0152 bDesc.binning.push_back(
0153 ProtoBinning(bData.binvalue, boundaryType, edges, 0u));
0154 }
0155 }
0156 return bDesc;
0157 }
0158
0159
0160
0161 BinUtility toBinUtility() const {
0162 BinUtility binUtility;
0163 for (const auto& b : binning) {
0164 Acts::BinningOption bOption =
0165 b.boundaryType == Acts::AxisBoundaryType::Bound ? Acts::open
0166 : Acts::closed;
0167 if (b.axisType == Acts::AxisType::Equidistant) {
0168 binUtility += BinUtility(b.bins(), b.edges.front(), b.edges.back(),
0169 bOption, b.binValue);
0170 } else {
0171 std::vector<float> edges;
0172 std::for_each(b.edges.begin(), b.edges.end(),
0173 [&](ActsScalar edge) { edges.push_back(edge); });
0174 binUtility += BinUtility(edges, bOption, b.binValue);
0175 }
0176 }
0177 return binUtility;
0178 }
0179
0180
0181 std::vector<ProtoBinning> binning;
0182
0183
0184 std::string toString() const {
0185 std::stringstream ss;
0186 ss << "BinningDescription: " << binning.size() << "D" << std::endl;
0187 for (const auto& b : binning) {
0188 ss << " " << b.toString() << std::endl;
0189 }
0190 return ss.str();
0191 }
0192 };
0193
0194 }