Detector model interface

In addition to the neutrino data itself, the IceCube collaboration provides some information about the detector that can be useful to construct simple simulations and fits. For example, the effective area is needed to connect between incident neutrino fluxes and expected number of events in the detector.

icecube_tools also provides an quick interface to loading and working with such information. This is a work in progress and only certain datasets are currently implemented, such as the ones demonstrated below.

[1]:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import LogNorm

from icecube_tools.utils.data import IceCubeData
from icecube_tools.detector.detector import IceCube, TimeDependentIceCube

The IceCubeData class can be used for a quick check of the available datasets on the IceCube website.

[2]:
my_data = IceCubeData()
my_data.datasets
[2]:
['2021_03-all_data_releases.zip',
 '20080911_AMANDA_7_Year_Data.zip',
 '20090521_IceCube-22_Solar_WIMP_Data.zip',
 '20110905_IceCube-40_String_Data.zip',
 '20131121_Search_for_contained_neutrino_events_at_energies_above_30_TeV_in_2_years_of_data.zip',
 '20150127_IceCube_Oscillations%20_3_years_muon_neutrino_disappearance_data.zip',
 '20150219_Search_for_contained_neutrino_events_at_energies_greater_than_1_TeV_in_2_years_of_data.zip',
 '20150619_IceCube-59%20_Search_for_point_sources_using_muon_events.zip',
 '20150820_Astrophysical_muon_neutrino_flux_in_the_northern_sky_with_2_years_of_IceCube_data.zip',
 '20151021_Observation_of_Astrophysical_Neutrinos_in_Four_Years_of_IceCube_Data.zip',
 '20160105_The_79-string_IceCube_search_for_dark_matter.zip',
 '20160624_Search_for_sterile_neutrinos_with_one_year_of_IceCube_data.zip',
 '20161101_Search_for_point_sources_with_first_year_of_IC86_data.zip',
 '20161115_A_combined_maximum-likelihood_analysis_of_the_astrophysical_neutrino_flux.zip',
 '20180213_Measurement_of_atmospheric_neutrino_oscillations_with_three_years_of_data_from_the_full_sky.zip',
 '20180712_IceCube_catalog_of_alert_events_up_through_IceCube-170922A.zip',
 '20180712_IceCube_data_from_2008_to_2017_related_to_analysis_of_TXS_0506+056.zip',
 '20181018_All-sky_point-source_IceCube_data%20_years_2010-2012.zip',
 '20190515_Three-year_high-statistics_neutrino_oscillation_samples.zip',
 '20190904_Bayesian_posterior_for_IceCube_7-year_point-source_search_with_neutrino-count_statistics.zip',
 '20200227_All-sky_point-source_IceCube_data%20_years_2012-2015.zip',
 '20200421_IceCube_Upgrade_Neutrino_Monte_Carlo_Simulation.zip',
 '20200514_South_Pole_ice_temperature.zip',
 '20210126_PS-IC40-IC86_VII.zip',
 '20210310_IceCube_data_for_the_first_Glashow_resonance_candidate.zip',
 '20211217_HESE-7-5-year-data.zip',
 '20220201_Density_of_GeV_muons_in_air_showers_measured_with_IceTop.zip',
 '20220902_Evidence_for_neutrino_emission_from_the_nearby_active_galaxy_NGC_1068_data.zip',
 '20220913_Evidence_for_neutrino_emission_from_the_nearby_active_galaxy_NGC_1068_data.zip',
 'ic22-solar-wimp-histograms.zip']

Effective area, angular resolution and energy resolution of 10 year data

We can now use the date string to identify certain datasets. Let’s say we want to use the effective area and angular resolution from the 20210126 dataset. If you don’t already have the dataset downloaded, icecube_tools will do this for you automatically. This 10 year data release provides more detailed IRF data.

We restrict our examples to the Northern hemisphere.

The simpler, earlier versions are explained afterwards.

The format of the effective area has not changed, though.

