Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coincidence method for the MAGIC broken timestamp data #231

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7675799
Added find_offset function
Ryo-Imazawa May 29, 2024
2445ccc
Added find_offset module
Ryo-Imazawa May 29, 2024
3af3220
Added -t --input-dir_toff option to load the time offset
Ryo-Imazawa May 29, 2024
c3c7474
The script for obtain the time offset
Ryo-Imazawa May 29, 2024
f2596b4
minor change
Ryo-Imazawa May 30, 2024
94d7b19
Added the explanation and usage
Ryo-Imazawa May 30, 2024
4cd38a3
minor changes
Ryo-Imazawa May 30, 2024
651b772
This script make coincidence dl1 for each subrun.
Ryo-Imazawa May 30, 2024
4043cfb
fixed the typo
Ryo-Imazawa May 31, 2024
82f2b24
Delete magicctapipe/scripts/lst1_magic/launch_coincidence_for_brokenG…
Ryo-Imazawa Jun 10, 2024
3e0fd76
Delete magicctapipe/scripts/lst1_magic/lst1_magic_find_time_offset.py
Ryo-Imazawa Jun 10, 2024
4787d99
Create coincident_brokenGPS.py
Ryo-Imazawa Jun 10, 2024
528631e
Changed the output file name
Ryo-Imazawa Jun 10, 2024
4566623
Added the explanation for the Parameters/Returns
Ryo-Imazawa Jun 10, 2024
754badf
minor format change
Ryo-Imazawa Jun 10, 2024
d286eab
Minor format change
Ryo-Imazawa Jun 10, 2024
5b3c6eb
Minor format change
Ryo-Imazawa Jun 10, 2024
7837632
modified the bug in coincidence
Ryo-Imazawa Jul 19, 2024
1695783
modified bug of t_magic_all
Ryo-Imazawa Jul 21, 2024
cb4ccb9
modified the bug
Ryo-Imazawa Jul 22, 2024
2d06605
modified the scripts
Ryo-Imazawa Sep 30, 2024
b4d911e
modified error
Ryo-Imazawa Sep 30, 2024
eb53697
modified the logging error
Ryo-Imazawa Oct 1, 2024
8e168fc
modified some statement in coincidence_brokenGPS
Ryo-Imazawa Oct 1, 2024
2a1eae4
modified some bugs
Ryo-Imazawa Oct 3, 2024
d49ff0c
Fixed so that it can be applied to files with the name dl1_MAGIC.RunX…
Ryo-Imazawa Oct 4, 2024
0ad568b
fixed the conflicts of the branch
Ryo-Imazawa Oct 4, 2024
271e258
modified the conflict
Ryo-Imazawa Oct 4, 2024
ce742a1
fixed the conflict
Ryo-Imazawa Oct 4, 2024
fa09a4e
fixed the conflict
Ryo-Imazawa Oct 4, 2024
63c1464
fixed conflict
Ryo-Imazawa Oct 4, 2024
392cefe
fixed conflict
Ryo-Imazawa Oct 4, 2024
5ac261f
fixed the conflict
Ryo-Imazawa Oct 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions magicctapipe/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from .io import (
check_input_list,
find_offset,
format_object,
get_dl2_mean,
get_stereo_events,
Expand Down Expand Up @@ -53,4 +54,5 @@
"save_pandas_data_in_table",
"telescope_combinations",
"resource_file",
"find_offset",
]
168 changes: 168 additions & 0 deletions magicctapipe/io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"save_pandas_data_in_table",
"telescope_combinations",
"resource_file",
"find_offset",
]

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -1419,3 +1420,170 @@ def resource_file(filename):
Absolute path of the input filename
"""
return files("magicctapipe").joinpath("resources", filename)

def make_time_select(N_time_magic_start, N_time_magic_end, time_offset_width1, time_offset_width2):
time_offset_width1, time_offset_width2 = (
time_offset_center - 1.6e-6,
time_offset_center + 1.6e-6,
)
time_select = (
data_lst_["trigger_time"].values
> (data_magic_["trigger_time"].values[N_time_magic_start] + time_offset_width1)
) & (
data_lst_["trigger_time"].values
< (data_magic_["trigger_time"].values[N_time_magic_end] + time_offset_width2)
)
if max(time_select) == True:
t_lst_all = data_lst_["trigger_time"].values[time_select & select]
t_magic_all = data_magic_["trigger_time"].values[N_time_magic_start:N_time_magic_end]
t_magic_ave[n] = np.mean(t_magic_all)
event_id_magic_ave[n] = np.mean(
data_magic_["event_id"].values[N_start_2:N_end_2]
)
comb_array = np.array(np.meshgrid(t_lst_all, t_magic_all)).reshape(2, -1)
time_offsets = comb_array[0] - comb_array[1]
b = np.array(
[
np.sum(np.abs(time_offsets - T) < time_window)
for T in time_offsets
]
)
n_coincident[n] = np.max(b)
time_offset_best[n] = np.mean(time_offsets[b == np.max(b)])
logger.info(
f"new time offset = {time_offset_best[n]}, drift = {int((time_offset_best[n] - time_offset_center) * 1e9)} ns, n_coinc = {n_coincident[n]}"
)
time_offset_center = time_offset_best[n]
else:
t_magic_ave[n] = np.mean(data_magic_["trigger_time"].values)
event_id_magic_ave[n] = np.mean(
data_magic_["event_id"].values[N_start_2:N_end_2]
)
n_coincident[n] = 0
if n == 0:
time_offset_best[n] = initial_time_offset
else:
time_offset_best[n] = time_offset_best[n - 1]
return time_offsets

def find_offset(data_magic_, data_lst_, N_start=0, N_end=20, initial_time_offset=None):
"""
Find the time offset between MAGIC and LST.

