File indexing completed on 2026-12-05 10:01:50
0001
0002
0003
0004
0005
0006
0007
0008 #include <DD4hep/Handle.h>
0009 #include <DD4hep/Readout.h>
0010 #include <Evaluator/DD4hepUnits.h>
0011 #include <algorithms/service.h>
0012 #include <edm4hep/Vector2f.h>
0013 #include <edm4hep/Vector3f.h>
0014 #include <edm4hep/utils/vector_utils.h>
0015 #include <fmt/ranges.h>
0016 #include <cmath>
0017 #include <format>
0018 #include <iterator>
0019 #include <map>
0020 #include <ranges>
0021 #include <set>
0022 #include <stdexcept>
0023 #include <string>
0024 #include <tuple>
0025 #include <unordered_map>
0026 #include <utility>
0027 #include <variant>
0028 #include <vector>
0029
0030 #include "CalorimeterIslandCluster.h"
0031 #include "algorithms/calorimetry/CalorimeterIslandClusterConfig.h"
0032 #include "algorithms/interfaces/detail/multilambda.h"
0033 #include "services/evaluator/EvaluatorSvc.h"
0034
0035 using namespace edm4eic;
0036
0037 namespace eicrecon {
0038
0039 static double Phi_mpi_pi(double phi) { return std::remainder(phi, 2 * M_PI); }
0040
0041 static edm4hep::Vector2f localDistXY(const CaloHit& h1, const CaloHit& h2) {
0042 const auto delta = h1.getLocal() - h2.getLocal();
0043 return {delta.x, delta.y};
0044 }
0045 static edm4hep::Vector2f localDistXZ(const CaloHit& h1, const CaloHit& h2) {
0046 const auto delta = h1.getLocal() - h2.getLocal();
0047 return {delta.x, delta.z};
0048 }
0049 static edm4hep::Vector2f localDistYZ(const CaloHit& h1, const CaloHit& h2) {
0050 const auto delta = h1.getLocal() - h2.getLocal();
0051 return {delta.y, delta.z};
0052 }
0053 static edm4hep::Vector2f dimScaledLocalDistXY(const CaloHit& h1, const CaloHit& h2) {
0054 const auto delta = h1.getLocal() - h2.getLocal();
0055
0056 const auto dimsum = h1.getDimension() + h2.getDimension();
0057
0058 return {2 * delta.x / dimsum.x, 2 * delta.y / dimsum.y};
0059 }
0060 static edm4hep::Vector2f globalDistRPhi(const CaloHit& h1, const CaloHit& h2) {
0061 using vector_type = decltype(edm4hep::Vector2f::a);
0062 return {static_cast<vector_type>(edm4hep::utils::magnitude(h1.getPosition()) -
0063 edm4hep::utils::magnitude(h2.getPosition())),
0064 static_cast<vector_type>(Phi_mpi_pi(edm4hep::utils::angleAzimuthal(h1.getPosition()) -
0065 edm4hep::utils::angleAzimuthal(h2.getPosition())))};
0066 }
0067 static edm4hep::Vector2f globalDistEtaPhi(const CaloHit& h1, const CaloHit& h2) {
0068 using vector_type = decltype(edm4hep::Vector2f::a);
0069 return {static_cast<vector_type>(edm4hep::utils::eta(h1.getPosition()) -
0070 edm4hep::utils::eta(h2.getPosition())),
0071 static_cast<vector_type>(Phi_mpi_pi(edm4hep::utils::angleAzimuthal(h1.getPosition()) -
0072 edm4hep::utils::angleAzimuthal(h2.getPosition())))};
0073 }
0074
0075
0076
0077
0078 void CalorimeterIslandCluster::init() {
0079
0080 multilambda _toDouble = {
0081 [](const std::string& v) { return dd4hep::_toDouble(v); },
0082 [](const double& v) { return v; },
0083 };
0084
0085 if (m_cfg.localDistXY.size() == 2) {
0086 m_localDistXY.push_back(std::visit(_toDouble, m_cfg.localDistXY[0]));
0087 m_localDistXY.push_back(std::visit(_toDouble, m_cfg.localDistXY[1]));
0088 }
0089
0090 static std::map<std::string,
0091 std::tuple<std::function<edm4hep::Vector2f(const CaloHit&, const CaloHit&)>,
0092 std::vector<double>>>
0093 distMethods{{"localDistXY", {localDistXY, {dd4hep::mm, dd4hep::mm}}},
0094 {"localDistXZ", {localDistXZ, {dd4hep::mm, dd4hep::mm}}},
0095 {"localDistYZ", {localDistYZ, {dd4hep::mm, dd4hep::mm}}},
0096 {"dimScaledLocalDistXY", {dimScaledLocalDistXY, {1., 1.}}},
0097 {"globalDistRPhi", {globalDistRPhi, {dd4hep::mm, dd4hep::rad}}},
0098 {"globalDistEtaPhi", {globalDistEtaPhi, {1., dd4hep::rad}}}};
0099
0100
0101 auto set_dist_method = [this](std::pair<std::string, std::vector<double>> uprop) {
0102 if (uprop.second.empty()) {
0103 return false;
0104 }
0105 auto& [method, units] = distMethods[uprop.first];
0106 if (uprop.second.size() != units.size()) {
0107 warning("Expect {} values from {}, received {}. ignored it.", units.size(), uprop.first,
0108 uprop.second.size());
0109 return false;
0110 }
0111 for (std::size_t i = 0; i < units.size(); ++i) {
0112
0113 neighbourDist[i] = uprop.second[i] / units[i];
0114 }
0115 hitsDist = method;
0116 info("Clustering uses {} with distances <= [{}]", uprop.first, fmt::join(neighbourDist, ","));
0117
0118 return true;
0119 };
0120
0121 std::vector<std::pair<std::string, std::vector<double>>> uprops{
0122 {"localDistXY", m_localDistXY},
0123 {"localDistXZ", m_cfg.localDistXZ},
0124 {"localDistYZ", m_cfg.localDistYZ},
0125 {"globalDistRPhi", m_cfg.globalDistRPhi},
0126 {"globalDistEtaPhi", m_cfg.globalDistEtaPhi},
0127
0128 {"dimScaledLocalDistXY", m_cfg.dimScaledLocalDistXY}};
0129
0130 auto& serviceSvc = algorithms::ServiceSvc::instance();
0131
0132 std::function hit_pair_to_map = [this](const edm4eic::CalorimeterHit& h1,
0133 const edm4eic::CalorimeterHit& h2) {
0134 std::unordered_map<std::string, double> params;
0135 for (const auto& p : m_idSpec.fields()) {
0136 const std::string& name = p.first;
0137 const dd4hep::IDDescriptor::Field* field = p.second;
0138 params.emplace(name + "_1", field->value(h1.getCellID()));
0139 params.emplace(name + "_2", field->value(h2.getCellID()));
0140 trace("{}_1 = {}", name, field->value(h1.getCellID()));
0141 trace("{}_2 = {}", name, field->value(h2.getCellID()));
0142 }
0143 return params;
0144 };
0145
0146 if (m_cfg.readout.empty()) {
0147 if ((!m_cfg.adjacencyMatrix.empty()) || (!m_cfg.peakNeighbourhoodMatrix.empty())) {
0148 throw std::runtime_error(
0149 "'readout' is not provided, it is needed to know the fields in readout ids");
0150 }
0151 } else {
0152 m_idSpec = m_detector->readout(m_cfg.readout).idSpec();
0153 }
0154
0155 bool method_found = false;
0156
0157
0158 if (!m_cfg.adjacencyMatrix.empty()) {
0159 is_neighbour = serviceSvc.service<EvaluatorSvc>("EvaluatorSvc")
0160 ->compile(m_cfg.adjacencyMatrix, hit_pair_to_map);
0161 method_found = true;
0162 }
0163
0164
0165 if (not method_found) {
0166 for (auto& uprop : uprops) {
0167 if (set_dist_method(uprop)) {
0168 method_found = true;
0169
0170 is_neighbour = [this](const CaloHit& h1, const CaloHit& h2) {
0171
0172 if (h1.getSector() == h2.getSector()) {
0173 auto dist = hitsDist(h1, h2);
0174 return (std::abs(dist.a) <= neighbourDist[0]) && (std::abs(dist.b) <= neighbourDist[1]);
0175
0176 }
0177
0178
0179
0180 const auto& p1 = h1.getPosition();
0181 const auto& p2 = h2.getPosition();
0182 const double r1 = std::hypot(p1.x, p1.y);
0183 const double r2 = std::hypot(p2.x, p2.y);
0184 const double dphi =
0185 Phi_mpi_pi(edm4hep::utils::angleAzimuthal(p1) - edm4hep::utils::angleAzimuthal(p2));
0186 const double dmax = m_cfg.sectorDist / dd4hep::mm;
0187 return (std::abs(r1 - r2) <= dmax) && (std::abs(0.5 * (r1 + r2) * dphi) <= dmax);
0188 };
0189
0190 break;
0191 }
0192 }
0193 }
0194
0195 if (not method_found) {
0196 throw std::runtime_error("Cannot determine the clustering coordinates");
0197 }
0198
0199 if (m_cfg.splitCluster) {
0200 if (!m_cfg.peakNeighbourhoodMatrix.empty()) {
0201 is_maximum_neighbourhood = serviceSvc.service<EvaluatorSvc>("EvaluatorSvc")
0202 ->compile(m_cfg.peakNeighbourhoodMatrix, hit_pair_to_map);
0203 } else {
0204 is_maximum_neighbourhood = is_neighbour;
0205 }
0206
0207 auto transverseEnergyProfileMetric_it = std::ranges::find_if(
0208 distMethods, [&](auto& p) { return m_cfg.transverseEnergyProfileMetric == p.first; });
0209 if (transverseEnergyProfileMetric_it == distMethods.end()) {
0210 throw std::runtime_error(
0211 std::format(R"(Unsupported value "{}" for "transverseEnergyProfileMetric")",
0212 m_cfg.transverseEnergyProfileMetric));
0213 }
0214 transverseEnergyProfileMetric = std::get<0>(transverseEnergyProfileMetric_it->second);
0215 std::vector<double>& units = std::get<1>(transverseEnergyProfileMetric_it->second);
0216 for (auto unit : units) {
0217 if (unit != units[0]) {
0218 throw std::runtime_error(std::format("Metric {} has incompatible dimension units",
0219 m_cfg.transverseEnergyProfileMetric));
0220 }
0221 }
0222 transverseEnergyProfileScaleUnits = units[0];
0223 }
0224 }
0225
0226 void CalorimeterIslandCluster::process(const CalorimeterIslandCluster::Input& input,
0227 const CalorimeterIslandCluster::Output& output) const {
0228
0229 const auto [hits] = input;
0230 auto [proto_clusters] = output;
0231
0232
0233 std::vector<std::set<std::size_t>> groups;
0234
0235 std::vector<bool> visits(hits->size(), false);
0236 for (std::size_t i = 0; i < hits->size(); ++i) {
0237
0238 {
0239 const auto& hit = (*hits)[i];
0240 debug("hit {:d}: energy = {:.4f} MeV, local = ({:.4f}, {:.4f}) mm, global=({:.4f}, {:.4f}, "
0241 "{:.4f}) mm",
0242 i, hit.getEnergy() * 1000., hit.getLocal().x, hit.getLocal().y, hit.getPosition().x,
0243 hit.getPosition().y, hit.getPosition().z);
0244 }
0245
0246 if (visits[i]) {
0247 continue;
0248 }
0249 groups.emplace_back();
0250
0251 bfs_group(*hits, groups.back(), i, visits);
0252 }
0253
0254 for (auto& group : groups) {
0255 if (group.empty()) {
0256 continue;
0257 }
0258 auto maxima = find_maxima(*hits, group, !m_cfg.splitCluster);
0259 split_group(*hits, group, maxima, proto_clusters);
0260
0261 debug("hits in a group: {}, local maxima: {}", group.size(), maxima.size());
0262 }
0263 }
0264
0265
0266 void CalorimeterIslandCluster::bfs_group(const edm4eic::CalorimeterHitCollection& hits,
0267 std::set<std::size_t>& group, std::size_t idx,
0268 std::vector<bool>& visits) const {
0269 visits[idx] = true;
0270
0271
0272 if (hits[idx].getEnergy() < m_cfg.minClusterHitEdep) {
0273 return;
0274 }
0275
0276 group.insert(idx);
0277 std::size_t prev_size = 0;
0278
0279 while (prev_size != group.size()) {
0280 prev_size = group.size();
0281 for (std::size_t idx1 : group) {
0282
0283 for (std::size_t idx2 = 0; idx2 < hits.size(); ++idx2) {
0284
0285 if (hits[idx2].getEnergy() < m_cfg.minClusterHitEdep) {
0286 continue;
0287 }
0288 if ((!visits[idx2]) && is_neighbour(hits[idx1], hits[idx2])) {
0289 group.insert(idx2);
0290 visits[idx2] = true;
0291 }
0292 }
0293 }
0294 }
0295 }
0296
0297
0298 std::vector<std::size_t>
0299 CalorimeterIslandCluster::find_maxima(const edm4eic::CalorimeterHitCollection& hits,
0300 const std::set<std::size_t>& group, bool global) const {
0301 std::vector<std::size_t> maxima;
0302 if (group.empty()) {
0303 return maxima;
0304 }
0305
0306 if (global) {
0307 std::size_t mpos = *group.begin();
0308 for (auto idx : group) {
0309 if (hits[mpos].getEnergy() < hits[idx].getEnergy()) {
0310 mpos = idx;
0311 }
0312 }
0313 if (hits[mpos].getEnergy() >= m_cfg.minClusterCenterEdep) {
0314 maxima.push_back(mpos);
0315 }
0316 return maxima;
0317 }
0318
0319 for (std::size_t idx1 : group) {
0320
0321 if (hits[idx1].getEnergy() < m_cfg.minClusterCenterEdep) {
0322 continue;
0323 }
0324
0325 bool maximum = true;
0326 for (std::size_t idx2 : group) {
0327 if (idx1 == idx2) {
0328 continue;
0329 }
0330
0331 if (is_maximum_neighbourhood(hits[idx1], hits[idx2]) &&
0332 (hits[idx2].getEnergy() > hits[idx1].getEnergy())) {
0333 maximum = false;
0334 break;
0335 }
0336 }
0337
0338 if (maximum) {
0339 maxima.push_back(idx1);
0340 }
0341 }
0342
0343 return maxima;
0344 }
0345
0346
0347
0348 void CalorimeterIslandCluster::split_group(const edm4eic::CalorimeterHitCollection& hits,
0349 std::set<std::size_t>& group,
0350 const std::vector<std::size_t>& maxima,
0351 edm4eic::ProtoClusterCollection* protoClusters) const {
0352
0353 if (maxima.empty()) {
0354 debug("No maxima found, not building any clusters");
0355 return;
0356 } else if (maxima.size() == 1) {
0357 edm4eic::MutableProtoCluster pcl = protoClusters->create();
0358 for (std::size_t idx : group) {
0359 pcl.addToHits(hits[idx]);
0360 pcl.addToWeights(1.);
0361 }
0362
0363 debug("A single maximum found, added one ProtoCluster");
0364
0365 return;
0366 }
0367
0368
0369
0370 std::vector<double> weights(maxima.size(), 1.);
0371 std::vector<edm4eic::MutableProtoCluster> pcls;
0372 for (std::size_t k = 0; k < maxima.size(); ++k) {
0373 pcls.push_back(protoClusters->create());
0374 }
0375
0376 for (std::size_t idx : group) {
0377 std::size_t j = 0;
0378
0379 for (std::size_t cidx : maxima) {
0380 double energy = hits[cidx].getEnergy();
0381 double dist = edm4hep::utils::magnitude(transverseEnergyProfileMetric(hits[cidx], hits[idx]));
0382 weights[j] =
0383 std::exp(-dist * transverseEnergyProfileScaleUnits / m_cfg.transverseEnergyProfileScale) *
0384 energy;
0385 j += 1;
0386 }
0387
0388
0389 vec_normalize(weights);
0390
0391
0392 for (auto& w : weights) {
0393 if (w < 0.02) {
0394 w = 0;
0395 }
0396 }
0397 vec_normalize(weights);
0398
0399
0400 for (std::size_t k = 0; k < maxima.size(); ++k) {
0401 double weight = weights[k];
0402 if (weight <= 1e-6) {
0403 continue;
0404 }
0405 pcls[k].addToHits(hits[idx]);
0406 pcls[k].addToWeights(weight);
0407 }
0408 }
0409 debug("Multiple ({}) maxima found, added a ProtoClusters for each maximum", maxima.size());
0410 }
0411
0412 }