Skip to content

Commit 53a6a87

Browse files
Fixes for pp reconstruction to use typed PseudoJets
It is very important to pass the fully typed PseudoJet{T} around, not a generic PseudoJet
1 parent e097fb3 commit 53a6a87

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

src/PlainAlgo.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,14 @@ function plain_jet_reconstruct(particles::AbstractArray{T, 1}; p::Union{Real, No
230230
# Integer p if possible
231231
p = (round(p) == p) ? Int(p) : p
232232

233-
if T == PseudoJet
233+
if T isa PseudoJet
234234
# recombination_particles will become part of the cluster sequence, so size it for
235235
# the starting particles and all N recombinations
236236
recombination_particles = copy(particles)
237237
sizehint!(recombination_particles, length(particles) * 2)
238238
else
239-
recombination_particles = PseudoJet[]
239+
ParticleType = typeof(px(particles[1]))
240+
recombination_particles = PseudoJet{ParticleType}[]
240241
sizehint!(recombination_particles, length(particles) * 2)
241242
for i in eachindex(particles)
242243
push!(recombination_particles,
@@ -278,23 +279,26 @@ generalised k_t algorithm.
278279
- `clusterseq`: The resulting `ClusterSequence` object representing the
279280
reconstructed jets.
280281
"""
281-
function _plain_jet_reconstruct(; particles::Vector{PseudoJet}, p = -1, R = 1.0,
282+
function _plain_jet_reconstruct(; particles::Vector{PseudoJet{T}}, p = -1, R = 1.0,
282283
algorithm::JetAlgorithm.Algorithm = JetAlgorithm.AntiKt,
283-
recombine = +)
284+
recombine = +) where T <: Real
284285
# Bounds
285286
N::Int = length(particles)
286287
# Parameters
287288
R2 = R^2
288289

290+
# Numerical type?
291+
ParticleType = typeof(particles[1].E)
292+
289293
# Optimised compact arrays for determining the next merge step
290294
# We make sure these arrays are type stable - have seen issues where, depending on the values
291295
# returned by the methods, they can become unstable and performance degrades
292-
kt2_array::Vector{Float64} = pt2.(particles) .^ p
293-
phi_array::Vector{Float64} = phi.(particles)
294-
rapidity_array::Vector{Float64} = rapidity.(particles)
296+
kt2_array::Vector{ParticleType} = pt2.(particles) .^ p
297+
phi_array::Vector{ParticleType} = phi.(particles)
298+
rapidity_array::Vector{ParticleType} = rapidity.(particles)
295299
nn::Vector{Int} = Vector(1:N) # nearest neighbours
296-
nndist::Vector{Float64} = fill(float(R2), N) # geometric distances to the nearest neighbour
297-
nndij::Vector{Float64} = zeros(N) # dij metric distance
300+
nndist::Vector{ParticleType} = fill(float(R2), N) # geometric distances to the nearest neighbour
301+
nndij::Vector{ParticleType} = zeros(N) # dij metric distance
298302

299303
# Maps index from the compact array to the clusterseq jet vector
300304
clusterseq_index::Vector{Int} = collect(1:N)
@@ -304,7 +308,7 @@ function _plain_jet_reconstruct(; particles::Vector{PseudoJet}, p = -1, R = 1.0,
304308
# Current implementation mutates the particles vector, so need to copy it
305309
# for the cluster sequence (there is too much copying happening, so this
306310
# needs to be rethought and reoptimised)
307-
clusterseq = ClusterSequence(algorithm, p, R, RecoStrategy.N2Plain, particles, history,
311+
clusterseq = ClusterSequence{PseudoJet{ParticleType}}(algorithm, p, R, RecoStrategy.N2Plain, particles, history,
308312
Qtot)
309313

310314
# Initialize nearest neighbours

src/Pseudojet.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,16 @@ history index.
6969
A `PseudoJet` object.
7070
"""
7171
PseudoJet(px::T, py::T, pz::T, E::T,
72-
_cluster_hist_index::Int,
73-
pt2::T) where {T <: Real} = PseudoJet{T}(px,
74-
py, pz, E, _cluster_hist_index,
75-
pt2, 1.0 / pt2, _invalid_rap, _invalid_phi)
72+
_cluster_hist_index::Int,
73+
pt2::T) where {T <: Real} = PseudoJet{T}(px,
74+
py, pz, E, _cluster_hist_index,
75+
pt2, 1.0 / pt2, _invalid_rap, _invalid_phi)
76+
77+
PseudoJet{T}(px::T, py::T, pz::T, E::T,
78+
_cluster_hist_index::Int,
79+
pt2::T) where {T <: Real} = PseudoJet{T}(px,
80+
py, pz, E, _cluster_hist_index,
81+
pt2, 1.0 / pt2, _invalid_rap, _invalid_phi)
7682

7783
"""
7884
PseudoJet(px::T, py::T, pz::T, E::T) where T <: Real
@@ -90,6 +96,9 @@ A PseudoJet object.
9096
"""
9197
PseudoJet(px::T, py::T,
9298
pz::T, E::T) where {T <: Real} = PseudoJet(px, py, pz, E, 0, px^2 + py^2)
99+
PseudoJet{T}(px::T, py::T,
100+
pz::T, E::T) where {T <: Real} = PseudoJet{T}(px, py, pz, E, 0, px^2 + py^2)
101+
93102

94103
import Base.show
95104
"""

src/TiledAlgoLL.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -374,17 +374,18 @@ function tiled_jet_reconstruct(particles::AbstractArray{T, 1}; p::Union{Real, No
374374
(p, algorithm) = get_algorithm_power_consistency(p = p, algorithm = algorithm)
375375

376376
# If we have PseudoJets, we can just call the main algorithm...
377-
if T == PseudoJet
377+
if T isa PseudoJet
378378
# recombination_particles will become part of the cluster sequence, so size it for
379379
# the starting particles and all N recombinations
380380
recombination_particles = copy(particles)
381381
sizehint!(recombination_particles, length(particles) * 2)
382382
else
383-
recombination_particles = PseudoJet[]
383+
ParticleType = typeof(px(particles[1]))
384+
recombination_particles = PseudoJet{ParticleType}[]
384385
sizehint!(recombination_particles, length(particles) * 2)
385386
for i in eachindex(particles)
386387
push!(recombination_particles,
387-
PseudoJet(px(particles[i]), py(particles[i]), pz(particles[i]),
388+
PseudoJet{ParticleType}(px(particles[i]), py(particles[i]), pz(particles[i]),
388389
energy(particles[i])))
389390
end
390391
end
@@ -421,9 +422,9 @@ of data types are done.
421422
tiled_jet_reconstruct(particles::Vector{PseudoJet}; p = 1, R = 1.0, recombine = +)
422423
```
423424
"""
424-
function _tiled_jet_reconstruct(particles::Vector{PseudoJet}; p::Real = -1, R = 1.0,
425+
function _tiled_jet_reconstruct(particles::Vector{PseudoJet{T}}; p::Real = -1, R = 1.0,
425426
algorithm::JetAlgorithm.Algorithm = JetAlgorithm.AntiKt,
426-
recombine = +)
427+
recombine = +) where T <: Real
427428
# Bounds
428429
N::Int = length(particles)
429430

@@ -439,13 +440,16 @@ function _tiled_jet_reconstruct(particles::Vector{PseudoJet}; p::Real = -1, R =
439440
R2::Float64 = R * R
440441
p = (round(p) == p) ? Int(p) : p # integer p if possible
441442

443+
# Numerical type?
444+
ParticleType = typeof(particles[1].E)
445+
442446
# This will be used quite deep inside loops, so declare it here so that
443447
# memory (de)allocation gets done only once
444448
tile_union = Vector{Int}(undef, 3 * _n_tile_neighbours)
445449

446450
# Container for pseudojets, sized for all initial particles, plus all of the
447451
# merged jets that can be created during reconstruction
448-
jets = PseudoJet[]
452+
jets = PseudoJet{ParticleType}[]
449453
sizehint!(jets, N * 2)
450454
resize!(jets, N)
451455

@@ -456,15 +460,15 @@ function _tiled_jet_reconstruct(particles::Vector{PseudoJet}; p::Real = -1, R =
456460
history, Qtot = initial_history(jets)
457461

458462
# Now get the tiling setup
459-
_eta = Vector{Float64}(undef, length(particles))
463+
_eta = Vector{ParticleType}(undef, length(particles))
460464
for ijet in 1:length(particles)
461465
_eta[ijet] = rapidity(particles[ijet])
462466
end
463467

464468
tiling = Tiling(setup_tiling(_eta, R))
465469

466470
# ClusterSequence is the struct that holds the state of the reconstruction
467-
clusterseq = ClusterSequence(algorithm, p, R, RecoStrategy.N2Tiled, jets, history, Qtot)
471+
clusterseq = ClusterSequence{PseudoJet{ParticleType}}(algorithm, p, R, RecoStrategy.N2Tiled, jets, history, Qtot)
468472

469473
# Tiled jets is a structure that has additional variables for tracking which tile a jet is in
470474
tiledjets = similar(clusterseq.jets, TiledJet)

0 commit comments

Comments
 (0)