Metadata-Version: 2.2
Name: LeptonInjector
Version: 1.1.0
Summary: Generates neutrino events for large volume Cherenkov telescopes
Author: IceCube Collaboration
License:                    GNU LESSER GENERAL PUBLIC LICENSE
                                Version 3, 29 June 2007
         
          Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
          Everyone is permitted to copy and distribute verbatim copies
          of this license document, but changing it is not allowed.
         
         
           This version of the GNU Lesser General Public License incorporates
         the terms and conditions of version 3 of the GNU General Public
         License, supplemented by the additional permissions listed below.
         
           0. Additional Definitions.
         
           As used herein, "this License" refers to version 3 of the GNU Lesser
         General Public License, and the "GNU GPL" refers to version 3 of the GNU
         General Public License.
         
           "The Library" refers to a covered work governed by this License,
         other than an Application or a Combined Work as defined below.
         
           An "Application" is any work that makes use of an interface provided
         by the Library, but which is not otherwise based on the Library.
         Defining a subclass of a class defined by the Library is deemed a mode
         of using an interface provided by the Library.
         
           A "Combined Work" is a work produced by combining or linking an
         Application with the Library.  The particular version of the Library
         with which the Combined Work was made is also called the "Linked
         Version".
         
           The "Minimal Corresponding Source" for a Combined Work means the
         Corresponding Source for the Combined Work, excluding any source code
         for portions of the Combined Work that, considered in isolation, are
         based on the Application, and not on the Linked Version.
         
           The "Corresponding Application Code" for a Combined Work means the
         object code and/or source code for the Application, including any data
         and utility programs needed for reproducing the Combined Work from the
         Application, but excluding the System Libraries of the Combined Work.
         
           1. Exception to Section 3 of the GNU GPL.
         
           You may convey a covered work under sections 3 and 4 of this License
         without being bound by section 3 of the GNU GPL.
         
           2. Conveying Modified Versions.
         
           If you modify a copy of the Library, and, in your modifications, a
         facility refers to a function or data to be supplied by an Application
         that uses the facility (other than as an argument passed when the
         facility is invoked), then you may convey a copy of the modified
         version:
         
            a) under this License, provided that you make a good faith effort to
            ensure that, in the event an Application does not supply the
            function or data, the facility still operates, and performs
            whatever part of its purpose remains meaningful, or
         
            b) under the GNU GPL, with none of the additional permissions of
            this License applicable to that copy.
         
           3. Object Code Incorporating Material from Library Header Files.
         
           The object code form of an Application may incorporate material from
         a header file that is part of the Library.  You may convey such object
         code under terms of your choice, provided that, if the incorporated
         material is not limited to numerical parameters, data structure
         layouts and accessors, or small macros, inline functions and templates
         (ten or fewer lines in length), you do both of the following:
         
            a) Give prominent notice with each copy of the object code that the
            Library is used in it and that the Library and its use are
            covered by this License.
         
            b) Accompany the object code with a copy of the GNU GPL and this license
            document.
         
           4. Combined Works.
         
           You may convey a Combined Work under terms of your choice that,
         taken together, effectively do not restrict modification of the
         portions of the Library contained in the Combined Work and reverse
         engineering for debugging such modifications, if you also do each of
         the following:
         
            a) Give prominent notice with each copy of the Combined Work that
            the Library is used in it and that the Library and its use are
            covered by this License.
         
            b) Accompany the Combined Work with a copy of the GNU GPL and this license
            document.
         
            c) For a Combined Work that displays copyright notices during
            execution, include the copyright notice for the Library among
            these notices, as well as a reference directing the user to the
            copies of the GNU GPL and this license document.
         
            d) Do one of the following:
         
                0) Convey the Minimal Corresponding Source under the terms of this
                License, and the Corresponding Application Code in a form
                suitable for, and under terms that permit, the user to
                recombine or relink the Application with a modified version of
                the Linked Version to produce a modified Combined Work, in the
                manner specified by section 6 of the GNU GPL for conveying
                Corresponding Source.
         
                1) Use a suitable shared library mechanism for linking with the
                Library.  A suitable mechanism is one that (a) uses at run time
                a copy of the Library already present on the user's computer
                system, and (b) will operate properly with a modified version
                of the Library that is interface-compatible with the Linked
                Version.
         
            e) Provide Installation Information, but only if you would otherwise
            be required to provide such information under section 6 of the
            GNU GPL, and only to the extent that such information is
            necessary to install and execute a modified version of the
            Combined Work produced by recombining or relinking the
            Application with a modified version of the Linked Version. (If
            you use option 4d0, the Installation Information must accompany
            the Minimal Corresponding Source and Corresponding Application
            Code. If you use option 4d1, you must provide the Installation
            Information in the manner specified by section 6 of the GNU GPL
            for conveying Corresponding Source.)
         
           5. Combined Libraries.
         
           You may place library facilities that are a work based on the
         Library side by side in a single library together with other library
         facilities that are not Applications and are not covered by this
         License, and convey such a combined library under terms of your
         choice, if you do both of the following:
         
            a) Accompany the combined library with a copy of the same work based
            on the Library, uncombined with any other library facilities,
            conveyed under the terms of this License.
         
            b) Give prominent notice with the combined library that part of it
            is a work based on the Library, and explaining where to find the
            accompanying uncombined form of the same work.
         
           6. Revised Versions of the GNU Lesser General Public License.
         
           The Free Software Foundation may publish revised and/or new versions
         of the GNU Lesser General Public License from time to time. Such new
         versions will be similar in spirit to the present version, but may
         differ in detail to address new problems or concerns.
         
           Each version is given a distinguishing version number. If the
         Library as you received it specifies that a certain numbered version
         of the GNU Lesser General Public License "or any later version"
         applies to it, you have the option of following the terms and
         conditions either of that published version or of any later version
         published by the Free Software Foundation. If the Library as you
         received it does not specify a version number of the GNU Lesser
         General Public License, you may choose any version of the GNU Lesser
         General Public License ever published by the Free Software Foundation.
         
           If the Library as you received it specifies that a proxy can decide
         whether future versions of the GNU Lesser General Public License shall
         apply, that proxy's public statement of acceptance of any version is
         permanent authorization for you to choose that version for the
         Library.
         
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: C++
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.8
Requires-Dist: numpy
Provides-Extra: hdf5
Requires-Dist: h5py; extra == "hdf5"
Description-Content-Type: text/markdown

