Skip to main content

Pure python library for calculating the weights of Monte Carlo simulation for IceCube.

Project description

pre-commit.ci status tests docs codecov LICENSE PyPi

SimWeights

Pure python library for calculating the weights of Monte Carlo simulation for IceCube.

SimWeights was designed with goal of calculating weights for IceCube simulation in a way that it is easy to combine combine datasets with different generation parameters into a single sample. It was also designed to be a stand alone project which does not depend on IceTray in any way so that it can be installed easily on laptops. SimWeights gathers all the information it needs form information in the hdf5 file so there is no need for access to the simulation production database. SimWeights works with files produced with corsika-reader, neutrino-generator, and genie-reader.

Prerequisites

Required: numpy, scipy

Installation

To install from pypi run:

pip install simweights

Alternatively, if you need to install unreleased code from main you can run:

pip install git+https://github.com/icecube/simweights.git

On certain installs of python on cvmfs you might get the following error: ModuleNotFoundError: No module named 'glob'. If this happens you can add the following option --no-build-isolation to the above command.

If you want to develop simweights you can install directly with flit. The -s option will symlink the module into site-packages rather than copying it, so that you can test changes without reinstalling the module:

pip install flit
git clone git@github.com:icecube/simweights.git
cd simweights
flit install [--user] -s

Basic Usage

For triggered CORSIKA or CORSIKA produced by corsika-reader with S-Frames files use CorsikaWeighter() without any additional arguments:

>>> import simweights, pandas
>>> simfile = pandas.HDFStore("Level2_IC86.2016_corsika.021889.000000.hdf5", "r")
>>> flux_model = simweights.GaisserH4a()
>>> weight_obj = simweights.CorsikaWeighter(simfile)
>>> weights = weight_obj.get_weights(flux_model)
>>> print(f"Rate = {weights.sum():5.2f} Hz")
Rate = 122.84 Hz

The value returned by get_weights() is the rate of events in Hz

For traditional CORSIKA files made with corsika-reader you will also use simweights.CorsikaWeighter(), but you need to know the number of .i3 files that contributed to create this hdf5 file and pass it as the nfiles parameter.

For neutrino-generator you can use NuGenWeighter() which also requires you to know the number of files. Flux models from nuflux can be used:

>>> import nuflux
>>> simfile = pandas.HDFStore("Level2_IC86.2016_NuMu.020878.000000.hdf5")
>>> flux_model = nuflux.makeFlux("CORSIKA_GaisserH3a_QGSJET-II")
>>> weight_obj = simweights.NuGenWeighter(simfile, nfiles=1)
>>> weights = weight_obj.get_weights(flux_model)
>>> print(f"Rate = {weights.sum():5.2e} Hz")
Rate = 1.41e-02 Hz

To weight a spectrum with a function you can also pass a callable to get_weights()

>>> weights = weight_obj.get_weights(lambda energy: 7.2e-8 * energy**-2.2)
>>> print(f"Rate = {weights.sum():5.2e} Hz")
Rate = 2.34e-05 Hz

You can also pass flux values as a numpy array with the same length as the sample

>>> fluxes = 7.2e-8 * simfile["I3MCWeightDict"]["PrimaryNeutrinoEnergy"] ** -2.2
>>> weights = weight_obj.get_weights(fluxes)
>>> print(f"Rate = {weights.sum():5.2e} Hz")
Rate = 2.34e-05 Hz

You can also pass a scalar to weight all events with the same flux. Passing a value of 1.0 will result in the well known quantity OneWeight divided by the number of events.

>>> OneWeight = weight_obj.get_weights(1.0)
>>> OldOneWeight = simfile["I3MCWeightDict"]["OneWeight"] / (simfile["I3MCWeightDict"]["NEvents"] / 2)
>>> (OneWeight - OldOneWeight).median()
0.0

Simulation created with genie-reader can be weighted with GenieWeighter():

>>> simfile = pandas.HDFStore("genie_reader_NuE.hdf5")
>>> flux_model = nuflux.makeFlux("IPhonda2014_spl_solmax")
>>> weight_obj = simweights.GenieWeighter(simfile)
>>> weights = weight_obj.get_weights(flux_model)
>>> print(f"Rate = {weights.sum():5.2e} Hz")
Rate = 3.78e+00 Hz

Also note that these examples use pandas. SimWeights will work equally well with pandas, h5py, or pytables.

Documentation

Full documentation is available on the IceCube Documentation Server.

Getting Help

Please direct any questions to @kjm on the slack channel #software.

Contributing

See the contributing guide

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simweights-0.1.2.tar.gz (301.2 kB view details)

Uploaded Source

Built Distribution

simweights-0.1.2-py3-none-any.whl (118.5 kB view details)

Uploaded Python 3

File details

Details for the file simweights-0.1.2.tar.gz.

File metadata

  • Download URL: simweights-0.1.2.tar.gz
  • Upload date:
  • Size: 301.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for simweights-0.1.2.tar.gz
Algorithm Hash digest
SHA256 179997c397e45eda67f6c881ec229e48be5dd6a3060d3ae5ced861319b90eec1
MD5 f5cc859e2ce3e4b44e79335588dfde2e
BLAKE2b-256 1718130e1a94cc413c60461c723b7d61dc0d153910bf89446581a30ab2d44310

See more details on using hashes here.

File details

Details for the file simweights-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: simweights-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 118.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for simweights-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 290f36c93966cf5febad52977e36518195770f55a7462d52ecd4dd27b14af117
MD5 3aabd25a00216a125479425ead0e60c4
BLAKE2b-256 92492a41957905987bd744736adf7ff1e7d70a094a65d11a9da6c6a54ca5b887

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page