For this latest data set, we have different detector configurations available, as they changed through time (the detector was expanded). We can invoke a chosen configuration throught the second argument in EffectiveArea.from_dataset() for this particular data set only.

[3]:
from icecube_tools.detector.r2021 import R2021IRF
from icecube_tools.detector.effective_area import EffectiveArea
[4]:
my_aeff = EffectiveArea.from_dataset("20210126", "IC86_II")
20210126_PS-IC40-IC86_VII.zip: 100%|██████████| 39848048/39848048 [01:49<00:00, 364481.08it/s]
[5]:
fig, ax = plt.subplots()
h = ax.pcolor(
    my_aeff.true_energy_bins, my_aeff.cos_zenith_bins, my_aeff.values.T, norm=LogNorm()
)
cbar = fig.colorbar(h)
ax.set_xscale("log")
ax.set_xlim(1e2, 1e9)
ax.set_xlabel("True energy [GeV]")
ax.set_ylabel("cos(zenith)")
cbar.set_label("Aeff [m^2]")
../_images/notebooks_detector_model_7_0.png

Energy resolution

Angular resolution depends on the energy resolution. The paper accompaying the data release explains the dependency: For each bin of true energy and declination a certain amount of events is simulated. These are sorted first into bins of reconstructed energy. These are then reconstructed in terms of PSF(the kinematic angle between the incoming neutrino and the outgoing muon after a collision) and actual angular error. Data is given as fractional counts in the bin \((E_\mathrm{reco}, \mathrm{PSF}, \mathrm{ang\_err})\) of all counts in bin (E_:nbsphinx-math:mathrm{true}, \delta). This is nothing but a histogram, corresponding to a probability of finding an event with given true energy and true declination: \(p(E_\mathrm{reco}, \mathrm{PSF}, \mathrm{ang\_err} \vert E_\mathrm{true}, \delta)\).

We find the energy resolution, i.e. \(p(E_\mathrm{reco} \vert E_\mathrm{true}, \delta)\), by summing over (marginalising over) all entries of \(\mathrm{PSF}, \mathrm{ang\_err}\) for the reconstructed energy we are interested in.

The R2021IRF()class is able to do so:

[6]:
irf = R2021IRF.from_period("IC86_II")
[7]:
fig, ax = plt.subplots()
idx = [0, 3, 6, 9, 12]
# plotting Ereco for different true energy bins, the declination bin here is always from +10 to +90 degrees.
for i in idx:

    x = np.linspace(*irf.reco_energy[i, 2].support(), num=1000)
    ax.plot(x, irf.reco_energy[i, 2].pdf(x), label=irf.true_energy_bins[i])
ax.legend()
[7]:
<matplotlib.legend.Legend at 0x7f2c5386e760>
../_images/notebooks_detector_model_10_1.png

This should look like the Fig.4, left panel, of the mentioned paper, the y-axis is only scaled by a constant factor, corresponding to a properly normalised distribution. On this topic, it should be mentioned that the quantities distributed according to these histograms are the logarithms of reconstructed energy, PSF and angular uncertainty! Accordingly, logarithmic quantities are drawn as samples and only exponentiated for calculations and final data products.

Etrue vs. Ereco

Below, a colormap of the conditional probability \(P(E_\mathrm{reco} \vert E_\mathrm{true})\) is shown. It is normalised for each Etrue bin.

[8]:
etrue = irf.true_energy_bins
ereco = np.linspace(1, 8, num=100)
[9]:
vals = np.zeros((etrue.size - 1, ereco.size - 1))
for c, et in enumerate(etrue[:-1]):
    vals[c, :] = irf.reco_energy[c, 2].pdf(ereco[:-1])
[10]:
fig, ax = plt.subplots()
h = ax.pcolor(np.power(10, etrue), np.power(10, ereco), vals.T, norm=LogNorm())
cbar = fig.colorbar(h)
ax.set_xlim(1e2, 1e9)
ax.set_ylim(1e1, 1e8)
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel("True energy [GeV]")
ax.set_ylabel("Reconstructed energy [GeV]")
cbar.set_label("P(Ereco|Etrue)")
../_images/notebooks_detector_model_15_0.png

