-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
222 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# SPDX-FileCopyrightText: © 2022 the SimWeights contributors | ||
# | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
import nuflux | ||
import numpy as np | ||
import pandas as pd | ||
|
||
import simweights | ||
|
||
simfile = pd.HDFStore("level2_genie-icetray.140000_000000.hdf") | ||
flux_model = nuflux.makeFlux("IPhonda2014_spl_solmax") | ||
weight_obj = simweights.GenieWeighter(simfile, 1) | ||
weights = weight_obj.get_weights(flux_model) | ||
print("Rate from simweights", weights.sum(), "Hz") | ||
simfile.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/usr/bin/env python | ||
|
||
# SPDX-FileCopyrightText: © 2022 the SimWeights contributors | ||
# | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
import os | ||
import sys | ||
from pathlib import Path | ||
|
||
import h5py | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
import tables | ||
|
||
from simweights import GenieWeighter | ||
|
||
datasets = [ | ||
"genie-icetray.140000A_000000.hdf", | ||
"genie-icetray.140000B_000000.hdf", | ||
"genie-icetray.140000C_000000.hdf", | ||
"genie-icetray.140000D_000000.hdf", | ||
"level2_genie-icetray.140000_000000.hdf", | ||
] | ||
approx = pytest.approx | ||
datadir = os.environ.get("SIMWEIGHTS_TESTDATA", None) | ||
|
||
@pytest.mark.parametrize("fname", datasets) | ||
@pytest.mark.skipif(not datadir, reason="environment variable SIMWEIGHTS_TESTDATA not set") | ||
def test_dataset(fname): | ||
filename = Path(datadir) / fname | ||
reffile = h5py.File(str(filename), "r") | ||
|
||
wd = reffile["I3MCWeightDict"] | ||
grd = reffile["I3GENIEResultDict"] | ||
pdgid = grd["neu"] | ||
emin, emax = 10**wd["MinEnergyLog"], 10**wd["MaxEnergyLog"] | ||
|
||
solid_angle = 2 * np.pi * (np.cos(wd["MinZenith"]) - np.cos(wd["MaxZenith"])) | ||
injection_area_cm = 1e4 * np.pi * wd["InjectionSurfaceR"]**2 | ||
total_weight = wd["TotalInteractionProbabilityWeight"] | ||
|
||
type_weight = np.empty_like(wd["OneWeight"]) | ||
type_weight[pdgid>0] = 0.7 | ||
type_weight[pdgid<0] = 0.3 | ||
w0 = wd["OneWeight"] / (wd["NEvents"] * type_weight) | ||
|
||
fobjs = [ | ||
tables.open_file(str(filename), "r"), | ||
pd.HDFStore(str(filename), "r"), | ||
] | ||
|
||
for fobj in fobjs: | ||
w = GenieWeighter(fobj, nfiles=1) | ||
|
||
event_weight = w.get_weight_column("wght") | ||
assert event_weight == approx(total_weight) | ||
|
||
for particle in np.unique(pdgid): | ||
for spectrum in w.surface.spectra[particle]: | ||
power_min, power_max = spectrum.dists[1].a, spectrum.dists[1].b | ||
event_mask = (pdgid == particle) & (emin == power_min) & (emax == power_max) | ||
|
||
power_law = spectrum.dists[1] | ||
energy_factor = 1 / power_law.pdf(w.get_weight_column("energy")) | ||
|
||
one_weight = (w.get_weight_column("wght")[event_mask] | ||
* energy_factor[event_mask] | ||
* solid_angle[event_mask] | ||
* injection_area_cm[event_mask]) | ||
|
||
assert one_weight == approx(wd["OneWeight"][event_mask]) | ||
|
||
assert w0 == approx(w.get_weights(1), rel=1e-5) | ||
fobj.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(pytest.main(["-v", __file__, *sys.argv[1:]])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#!/usr/bin/env python | ||
|
||
# SPDX-FileCopyrightText: © 2022 the SimWeights contributors | ||
# | ||
# SPDX-License-Identifier: BSD-2-Clause | ||
|
||
import unittest | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
from simweights import CircleInjector, GenieWeighter, PowerLaw | ||
|
||
mcwd_keys = [ | ||
"NEvents", | ||
"MinZenith", | ||
"MaxZenith", | ||
"PowerLawIndex", | ||
"MinEnergyLog", | ||
"MaxEnergyLog", | ||
"InjectionSurfaceR", | ||
"PrimaryNeutrinoEnergy", | ||
"GeneratorVolume", | ||
] | ||
|
||
grd_keys = [ | ||
"neu", | ||
"pxv", | ||
"pyv", | ||
"pzv", | ||
"Ev", | ||
"wght", | ||
"_glbprbscale"] | ||
|
||
|
||
def make_new_table(pdgid, nevents, spatial, spectrum, coszen): | ||
dtype = [(k, float) for k in mcwd_keys] | ||
weight = np.zeros(nevents, dtype=dtype) | ||
weight["NEvents"] = nevents | ||
weight["MinZenith"] = np.arccos(spatial.cos_zen_max) | ||
weight["MaxZenith"] = np.arccos(spatial.cos_zen_min) | ||
weight["PowerLawIndex"] = -1 * spectrum.g | ||
weight["MinEnergyLog"] = np.log10(spectrum.a) | ||
weight["MaxEnergyLog"] = np.log10(spectrum.b) | ||
weight["InjectionSurfaceR"] = spatial.radius | ||
weight["GeneratorVolume"] = 1. | ||
weight["PrimaryNeutrinoEnergy"] = spectrum.ppf(np.linspace(0, 1, nevents, endpoint=False)) | ||
|
||
dtype = [(k, float) for k in grd_keys] | ||
resultdict = np.zeros(nevents, dtype=dtype) | ||
resultdict["neu"] = pdgid | ||
resultdict["pxv"] = 1 | ||
resultdict["pyv"] = 1 | ||
resultdict["pzv"] = -coszen | ||
resultdict["Ev"] = weight["PrimaryNeutrinoEnergy"] | ||
resultdict["wght"] = 1.0 | ||
resultdict["_glbprbscale"] = 1.0 | ||
|
||
return weight, resultdict | ||
|
||
|
||
class TestGenieIcetrayWeighter(unittest.TestCase): | ||
def test_genie_icetray(self): | ||
nevents = 100000 | ||
coszen = 0.7 | ||
pdgid = 12 | ||
c1 = CircleInjector(300, 0, 1) | ||
p1 = PowerLaw(0, 1e3, 1e4) | ||
|
||
t1 = make_new_table(pdgid, nevents, c1, p1, coszen) | ||
|
||
mcwd = pd.DataFrame(t1[0]) | ||
grd = pd.DataFrame(t1[1]) | ||
|
||
f1 = {"I3MCWeightDict": mcwd, | ||
"I3GENIEResultDict": grd} | ||
|
||
for nfiles in [1, 10, 100]: | ||
wf = GenieWeighter(f1, nfiles=nfiles) | ||
for flux in [1e-6, 1, 1e6]: | ||
w1 = wf.get_weights(flux) | ||
w2 = flux * p1.integral * c1.etendue / (0.7 * nfiles) | ||
np.testing.assert_allclose( | ||
w1.sum(), | ||
w2 | ||
) | ||
E = mcwd["PrimaryNeutrinoEnergy"] | ||
y, x = np.histogram(E, weights=w1, bins=51, range=[p1.a, p1.b]) | ||
Ewidth = np.ediff1d(x) | ||
np.testing.assert_allclose(y, flux * Ewidth * c1.etendue / (0.7 * nfiles), 6e-3) | ||
|
||
def test_empty(self): | ||
with self.assertRaises(RuntimeError): | ||
x = {"I3MCWeightDict": {key:[] for key in mcwd_keys}, | ||
"I3GENIEResultDict": {key:[] for key in grd_keys}} | ||
GenieWeighter(x, nfiles=1) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |