Skip to content
Snippets Groups Projects
Commit 83525f42 authored by Tamas Gal's avatar Tamas Gal :speech_balloon:
Browse files

Merge branch 'support-for-floor-1-bug' into 'main'

Add support for floor == -1 DETX bug

See merge request !19
parents 7566e01e e400a346
No related branches found
No related tags found
1 merge request!19Add support for floor == -1 DETX bug
...@@ -20,7 +20,7 @@ UnROOT = "3cd96dde-e98d-4713-81e9-a4a1b0235ce9" ...@@ -20,7 +20,7 @@ UnROOT = "3cd96dde-e98d-4713-81e9-a4a1b0235ce9"
[compat] [compat]
DocStringExtensions = "0.8, 0.9" DocStringExtensions = "0.8, 0.9"
HDF5 = "^0.16.15, ^0.17" HDF5 = "^0.16.15, ^0.17"
KM3NeTTestData = "^0.4.3" KM3NeTTestData = "^0.4.8"
StaticArrays = "1" StaticArrays = "1"
UnROOT = "^0.10.15" UnROOT = "^0.10.15"
julia = "1" julia = "1"
......
...@@ -361,9 +361,32 @@ function Detector(io::IO) ...@@ -361,9 +361,32 @@ function Detector(io::IO)
locations = Dict{Tuple{Int, Int}, DetectorModule}() locations = Dict{Tuple{Int, Int}, DetectorModule}()
strings = Int8[] strings = Int8[]
# a counter to work around the floor == -1 bug in some older DETX files
floor_counter = 1
last_string = -1
floorminusone_warning_has_been_shown = false
for mod 1:n_modules for mod 1:n_modules
elements = split(lines[idx]) elements = split(lines[idx])
module_id, string, floor = map(x->parse(Int, x), elements[1:3]) module_id, string, floor = map(x->parse(Int, x), elements[1:3])
# floor == -1 bug. We determine the floor position by assuming an ascending order
# of modules in the DETX file
if floor == -1
if !floorminusone_warning_has_been_shown
@warn "'Floor == -1' found in the detector file. The actual floor number will be inferred, assuming that modules and lines are sorted."
floorminusone_warning_has_been_shown = true
end
if last_string == -1
last_string = string
elseif last_string != string
floor_counter = 1
last_string = string
end
floor = floor_counter
floor_counter += 1
end
if !(string in strings) if !(string in strings)
push!(strings, string) push!(strings, string)
end end
...@@ -406,7 +429,8 @@ function Detector(io::IO) ...@@ -406,7 +429,8 @@ function Detector(io::IO)
t₀ = mean([pmt.t₀ for pmt in pmts]) t₀ = mean([pmt.t₀ for pmt in pmts])
end end
if ismissing(t₀) && floor == 0 # If t₀ is still missing, we default to 0.0
if ismissing(t₀)
t₀ = 0.0 t₀ = 0.0
end end
......
using Test using Test
using KM3io using KM3io
using KM3NeTTestData
using Dates using Dates
const SAMPLES_DIR = joinpath(@__DIR__, "samples") const SAMPLES_DIR = joinpath(@__DIR__, "samples")
@testset "io" begin @testset "DETX parsing" begin
@testset "DETX parsing" begin for version 1:5
for version 1:5 d = Detector(joinpath(SAMPLES_DIR, "v$(version).detx"))
d = Detector(joinpath(SAMPLES_DIR, "v$(version).detx"))
@test version == d.version @test version == d.version
mods = DetectorModule[] mods = DetectorModule[]
for mod in d for mod in d
push!(mods, mod) push!(mods, mod)
end end
if version < 4 if version < 4
# no base modules in DETX version <4 # no base modules in DETX version <4
@test 342 == length(d) @test 342 == length(d)
@test 342 == length(d.modules) @test 342 == length(d.modules)
@test 342 == length(modules(d)) @test 342 == length(modules(d))
@test DetectorModule == eltype(d) @test DetectorModule == eltype(d)
else else
@test 361 == length(mods) @test 361 == length(mods)
@test 361 == length(modules(d)) @test 361 == length(modules(d))
@test 106.95 d.modules[808469291].pos.y # base module @test 106.95 d.modules[808469291].pos.y # base module
@test 97.3720395 d.modules[808974928].pos.z # base module @test 97.3720395 d.modules[808974928].pos.z # base module
@test 0.0 == d.modules[808469291].t₀ # base module @test 0.0 == d.modules[808469291].t₀ # base module
end end
@test 116.600 d.modules[808992603].pos.x atol=1e-5 # optical module @test 116.600 d.modules[808992603].pos.x atol=1e-5 # optical module
if version > 3 if version > 3
@test Quaternion(1, 0, 0, 0) d.modules[808995481].q @test Quaternion(1, 0, 0, 0) d.modules[808995481].q
@test 19 == length(collect(m for m d if isbasemodule(m))) @test 19 == length(collect(m for m d if isbasemodule(m)))
@test 19 == length(d[:, 0]) @test 19 == length(d[:, 0])
@test 19 == length(d[30, :]) @test 19 == length(d[30, :])
@test 5 == length(d[30, 0:4]) @test 5 == length(d[30, 0:4])
for m in d[30, 0:4] for m in d[30, 0:4]
@test m.location.floor in 0:4 @test m.location.floor in 0:4
end
else
@test 0 == length(collect(m for m d if isbasemodule(m)))
@test 0 == length(d[:, 0])
@test 18 == length(d[30, :])
@test 4 == length(d[30, 1:4])
for m in d[30, 1:4]
@test m.location.floor in 1:4
end
end end
else
if version > 4 @test 0 == length(collect(m for m d if isbasemodule(m)))
# module status introduced in v5 @test 0 == length(d[:, 0])
@test 0 == d.modules[808966287].status @test 18 == length(d[30, :])
@test 4 == length(d[30, 1:4])
for m in d[30, 1:4]
@test m.location.floor in 1:4
end end
end
if version > 1 if version > 4
@test UTMPosition(587600, 4016800, -3450) d.pos # module status introduced in v5
@test 1654207200.0 == datetime2unix(d.validity.from) @test 0 == d.modules[808966287].status
@test 9999999999.0 == datetime2unix(d.validity.to) end
end
@test 31 == d.modules[808992603].n_pmts if version > 1
@test 30 == d.modules[817287557].location.string @test UTMPosition(587600, 4016800, -3450) d.pos
@test 18 == d.modules[817287557].location.floor @test 1654207200.0 == datetime2unix(d.validity.from)
@test Location(30, 18) == d[817287557].location @test 9999999999.0 == datetime2unix(d.validity.to)
@test 817287557 == d[30, 18].id end
@test 817287557 == getmodule(d, 30, 18).id
@test 817287557 == getmodule(d, (30, 18)).id
@test 817287557 == getmodule(d, Location(30, 18)).id
@test 19 == length(d[:, 18])
for m in d[:, 17]
@test 17 == m.location.floor
end
for m in d[:, 1:4]
@test m.location.floor in 1:4
end
@test 478392.31980645156 d.modules[808992603].t₀ @test 31 == d.modules[808992603].n_pmts
@test 30 == d.modules[817287557].location.string
@test 18 == d.modules[817287557].location.floor
@test Location(30, 18) == d[817287557].location
@test 817287557 == d[30, 18].id
@test 817287557 == getmodule(d, 30, 18).id
@test 817287557 == getmodule(d, (30, 18)).id
@test 817287557 == getmodule(d, Location(30, 18)).id
@test 19 == length(d[:, 18])
for m in d[:, 17]
@test 17 == m.location.floor
end
for m in d[:, 1:4]
@test m.location.floor in 1:4
end
@test 9 == d.strings[1] @test 478392.31980645156 d.modules[808992603].t₀
@test 30 == d.strings[end]
@test 19 == length(d.strings)
@test isapprox([116.60000547853453, 106.95689770873874, 60.463039635848226], d.modules[808992603].pos; atol=0.008) @test 9 == d.strings[1]
@test 30 == d.strings[end]
@test 19 == length(d.strings)
@test 78.3430067946102 getpmt(d[15, 13], 0).pos.x @test isapprox([116.60000547853453, 106.95689770873874, 60.463039635848226], d.modules[808992603].pos; atol=0.008)
m = d[15, 13]
@test m.n_pmts == length(getpmts(m))
end
comments = Detector(joinpath(SAMPLES_DIR, "v3.detx")).comments @test 78.3430067946102 getpmt(d[15, 13], 0).pos.x
@test 2 == length(comments) m = d[15, 13]
@test "This is a comment" == comments[1] @test m.n_pmts == length(getpmts(m))
@test "This is another comment" == comments[2]
end end
@testset "DETX writing" begin
for from_version 1:5 comments = Detector(joinpath(SAMPLES_DIR, "v3.detx")).comments
for to_version 1:5 @test 2 == length(comments)
out = tempname() @test "This is a comment" == comments[1]
d₀ = Detector(joinpath(SAMPLES_DIR, "v$(from_version).detx")) @test "This is another comment" == comments[2]
write(out, d₀; version=to_version) end
d = Detector(out) @testset "DETX samples" begin
@test length(d₀) == length(d) for fname readdir(datapath("detx"); join=true)
if to_version >= from_version det = Detector(fname)
for module_id collect(keys(d₀.modules))[:23] for m det
@test d₀.modules[module_id].pos d.modules[module_id].pos continue
if from_version >= 3 end
@test d₀.modules[module_id].t₀ d.modules[module_id].t₀ @test 0 < length(det)
end end
end
@testset "DETX floor == -1 bug" begin
det = Detector(datapath("detx", "orca_115strings_av20min17mhorizontal_18OMs_alt9mvertical_v2.detx"))
@assert Location(1, 1) == det[1].location
@assert Location(1, 2) == det[2].location
@assert Location(1, 18) == det[18].location
@assert Location(2, 1) == det[19].location
@assert Location(3, 1) == det[37].location
@assert Location(115, 18) == det[2070].location
end
@testset "DETX writing" begin
for from_version 1:5
for to_version 1:5
out = tempname()
d₀ = Detector(joinpath(SAMPLES_DIR, "v$(from_version).detx"))
write(out, d₀; version=to_version)
d = Detector(out)
@test length(d₀) == length(d)
if to_version >= from_version
for module_id collect(keys(d₀.modules))[:23]
@test d₀.modules[module_id].pos d.modules[module_id].pos
if from_version >= 3
@test d₀.modules[module_id].t₀ d.modules[module_id].t₀
end end
end end
end end
end end
end end
@testset "hydrophones" begin end
hydrophones = read(joinpath(SAMPLES_DIR, "hydrophone.txt"), Hydrophone) @testset "hydrophones" begin
@test 19 == length(hydrophones) hydrophones = read(joinpath(SAMPLES_DIR, "hydrophone.txt"), Hydrophone)
@test Location(10, 0) == hydrophones[1].location @test 19 == length(hydrophones)
@test Position(0.770, -0.065, 1.470) hydrophones[1].pos @test Location(10, 0) == hydrophones[1].location
@test Location(28, 0) == hydrophones[end].location @test Position(0.770, -0.065, 1.470) hydrophones[1].pos
@test Position(0.770, -0.065, 1.470) hydrophones[end].pos @test Location(28, 0) == hydrophones[end].location
end @test Position(0.770, -0.065, 1.470) hydrophones[end].pos
end
@testset "tripod" begin @testset "tripod" begin
tripods = read(joinpath(SAMPLES_DIR, "tripod.txt"), Tripod) tripods = read(joinpath(SAMPLES_DIR, "tripod.txt"), Tripod)
@test 6 == length(tripods) @test 6 == length(tripods)
@test 7 == tripods[1].id @test 7 == tripods[1].id
@test Position(+587198.628 ,+4016228.693 ,-3433.306) tripods[1].pos @test Position(+587198.628 ,+4016228.693 ,-3433.306) tripods[1].pos
@test 13 == tripods[end].id @test 13 == tripods[end].id
@test Position(+587510.740 ,+4016869.160 ,-3451.700) tripods[end].pos @test Position(+587510.740 ,+4016869.160 ,-3451.700) tripods[end].pos
end end
@testset "waveform" begin @testset "waveform" begin
waveform = read(joinpath(SAMPLES_DIR, "waveform.txt"), Waveform) waveform = read(joinpath(SAMPLES_DIR, "waveform.txt"), Waveform)
@test 10 == length(waveform.ids) @test 10 == length(waveform.ids)
@test waveform.ids[16] == 3 @test waveform.ids[16] == 3
@test waveform.ids[-15] == 7 @test waveform.ids[-15] == 7
end end
@testset "triggerparameter" begin @testset "triggerparameter" begin
trigger = read(joinpath(SAMPLES_DIR, "acoustics_trigger_parameters.txt"), AcousticsTriggerParameter) trigger = read(joinpath(SAMPLES_DIR, "acoustics_trigger_parameters.txt"), AcousticsTriggerParameter)
@test trigger.q == 0.0 @test trigger.q == 0.0
@test trigger.tmax == 0.004 @test trigger.tmax == 0.004
@test trigger.nmin == 90 @test trigger.nmin == 90
end end
@testset "mechanics" begin @testset "mechanics" begin
mechanics = read(joinpath(SAMPLES_DIR, "mechanics.txt"), StringMechanics) mechanics = read(joinpath(SAMPLES_DIR, "mechanics.txt"), StringMechanics)
@test 0.00094 mechanics[1].a @test 0.00094 mechanics[1].a
@test 294.291 mechanics[1].b @test 294.291 mechanics[1].b
@test 0.00094 mechanics[-1].a @test 0.00094 mechanics[-1].a
@test 294.291 mechanics[-1].b @test 294.291 mechanics[-1].b
@test 0.00094 mechanics[42].a @test 0.00094 mechanics[42].a
@test 294.291 mechanics[42].b @test 294.291 mechanics[42].b
@test 5.6 mechanics[5].a @test 5.6 mechanics[5].a
@test 7.8 mechanics[5].b @test 7.8 mechanics[5].b
end end
@testset "utilities" begin @testset "utilities" begin
mod = DetectorModule(1, UTMPosition(0, 0, 0), Location(0, 0), 0, PMT[], missing, 0, 0) mod = DetectorModule(1, UTMPosition(0, 0, 0), Location(0, 0), 0, PMT[], missing, 0, 0)
@test hydrophoneenabled(mod) @test hydrophoneenabled(mod)
@test piezoenabled(mod) @test piezoenabled(mod)
for version 1:5 for version 1:5
d = Detector(joinpath(SAMPLES_DIR, "v$(version).detx")) d = Detector(joinpath(SAMPLES_DIR, "v$(version).detx"))
@test isapprox([83.4620946059086, 312.254188175614, 377.8839470243232], center(d); atol=0.01) @test isapprox([83.4620946059086, 312.254188175614, 377.8839470243232], center(d); atol=0.01)
end
end end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment