forked from sPHENIX-Collaboration/acts
/
MaterialMapUtils.cpp
198 lines (177 loc) · 7.37 KB
/
MaterialMapUtils.cpp
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
// This file is part of the Acts project.
//
// Copyright (C) 2019-2020 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 http://mozilla.org/MPL/2.0/.
#include "Acts/Material/MaterialMapUtils.hpp"
#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/detail/Axis.hpp"
#include "Acts/Utilities/detail/Grid.hpp"
#include <iostream>
#include <limits>
using Acts::VectorHelpers::perp;
using Acts::VectorHelpers::phi;
auto Acts::materialMapperRZ(
const std::function<size_t(std::array<size_t, 2> binsRZ,
std::array<size_t, 2> nBinsRZ)>&
materialVectorToGridMapper,
std::vector<double> rPos, std::vector<double> zPos,
std::vector<Acts::Material> material, double lengthUnit)
-> MaterialMapper<detail::Grid<ActsVectorF<5>, detail::EquidistantAxis,
detail::EquidistantAxis>> {
// [1] Decompose material
std::vector<ActsVectorF<5>> materialVector;
materialVector.reserve(material.size());
for (Material& mat : material) {
materialVector.push_back(mat.parameters());
}
// [2] Create Grid
// sort the values
std::sort(rPos.begin(), rPos.end());
std::sort(zPos.begin(), zPos.end());
// Get unique values
rPos.erase(std::unique(rPos.begin(), rPos.end()), rPos.end());
zPos.erase(std::unique(zPos.begin(), zPos.end()), zPos.end());
rPos.shrink_to_fit();
zPos.shrink_to_fit();
// get the number of bins
size_t nBinsR = rPos.size();
size_t nBinsZ = zPos.size();
// get the minimum and maximum
auto minMaxR = std::minmax_element(rPos.begin(), rPos.end());
auto minMaxZ = std::minmax_element(zPos.begin(), zPos.end());
double rMin = *minMaxR.first;
double zMin = *minMaxZ.first;
double rMax = *minMaxR.second;
double zMax = *minMaxZ.second;
// calculate maxima (add one last bin, because bin value always corresponds to
// left boundary)
double stepZ = std::fabs(zMax - zMin) / (nBinsZ - 1);
double stepR = std::fabs(rMax - rMin) / (nBinsR - 1);
rMax += stepR;
zMax += stepZ;
// Create the axis for the grid
detail::EquidistantAxis rAxis(rMin * lengthUnit, rMax * lengthUnit, nBinsR);
detail::EquidistantAxis zAxis(zMin * lengthUnit, zMax * lengthUnit, nBinsZ);
// Create the grid
using Grid_t = detail::Grid<ActsVectorF<5>, detail::EquidistantAxis,
detail::EquidistantAxis>;
Grid_t grid(std::make_tuple(std::move(rAxis), std::move(zAxis)));
// [3] Set the material values
for (size_t i = 1; i <= nBinsR; ++i) {
for (size_t j = 1; j <= nBinsZ; ++j) {
std::array<size_t, 2> nIndices = {{rPos.size(), zPos.size()}};
Grid_t::index_t indices = {{i, j}};
// std::vectors begin with 0 and we do not want the user needing to
// take underflow or overflow bins in account this is why we need to
// subtract by one
grid.atLocalBins(indices) = materialVector.at(
materialVectorToGridMapper({{i - 1, j - 1}}, nIndices));
}
}
ActsVectorF<5> vec;
vec << std::numeric_limits<float>::max(), std::numeric_limits<float>::max(),
0., 0., 0.;
grid.setExteriorBins(vec);
// [4] Create the transformation for the position
// map (x,y,z) -> (r,z)
auto transformPos = [](const Vector3D& pos) {
return Vector2D(perp(pos), pos.z());
};
// [5] Create the mapper & BField Service
// create material mapping
return MaterialMapper<detail::Grid<ActsVectorF<5>, detail::EquidistantAxis,
detail::EquidistantAxis>>(transformPos,
std::move(grid));
}
auto Acts::materialMapperXYZ(
const std::function<size_t(std::array<size_t, 3> binsXYZ,
std::array<size_t, 3> nBinsXYZ)>&
materialVectorToGridMapper,
std::vector<double> xPos, std::vector<double> yPos,
std::vector<double> zPos, std::vector<Material> material, double lengthUnit)
-> MaterialMapper<
detail::Grid<ActsVectorF<5>, detail::EquidistantAxis,
detail::EquidistantAxis, detail::EquidistantAxis>> {
// [1] Decompose material
std::vector<ActsVectorF<5>> materialVector;
materialVector.reserve(material.size());
for (Material& mat : material) {
materialVector.push_back(mat.parameters());
}
// [2] Create Grid
// Sort the values
std::sort(xPos.begin(), xPos.end());
std::sort(yPos.begin(), yPos.end());
std::sort(zPos.begin(), zPos.end());
// Get unique values
xPos.erase(std::unique(xPos.begin(), xPos.end()), xPos.end());
yPos.erase(std::unique(yPos.begin(), yPos.end()), yPos.end());
zPos.erase(std::unique(zPos.begin(), zPos.end()), zPos.end());
xPos.shrink_to_fit();
yPos.shrink_to_fit();
zPos.shrink_to_fit();
// get the number of bins
size_t nBinsX = xPos.size();
size_t nBinsY = yPos.size();
size_t nBinsZ = zPos.size();
// get the minimum and maximum
auto minMaxX = std::minmax_element(xPos.begin(), xPos.end());
auto minMaxY = std::minmax_element(yPos.begin(), yPos.end());
auto minMaxZ = std::minmax_element(zPos.begin(), zPos.end());
// Create the axis for the grid
// get minima
double xMin = *minMaxX.first;
double yMin = *minMaxY.first;
double zMin = *minMaxZ.first;
// get maxima
double xMax = *minMaxX.second;
double yMax = *minMaxY.second;
double zMax = *minMaxZ.second;
// calculate maxima (add one last bin, because bin value always corresponds to
// left boundary)
double stepZ = std::fabs(zMax - zMin) / (nBinsZ - 1);
double stepY = std::fabs(yMax - yMin) / (nBinsY - 1);
double stepX = std::fabs(xMax - xMin) / (nBinsX - 1);
xMax += stepX;
yMax += stepY;
zMax += stepZ;
detail::EquidistantAxis xAxis(xMin * lengthUnit, xMax * lengthUnit, nBinsX);
detail::EquidistantAxis yAxis(yMin * lengthUnit, yMax * lengthUnit, nBinsY);
detail::EquidistantAxis zAxis(zMin * lengthUnit, zMax * lengthUnit, nBinsZ);
// Create the grid
using Grid_t = detail::Grid<ActsVectorF<5>, detail::EquidistantAxis,
detail::EquidistantAxis, detail::EquidistantAxis>;
Grid_t grid(
std::make_tuple(std::move(xAxis), std::move(yAxis), std::move(zAxis)));
// [3] Set the bField values
for (size_t i = 1; i <= nBinsX; ++i) {
for (size_t j = 1; j <= nBinsY; ++j) {
for (size_t k = 1; k <= nBinsZ; ++k) {
Grid_t::index_t indices = {{i, j, k}};
std::array<size_t, 3> nIndices = {
{xPos.size(), yPos.size(), zPos.size()}};
// std::vectors begin with 0 and we do not want the user needing to
// take underflow or overflow bins in account this is why we need to
// subtract by one
grid.atLocalBins(indices) = materialVector.at(
materialVectorToGridMapper({{i - 1, j - 1, k - 1}}, nIndices));
}
}
}
ActsVectorF<5> vec;
vec << std::numeric_limits<float>::max(), std::numeric_limits<float>::max(),
0., 0., 0.;
grid.setExteriorBins(vec);
// [4] Create the transformation for the position
// map (x,y,z) -> (r,z)
auto transformPos = [](const Vector3D& pos) { return pos; };
// [5] Create the mapper & BField Service
// create material mapping
return MaterialMapper<
detail::Grid<ActsVectorF<5>, detail::EquidistantAxis,
detail::EquidistantAxis, detail::EquidistantAxis>>(
transformPos, std::move(grid));
}