# LeptonInjector

LeptonInjector is a group of modules used to create events in IceCube. This code represents a standalone version of the original LeptonInjector which has been trimmed of the proprietary Icetray dependencies. It is currently fully functional and compatible with LeptonWeighter. 

To use it, you will

    1. Prepare a Injector object (or list of Injector objects).

    2. Use one injector object, along with several generation parameters, to create a Controller object. These were called MultiLeptonInjector in the original code. 

    3. Add more injectors to the controller object using the add injector function. Verify that the controller is properly configured.
    
    4. Specify the full paths and names of the destination output file and LeptonInjector Configuration (LIC) file.

    5. Call Controller.Execute(). This will run the simulation. 

For an example of this in action, see $root/resources/example/inject_muons.py

To learn about the LIC files and weighting, see https://github.com/IceCubeOpenSource/LeptonWeighter

# Installation

## Quick Start: pip install (Recommended)

The easiest way to install LeptonInjector is via pip:

```bash
pip install .
```

This will automatically:
- Build the C++ library with pybind11 bindings
- Install all Python dependencies
- Set up the package for immediate use

### Requirements for pip install

- Python >= 3.8
- A C++ compiler with C++17 support
- CMake >= 3.15
- The following libraries must be installed on your system:
  - HDF5 C libraries
  - Photospline (https://github.com/IceCubeOpenSource/photospline)
  - Boost (headers only)

On **macOS** with Homebrew:
```bash
brew install hdf5 boost cmake
# Install photospline separately following its documentation
```

On **Ubuntu/Debian**:
```bash
sudo apt-get install libhdf5-dev libboost-all-dev cmake
# Install photospline separately following its documentation
```

### Optional dependencies

For HDF5 file handling in Python:
```bash
pip install ".[hdf5]"
```

## Python Usage

Once installed, you can import and use LeptonInjector directly:

```python
import LeptonInjector as LI

# Create an injector for muon neutrino charged-current interactions
injector = LI.Injector(
    NEvents=1000,
    FinalType1=LI.Particle.ParticleType.MuMinus,
    FinalType2=LI.Particle.ParticleType.Hadrons,
    DoublyDifferentialCrossSectionFile="path/to/differential_xs.fits",
    TotalCrossSectionFile="path/to/total_xs.fits",
    Ranged=True
)

# Create a controller to manage the simulation
controller = LI.Controller(
    injectors=injector,
    minimum_energy=1e2,  # GeV
    maximum_energy=1e6,  # GeV
    spectral_index=2.0,
    minimum_azimuth=0,
    maximum_azimuth=2*LI.Constants.pi,
    minimum_zenith=0,
    maximum_zenith=LI.Constants.pi
)

# Configure output files
controller.NameOutfile("output.h5")
controller.NameLicFile("config.lic")

# Run the simulation
controller.Execute()
```

### Earth Model Services

LeptonInjector includes Earth model services for density calculations:

```python
import LeptonInjector as LI

# Create an Earth model service with PREM
ems = LI.EarthModelService(
    "mymodel",
    tablepath="/path/to/earthparams/",
    earthmodels=["PREM_mmc.dat"],
    materialmodels=["Standard.dat"],
    icecapname="SimpleIceCap",
    icecapangle=20.0 * LI.Constants.degrees,
    detectordepth=1948.0 * LI.Constants.m
)

# Get density at a position (detector-centered coordinates)
pos = LI.LI_Position(0, 0, -1000)  # 1km below detector center
density = ems.GetDensityInCGS(pos)
print(f"Density: {density} g/cm^3")

# Convert to Earth-centered coordinates
earth_pos = ems.GetEarthCoordPosFromDetCoordPos(pos)

# Get the medium type at that position
medium = ems.GetMedium(earth_pos)
print(f"Medium: {LI.EarthModelService.GetMediumTypeString(medium)}")

# Calculate column depth between two points
pos1 = LI.LI_Position(0, 0, 0)
pos2 = LI.LI_Position(0, 0, -10000)
column_depth = ems.GetColumnDepthInCGS(pos1, pos2)
print(f"Column depth: {column_depth} g/cm^2")

# Get lepton range
direction = LI.LI_Direction(LI.Constants.pi, 0)  # straight down
energy = 1e5  # 100 TeV
muon_range = ems.GetLeptonRangeInMeterFrom(pos, direction, energy, isTau=False)
print(f"Muon range: {muon_range} m")
```

### Available Earth Model Enums

```python
# Medium types
LI.MediumType.INNERCORE
LI.MediumType.OUTERCORE
LI.MediumType.MANTLE
LI.MediumType.ROCK
LI.MediumType.ICE
LI.MediumType.WATER
LI.MediumType.AIR
LI.MediumType.VACUUM

# Ice cap types
LI.IceCapType.NOICE
LI.IceCapType.ICESHEET
LI.IceCapType.SIMPLEICECAP

# Integration types for density integration
LI.IntegType.PATH    # Along a path
LI.IntegType.RADIUS  # Projected on radial direction
LI.IntegType.CIRCLE  # 2*pi*r weighted
LI.IntegType.SPHERE  # 4*pi*r^2 weighted (volume mass)

# Lepton range calculation options
LI.LeptonRangeOption.DEFAULT  # Dima's fitting function
LI.LeptonRangeOption.LEGACY   # Legacy NuGen equation
LI.LeptonRangeOption.NUSIM    # Gary's fitting function
```

### Utility Functions

```python
# Geometry calculations
impact, t, closest_pos = LI.GetImpactParameter(position, direction)
n_intersections, start_pos, end_pos = LI.GetIntersectionsWithSphere(pos, dir, radius)
n_intersections, enter_dist, exit_dist = LI.GetDistsToIntersectionsWithSphere(pos, dir, radius)

# Lepton range in meter-water-equivalent
range_mwe = LI.GetLeptonRange(energy, isTau=False)

# Unit conversions
mwe = LI.ColumnDepthCGStoMWE(column_depth_cgs)
cgs = LI.MWEtoColumnDepthCGS(range_mwe)
```

---

# Dependencies (for manual installation)

All of the dependencies are already installed on the CVMFS environments on the IceCube Cobalt testbeds.

For local installations, you need the following:

* A C++ compiler with C++17 support.

* The `HDF5` C libraries. Read more about it here: https://portal.hdfgroup.org/display/support. These libraries are, of course, used to save the data files.

* It also requires Photospline to create and to read cross sections. Read more about it, and its installation at https://github.com/IceCubeOpenSource/photospline. Note that Photospline has dependencies that you will need that are not listed here.

* LeptonInjector requires Photospline's `SuiteSparse` capabilities, whose dependencies are available here http://faculty.cse.tamu.edu/davis/suitesparse.html

For building py-bindings:

* Python >= 3.8

* `pybind11` (used for the pip-installable Python bindings)

* `BOOST` headers (for some internal functionality)


# Included Dependencies

These are not ostensibly a part of LeptonInjector, but are included for its functionality. They were developed by the IceCube Collaboration and modified slightly to use the LeptonInjector datatypes instead of the IceCube proprietary ones. 

* I3CrossSections: provies the tools for sampling DIS and GR cross sections. 

* Earthmodel Services: provides the PREM for column depth calculations. 

# Manual Compilation with CMake (Alternative)

If you prefer to build manually or need more control over the build process, you can use CMake directly.

First, clone the repository:

```bash
git clone git@github.com:IceCubeOpenSource/LeptonInjector.git
cd LeptonInjector
```

Create build and install directories:

```bash
mkdir build install
cd build
```

Configure with CMake:

```bash
cmake -DCMAKE_INSTALL_PREFIX=../install ..
```

Build and install:

```bash
make -j4 && make install
```

Set up environment variables (add to your `.bashrc` or `.bash_profile`):

```bash
# Allow Python to find the installed library
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/your/install/path/

# Allow the EarthModel to find Earth parameter files
export EARTH_PARAMS=/path/to/LeptonInjector/resources/earthparams/
```

**Note:** When using pip install, these environment variables are not needed as the package is installed into your Python environment and resources are bundled with the package.

# Structure
The code base is divided into several files. 
* Constants: a header defining various constants. 
* Controller: implements a class for managing the simulation
* DataWriter: writes event properties and MCTrees to an HDF5 file
* EventProps: implements a few structures used to write events in the hdf5 file. 
* h5write: may be renamed soon. This will be used to write the configurations onto a file
* LeptonInjector (the file): defines the Injector objects described above in addition to several configuration objects and event generators 
* Particle: simple implementation of particles. Includes a big enum. 
* Random: object for random number sampling.

# Cross Sections
For generating events you will need fits files of splines specifying the cross sections (total and differential cross sections). These should be made with photospline. 

# Making Contributions
If you would like to make contributions to this project, please create a branch off of the `master` branch and name it something following the template: `$YourLastName/$YourSubProject`. 
Work on this branch until you have made the changes you wished to see and your branch is _stable._ 
Then, pull from master, and create a pull request to merge your branch back into master. 

# Detailed Author Contributions and Citation

The LeptonInjector and LeptonWeighter modules were motivated by the high-energy light sterile neutrino search performed by B. Jones and C. Argüelles. C. Weaver wrote the first implementation of LeptonInjector using the IceCube internal software framework, icetray, and wrote the specifications for LeptonWeighter. In doing so, he also significantly enhanced the functionality of IceCube's Earth-model service. These weighting specifications were turned into code by C. Argüelles in LeptonWeighter. B. Jones performed the first detailed Monte Carlo comparisons that showed that this code had similar performance to the standard IceCube neutrino generator at the time for throughgoing muon neutrinos.

It was realized that these codes could have use beyond IceCube and could benefit the broader neutrino community. The codes were copied from IceCube internal subversion repositories to this GitHub repository; unfortunately, the code commit history was not preserved in this process. Thus the current commits do not represent the contributions from the original authors, particularly from the initial work by C. Weaver and C. Argüelles. 

The transition to this public version of the code has been spearheaded by A. Schneider and B. Smithers, with significant input and contributions from C. Weaver and C. Argüelles. B. Smithers isolated the components of the code needed to make the code public, edited the examples, and improved the interface of the code. A. Schneider contributed to improving the weighting algorithm, particularly to making it work for volume mode cascades, as well as in writing the general weighting formalism that enables joint weighting of volume and range mode.

This project also received contributions and suggestions from internal IceCube reviewers and the collaboration as a whole. Please cite this work as:

LeptonInjector and LeptonWeighter: A neutrino event generator and weighter for neutrino observatories
IceCube Collaboration
https://arxiv.org/abs/2012.10449

## CRediT

**Austin Schneider**: Software, Validation, Writing - Original Draft, Writing - Review & Editing;
**Benjamin Jones**: Conceptualization, Validation;
**Benjamin Smithers**: Software, Validation, Writing - Original Draft, Visualization, Writing - Review & Editing;
**Carlos Argüelles**: Conceptualization, Software, Writing - Original Draft, Writing - Review & Editing, Supervision;
**Chris Weaver**: Methodology, Software, Writing - Review & Editing
