Skip to content

Commit

Permalink
Fix column order and make sure spectrum is the same
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Oct 5, 2020
1 parent 2ffbc37 commit c33d6ff
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
10 changes: 2 additions & 8 deletions docs/notebooks/comparison_with_EventDisplay.ipynb
Expand Up @@ -268,6 +268,7 @@
"sensitivity['reco_energy_low'].info.format = '.3g'\n",
"sensitivity['reco_energy_high'].info.format = '.3g'\n",
"sensitivity['reco_energy_center'].info.format = '.3g'\n",
"sensitivity['n_signal'].info.format = '.1f'\n",
"sensitivity['n_signal_weighted'].info.format = '.1f'\n",
"sensitivity['n_background_weighted'].info.format = '.1f'\n",
"sensitivity['n_background'].info.format = '.1f'\n",
Expand Down Expand Up @@ -679,13 +680,6 @@
"\n",
"None # to remove clutter by mpl objects"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -704,7 +698,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.7.7"
}
},
"nbformat": 4,
Expand Down
10 changes: 7 additions & 3 deletions examples/calculate_eventdisplay_irfs.py
Expand Up @@ -179,6 +179,7 @@ def main():
)
gammas["selected"] = gammas["selected_theta"] & gammas["selected_gh"]

# calculate sensitivity
signal_hist = create_histogram_table(
gammas[gammas["selected"]], bins=sensitivity_bins
)
Expand All @@ -189,12 +190,15 @@ def main():
alpha=ALPHA,
background_radius=MAX_BG_RADIUS,
)
sensitivity = calculate_sensitivity(signal_hist, background_hist, alpha=ALPHA)
sensitivity = calculate_sensitivity(
signal_hist, background_hist, alpha=ALPHA
)

# scale relative sensitivity by Crab flux to get the flux sensitivity
spectrum = particles['gamma']['target_spectrum']
for s in (sensitivity_step_2, sensitivity):
s["flux_sensitivity"] = s["relative_sensitivity"] * CRAB_HEGRA(
s["reco_energy_center"]
s["flux_sensitivity"] = (
s["relative_sensitivity"] * spectrum(s['reco_energy_center'])
)

log.info('Calculating IRFs')
Expand Down
35 changes: 18 additions & 17 deletions pyirf/sensitivity.py
Expand Up @@ -155,17 +155,8 @@ def calculate_sensitivity(
"""
check_histograms(signal_hist, background_hist)

s = QTable()
for key in ("low", "high", "center"):
k = "reco_energy_" + key
s[k] = signal_hist[k]

key = 'relative_sensitivity'
# add event number information
s["n_background"] = background_hist["n"]
s["n_background_weighted"] = background_hist["n_weighted"]

s[key] = [
# calculate sensitivity in each bin
rel_sens = np.array([
relative_sensitivity(
n_on=n_signal + alpha * n_background,
n_off=n_background,
Expand All @@ -174,14 +165,22 @@ def calculate_sensitivity(
for n_signal, n_background in zip(
signal_hist["n_weighted"], background_hist["n_weighted"]
)
]
])

# fill output table
s = QTable()
for key in ("low", "high", "center"):
k = "reco_energy_" + key
s[k] = signal_hist[k]

s["n_signal"] = signal_hist["n"] * rel_sens
s["n_background"] = background_hist["n"]
s["n_signal_weighted"] = signal_hist["n_weighted"] * rel_sens
s["n_background_weighted"] = background_hist["n_weighted"]

# perform safety checks
# we use the number of signal events at the flux level that yields
# the target significance
s["n_signal"] = signal_hist["n"] * s[key]
s["n_signal_weighted"] = signal_hist["n_weighted"] * s[key]

# safety checks according to the IRF document
# at least ten signal events and the number of signal events
# must be larger then five percent of the remaining background
Expand All @@ -190,10 +189,12 @@ def calculate_sensitivity(
s['failed_checks'] = (
(s['n_signal_weighted'] < 10)
| ((s['n_signal_weighted'] < min_excess) << 1)
| ((s[key] > 1) << 2)
| ((rel_sens > 1) << 2)
)

s[key][s['failed_checks'] > 0] = np.nan
rel_sens[s['failed_checks'] > 0] = np.nan

s["relative_sensitivity"] = rel_sens

return s

Expand Down

0 comments on commit c33d6ff

Please sign in to comment.