{ "cells": [ { "cell_type": "markdown", "metadata": { "pycharm": { "is_executing": true, "name": "#%% md\n" } }, "source": [ "# Tutorial for *GenMultiDecay* class\n", "This tutorial shows how ``phasespace.fromdecay.GenMultiDecay`` can be used.\n", "\n", "In order to use this functionality, you need to install the extra dependencies, for example through\n", "`pip install phasespace[fromdecay]`.\n", "\n", "This submodule makes it possible for `phasespace` and [`DecayLanguage`](https://github.com/scikit-hep/decaylanguage/) to work together.\n", "More generally, `GenMultiDecay` can also be used as a high-level interface for simulating particles that can decay in multiple different ways." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "# Import libraries\n", "from pprint import pprint\n", "\n", "import zfit\n", "from particle import Particle\n", "from decaylanguage import DecFileParser, DecayChainViewer, DecayChain, DecayMode\n", "import tensorflow as tf\n", "\n", "from phasespace.fromdecay import GenMultiDecay" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Quick Intro to DecayLanguage\n", "DecayLanguage can be used to parse and view .dec files. These files contain information about how a particle decays and with which probability. For more information about DecayLanguage and .dec files, see the [DecayLanguage](https://github.com/scikit-hep/decaylanguage) documentation.\n", "\n", "We will begin by parsing a .dec file using DecayLanguage:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "parser = DecFileParser(\"../tests/fromdecay/example_decays.dec\")\n", "parser.parse()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From the `parser` variable, one can access a certain decay for a particle using `parser.build_decay_chains`. This will be a `dict` that contains all information about how the mother particle, daughter particles etc. decay." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pi0_chain = parser.build_decay_chains(\"pi0\")\n", "pprint(pi0_chain)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This `dict` can also be displayed in a more human-readable way using `DecayChainViewer`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "DecayChainViewer(pi0_chain)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also create a decay using the `DecayChain` and `DecayMode` classes. However, a DecayChain can only contain one chain, i.e., a particle cannot decay in multiple ways." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dplus_decay = DecayMode(1, \"K- pi+ pi+ pi0\", model=\"PHSP\")\n", "pi0_decay = DecayMode(1, \"gamma gamma\")\n", "dplus_single = DecayChain(\"D+\", {\"D+\": dplus_decay, \"pi0\": pi0_decay})\n", "DecayChainViewer(dplus_single.to_dict())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a GenMultiDecay object\n", "A regular `phasespace.GenParticle` instance would not be able to simulate this decay, since the $\\pi^0$ particle can decay in four different ways. However, a `GenMultiDecay` object can be created directly from a DecayLanguage dict:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pi0_decay = GenMultiDecay.from_dict(pi0_chain)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When creating a `GenMultiDecay` object, the DecayLanguage dict is \"unpacked\" into separate GenParticle instances, where each GenParticle instance corresponds to one way that the particle can decay.\n", "\n", "These GenParticle instances and the probabilities of that decay mode can be accessed via `GenMultiDecay.gen_particles`. This is a list of tuples, where the first element in the tuple is the probability and the second element is the GenParticle." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for probability, particle in pi0_decay.gen_particles:\n", " print(\n", " f\"There is a probability of {probability} \"\n", " f\"that pi0 decays into {', '.join(child.name for child in particle.children)}\"\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One can simulate this decay using the `.generate` method, which works the same as the `GenParticle.generate` method.\n", "\n", "When calling the `GenMultiDecay.generate` method, it internally calls the generate method on the of the GenParticle instances in `GenMultiDecay.gen_particles`. The outputs are placed in a list, which is returned." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "weights, events = pi0_decay.generate(n_events=10_000)\n", "print(\"Number of events for each decay mode:\", \", \".join(str(len(w)) for w in weights))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can confirm that the counts above are close to the expected counts based on the probabilities." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Changing mass settings\n", "Since DecayLanguage dicts do not contain any information about the mass of a particle, the `fromdecay` submodule uses the [particle](https://github.com/scikit-hep/particle) package to find the mass of a particle based on its name.\n", "The mass can either be a constant value or a function (besides the top particle, which is always a constant).\n", "These settings can be modified by passing in additional parameters to `GenMultiDecay.from_dict`.\n", "There are two optional parameters that can be passed to `GenMultiDecay.from_dict`: `tolerance` and `mass_converter`.\n", "\n", "### Constant vs variable mass\n", "If a particle has a width less than `tolerance`, its mass is set to a constant value.\n", "This will be demonsttrated with the decay below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dsplus_chain = parser.build_decay_chains(\"D*+\", stable_particles=[\"D+\"])\n", "DecayChainViewer(dsplus_chain)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\n", " f\"pi0 width = {Particle.from_evtgen_name('pi0').width}\\n\"\n", " f\"D0 width = {Particle.from_evtgen_name('D0').width}\"\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\pi^0$ has a greater width than $D^0$.\n", "If the tolerance is set to a value between their widths, the $D^0$ particle will have a constant mass while $\\pi^0$ will not." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dstar_decay = GenMultiDecay.from_dict(dsplus_chain, tolerance=1e-8)\n", "# Loop over D0 and pi+ particles, see graph above\n", "for particle in dstar_decay.gen_particles[0][1].children:\n", " # If a particle width is less than tolerance or if it does not have any children, its mass will be fixed.\n", " assert particle.has_fixed_mass\n", "\n", "# Loop over D+ and pi0. See above.\n", "for particle in dstar_decay.gen_particles[1][1].children:\n", " if particle.name == \"pi0\":\n", " assert not particle.has_fixed_mass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Configuring mass functions\n", "By default, the mass function used for variable mass is the relativistic Breit-Wigner distribution. This can however be changed. If you want the mother particle to have a specific mass function for a specific decay, you can add a `zfit` parameter to the DecayLanguage dict. Consider for example the previous $D^{*+}$ example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dsplus_custom_mass_func = dsplus_chain.copy()\n", "dsplus_chain_subset = dsplus_custom_mass_func[\"D*+\"][1][\"fs\"][1]\n", "print(\"Before:\")\n", "pprint(dsplus_chain_subset)\n", "# Set the mass function of pi0 to a gaussian distribution when it decays into two photons (gamma)\n", "dsplus_chain_subset[\"pi0\"][0][\"zfit\"] = \"gauss\"\n", "print(\"After:\")\n", "pprint(dsplus_chain_subset)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice the added `zfit` field to the first decay mode of the $\\pi^0$ particle. This dict can then be passed to `GenMultiDecay.from_dict`, like before." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "GenMultiDecay.from_dict(dsplus_custom_mass_func)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want all $\\pi^0$ particles to decay with the same mass function, you do not need to specify the `zfit` parameter for each decay in the `dict`. Instead, one can pass the `particle_model_map` parameter to the constructor:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "GenMultiDecay.from_dict(\n", " dsplus_chain, particle_model_map={\"pi0\": \"gauss\"}\n", ") # pi0 always decays with a gaussian mass distribution." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When using `DecayChain`s, the syntax for specifying the mass function becomes cleaner:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dplus_decay = DecayMode(\n", " 1, \"K- pi+ pi+ pi0\", model=\"PHSP\"\n", ") # The model parameter will be ignored by GenMultiDecay\n", "pi0_decay = DecayMode(\n", " 1, \"gamma gamma\", zfit=\"gauss\"\n", ") # Make pi0 have a gaussian mass distribution\n", "dplus_single = DecayChain(\"D+\", {\"D+\": dplus_decay, \"pi0\": pi0_decay})\n", "GenMultiDecay.from_dict(dplus_single.to_dict())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Custom mass functions\n", "The built-in supported mass function names are `gauss`, `bw`, and `relbw`, with `gauss` being the gaussian distribution, `bw` being the Breit-Wigner distribution, and `relbw` being the relativistic Breit-Wigner distribution.\n", "\n", "If a non-supported value for the `zfit` parameter is not specified, it will automatically use the relativistic Breit-Wigner distribution. This behavior can be changed by changing the value of `GenMultiDecay.DEFAULT_MASS_FUNC` to a different string, e.g., `\"gauss\"`. If an invalid value for the `zfit` parameter is used, a `KeyError` is raised.\n", "\n", "It is also possible to add your own mass functions besides the built-in ones. You should then create a function that takes the mass and width of a particle and returns a mass function which with the [format](https://phasespace.readthedocs.io/en/stable/usage.html#resonances-with-variable-mass) that is used for all phasespace mass functions. Below is an example of a custom gaussian distribution (implemented in the same way as the built-in gaussian distribution), which uses `zfit` PDFs:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def custom_gauss(mass, width):\n", " particle_mass = tf.cast(mass, tf.float64)\n", " particle_width = tf.cast(width, tf.float64)\n", "\n", " # This is the actual mass function that will be returned\n", " def mass_func(min_mass, max_mass, n_events):\n", " min_mass = tf.cast(min_mass, tf.float64)\n", " max_mass = tf.cast(max_mass, tf.float64)\n", " # Use a zfit PDF\n", " pdf = zfit.pdf.Gauss(mu=particle_mass, sigma=particle_width, obs=\"\")\n", " iterator = tf.stack([min_mass, max_mass], axis=-1)\n", " return tf.vectorized_map(\n", " lambda lim: pdf.sample(1, limits=(lim[0], lim[1])), iterator\n", " )\n", "\n", " return mass_func" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This function can then be passed to `GenMultiDecay.from_dict` as a dict, where the key specifies the `zfit` parameter name. In the example below, it is set to `\"custom_gauss\"`. However, this name can be chosen arbitrarily and does not need to be the same as the function name." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dsplus_chain_subset = dsplus_custom_mass_func[\"D*+\"][1][\"fs\"][1]\n", "print(\"Before:\")\n", "pprint(dsplus_chain_subset)\n", "\n", "# Set the mass function of pi0 to the custom gaussian distribution\n", "# when it decays into an electron-positron pair and a photon (gamma)\n", "dsplus_chain_subset[\"pi0\"][1][\"zfit\"] = \"custom_gauss\"\n", "print(\"After:\")\n", "pprint(dsplus_chain_subset)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "GenMultiDecay.from_dict(dsplus_custom_mass_func, {\"custom_gauss\": custom_gauss})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "interpreter": { "hash": "bb65b8ec85759003b5d99658bc6210aa8fd7b9c8f144db79d452bd242727ce5f" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 1 }