Angular resolution

Now that we have the reconstructed energy for an event with some \(E_\mathrm{true}, \delta\), we can proceed in finding the angular resolution.

First, from the given “history” of the event, we sample the matching distribution/histogram of \(\mathrm{PSF}\), again by marginalising over the uninteresting quantities, in this case only \(\mathrm{ang\_err}\). We thus sample a value of \(\mathrm{PSF}\), to whom a distribution of \(\mathrm{ang\_err}\) belongs, which we subsequently sample. This is now to be understood as a cone of a given angular radius, within which the true arrival direction lies with a probability of 50%.

For both steps, the histograms are created by R2021IRF() when instructed to do so: we pass a tuple of vectors (ra, dec) in radians and a vector of \(\log_{10}(E_\mathrm{true})\) to the method sample(). Returned are sampled ra, dec (both in radians), angular error (68%, in degrees) and reconstructed energy in GeV.

[11]:
irf.sample((np.full(4, np.pi), np.full(4, np.pi / 4)), np.full(4, 2))
[11]:
(array([3.13127578, 3.18734926, 3.14257287, 3.16627535]),
 array([0.78812178, 0.76590965, 0.76784664, 0.76967353]),
 array([1.04465202, 1.51769649, 1.07965682, 1.99991027]),
 array([ 638.45805228, 1580.39738365,  985.09144625,  837.29369041]))

If you are interested in the actual distributions, they are accessible through the attributes R2021IRF().reco_energy (2d numpy array storing scipy.stats.rv_histogram instances) and R2021IRF().maginal_pdf_psf (a modified ditionary class instance, indexed by a chain of keys):

[12]:
etrue_bin = 0
dec_bin = 2
ereco_bin = 10
print(irf.marginal_pdf_psf(etrue_bin, dec_bin, ereco_bin, "bins"))
print(irf.marginal_pdf_psf(etrue_bin, dec_bin, ereco_bin, "pdf"))
[-3.13065092 -2.86582289 -2.60084567 -2.33592241 -2.07109231 -1.80631897
 -1.54136215 -1.27646224 -1.01153023 -0.74666199 -0.48174935 -0.21688286
  0.04805317  0.31281183  0.57772152  0.84267163  1.10754913  1.37235958
  1.63728955  1.90222053  2.1670218 ]
<scipy.stats._continuous_distns.rv_histogram object at 0x7f2c5327f880>

The same works for marginal_pdf_angerr. The entries are only created once the sample() method needs to. See the class defintion of ddict in icecube_tools.utils.data for more details.

Mean angular uncertainty

We can find the mean angular uncertainty by sampling a large amount of events for different true energies, assuming that the average is sensibly defined as the average of logarithmic quantities.

[13]:
num = 10000
loge = irf.true_energy_bins
mean_uncertainty = np.zeros(loge.shape)
for c, e in enumerate(loge[:-1]):
    _, _, samples, _ = irf.sample(
        (np.full(num, np.pi), np.full(num, np.pi / 4)), np.full(num, e)
    )
    mean_uncertainty[c] = np.power(10, np.average(np.log10(samples)))
mean_uncertainty[-1] = mean_uncertainty[-2]
plt.step(loge, mean_uncertainty, where="post")
plt.xlabel("$\log E_\mathrm{true} / \mathrm{GeV}$")
plt.ylabel("mean angular uncertainty [degrees]")
[13]:
Text(0, 0.5, 'mean angular uncertainty [degrees]')
../_images/notebooks_detector_model_22_1.png

Constructing a detector

A detector used for e.g. simulations can be constructed from angular/energy uncertainties and an effective area:

[14]:
detector = IceCube(my_aeff, irf, irf, "IC86_II")

irf = R2021IRF() is used both as spatial and energy resolution, because it encompasses both types of information and inherits from both classes.

Time dependent detector

We can construct a “meta-detector” spanning multiple data periods through the class TimeDependentIceCube from strings defining the data periods. Alternatively, a

