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

API clean up and restructure

parent fadba84c
No related branches found
No related tags found
1 merge request!2API cleanup for online files reading
......@@ -6,11 +6,13 @@ import Statistics: mean
using Printf: @printf
using Dates: DateTime, datetime2unix, unix2datetime
using StaticArrays: FieldVector
import UnROOT
using UnROOT
export OnlineFile
export Position, UTMPosition, Location, Quaternion
export Detector, DetectorModule, PMT, Tripod, Hydrophone
export Waveform, AcousticsTriggerParameter, piezoenabled, hydrophoneenabled
export is3dshower, ismxshower, is3dmuon, isnb
......@@ -24,9 +26,9 @@ end
include("types.jl")
include("hardware.jl")
include("daq.jl")
include("root/online.jl")
include("root/offline.jl")
include("daq.jl")
include("acoustics.jl")
include("tools.jl")
......
......@@ -5,34 +5,35 @@ function Base.read(s::IO, ::Type{T}; legacy=false) where T<:DAQEvent
type = read(s, Int32)
version = Int16(0)
!legacy && (version = read(s, Int16))
det_id = read(s, Int32)
run_id = read(s, Int32)
timeslice_id = read(s, Int32)
_timestamp_field = read(s, UInt32)
whiterabbit_status = _timestamp_field & 0x80000000 # most significant bit
timestamp = _timestamp_field & 0x7FFFFFFF # skipping the most significant bit
ticks = read(s, UInt32)
ticks = read(s, UInt32) # 16ns ticks
trigger_counter = read(s, Int64)
trigger_mask = read(s, Int64)
overlays = read(s, Int32)
header = KM3NETDAQEventHeader(det_id, run_id, timeslice_id, timestamp, ticks, trigger_counter, trigger_mask, overlays)
n_triggered_hits = read(s, Int32)
triggered_hits = Vector{TriggeredHit}()
triggered_hits = Vector{KM3NETDAQTriggeredHit}()
sizehint!(triggered_hits, n_triggered_hits)
triggered_map = Dict{Tuple{Int32, UInt8, Int32, UInt8}, Int64}()
@inbounds for i 1:n_triggered_hits
dom_id = read(s, Int32)
channel_id = read(s, UInt8)
time = bswap(read(s, Int32))
tot = read(s, UInt8)
trigger_mask = read(s, Int64)
triggered_map[(dom_id, channel_id, time, tot)] = trigger_mask
push!(triggered_hits, TriggeredHit(dom_id, channel_id, time, tot, trigger_mask))
push!(triggered_hits, KM3NETDAQTriggeredHit(dom_id, channel_id, time, tot, trigger_mask))
end
n_hits = read(s, Int32)
hits = Vector{Hit}()
sizehint!(hits, n_hits)
snapshot_hits = Vector{KM3NETDAQSnapshotHit}()
sizehint!(snapshot_hits, n_hits)
@inbounds for i 1:n_hits
dom_id = read(s, Int32)
channel_id = read(s, UInt8)
......@@ -40,11 +41,8 @@ function Base.read(s::IO, ::Type{T}; legacy=false) where T<:DAQEvent
tot = read(s, UInt8)
key = (dom_id, channel_id, time, tot)
triggered = false
if haskey(triggered_map, key)
triggered = true
end
push!(hits, Hit(channel_id, dom_id, time, tot, triggered))
push!(snapshot_hits, KM3NETDAQSnapshotHit(dom_id, channel_id, time, tot))
end
T(det_id, run_id, timeslice_id, whiterabbit_status, timestamp, ticks, trigger_counter, trigger_mask, overlays, n_triggered_hits, triggered_hits, n_hits, hits)
T(header, snapshot_hits, triggered_hits)
end
......@@ -7,7 +7,7 @@ end
function UnROOT.readtype(io, T::Type{KM3NETDAQSnapshotHit})
T(UnROOT.readtype(io, Int32), read(io, UInt8), read(io, Int32), read(io, UInt8))
end
function UnROOT.interped_data(rawdata, rawoffsets, ::Type{Vector{KM3NETDAQSnapshotHit}}, ::Type{J}) where {T, J <: UnROOT.JaggType}
function UnROOT.interped_data(rawdata, rawoffsets, ::Type{Vector{KM3NETDAQSnapshotHit}}, ::Type{T}) where {T <: UnROOT.JaggType}
UnROOT.splitup(rawdata, rawoffsets, KM3NETDAQSnapshotHit, skipbytes=10)
end
......@@ -31,7 +31,7 @@ function UnROOT.readtype(io, T::Type{KM3NETDAQTriggeredHit})
T(dom_id, channel_id, tdc, tot, trigger_mask)
end
function UnROOT.interped_data(rawdata, rawoffsets, ::Type{Vector{KM3NETDAQTriggeredHit}}, ::Type{J}) where {T, J <: UnROOT.JaggType}
function UnROOT.interped_data(rawdata, rawoffsets, ::Type{Vector{KM3NETDAQTriggeredHit}}, ::Type{T}) where {T <: UnROOT.JaggType}
UnROOT.splitup(rawdata, rawoffsets, KM3NETDAQTriggeredHit, skipbytes=10)
end
......@@ -62,21 +62,31 @@ function UnROOT.readtype(io::IO, T::Type{KM3NETDAQEventHeader})
overlays = UnROOT.readtype(io, UInt32)
T(detector_id, run, frame_index, UTC_seconds, UTC_16nanosecondcycles, trigger_counter, trigger_mask, overlays)
end
function UnROOT.interped_data(rawdata, rawoffsets, ::Type{KM3NETDAQEventHeader}, ::Type{J}) where {T, J <: UnROOT.JaggType}
function UnROOT.interped_data(rawdata, rawoffsets, ::Type{KM3NETDAQEventHeader}, ::Type{T}) where {T <: UnROOT.JaggType}
UnROOT.splitup(rawdata, rawoffsets, KM3NETDAQEventHeader, jagged=false)
end
struct OnlineEvent
struct DAQEvent
header::KM3NETDAQEventHeader
snapshot_hits::Vector{KM3NETDAQSnapshotHit}
triggered_hits::Vector{KM3NETDAQTriggeredHit}
end
function Base.show(io::IO, e::DAQEvent)
print(io, "$(typeof(e)) with $(length(e.snapshot_hits)) snapshot and $(length(e.triggered_hits)) triggered hits")
end
struct OnlineFile
fobj::UnROOT.ROOTFile
headers::Vector{KM3NETDAQEventHeader}
struct EventContainer
headers
snapshot_hits
triggered_hits
end
function Base.show(io::IO, e::EventContainer)
print(io, "$(typeof(e)) with $(length(e.headers)) events")
end
struct OnlineFile
_fobj::UnROOT.ROOTFile
events::EventContainer
function OnlineFile(filename::AbstractString)
customstructs = Dict(
......@@ -87,17 +97,19 @@ struct OnlineFile
fobj = UnROOT.ROOTFile(filename, customstructs=customstructs)
new(fobj,
fobj["KM3NET_EVENT/KM3NET_EVENT/KM3NETDAQ::JDAQEventHeader"],
fobj["KM3NET_EVENT/KM3NET_EVENT/snapshotHits"],
fobj["KM3NET_EVENT/KM3NET_EVENT/triggeredHits"])
EventContainer(
LazyBranch(fobj, "KM3NET_EVENT/KM3NET_EVENT/KM3NETDAQ::JDAQEventHeader"),
LazyBranch(fobj, "KM3NET_EVENT/KM3NET_EVENT/snapshotHits"),
LazyBranch(fobj, "KM3NET_EVENT/KM3NET_EVENT/triggeredHits"))
)
end
end
Base.getindex(f::OnlineFile, idx::Integer) = OnlineEvent(
f.headers[idx], f.snapshot_hits[idx], f.triggered_hits[idx]
)
Base.length(f::OnlineFile) = length(f.headers)
Base.getindex(c::EventContainer, idx::Integer) = DAQEvent(c.headers[idx], c.snapshot_hits[idx], c.triggered_hits[idx])
Base.getindex(c::EventContainer, r::UnitRange) = [c[idx] for idx r]
Base.getindex(c::EventContainer, mask::BitArray) = [c[idx] for (idx, selected) enumerate(mask) if selected]
Base.close(f::OnlineFile) = close(f.fobj)
Base.length(c::EventContainer) = length(f.headers)
Base.close(c::OnlineFile) = close(f._fobj)
function read_headers(f::OnlineFile)
data, offsets = UnROOT.array(f.fobj, "KM3NET_EVENT/KM3NET_EVENT/KM3NETDAQ::JDAQEventHeader"; raw=true)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment