From a8ff94f14842766d741bb7d623f8b302ed6ae531 Mon Sep 17 00:00:00 2001
From: Tamas Gal <himself@tamasgal.com>
Date: Tue, 17 Oct 2023 12:47:56 +0200
Subject: [PATCH] Add combine() to combine snapshot/trig-hits

---
 docs/src/api.md    |  1 +
 src/KM3io.jl       |  2 +-
 src/calibration.jl | 26 ++++++++++++++++++++++++++
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/docs/src/api.md b/docs/src/api.md
index ff63f9b3..11b3ac01 100644
--- a/docs/src/api.md
+++ b/docs/src/api.md
@@ -82,6 +82,7 @@ read(filename::AbstractString, T::Type{AcousticsTriggerParameter})
 ## Calibration
 ```@docs
 calibrate
+combine
 floordist
 slew
 ```
diff --git a/src/KM3io.jl b/src/KM3io.jl
index 9cbf6fd2..0bf12207 100644
--- a/src/KM3io.jl
+++ b/src/KM3io.jl
@@ -37,7 +37,7 @@ export Evt, Hit, TriggeredHit, Trk, CalibratedHit, XCalibratedHit, MCTrk, Calibr
 
 export K40Rates
 
-export calibrate, floordist, slew
+export calibrate, floordist, slew, combine
 
 export besttrack, bestjppmuon, bestjppshower, bestaashower,
        RecStageRange, hashistory, hasjppmuonprefit, hasjppmuonsimplex, hasjppmuongandalf,
diff --git a/src/calibration.jl b/src/calibration.jl
index 6fc3fe79..4bc63d5e 100644
--- a/src/calibration.jl
+++ b/src/calibration.jl
@@ -25,6 +25,32 @@ function calibrate(det::Detector, hits)
     calibrated_hits
 end
 
+"""
+Combine snapshot and triggered hits to a single hits-vector.
+
+This should be used to transfer the trigger information to the
+snapshot hits from a DAQEvent. The triggered hits are a subset
+of the snapshot hits.
+
+"""
+function combine(snapshot_hits::Vector{KM3io.SnapshotHit}, triggered_hits::Vector{KM3io.TriggeredHit})
+    triggermasks = Dict{Tuple{UInt8, Int32, Int32, UInt8}, Int64}()
+    for hit ∈ triggered_hits
+        triggermasks[(hit.channel_id, hit.dom_id, hit.t, hit.tot)] = hit.trigger_mask
+    end
+    n = length(snapshot_hits)
+    hits = sizehint!(Vector{TriggeredHit}(), n)
+    for hit in snapshot_hits
+        channel_id = hit.channel_id
+        dom_id = hit.dom_id
+        t = hit.t
+        tot = hit.tot
+        triggermask = get(triggermasks, (channel_id, dom_id, t, tot), 0)
+        push!(hits, TriggeredHit(dom_id, channel_id, t, tot, triggermask))
+    end
+    hits
+end
+
 
 """
 Calculates the average floor distance between neighboured modules.
-- 
GitLab