[15]:
tic = TimeDependentIceCube.from_periods("IC86_I", "IC86_II")
tic.detectors
/opt/hostedtoolcache/Python/3.9.13/x64/lib/python3.9/site-packages/icecube_tools/detector/r2021.py:48: RuntimeWarning: divide by zero encountered in log10
  self.dataset[:, 6:-1] = np.log10(self.dataset[:, 6:-1])
[15]:
{'IC86_I': <icecube_tools.detector.detector.IceCube at 0x7f2c53758b80>,
 'IC86_II': <icecube_tools.detector.detector.IceCube at 0x7f2c50375ac0>}

Available periods are

[16]:
TimeDependentIceCube._available_periods
[16]:
['IC40', 'IC59', 'IC79', 'IC86_I', 'IC86_II']

Effective area, angular resolution and energy resolution of earlier releases

Repeating the prodecure for the 20181018 dataset.

[17]:
from icecube_tools.detector.effective_area import EffectiveArea
from icecube_tools.detector.energy_resolution import EnergyResolution
from icecube_tools.detector.angular_resolution import AngularResolution
[18]:
my_aeff = EffectiveArea.from_dataset("20181018")
my_angres = AngularResolution.from_dataset("20181018")
[19]:
fig, ax = plt.subplots()
h = ax.pcolor(
    my_aeff.true_energy_bins, my_aeff.cos_zenith_bins, my_aeff.values.T, norm=LogNorm()
)
cbar = fig.colorbar(h)
ax.set_xscale("log")
ax.set_xlabel("True energy [GeV]")
ax.set_ylabel("cos(zenith)")
cbar.set_label("Aeff [m^2]")
../_images/notebooks_detector_model_33_0.png
[20]:
fig, ax = plt.subplots()
ax.plot(my_angres.true_energy_values, my_angres.values)
ax.set_xscale("log")
ax.set_xlabel("True energy [GeV]")
ax.set_ylabel("Mean angular error [deg]")
[20]:
Text(0, 0.5, 'Mean angular error [deg]')
../_images/notebooks_detector_model_34_1.png

We can also easily check what datasets are supported by the different detector information classes:

[21]:
EffectiveArea.supported_datasets
[21]:
['20131121', '20150820', '20181018', '20210126']
[22]:
AngularResolution.supported_datasets
[22]:
['20181018']
[23]:
EnergyResolution.supported_datasets
[23]:
['20150820']

If you would like to see some other datasets supported, please feel free to open an issue or contribute your own!

For the 20150820 dataset, for which we also have the energy resolution available…

[24]:
my_aeff = EffectiveArea.from_dataset("20150820")
my_eres = EnergyResolution.from_dataset("20150820")
20150820_Astrophysical_muon_neutrino_flu...: 100%|██████████| 43711022/43711022 [01:42<00:00, 426690.42it/s]
[25]:
fig, ax = plt.subplots()
h = ax.pcolor(
    my_aeff.true_energy_bins, my_aeff.cos_zenith_bins, my_aeff.values.T, norm=LogNorm()
)
cbar = fig.colorbar(h)
ax.set_xscale("log")
ax.set_xlabel("True energy [GeV]")
ax.set_ylabel("cos(zenith)")
cbar.set_label("Aeff [m^2]")
../_images/notebooks_detector_model_41_0.png
[26]:
fig, ax = plt.subplots()
h = ax.pcolor(
    my_eres.true_energy_bins, my_eres.reco_energy_bins, my_eres.values.T, norm=LogNorm()
)
cbar = fig.colorbar(h)
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel("True energy [GeV]")
ax.set_ylabel("Reconstructed energy [GeV]")
cbar.set_label("P(Ereco|Etrue)")
../_images/notebooks_detector_model_42_0.png

Detector model

We can bring together these properties to make a detector model that can be used for simulations.

[27]:
from icecube_tools.detector.detector import IceCube
[28]:
my_detector = IceCube(my_aeff, my_eres, my_angres)
[ ]: