Skip to content

Commit

Permalink
update 0070 and 0071
Browse files Browse the repository at this point in the history
  • Loading branch information
relleums committed Jun 25, 2024
1 parent 3a6dbfa commit 435a268
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 123 deletions.
104 changes: 44 additions & 60 deletions plenoirf/summary/scripts/0070_trigger_probability_vs_cherenkov_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@
import sparse_numeric_table as snt
import json_utils

argv = irf.summary.argv_since_py(sys.argv)
pa = irf.summary.paths_from_argv(argv)

irf_config = irf.summary.read_instrument_response_config(
run_dir=paths["plenoirf_dir"]
)
sum_config = irf.summary.read_summary_config(summary_dir=paths["analysis_dir"])

paths = irf.summary.paths_from_argv(sys.argv)
res = irf.summary.Resources.from_argv(sys.argv)
os.makedirs(paths["out_dir"], exist_ok=True)

num_size_bins = 12
Expand All @@ -24,55 +18,45 @@
os.path.join(paths["analysis_dir"], "0055_passing_trigger")
)

for site_key in irf_config["config"]["sites"]:
for particle_key in irf_config["config"]["particles"]:
site_particle_dir = opj(paths["out_dir"], site_key, particle_key)
os.makedirs(site_particle_dir, exist_ok=True)

event_table = snt.read(
path=os.path.join(
paths["plenoirf_dir"],
"event_table",
site_key,
particle_key,
"event_table.tar",
),
structure=irf.table.STRUCTURE,
)

key = "trigger_probability_vs_cherenkov_size"

mask_pasttrigger = snt.make_mask_of_right_in_left(
left_indices=event_table["trigger"][snt.IDX],
right_indices=passing_trigger[site_key][particle_key]["idx"],
)

num_thrown = np.histogram(
event_table["trigger"]["num_cherenkov_pe"], bins=size_bin_edges
)[0]

num_pasttrigger = np.histogram(
event_table["trigger"]["num_cherenkov_pe"],
bins=size_bin_edges,
weights=mask_pasttrigger,
)[0]

trigger_probability = irf.utils._divide_silent(
numerator=num_pasttrigger, denominator=num_thrown, default=np.nan
)

trigger_probability_unc = irf.utils._divide_silent(
numerator=np.sqrt(num_pasttrigger),
denominator=num_pasttrigger,
default=np.nan,
)

json_utils.write(
os.path.join(site_particle_dir, key + ".json"),
{
"true_Cherenkov_size_bin_edges_pe": size_bin_edges,
"unit": "1",
"mean": trigger_probability,
"relative_uncertainty": trigger_probability_unc,
},
)
for pk in res.PARTICLES:
pk_dir = opj(paths["out_dir"], pk)
os.makedirs(pk_dir, exist_ok=True)

event_table = res.read_event_table(particle_key=pk)

key = "trigger_probability_vs_cherenkov_size"

mask_pasttrigger = snt.make_mask_of_right_in_left(
left_indices=event_table["trigger"][snt.IDX],
right_indices=passing_trigger[pk]["idx"],
).astype(float)

num_thrown = np.histogram(
event_table["trigger"]["num_cherenkov_pe"], bins=size_bin_edges
)[0]

num_pasttrigger = np.histogram(
event_table["trigger"]["num_cherenkov_pe"],
bins=size_bin_edges,
weights=mask_pasttrigger,
)[0]

trigger_probability = irf.utils._divide_silent(
numerator=num_pasttrigger, denominator=num_thrown, default=np.nan
)

trigger_probability_unc = irf.utils._divide_silent(
numerator=np.sqrt(num_pasttrigger),
denominator=num_pasttrigger,
default=np.nan,
)

