11# # This file is a part of SolidStateDetectors.jl, licensed under the MIT License (MIT).
22
3- # breaking changes in Clustering v0.14 -> v0.15
4- @inline _get_clusters(clusters:: Clustering.DbscanResult ) = clusters. clusters
5- @inline _get_clusters(clusters:: Vector{Clustering.DbscanCluster} ) = clusters
6-
73function cluster_detector_hits(
8- detno:: AbstractVector{<:Integer} ,
9- edep:: AbstractVector{TT} ,
10- pos:: AbstractVector{<:StaticVector{3,PT}} ,
11- cluster_radius:: RealQuantity
12- ) where {TT<: RealQuantity , PT <: RealQuantity }
13- Table = TypedTables . Table
14- unsorted = Table(detno = detno, edep = edep, pos = pos)
4+ detno:: AbstractVector{<:Integer} ,
5+ edep:: AbstractVector{TT} ,
6+ pos:: AbstractVector{<:Union{<: StaticVector{3,PT}, <:CartesianPoint{PT} }} ,
7+ cluster_radius:: RealQuantity
8+ ) where {TT <: RealQuantity , PT <: RealQuantity }
9+
10+ unsorted = TypedTables . Table(detno = detno, edep = edep, pos = pos)
1511 sorting_idxs = sortperm(unsorted. detno)
1612 sorted = unsorted[sorting_idxs]
17- grouped = Table(consgroupedview(sorted. detno, TypedTables. columns(sorted)))
13+ grouped = TypedTables . Table(consgroupedview(sorted. detno, TypedTables. columns(sorted)))
1814
1915 r_detno = similar(detno, 0 )
2016 r_edep = similar(edep, 0 )
@@ -24,25 +20,26 @@ function cluster_detector_hits(
2420 ustripped_cradius = ustrip(posunit, cluster_radius)
2521
2622 for d_hits_nt in grouped
27- d_hits = Table(d_hits_nt)
23+ d_hits = TypedTables . Table(d_hits_nt)
2824 d_detno = first(d_hits. detno)
2925 @assert all(isequal(d_detno), d_hits. detno)
3026 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 )
32- for c in _get_clusters(clusters)
27+
28+ clusters = Clustering. dbscan(hcat((ustrip.(getindex.(d_hits. pos,i)) for i in 1 : 3 ). .. )' ,
29+ ustripped_cradius, leafsize = 20 , min_neighbors = 1 , min_cluster_size = 1 ). clusters
30+ for c in clusters
3331 idxs = vcat(c. boundary_indices, c. core_indices)
3432 @assert length(idxs) == c. size
3533 c_hits = view(d_hits, idxs)
3634
3735 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
36+ esum = sum(c_hits. edep)
37+ push!(r_edep, esum)
38+ if esum ≈ zero(TT)
4239 push!(r_pos, barycenter(c_hits. pos))
4340 else
44- weights = ustrip.(c_hits. edep) .* inv(esum)
45- push!(r_pos, barycenter(c_hits. pos, weights))
41+ weights = ustrip.(Unitful . NoUnits, c_hits. edep .* inv(esum) )
42+ push!(r_pos, barycenter(c_hits. pos, StatsBase . Weights( weights) ))
4643 end
4744 end
4845 else
0 commit comments