From eaee2f85b3588e2487c8ef69a646cde84700255d Mon Sep 17 00:00:00 2001 From: Alvaro Ezquerro Date: Fri, 3 Oct 2025 10:47:39 +0200 Subject: [PATCH 1/2] add option to fix the first and last node in kMeansClustering --- source/framework/core/inc/TRestVolumeHits.h | 2 +- source/framework/core/src/TRestVolumeHits.cxx | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/source/framework/core/inc/TRestVolumeHits.h b/source/framework/core/inc/TRestVolumeHits.h index 39281f125..fb733334c 100644 --- a/source/framework/core/inc/TRestVolumeHits.h +++ b/source/framework/core/inc/TRestVolumeHits.h @@ -69,7 +69,7 @@ class TRestVolumeHits : public TRestHits { return TMath::Sqrt(fSigmaX[n] * fSigmaX[n] + fSigmaY[n] * fSigmaY[n]); } - static void kMeansClustering(TRestVolumeHits* hits, TRestVolumeHits& vHits, int maxIt = 100); + static void kMeansClustering(TRestVolumeHits* hits, TRestVolumeHits& vHits, int maxIt = 100, bool fixBoundaries = false); // Constructor & Destructor TRestVolumeHits(); diff --git a/source/framework/core/src/TRestVolumeHits.cxx b/source/framework/core/src/TRestVolumeHits.cxx index 331d6b5d4..1223498f9 100644 --- a/source/framework/core/src/TRestVolumeHits.cxx +++ b/source/framework/core/src/TRestVolumeHits.cxx @@ -162,13 +162,13 @@ void TRestVolumeHits::PrintHits() const { } } -void TRestVolumeHits::kMeansClustering(TRestVolumeHits* hits, TRestVolumeHits& vHits, int maxIt) { +void TRestVolumeHits::kMeansClustering(TRestVolumeHits* hits, TRestVolumeHits& vHits, int maxIt, bool fixBoundaries) { const int nodes = vHits.GetNumberOfHits(); vector volHits(nodes); // std::cout<<"Nhits "<GetNumberOfHits()<<" Nodes "< centroid(nodes); - std::vector centroidOld(nodes, nullVector); + std::vector centroidOld(nodes, nullVector); // used for iterations for (int h = 0; h < nodes; h++) centroid[h] = vHits.GetPosition(h); @@ -178,6 +178,7 @@ void TRestVolumeHits::kMeansClustering(TRestVolumeHits* hits, TRestVolumeHits& v double minDist = 1E9; int clIndex = -1; for (int n = 0; n < nodes; n++) { + if (fixBoundaries && (n == 0 || n == nodes - 1)) continue; // Skip fixed nodes TVector3 hitPos = hits->GetPosition(i); double dist = (centroid[n] - hitPos).Mag(); if (dist < minDist) { @@ -188,8 +189,11 @@ void TRestVolumeHits::kMeansClustering(TRestVolumeHits* hits, TRestVolumeHits& v // cout< 0) - vHits.AddHit(volHits[n].GetMeanPosition(), volHits[n].GetTotalEnergy(), 0, volHits[n].GetType(0), - sigma); + if (fixBoundaries && (n == 0 || n == nodes - 1)) { + vHits.AddHit(centroid[n], 0, 0, vHits.GetType(n), sigma); + } else { + if (volHits[n].GetNumberOfHits() > 0) + vHits.AddHit(volHits[n].GetMeanPosition(), volHits[n].GetTotalEnergy(), 0, volHits[n].GetType(0), + sigma); + } } } From 4b1b39f7ff56d097f7ff738808bd7a852d5e3913 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Oct 2025 08:53:27 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- source/framework/core/inc/TRestVolumeHits.h | 3 ++- source/framework/core/src/TRestVolumeHits.cxx | 13 +++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/source/framework/core/inc/TRestVolumeHits.h b/source/framework/core/inc/TRestVolumeHits.h index fb733334c..94f0f442b 100644 --- a/source/framework/core/inc/TRestVolumeHits.h +++ b/source/framework/core/inc/TRestVolumeHits.h @@ -69,7 +69,8 @@ class TRestVolumeHits : public TRestHits { return TMath::Sqrt(fSigmaX[n] * fSigmaX[n] + fSigmaY[n] * fSigmaY[n]); } - static void kMeansClustering(TRestVolumeHits* hits, TRestVolumeHits& vHits, int maxIt = 100, bool fixBoundaries = false); + static void kMeansClustering(TRestVolumeHits* hits, TRestVolumeHits& vHits, int maxIt = 100, + bool fixBoundaries = false); // Constructor & Destructor TRestVolumeHits(); diff --git a/source/framework/core/src/TRestVolumeHits.cxx b/source/framework/core/src/TRestVolumeHits.cxx index 1223498f9..7f0c1c93d 100644 --- a/source/framework/core/src/TRestVolumeHits.cxx +++ b/source/framework/core/src/TRestVolumeHits.cxx @@ -162,13 +162,14 @@ void TRestVolumeHits::PrintHits() const { } } -void TRestVolumeHits::kMeansClustering(TRestVolumeHits* hits, TRestVolumeHits& vHits, int maxIt, bool fixBoundaries) { +void TRestVolumeHits::kMeansClustering(TRestVolumeHits* hits, TRestVolumeHits& vHits, int maxIt, + bool fixBoundaries) { const int nodes = vHits.GetNumberOfHits(); vector volHits(nodes); // std::cout<<"Nhits "<GetNumberOfHits()<<" Nodes "< centroid(nodes); - std::vector centroidOld(nodes, nullVector); // used for iterations + std::vector centroidOld(nodes, nullVector); // used for iterations for (int h = 0; h < nodes; h++) centroid[h] = vHits.GetPosition(h); @@ -178,7 +179,7 @@ void TRestVolumeHits::kMeansClustering(TRestVolumeHits* hits, TRestVolumeHits& v double minDist = 1E9; int clIndex = -1; for (int n = 0; n < nodes; n++) { - if (fixBoundaries && (n == 0 || n == nodes - 1)) continue; // Skip fixed nodes + if (fixBoundaries && (n == 0 || n == nodes - 1)) continue; // Skip fixed nodes TVector3 hitPos = hits->GetPosition(i); double dist = (centroid[n] - hitPos).Mag(); if (dist < minDist) { @@ -193,7 +194,7 @@ void TRestVolumeHits::kMeansClustering(TRestVolumeHits* hits, TRestVolumeHits& v // Update centroids and check for convergence bool converge = true; for (int n = 0; n < nodes; n++) { - if ( fixBoundaries && (n == 0 || n == nodes - 1) ) continue; // Skip fixed nodes + if (fixBoundaries && (n == 0 || n == nodes - 1)) continue; // Skip fixed nodes centroid[n] = volHits[n].GetMeanPosition(); converge &= (centroid[n] == centroidOld[n]); centroidOld[n] = centroid[n]; @@ -210,8 +211,8 @@ void TRestVolumeHits::kMeansClustering(TRestVolumeHits* hits, TRestVolumeHits& v vHits.AddHit(centroid[n], 0, 0, vHits.GetType(n), sigma); } else { if (volHits[n].GetNumberOfHits() > 0) - vHits.AddHit(volHits[n].GetMeanPosition(), volHits[n].GetTotalEnergy(), 0, volHits[n].GetType(0), - sigma); + vHits.AddHit(volHits[n].GetMeanPosition(), volHits[n].GetTotalEnergy(), 0, + volHits[n].GetType(0), sigma); } } }