json_utils.write(
os.path.join(pk_dir, f"{key}.json"),
{
"true_Cherenkov_size_bin_edges_pe": size_bin_edges,
"unit": "1",
"mean": trigger_probability,
"relative_uncertainty": trigger_probability_unc,
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,70 @@
import sebastians_matplotlib_addons as seb
import json_utils

argv = irf.summary.argv_since_py(sys.argv)
pa = irf.summary.paths_from_argv(argv)

irf_config = irf.summary.read_instrument_response_config(
run_dir=paths["plenoirf_dir"]
)
sum_config = irf.summary.read_summary_config(summary_dir=paths["analysis_dir"])
seb.matplotlib.rcParams.update(sum_config["plot"]["matplotlib"])

paths = irf.summary.paths_from_argv(sys.argv)
res = irf.summary.Resources.from_argv(sys.argv)
os.makedirs(paths["out_dir"], exist_ok=True)
seb.matplotlib.rcParams.update(res.analysis["plot"]["matplotlib"])

trigger_vs_size = json_utils.tree.read(
os.path.join(
paths["analysis_dir"], "0070_trigger_probability_vs_cherenkov_size"
)
)

particle_colors = sum_config["plot"]["particle_colors"]
particle_colors = res.analysis["plot"]["particle_colors"]

for site_key in irf_config["config"]["sites"]:
# all particles together
# ----------------------
fig = seb.figure(irf.summary.figure.FIGURE_STYLE)
ax = seb.add_axes(fig=fig, span=irf.summary.figure.AX_SPAN)
# all particles together
# ----------------------
fig = seb.figure(irf.summary.figure.FIGURE_STYLE)
ax = seb.add_axes(fig=fig, span=irf.summary.figure.AX_SPAN)

text_y = 0
for particle_key in irf_config["config"]["particles"]:
size_bin_edges = np.array(
trigger_vs_size[site_key][particle_key][
"trigger_probability_vs_cherenkov_size"
]["true_Cherenkov_size_bin_edges_pe"]
)
text_y = 0
for pk in res.PARTICLES:
size_bin_edges = np.array(
trigger_vs_size[pk]["trigger_probability_vs_cherenkov_size"][
"true_Cherenkov_size_bin_edges_pe"
]
)

prob = np.array(
trigger_vs_size[site_key][particle_key][
"trigger_probability_vs_cherenkov_size"
]["mean"]
)
prob_unc = np.array(
trigger_vs_size[site_key][particle_key][
"trigger_probability_vs_cherenkov_size"
]["relative_uncertainty"]
)
prob = np.array(
trigger_vs_size[pk]["trigger_probability_vs_cherenkov_size"]["mean"]
)
prob_unc = np.array(
trigger_vs_size[pk]["trigger_probability_vs_cherenkov_size"][
"relative_uncertainty"
]
)

seb.ax_add_histogram(
ax=ax,
bin_edges=size_bin_edges,
bincounts=prob,
linestyle="-",
linecolor=particle_colors[particle_key],
bincounts_upper=prob * (1 + prob_unc),
bincounts_lower=prob * (1 - prob_unc),
face_color=particle_colors[particle_key],
face_alpha=0.25,
)
ax.text(
0.85,
0.1 + text_y,
particle_key,
color=particle_colors[particle_key],
transform=ax.transAxes,
)
text_y += 0.06
ax.semilogx()
ax.semilogy()
ax.set_xlim([1e1, np.max(size_bin_edges)])
ax.set_ylim([1e-6, 1.5e-0])
ax.set_xlabel("true Cherenkov-size / p.e.")
ax.set_ylabel("trigger-probability / 1")
fig.savefig(
opj(
paths["out_dir"],
site_key + "_trigger_probability_vs_cherenkov_size.jpg",
)
seb.ax_add_histogram(
ax=ax,
bin_edges=size_bin_edges,
bincounts=prob,
linestyle="-",
linecolor=particle_colors[pk],
bincounts_upper=prob * (1 + prob_unc),
bincounts_lower=prob * (1 - prob_unc),
face_color=particle_colors[pk],
face_alpha=0.25,
)
ax.text(
0.85,
0.1 + text_y,
pk,
color=particle_colors[pk],
transform=ax.transAxes,
)
seb.close(fig)
text_y += 0.06
ax.semilogx()
ax.semilogy()
ax.set_xlim([1e1, np.max(size_bin_edges)])
ax.set_ylim([1e-6, 1.5e-0])
ax.set_xlabel("true Cherenkov-size / p.e.")
ax.set_ylabel("trigger-probability / 1")
fig.savefig(
opj(
paths["out_dir"],
"trigger_probability_vs_cherenkov_size.jpg",
)
)
seb.close(fig)

0 comments on commit 435a268

Please sign in to comment.