Skip to content

Commit 9689c70

Browse files
committed
Add test for cluster_detector_hits
1 parent 4f2e3e4 commit 9689c70

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/ChargeClustering/ChargeClustering.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
function cluster_detector_hits(
88
detno::AbstractVector{<:Integer},
99
edep::AbstractVector{TT},
10-
pos::AbstractVector{<:StaticVector{3,PT}},
10+
pos::AbstractVector{<:Union{<:StaticVector{PT}, <:CartesianPoint{PT}}},
1111
cluster_radius::RealQuantity
1212
) where {TT<:RealQuantity, PT <: RealQuantity}
13-
Table = TypedTables.Table
14-
unsorted = Table(detno = detno, edep = edep, pos = pos)
13+
14+
unsorted = TypedTables.Table(detno = detno, edep = edep, pos = pos)
1515
sorting_idxs = sortperm(unsorted.detno)
1616
sorted = unsorted[sorting_idxs]
17-
grouped = Table(consgroupedview(sorted.detno, TypedTables.columns(sorted)))
17+
grouped = TypedTables.Table(consgroupedview(sorted.detno, TypedTables.columns(sorted)))
1818

1919
r_detno = similar(detno, 0)
2020
r_edep = similar(edep, 0)
@@ -24,25 +24,25 @@ function cluster_detector_hits(
2424
ustripped_cradius = ustrip(posunit, cluster_radius)
2525

2626
for d_hits_nt in grouped
27-
d_hits = Table(d_hits_nt)
27+
d_hits = TypedTables.Table(d_hits_nt)
2828
d_detno = first(d_hits.detno)
2929
@assert all(isequal(d_detno), d_hits.detno)
3030
if length(d_hits) > 3
31-
clusters = Clustering.dbscan(ustrip.(flatview(d_hits.pos)), ustripped_cradius, leafsize = 20, min_neighbors = 1, min_cluster_size = 1)
31+
32+
clusters = Clustering.dbscan(hcat((ustrip.(getindex.(d_hits.pos,i)) for i in 1:3)...)', ustripped_cradius, leafsize = 20, min_neighbors = 1, min_cluster_size = 1)
3233
for c in _get_clusters(clusters)
3334
idxs = vcat(c.boundary_indices, c.core_indices)
3435
@assert length(idxs) == c.size
3536
c_hits = view(d_hits, idxs)
3637

3738
push!(r_detno, d_detno)
38-
esum_u = sum(c_hits.edep)
39-
push!(r_edep, esum_u)
40-
esum = ustrip(esum_u)
41-
if esum 0
39+
esum = sum(c_hits.edep)
40+
push!(r_edep, esum)
41+
if esum zero(TT)
4242
push!(r_pos, barycenter(c_hits.pos))
4343
else
44-
weights = ustrip.(c_hits.edep) .* inv(esum)
45-
push!(r_pos, barycenter(c_hits.pos, weights))
44+
weights = ustrip.(Unitful.NoUnits, c_hits.edep .* inv(esum))
45+
push!(r_pos, barycenter(c_hits.pos, StatsBase.Weights(weights)))
4646
end
4747
end
4848
else

test/test_geant4.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Test
44

55
using SolidStateDetectors
6+
using ArraysOfArrays
67
using RadiationDetectorSignals
78
using TypedTables
89
using Unitful
@@ -66,6 +67,11 @@ end
6667
@test evts isa Table
6768
@test length(evts) == 100
6869

70+
# Cluster events by radius
71+
clustered_evts = @test_nowarn SolidStateDetectors.cluster_detector_hits(evts, 10u"µm")
72+
@test length(clustered_evts) == length(evts)
73+
@test length(flatview(clustered_evts.pos)) <= length(flatview(evts.pos))
74+
6975
# Generate waveforms
7076
simulate!(sim, refinement_limits = [0.2,0.1,0.05,0.03,0.02])
7177
wf = simulate_waveforms(evts, sim, Δt = 1u"ns", max_nsteps = 2000)

0 commit comments

Comments
 (0)