Parameters
----------
data_magic_ : str
Input filename for MAGIC
data_lst_ : str
Input filename for LST
N_start, N_end : int
Start and stop event number for MAGIC event
initial_time_offset : float
The initial time offset value for search the time offset

Returns
-------
t_magic_ave : float
The average the MAGIC timestamp
event_id_magic_ave : float
The average of the MAGIC event ID
time_offset_best : float
The best time offset found from the combination
n_coincident : float
The coincidence event number
"""

select = data_lst_["event_type"] == 32
time_window = 500e-9

if initial_time_offset is None:
time_offset_width1, time_offset_width2 = -4, 4
time_select = (
data_lst_["trigger_time"].values
> (data_magic_["trigger_time"].values[N_start] + time_offset_width1)
) & (
data_lst_["trigger_time"].values
< (data_magic_["trigger_time"].values[N_end] + time_offset_width2)
)
if max(time_select) == True:
t_lst_all = data_lst_["trigger_time"].values[time_select & select]
logger.info(f"size of LST time array {len(t_lst_all)}")
t_magic_all = data_magic_[N_start:N_end]["trigger_time"].values

# compute time difference with all the combinations
comb_array = np.array(np.meshgrid(t_lst_all, t_magic_all)).reshape(2, -1)
time_offsets = comb_array[0] - comb_array[1]
logger.info(f"size of combination array: {time_offsets}")
time_window = 500e-9
b = np.array(
[np.sum(np.abs(time_offsets - T) < time_window) for T in time_offsets]
)
n_coincident = np.max(b)
time_offset_best = np.mean(time_offsets[b == np.max(b)])
std_b = np.std(b)
logger.info(f"time offset: {time_offset_best}")
logger.info(f"# of coincident events: {n_coincident}")
logger.info(f"standard deviation: {std_b}")
return t_magic_all, N_start, time_offset_best, n_coincident
else:
logger.info("No event was found")
return 0, 0, 0, 0

else:
EVENT_STEP = 15
N_ITR = len(data_magic_["trigger_time"].values[N_start:N_end]) // EVENT_STEP
t_magic_ave = np.zeros(N_ITR)
event_id_magic_ave = np.zeros(N_ITR)
time_offset_best = np.zeros(N_ITR)
n_coincident = np.zeros(N_ITR)

for n in range(N_ITR):
if n == 0:
time_offset_center = initial_time_offset
N_start_2 = n * EVENT_STEP
N_end_2 = (n + 1) * EVENT_STEP
time_offset_width1, time_offset_width2 = (
time_offset_center - 1.6e-6,
time_offset_center + 1.6e-6,
)
time_select = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what you do in each step is very similar to what you do at the beginning of the function with initial_time_offset None, maybe you can extract this into a separate function, or do the first case as 1 iteration case of multiple iterations?

data_lst_["trigger_time"].values
> (data_magic_["trigger_time"].values[N_start_2] + time_offset_width1)
) & (
data_lst_["trigger_time"].values
< (data_magic_["trigger_time"].values[N_end_2] + time_offset_width2)
)
if max(time_select) == True:
t_lst_all = data_lst_["trigger_time"].values[time_select & select]
t_magic_all = data_magic_["trigger_time"].values[N_start_2:N_end_2]
t_magic_ave[n] = np.mean(t_magic_all)
event_id_magic_ave[n] = np.mean(
data_magic_["event_id"].values[N_start_2:N_end_2]
)
comb_array = np.array(np.meshgrid(t_lst_all, t_magic_all)).reshape(2, -1)
time_offsets = comb_array[0] - comb_array[1]
time_window = 500e-9
b = np.array(
[
np.sum(np.abs(time_offsets - T) < time_window)
for T in time_offsets
]
)
n_coincident[n] = np.max(b)
time_offset_best[n] = np.mean(time_offsets[b == np.max(b)])
logger.info(
f"new time offset = {time_offset_best[n]}, drift = {int((time_offset_best[n] - time_offset_center) * 1e9)} ns, n_coinc = {n_coincident[n]}"
)
time_offset_center = time_offset_best[n]
else:
t_magic_ave[n] = np.mean(data_magic_["trigger_time"].values)
event_id_magic_ave[n] = np.mean(
data_magic_["event_id"].values[N_start_2:N_end_2]
)
n_coincident[n] = 0
if n == 0:
time_offset_best[n] = initial_time_offset
else:
time_offset_best[n] = time_offset_best[n - 1]

return t_magic_ave, event_id_magic_ave, time_offset_best, n_coincident
Loading