LDMX Software
StripClusterer.cxx
1#include "Tracking/Digitization/StripClusterer.h"
2
3#include <algorithm>
4#include <cmath>
5#include <deque>
6#include <map>
7#include <set>
8
9namespace tracking::digitization {
10
11StripClusterer::StripClusterer(double seed_threshold, double neighbor_threshold,
12 double cluster_threshold, double noise_sigma_adc,
13 double mean_time_ns, double time_window_ns,
14 double neighbor_delta_t_ns, double max_chi2_ndf)
15 : seed_threshold_(seed_threshold),
16 neighbor_threshold_(neighbor_threshold),
17 cluster_threshold_(cluster_threshold),
18 noise_sigma_adc_(noise_sigma_adc),
19 mean_time_ns_(mean_time_ns),
20 time_window_ns_(time_window_ns),
21 neighbor_delta_t_ns_(neighbor_delta_t_ns),
22 max_chi2_ndf_(max_chi2_ndf) {}
23
24// ---------------------------------------------------------------------------
25
26bool StripClusterer::passesSeedCuts(const ldmx::FittedSiStripHit& h) const {
27 // Timing window (disabled if time_window_ns_ <= 0)
28 if (time_window_ns_ > 0.0) {
29 if (std::abs(h.getT0() - mean_time_ns_) > time_window_ns_) return false;
30 }
31 // Chi2/ndf quality cut (disabled if max_chi2_ndf_ <= 0)
32 if (max_chi2_ndf_ > 0.0 && h.getNDF() > 0) {
33 if (h.getReducedChi2() > max_chi2_ndf_) return false;
34 }
35 return true;
36}
37
38bool StripClusterer::passesNeighborCuts(const ldmx::FittedSiStripHit& h,
39 double cluster_weighted_t,
40 double cluster_total_amp) const {
41 if (neighbor_delta_t_ns_ > 0.0 && cluster_total_amp > 0.0) {
42 const double cluster_t = cluster_weighted_t / cluster_total_amp;
43 if (std::abs(h.getT0() - cluster_t) > neighbor_delta_t_ns_) return false;
44 }
45 return true;
46}
47
48// ---------------------------------------------------------------------------
49
50std::vector<StripClusterer::ClusterCandidate> StripClusterer::findClusters(
51 const std::vector<ldmx::FittedSiStripHit>& hits) const {
52 // -------------------------------------------------------------------------
53 // Build channel → hit map.
54 // If two hits land on the same strip, keep the one with smaller |t0|.
55 // -------------------------------------------------------------------------
56 std::map<int, const ldmx::FittedSiStripHit*> channel_map;
57 for (const auto& h : hits) {
58 const int ch = h.getStripID();
59 auto it = channel_map.find(ch);
60 if (it == channel_map.end()) {
61 channel_map[ch] = &h;
62 } else {
63 // Keep the hit closest to the expected hit time
64 if (std::abs(h.getT0() - mean_time_ns_) <
65 std::abs(it->second->getT0() - mean_time_ns_)) {
66 it->second = &h;
67 }
68 }
69 }
70
71 // -------------------------------------------------------------------------
72 // Determine which strips are clusterable (≥ neighbor threshold) and which
73 // can seed a cluster (≥ seed threshold + timing/chi2 cuts).
74 //
75 // Thresholds are in units of the per-strip noise RMS. Each hit may carry its
76 // own measured noise (getNoise() > 0, from the real-data pedestal table);
77 // when it does not (MC), we fall back to the uniform ctor noise so the MC
78 // path is unchanged.
79 // -------------------------------------------------------------------------
80 std::set<int> clusterable_set;
81 std::vector<int> seed_channels;
82
83 for (const auto& [ch, hp] : channel_map) {
84 const double amp = hp->getAmplitude();
85 const double noise = hitNoise(*hp);
86 if (amp >= neighbor_threshold_ * noise) {
87 clusterable_set.insert(ch);
88 }
89 if (amp >= seed_threshold_ * noise && passesSeedCuts(*hp)) {
90 seed_channels.push_back(ch);
91 }
92 }
93
94 // Sort seeds by amplitude (highest first) so the strongest hit initiates.
95 std::sort(seed_channels.begin(), seed_channels.end(), [&](int a, int b) {
96 return channel_map.at(a)->getAmplitude() >
97 channel_map.at(b)->getAmplitude();
98 });
99
100 // -------------------------------------------------------------------------
101 // BFS expansion from each seed.
102 // -------------------------------------------------------------------------
103 std::vector<ClusterCandidate> clusters;
104
105 for (int seed_ch : seed_channels) {
106 // The seed might have already been claimed by an earlier cluster.
107 if (clusterable_set.find(seed_ch) == clusterable_set.end()) continue;
108
109 ClusterCandidate cand;
110 double cluster_weighted_t = 0.0;
111 double cluster_total_amp = 0.0;
112 double cluster_noise_sq = 0.0;
113
114 std::deque<int> unchecked;
115 unchecked.push_back(seed_ch);
116 clusterable_set.erase(seed_ch);
117
118 while (!unchecked.empty()) {
119 const int cur_ch = unchecked.front();
120 unchecked.pop_front();
121
122 const ldmx::FittedSiStripHit& hit = *channel_map.at(cur_ch);
123 const double amp = hit.getAmplitude();
124
125 // Accumulate cluster quantities.
126 const double noise = hitNoise(hit);
127 cand.strip_ids.push_back(cur_ch);
128 cluster_total_amp += amp;
129 cluster_weighted_t += amp * hit.getT0();
130 cluster_noise_sq += noise * noise;
131
132 // Check nearest neighbours (strip ± 1).
133 for (int delta : {-1, +1}) {
134 const int nb_ch = cur_ch + delta;
135 if (clusterable_set.find(nb_ch) == clusterable_set.end()) continue;
136
137 // Timing consistency with cluster so far.
138 if (!passesNeighborCuts(*channel_map.at(nb_ch), cluster_weighted_t,
139 cluster_total_amp)) {
140 continue;
141 }
142
143 unchecked.push_back(nb_ch);
144 clusterable_set.erase(nb_ch);
145 }
146 }
147
148 // Cluster S/N cut.
149 if (cluster_noise_sq <= 0.0) continue;
150 if (cluster_total_amp / std::sqrt(cluster_noise_sq) < cluster_threshold_) {
151 continue;
152 }
153
154 // -----------------------------------------------------------------------
155 // Compute the charge-weighted centroid strip and timing.
156 // -----------------------------------------------------------------------
157 double sum_amp_strip = 0.0;
158 for (int ch : cand.strip_ids) {
159 sum_amp_strip += channel_map.at(ch)->getAmplitude() * ch;
160 }
161
162 cand.centroid_strip = sum_amp_strip / cluster_total_amp;
163 cand.total_amplitude = cluster_total_amp;
164 cand.time_ns = cluster_weighted_t / cluster_total_amp;
165 cand.n_strips = static_cast<int>(cand.strip_ids.size());
166
167 // Charge-weighted RMS around the centroid [strips].
168 // For multi-strip clusters this uses the actual charge-sharing profile,
169 // giving sub-pitch resolution when charge is sharply peaked.
170 // For single-strip clusters the RMS is zero, so we floor at the binary
171 // single-strip uncertainty 1/√12.
172 double sum_amp_dsq = 0.0;
173 for (int ch : cand.strip_ids) {
174 double d = ch - cand.centroid_strip;
175 sum_amp_dsq += channel_map.at(ch)->getAmplitude() * d * d;
176 }
177 constexpr double k_single_strip_sigma = 1.0 / 3.4641; // 1/√12
178 const double rms = std::sqrt(sum_amp_dsq / cluster_total_amp);
179 cand.sigma_strip = (rms > 0.0) ? rms : k_single_strip_sigma;
180 cand.layer_id = channel_map.at(seed_ch)->getLayerID();
181
182 clusters.push_back(std::move(cand));
183 }
184
185 return clusters;
186}
187
188} // namespace tracking::digitization
Result of fitting a pulse shape to the ADC samples of a single readout strip.
float getAmplitude() const
Fitted pedestal-subtracted peak amplitude [ADC counts].
float getT0() const
Fitted hit arrival time [ns] in the sample-window reference frame.
double hitNoise(const ldmx::FittedSiStripHit &h) const
Per-strip noise RMS to use for h: the hit's own measured noise if set (> 0), otherwise the uniform no...
StripClusterer(double seed_threshold=4.0, double neighbor_threshold=3.0, double cluster_threshold=4.0, double noise_sigma_adc=5.0, double mean_time_ns=0.0, double time_window_ns=-1.0, double neighbor_delta_t_ns=-1.0, double max_chi2_ndf=-1.0)
std::vector< ClusterCandidate > findClusters(const std::vector< ldmx::FittedSiStripHit > &hits) const
Cluster a set of fitted strip hits from a single sensor layer.
double centroid_strip
Charge-weighted mean strip index.
double total_amplitude
Total cluster amplitude [ADC counts].
double time_ns
Amplitude-weighted mean hit time [ns].
double sigma_strip
Position uncertainty [strips].