18#include <fairlogger/Logger.h>
19#include <fmt/format.h>
35float medianOf(std::span<float>
values)
37 const auto middle =
values.begin() +
static_cast<std::ptrdiff_t
>(
values.size() / 2);
39 if (
values.size() % 2 == 1) {
42 return 0.5f * (*std::max_element(
values.begin(), middle) + *middle);
47std::pair<float, float> medianAndWidth(std::span<float>
values, std::span<float> deviations)
50 for (std::size_t
i = 0;
i <
values.size(); ++
i) {
51 deviations[
i] = std::abs(
values[
i] - median);
53 return {
median, 1.4826f * medianOf(deviations.first(
values.size()))};
56std::pair<float, float> medianAndWidth(std::vector<float>&
values)
58 std::vector<float> deviations(
values.size());
59 return medianAndWidth(
values, deviations);
67 mDictionary.mLayers.resize(nLayers);
72 const math_utils::Point3D<float>&
end)
74 const int layer = Dictionary::layerOf(cluster);
75 if (layer < 0 || layer >=
static_cast<int>(mInput.size())) {
76 throw std::runtime_error(fmt::format(
"{}: on no layer of the dictionary's {}", cluster.asString(), mInput.size()));
78 auto& input = mInput[
layer];
79 const auto nBytes =
static_cast<std::size_t
>(2 + ((
pattern[0] *
pattern[1] + 7) / 8));
80 std::string
bytes(
reinterpret_cast<const char*
>(
pattern), nBytes);
81 const auto [
index, inserted] = input.patternIndex.try_emplace(
bytes,
static_cast<uint32_t>(input.patterns.size()));
83 input.patterns.push_back(std::move(
bytes));
85 const auto cog = Dictionary::centreOfGravity(cluster,
pattern);
86 const auto a = Dictionary::localToFlat(cluster,
start);
87 const auto b = Dictionary::localToFlat(cluster,
end);
88 input.crossings.push_back({.pattern =
index->second,
89 .x0 =
a.X() - cog.X(),
91 .z0 =
a.Z() - cog.Z(),
94 .dz =
b.Z() -
a.Z()});
100 const auto& crossings = input.crossings;
101 float low = std::numeric_limits<float>::max(), high = std::numeric_limits<float>::lowest();
102 for (
const auto& crossing : crossings) {
103 low = std::min({low, crossing.y0, crossing.y0 + crossing.dy});
104 high = std::max({high, crossing.y0, crossing.y0 + crossing.dy});
106 const float step = mSettings.depthStep;
107 const int nSteps =
static_cast<int>(std::floor((high - low) / step));
109 throw std::runtime_error(fmt::format(
"layer {}: its hits span {} um of silicon, less than the depth step",
layer, 1e4 * (high - low)));
112 const auto depthAt = [low,
step](
int k) {
return low + ((
static_cast<float>(k) + 0.5f) *
step); };
113 const float first = depthAt(0),
last = depthAt(nSteps - 1);
114 std::vector<uint32_t> scanned;
115 for (std::size_t
i = 0;
i < crossings.size(); ++
i) {
116 const auto& crossing = crossings[
i];
117 if (crossing.dy != 0.f && std::min(crossing.y0, crossing.y0 + crossing.dy) <=
first && std::max(crossing.y0, crossing.y0 + crossing.dy) >= last) {
118 scanned.push_back(
static_cast<uint32_t>(
i));
121 if (scanned.size() < mSettings.minCount) {
122 throw std::runtime_error(fmt::format(
"layer {}: {} crossings through the silicon, fewer than {}",
layer, scanned.size(), mSettings.minCount));
125 std::vector<float> objective(nSteps);
126 const auto scan = [&](
int thread,
int nThreads) {
127 std::vector<float> residualsX(scanned.size()), residualsZ(scanned.size()), deviations(scanned.size());
128 for (
int k = thread; k < nSteps; k += nThreads) {
129 const float depth = depthAt(k);
130 for (std::size_t
i = 0;
i < scanned.size(); ++
i) {
131 const auto& crossing = crossings[scanned[
i]];
132 const float t = (
depth - crossing.y0) / crossing.dy;
133 residualsX[
i] = crossing.x0 + (
t * crossing.dx);
134 residualsZ[
i] = crossing.z0 + (
t * crossing.dz);
136 objective[k] = std::hypot(medianAndWidth(residualsX, deviations).second, medianAndWidth(residualsZ, deviations).second);
139 const int nThreads = std::clamp(mSettings.nThreads, 1, nSteps);
140 std::vector<std::thread> threads;
141 for (
int thread = 1; thread < nThreads; ++thread) {
142 threads.emplace_back(scan, thread, nThreads);
145 for (
auto& thread : threads) {
148 return depthAt(
static_cast<int>(std::ranges::min_element(objective) - objective.begin()));
152void BuildTopologyDictionary<DetID>::makeEntries(
int layer,
const LayerInput& input, LayerDictionary&
data)
const
154 constexpr int NSpanClasses = Dictionary::NSpanClasses;
155 const auto nPatterns = input.patterns.size();
156 const auto spansOf = [&input](
uint32_t pattern) {
return std::pair<int, int>{
static_cast<unsigned char>(input.patterns[
pattern][0]),
static_cast<unsigned char>(input.patterns[
pattern][1])}; };
159 std::vector<std::vector<float>> residualsX(nPatterns), residualsZ(nPatterns);
160 std::vector<float> layerX, layerZ;
161 for (
const auto& crossing : input.crossings) {
162 const float t = crossing.dy != 0.f ? (
data.depth - crossing.y0) / crossing.dy : -1.f;
163 if (!(
t >= 0.f &&
t <= 1.f)) {
166 const float x = crossing.x0 + (
t * crossing.dx),
z = crossing.z0 + (
t * crossing.dz);
167 residualsX[crossing.pattern].push_back(
x);
168 residualsZ[crossing.pattern].push_back(
z);
172 if (layerX.size() < mSettings.minCount) {
173 throw std::runtime_error(fmt::format(
"layer {}: {} residuals at its depth, fewer than {}",
layer, layerX.size(), mSettings.minCount));
177 std::vector<uint32_t> common;
179 if (residualsX[
pattern].
size() >= mSettings.minCount) {
184 return residualsX[
a].size() != residualsX[
b].
size() ? residualsX[
a].
size() > residualsX[
b].
size() : input.patterns[
a] < input.patterns[
b];
186 std::vector<bool> isCommon(nPatterns,
false);
187 for (
const auto pattern : common) {
189 const auto*
bytes =
reinterpret_cast<const unsigned char*
>(input.patterns[
pattern].data());
190 const auto [rowSpan, colSpan] = spansOf(
pattern);
191 DictionaryEntry
entry;
193 float cogRow = 0.f, cogCol = 0.f;
195 entry.xCOG = -cogRow;
200 data.entries.push_back(std::move(
entry));
204 std::vector<std::vector<float>> groupX(Dictionary::NGroups), groupZ(Dictionary::NGroups), classX(NSpanClasses), classZ(NSpanClasses);
209 const auto [rowSpan, colSpan] = spansOf(
pattern);
210 const int group = Dictionary::groupOf(rowSpan, colSpan);
213 auto& cx = classX[Dictionary::spanClass(rowSpan)];
214 auto& cz = classZ[Dictionary::spanClass(colSpan)];
215 cx.insert(cx.end(), residualsX[
pattern].begin(), residualsX[
pattern].end());
216 cz.insert(cz.end(), residualsZ[
pattern].begin(), residualsZ[
pattern].end());
218 const auto wholeX = medianAndWidth(layerX), wholeZ = medianAndWidth(layerZ);
219 const auto pooled = [
this](std::vector<float>&
group, std::vector<float>& spanClass,
const std::pair<float, float>& whole) {
220 if (
group.size() >= mSettings.minCount) {
221 return medianAndWidth(
group);
223 if (spanClass.size() >= mSettings.minCount) {
224 return medianAndWidth(spanClass);
229 DictionaryEntry
entry;
230 entry.isGroup =
true;
231 std::tie(
entry.xOffset,
entry.xWidth) = pooled(groupX[
group], classX[
group / NSpanClasses], wholeX);
232 std::tie(
entry.zOffset,
entry.zWidth) = pooled(groupZ[
group], classZ[
group % NSpanClasses], wholeZ);
233 data.entries.push_back(std::move(
entry));
236 for (std::size_t
id = 0;
id <
data.entries.size(); ++
id) {
238 if (!(
entry.xWidth > 0.f &&
entry.zWidth > 0.f && std::isfinite(
entry.xWidth) && std::isfinite(
entry.zWidth))) {
239 throw std::runtime_error(fmt::format(
"layer {}: entry {} has widths {} and {} cm",
layer,
id,
entry.xWidth,
entry.zWidth));
247 for (
int layer = 0; layer < static_cast<int>(mInput.size()); ++
layer) {
248 auto& input = mInput[
layer];
249 if (input.crossings.empty()) {
250 throw std::runtime_error(fmt::format(
"layer {} has no clusters",
layer));
256 input = LayerInput{};
258 data.entries.size() - Dictionary::NGroups);
265 TFile
output(fileName.c_str(),
"recreate");
266 output.WriteObjectAny(&mDictionary, Dictionary::Class(),
"ccdb_object");
Definition of the ClusterTopology class.
std::string getName(const TDataMember *dm, int index, int size)
configStandalone Reconstruction settings
Builds the TRK and FT3 topology dictionaries from MC clusters and their particles' crossings.
Static class with identifiers, bitmasks and names for ALICE detectors.
static int getCOG(int rowSpan, int colSpan, const unsigned char patt[MaxPatternBytes], float &xCOG, float &zCOG)
Static: Compute pattern's COG position. Returns the number of fired pixels.
static unsigned long getCompleteHash(int nRow, int nCol, const unsigned char patt[ClusterPattern::MaxPatternBytes])
Compute the complete hash as defined for mHash.
BuildTopologyDictionary(int nLayers, const Settings &settings)
void build()
Builds every layer; throws std::runtime_error for a layer with too few residuals.
typename Dictionary::ClusterType ClusterType
void accountCluster(const ClusterType &cluster, const unsigned char *pattern, const math_utils::Point3D< float > &start, const math_utils::Point3D< float > &end)
A cluster and its particle's crossing of the chip, in O2's local frame [cm].
void saveDictionaryRoot(const std::string &fileName) const
Writes the dictionary as "ccdb_object".
GLboolean GLboolean GLboolean b
GLenum GLsizei GLsizei GLint * values
GLint GLint GLsizei GLsizei GLsizei depth
GLenum GLuint GLint GLint layer
GLboolean GLboolean GLboolean GLboolean a
GLdouble GLdouble GLdouble z
R median(std::vector< T > v)
Enum< T >::Iterator begin(Enum< T >)
std::array< uint16_t, 5 > pattern