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

Add getevent

parent 0c1bdf60
No related branches found
No related tags found
No related merge requests found
......@@ -126,6 +126,7 @@ CHClient
### General tools
```@docs
getevent
categorize
nthbitset
most_frequent
......
......@@ -57,7 +57,7 @@ event = getevent(f.online, 2)
when two numbers are passed, the first one is interpreted as `frame_index` and the second one as `trigger_counter`:
```@example 1
event = getevent(f.offline, 127, 2)
event = getevent(f.online, 127, 1)
```
!!! note
......
......@@ -117,6 +117,7 @@ isnb,
most_frequent,
nthbitset,
triggered,
getevent,
# Physics and math helpers
CherenkovPhoton,
......
......@@ -152,6 +152,7 @@ end
struct OfflineTree{T}
_fobj::UnROOT.ROOTFile
header::Union{MCHeader, Missing}
_frame_index_trigger_counter_lookup_map::Dict{Tuple{Int, Int}, Int}
_t::T # carry the type to ensure type-safety
function OfflineTree(fobj::UnROOT.ROOTFile)
......@@ -201,7 +202,7 @@ struct OfflineTree{T}
header = "Head" keys(fobj) ? MCHeader(fobj["Head"]) : missing
new{typeof(t)}(fobj, header, t)
new{typeof(t)}(fobj, header, Dict{Tuple{Int, Int}, Int}(), t)
end
end
OfflineTree(filename::AbstractString) = OfflineTree(UnROOT.ROOTFile(filename))
......@@ -221,6 +222,7 @@ end
Base.getindex(f::OfflineTree, r::UnitRange) = [f[idx] for idx r]
Base.getindex(f::OfflineTree, mask::BitArray) = [f[idx] for (idx, selected) enumerate(mask) if selected]
function Base.getindex(f::OfflineTree, idx::Integer)
idx > length(f) && throw(BoundsError(f, idx))
e = f._t[idx] # the event as NamedTuple: struct of arrays
skip_mc_event_time = !hasproperty(e, :mc_event_time_Sec)
......
......@@ -141,6 +141,7 @@ struct OnlineTree
_fobj::UnROOT.ROOTFile
events::EventContainer
summaryslices::SummarysliceContainer
_frame_index_trigger_counter_lookup_map::Dict{Tuple{Int, Int}, Int}
function OnlineTree(fobj::UnROOT.ROOTFile)
new(fobj,
......@@ -152,7 +153,8 @@ struct OnlineTree
SummarysliceContainer(
UnROOT.LazyBranch(fobj, "KM3NET_SUMMARYSLICE/KM3NET_SUMMARYSLICE/KM3NETDAQ::JDAQSummarysliceHeader"),
UnROOT.LazyBranch(fobj, "KM3NET_SUMMARYSLICE/KM3NET_SUMMARYSLICE/vector<KM3NETDAQ::JDAQSummaryFrame>")
)
),
Dict{Tuple{Int, Int}, Int}()
)
end
......
......@@ -18,3 +18,43 @@ function Base.iterate(itr::MCEventMatcher, state=1)
state > length(itr) && return nothing
(itr[state], state + 1)
end
countevents(tree::OfflineTree) = length(tree)
countevents(tree::OnlineTree) = length(tree.events)
triggercounterof(e::Evt) = e.trigger_counter
frameindexof(e::Evt) = e.frame_index
triggercounterof(e::DAQEvent) = e.header.trigger_counter
frameindexof(e::DAQEvent) = e.header.frame_index
"""
Retrieves the event with for a given `frame_index` and `trigger_counter`.
"""
function getevent(tree::T, frame_index, trigger_counter) where T<:Union{OnlineTree, OfflineTree}
lookup = tree._frame_index_trigger_counter_lookup_map
key = (frame_index, trigger_counter)
if haskey(lookup, key)
event_idx = lookup[key]
return getevent(tree, event_idx)
end
highest_event_idx = length(lookup) == 0 ? 0 : maximum(values(lookup))
for event_idx in (highest_event_idx+1):countevents(tree)
event = getevent(tree, event_idx)
fi = frameindexof(event)
tc = triggercounterof(event)
lookup[(fi, tc)] = event_idx
if fi == frame_index && tc == trigger_counter
return event
end
end
error("No online event found for frame_index=$(frame_index) and trigger_counter=$(trigger_counter).")
end
getevent(tree::OfflineTree, idx) = tree[idx]
getevent(tree::OnlineTree, idx) = tree.events[idx]
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