{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example 4 Getting PPPC4DMID Data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Poor Particle Physicists' Cookbook for Dark Matter Indirect Detection (PPPC4DMID) [[1]](#cite_PPPC), provides a table of fluxes at production for dark matter annihilation into all Standard Model fundamental particles [[2]](#cite_weakCorrections),  which then subsequently decay promptly into stable particles. We provide the spectra of produced $e^+e^-$ and photons for use as inputs into DarkHistory. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Notebook Initialization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "import sys\n",
    "sys.path.append(\"..\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%autoreload\n",
    "\n",
    "import matplotlib\n",
    "matplotlib.rc_file('matplotlibrc')\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import numpy as np\n",
    "import darkhistory.spec.pppc as pppc\n",
    "from darkhistory.config import load_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Spectra"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, we define the abscissa at which we want to obtain the spectrum. We shall use the default abscissa used in DarkHistory that is required for use with the provided transfer functions. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "binning = load_data('binning')\n",
    "\n",
    "photeng = binning['phot']\n",
    "eleceng = binning['elec']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we call the function [*pppc.get_pppc_spec()*](https://darkhistory.readthedocs.io/en/latest/_autosummary/darkhistory/spec/pppc/darkhistory.spec.pppc.get_pppc_spec.html). We have to specify: the dark matter mass $m_\\text{DM}$ (in eV), the channel it promptly annihilates/decays into (e.g. $\\chi \\chi \\to b \\bar{b}$), the stable, secondary particle that we are interested in (either `phot` for photons or `elec` for $e^+e^-$), and whether we are interested in annihilation or decay (set by the `decay` flag). \n",
    "\n",
    "The user should refer to the documentation for a full list of allowed channels. \n",
    "\n",
    "The output is a `Spectrum` object (see Ex. 2 for how to use these objects) containing the spectrum as a function of kinetic energy. \n",
    "\n",
    "Here, we'll choose 100 GeV dark matter annihilating into $\\tau^+ \\tau^-$, and obtain both the photon and electron spectrum produced per annihilation. See the *Channels* section for more information on selecting other channels. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mDM = 1e11\n",
    "chan = 'tau'\n",
    "\n",
    "phot_spec = pppc.get_pppc_spec(mDM, photeng, chan, 'phot', decay=False)\n",
    "elec_spec = pppc.get_pppc_spec(mDM, eleceng, chan, 'elec', decay=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And a plot of the spectra. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "\n",
    "plt.loglog()\n",
    "\n",
    "phot_plot, = plt.plot(photeng, photeng*phot_spec.dNdE, label='Photons')\n",
    "elec_plot, = plt.plot(eleceng, eleceng*elec_spec.dNdE, label=r'Electrons + Positrons')\n",
    "\n",
    "plt.xlabel('Energy [eV]')\n",
    "plt.ylabel(r'$E \\, dN/dE$')\n",
    "plt.title(r'\\bf{Secondary Spectra}')\n",
    "\n",
    "leg = plt.legend(handles=[phot_plot, elec_plot],loc=2)\n",
    "leg.set_title(r'$\\chi \\chi \\to \\tau^+ \\tau^-$, $m_\\chi$ = 100 GeV')\n",
    "\n",
    "\n",
    "plt.axis([1e-1, 5e12, 1e-6, 0.3e3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below we compute the fraction of the mass-energy of dark matter deposited into each channel. These fractions agree very well with the result obtained by numerically integrating the PPPC4DMID Mathematica interpolation tables appropriately."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Fraction of energy in photons:   ', phot_spec.toteng()/(2*mDM)) \n",
    "print('Fraction of energy in e+e-:      ', elec_spec.toteng()/(2*mDM))\n",
    "print('Fraction of energy in EM states: ', phot_spec.toteng()/(2*mDM) + elec_spec.toteng()/(2*mDM))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Interpolation Details"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The interpolation goes from $m_\\text{DM} =$ 5 GeV to 100 TeV, and $E/m_{\\text{DM}} = 10^{-9}$ to $E/m_{\\text{DM}} = 1$, where $E$ is the kinetic energy. Outside of this range, we set the interpolation output to a negligible but non-zero value for ease of log interpolation.\n",
    "\n",
    "If the dark matter mass lies below the mass threshold of the products, a spectrum of zeroes is automatically returned. There is a small range of masses between some of the thresholds and the first DM mass data point where the interpolator will return an error, although this can easily be fixed in `darkhistory.spec.pppc`  if an extrapolation is desired instead. \n",
    "\n",
    "We use piecewise cubic Hermite interpolating polynomials (PCHIP) to interpolate first along $m_\\text{DM}$ and then along $E/m_\\text{DM}$. This is not exactly the same method as in the PPPC4DMID Mathematica notebook, which uses a 2D version of this interpolation method. This may result in significant differences, especially in very peaky spectra. We emphasize however that this is merely a difference in interpolation choice. \n",
    "\n",
    "The interpolator tries its best to maintain energy conservation even when the binning provided is very coarse. This is done by always interpolating over a very fine grid (at least 50,000 points in the interpolation range), and then reassigning the particles into the coarser binning in a way that conserves total number and total energy through the function `Spectrum.rebin()` (see Ex. 2). "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bibliography"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[1]<a id='cite_PPPC'></a> M.Cirelli, G.Corcella, A.Hektor, G.Hütsi, M.Kadastik, P.Panci, M.Raidal, F.Sala, A.Strumia, \"PPPC 4 DM ID: A Poor Particle Physicist Cookbook for Dark Matter Indirect Detection,\" arXiv 1012.4515, JCAP 1103 (2011) 051. Erratum: JCAP 1210 (2012) E01.\n",
    "\n",
    "[2]<a id='cite_weakCorrections'></a> P. Ciafaloni, D. Comelli, A. Riotto, F. Sala, A. Strumia, A. Urbano, \"Weak corrections are relevant for dark matter indirect detection,\" arXiv 1009.0224, JCAP 1103 (2011) 019 "
   ]
  }
 ],
 "metadata": {
  "cite2c": {
   "citations": {
    "6232408/DX4ASGRC": {
     "DOI": "10.1088/1475-7516/2011/03/019",
     "URL": "https://doi.org/10.1088%2F1475-7516%2F2011%2F03%2F019",
     "abstract": "The computation of the energy spectra of Standard Model particles originated from the annihilation/decay of dark matter particles is of primary importance in indirect searches of dark matter. We compute how the inclusion of electroweak corrections significantly alter such spectra when the mass M of dark matter particles is larger than the electroweak scale: soft electroweak gauge bosons are copiously radiated opening new channels in the final states which otherwise would be forbidden if such corrections are neglected. All stable particles are therefore present in the final spectrum, independently of the primary channel of dark matter annihilation/decay. Such corrections are model-independent.",
     "accessed": {
      "day": 15,
      "month": 3,
      "year": 2019
     },
     "author": [
      {
       "family": "Ciafaloni",
       "given": "Paolo"
      },
      {
       "family": "Comelli",
       "given": "Denis"
      },
      {
       "family": "Riotto",
       "given": "Antonio"
      },
      {
       "family": "Sala",
       "given": "Filippo"
      },
      {
       "family": "Strumia",
       "given": "Alessandro"
      },
      {
       "family": "Urbano",
       "given": "Alfredo"
      }
     ],
     "container-title": "Journal of Cosmology and Astroparticle Physics",
     "container-title-short": "J. Cosmol. Astropart. Phys.",
     "id": "6232408/DX4ASGRC",
     "issue": "03",
     "issued": {
      "month": 3,
      "year": 2011
     },
     "journalAbbreviation": "J. Cosmol. Astropart. Phys.",
     "language": "en",
     "page": "019–019",
     "page-first": "019",
     "title": "Weak corrections are relevant for dark matter indirect detection",
     "type": "article-journal",
     "volume": "2011"
    },
    "6232408/W944VR5Z": {
     "DOI": "10.1088/1475-7516/2011/03/051",
     "URL": "https://doi.org/10.1088%2F1475-7516%2F2011%2F03%2F051",
     "abstract": "We provide ingredients and recipes for computing signals of TeV-scale Dark Matter annihilations and decays in the Galaxy and beyond. For each DM channel, we present the energy spectra of at production, computed by high-statistics simulations. We estimate the Monte Carlo uncertainty by comparing the results yielded by the Pythia and Herwig event generators. We then provide the propagation functions for charged particles in the Galaxy, for several DM distribution profiles and sets of propagation parameters. Propagation of e± is performed with an improved semi-analytic method that takes into account position-dependent energy losses in the Milky Way. Using such propagation functions, we compute the energy spectra of e±,p̄ and d̄ at the location of the Earth. We then present the gamma ray fluxes, both from prompt emission and from Inverse Compton scattering in the galactic halo. Finally, we provide the spectra of extragalactic gamma rays. All results are available in numerical form and ready to be consumed.",
     "accessed": {
      "day": 15,
      "month": 3,
      "year": 2019
     },
     "author": [
      {
       "family": "Cirelli",
       "given": "Marco"
      },
      {
       "family": "Corcella",
       "given": "Gennaro"
      },
      {
       "family": "Hektor",
       "given": "Andi"
      },
      {
       "family": "Hütsi",
       "given": "Gert"
      },
      {
       "family": "Kadastik",
       "given": "Mario"
      },
      {
       "family": "Panci",
       "given": "Paolo"
      },
      {
       "family": "Raidal",
       "given": "Martti"
      },
      {
       "family": "Sala",
       "given": "Filippo"
      },
      {
       "family": "Strumia",
       "given": "Alessandro"
      }
     ],
     "container-title": "Journal of Cosmology and Astroparticle Physics",
     "container-title-short": "J. Cosmol. Astropart. Phys.",
     "id": "6232408/W944VR5Z",
     "issue": "03",
     "issued": {
      "month": 3,
      "year": 2011
     },
     "journalAbbreviation": "J. Cosmol. Astropart. Phys.",
     "language": "en",
     "page": "051–051",
     "page-first": "051",
     "shortTitle": "PPPC 4 DM ID",
     "title": "PPPC 4 DM ID: a poor particle physicist cookbook for dark matter indirect detection",
     "title-short": "PPPC 4 DM ID",
     "type": "article-journal",
     "volume": "2011"
    }
   }
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
