{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Import" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\users\\sa_li\\.conda\\envs\\rmd\\lib\\site-packages\\zfit\\util\\execution.py:53: UserWarning: Not running on Linux. Determining available cpus for thread can failand be overestimated. Workaround (only if too many cpus are used):`zfit.run.set_n_cpu(your_cpu_number)`\n", " warnings.warn(\"Not running on Linux. Determining available cpus for thread can fail\"\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.\n", "For more information, please see:\n", " * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n", " * https://github.com/tensorflow/addons\n", "If you depend on functionality not listed there, please file an issue.\n", "\n" ] } ], "source": [ "import numpy as np\n", "from pdg_const import pdg\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "import pickle as pkl\n", "import sys\n", "import time\n", "from helperfunctions import display_time, prepare_plot\n", "import cmath as c\n", "import scipy.integrate as integrate\n", "from scipy.optimize import fminbound\n", "from array import array as arr\n", "import collections\n", "from itertools import compress\n", "import tensorflow as tf\n", "import zfit\n", "from zfit import ztf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Build model and graphs\n", "## Create graphs" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def formfactor( q2, subscript): #returns real value\n", " #check if subscript is viable\n", "\n", " if subscript != \"0\" and subscript != \"+\" and subscript != \"T\":\n", " raise ValueError('Wrong subscript entered, choose either 0, + or T')\n", "\n", " #get constants\n", "\n", " mK = ztf.constant(pdg['Ks_M'])\n", " mbstar0 = ztf.constant(pdg[\"mbstar0\"])\n", " mbstar = ztf.constant(pdg[\"mbstar\"])\n", " b0 = ztf.constant(pdg[\"b0\"])\n", " bplus = ztf.constant(pdg[\"bplus\"])\n", " bT = ztf.constant(pdg[\"bT\"])\n", "\n", " mmu = ztf.constant(pdg['muon_M'])\n", " mb = ztf.constant(pdg['bquark_M'])\n", " ms = ztf.constant(pdg['squark_M'])\n", " mB = ztf.constant(pdg['Bplus_M'])\n", "\n", " #N comes from derivation in paper\n", "\n", " N = 3\n", "\n", " #some helperfunctions\n", "\n", " tpos = (mB - mK)**2\n", " tzero = (mB + mK)*(ztf.sqrt(mB)-ztf.sqrt(mK))**2\n", "\n", " z_oben = ztf.sqrt(tpos - q2) - ztf.sqrt(tpos - tzero)\n", " z_unten = ztf.sqrt(tpos - q2) + ztf.sqrt(tpos - tzero)\n", " z = tf.divide(z_oben, z_unten)\n", "\n", " #calculate f0\n", "\n", " if subscript == \"0\":\n", " prefactor = 1/(1 - q2/(mbstar0**2))\n", " _sum = 0\n", "\n", " for i in range(N):\n", " _sum += b0[i]*(tf.pow(z,i))\n", "\n", " return tf.complex(prefactor * _sum, ztf.constant(0.0))\n", "\n", " #calculate f+ or fT\n", "\n", " else:\n", " prefactor = 1/(1 - q2/(mbstar**2))\n", " _sum = 0\n", "\n", " if subscript == \"T\":\n", " b = bT\n", " else:\n", " b = bplus\n", "\n", " for i in range(N):\n", " _sum += b[i] * (tf.pow(z, i) - ((-1)**(i-N)) * (i/N) * tf.pow(z, N))\n", "\n", " return tf.complex(prefactor * _sum, ztf.constant(0.0))\n", "\n", "def resonance(q, _mass, width, phase, scale):\n", "\n", " q2 = tf.pow(q, 2)\n", "\n", " mmu = ztf.constant(pdg['muon_M'])\n", "\n", " p = 0.5 * ztf.sqrt(q2 - 4*(mmu**2))\n", "\n", " p0 = 0.5 * ztf.sqrt(_mass**2 - 4*mmu**2)\n", "\n", " gamma_j = tf.divide(p, q2) * _mass * width / p0\n", "\n", " #Calculate the resonance\n", "\n", " _top = tf.complex(_mass * width, ztf.constant(0.0))\n", "\n", " _bottom = tf.complex(_mass**2 - q2, -_mass*gamma_j)\n", "\n", " com = _top/_bottom\n", "\n", " #Rotate by the phase\n", "\n", " r = tf.abs(com)\n", "\n", " _phase = tf.angle(com)\n", "\n", " _phase += phase\n", "\n", " x = tf.cos(phase)*r\n", " y = tf.sin(phase)*r\n", "\n", " com = tf.complex(scale* x, scale * y)\n", "\n", " return com\n", "\n", "def bifur_gauss(q, mean, sigma_L, sigma_R, scale):\n", "\n", " _exp = tf.where(q < mean, ztf.exp(- tf.pow((q-mean),2) / (2 * sigma_L**2)), ztf.exp(- tf.pow((q-mean),2) / (2 * sigma_R**2)))\n", "\n", " #Scale so the total area under curve is 1 and the top of the cusp is continuous\n", "\n", " dgamma = scale*_exp/(ztf.sqrt(2*np.pi))*2*(sigma_L*sigma_R)/(sigma_L+sigma_R)\n", "\n", " com = ztf.complex(dgamma, ztf.constant(0.0))\n", "\n", " return com\n", "\n", "def axiv_nonres(q):\n", "\n", " GF = ztf.constant(pdg['GF'])\n", " alpha_ew = ztf.constant(pdg['alpha_ew'])\n", " Vtb = ztf.constant(pdg['Vtb'])\n", " Vts = ztf.constant(pdg['Vts'])\n", " C10eff = ztf.constant(pdg['C10eff'])\n", "\n", " mmu = ztf.constant(pdg['muon_M'])\n", " mb = ztf.constant(pdg['bquark_M'])\n", " ms = ztf.constant(pdg['squark_M'])\n", " mK = ztf.constant(pdg['Ks_M'])\n", " mB = ztf.constant(pdg['Bplus_M'])\n", "\n", " q2 = tf.pow(q, 2)\n", "\n", " #Some helperfunctions\n", "\n", " beta = ztf.sqrt(tf.abs(1. - 4. * mmu**2. / q2))\n", "\n", " kabs = ztf.sqrt(mB**2. +tf.pow(q2, 2)/mB**2. + mK**4./mB**2. - 2. * (mB**2. * mK**2. + mK**2. * q2 + mB**2. * q2) / mB**2.)\n", "\n", " #prefactor in front of whole bracket\n", "\n", " prefactor1 = GF**2. *alpha_ew**2. * (tf.abs(Vtb*Vts))**2. * kabs * beta / (128. * np.pi**5.)\n", "\n", " #left term in bracket\n", "\n", " bracket_left = 2./3. * kabs**2. * beta**2. *tf.abs(tf.complex(C10eff, ztf.constant(0.0))*formfactor(q2, \"+\"))**2.\n", "\n", " #middle term in bracket\n", "\n", " _top = 4. * mmu**2. * (mB**2. - mK**2.) * (mB**2. - mK**2.)\n", "\n", " _under = q2 * mB**2.\n", "\n", " bracket_middle = _top/_under *tf.pow(tf.abs(tf.complex(C10eff, ztf.constant(0.0)) * formfactor(q2, \"0\")), 2)\n", "\n", " #Note sqrt(q2) comes from derivation as we use q2 and plot q\n", "\n", " return prefactor1 * (bracket_left + bracket_middle) * 2 *ztf.sqrt(q2)\n", "\n", "def vec(q, funcs):\n", " \n", " q2 = tf.pow(q, 2)\n", "\n", " GF = ztf.constant(pdg['GF'])\n", " alpha_ew = ztf.constant(pdg['alpha_ew'])\n", " Vtb = ztf.constant(pdg['Vtb'])\n", " Vts = ztf.constant(pdg['Vts'])\n", " C7eff = ztf.constant(pdg['C7eff'])\n", "\n", " mmu = ztf.constant(pdg['muon_M'])\n", " mb = ztf.constant(pdg['bquark_M'])\n", " ms = ztf.constant(pdg['squark_M'])\n", " mK = ztf.constant(pdg['Ks_M'])\n", " mB = ztf.constant(pdg['Bplus_M'])\n", "\n", " #Some helperfunctions\n", "\n", " beta = ztf.sqrt(tf.abs(1. - 4. * mmu**2. / q2))\n", "\n", " kabs = ztf.sqrt(mB**2. + tf.pow(q2, 2)/mB**2. + mK**4./mB**2. - 2 * (mB**2 * mK**2 + mK**2 * q2 + mB**2 * q2) / mB**2)\n", "\n", " #prefactor in front of whole bracket\n", "\n", " prefactor1 = GF**2. *alpha_ew**2. * (tf.abs(Vtb*Vts))**2 * kabs * beta / (128. * np.pi**5.)\n", "\n", " #right term in bracket\n", "\n", " prefactor2 = kabs**2 * (1. - 1./3. * beta**2)\n", "\n", " abs_bracket = tf.abs(c9eff(q, funcs) * formfactor(q2, \"+\") + tf.complex(2.0 * C7eff * (mb + ms)/(mB + mK), ztf.constant(0.0)) * formfactor(q2, \"T\"))**2\n", "\n", " bracket_right = prefactor2 * abs_bracket\n", "\n", " #Note sqrt(q2) comes from derivation as we use q2 and plot q\n", "\n", " return prefactor1 * bracket_right * 2 * ztf.sqrt(q2)\n", "\n", "def c9eff(q, funcs):\n", "\n", " C9eff_nr = tf.complex(ztf.constant(pdg['C9eff']), ztf.constant(0.0))\n", "\n", " c9 = C9eff_nr\n", "\n", " c9 = c9 + funcs\n", "\n", " return c9" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def G(y):\n", " \n", " def inner_rect_bracket(q):\n", " return tf.log(ztf.to_complex((1+tf.sqrt(q))/(1-tf.sqrt(q)))-tf.complex(ztf.constant(0), -1*ztf.constant(np.pi))) \n", " \n", " def inner_right(q):\n", " return ztf.to_complex(2 * tf.atan(1/tf.sqrt(-q)))\n", " \n", " big_bracket = tf.where(y > ztf.const(0.0), inner_rect_bracket(y), inner_right(y))\n", " \n", " return ztf.to_complex(tf.sqrt(tf.abs(y))) * big_bracket\n", "\n", "def h_S(m, q):\n", " \n", " return tf.constant(2) - G(tf.constant(1) - 4*tf.pow(m, 2) / tf.pow(q, 2))\n", "\n", "def h_P(m,q):\n", " \n", " return 2/3 + (1 - (tf.constant(1) - 4*tf.pow(m, 2) / tf.pow(q, 2))) * h_S(m,q)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Build pdf" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "class total_pdf(zfit.pdf.ZPDF):\n", " _N_OBS = 1 # dimension, can be omitted\n", " _PARAMS = ['jpsi_mass', 'jpsi_scale', 'jpsi_phase', 'jpsi_width',\n", " 'psi2s_mass', 'psi2s_scale', 'psi2s_phase', 'psi2s_width',\n", " 'cusp_mass', 'sigma_L', 'sigma_R', 'cusp_scale'\n", " ] # the name of the parameters\n", "\n", " def _unnormalized_pdf(self, x):\n", " \n", " x = x.unstack_x()\n", "\n", " def jpsi_res(q):\n", " return resonance(q, _mass = self.params['jpsi_mass'], scale = self.params['jpsi_scale'], phase = self.params['jpsi_phase'], width = self.params['jpsi_width'])\n", "\n", " def psi2s_res(q):\n", " return resonance(q, _mass = self.params['psi2s_mass'], scale = self.params['psi2s_scale'], phase = self.params['psi2s_phase'], width = self.params['psi2s_width'])\n", "\n", " def cusp(q):\n", " return bifur_gauss(q, mean = self.params['cusp_mass'], sigma_L = self.params['sigma_L'], sigma_R = self.params['sigma_R'], scale = self.params['cusp_scale'])\n", "\n", " funcs = jpsi_res(x) + psi2s_res(x) + cusp(x)\n", "\n", " vec_f = vec(x, funcs)\n", "\n", " axiv_nr = axiv_nonres(x)\n", "\n", " tot = vec_f + axiv_nr\n", "\n", " return tot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load data" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "x_min = 2*pdg['muon_M']\n", "x_max = (pdg[\"Bplus_M\"]-pdg[\"Ks_M\"]-0.1)\n", "\n", "obs = zfit.Space('q', limits = (x_min, x_max))\n", "\n", "with open(r\"./data/slim_points/slim_points_toy_0_range({0}-{1}).pkl\".format(int(x_min), int(x_max)), \"rb\") as input_file:\n", " part_set = pkl.load(input_file)\n", "\n", "x_part = part_set['x_part']\n", "\n", "x_part = x_part.astype('float64')\n", "\n", "data = zfit.data.Data.from_numpy(array=x_part, obs=obs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup parameters" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:From c:\\users\\sa_li\\.conda\\envs\\rmd\\lib\\site-packages\\tensorflow\\python\\ops\\resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n", "Instructions for updating:\n", "Colocations handled automatically by placer.\n" ] } ], "source": [ "#jpsi\n", "\n", "jpsi_mass, jpsi_width, jpsi_phase, jpsi_scale = pdg[\"jpsi\"]\n", "\n", "jpsi_m = zfit.Parameter(\"jpsi_m\", ztf.constant(jpsi_mass), floating = False)\n", "jpsi_w = zfit.Parameter(\"jpsi_w\", ztf.constant(jpsi_width), floating = False)\n", "jpsi_p = zfit.Parameter(\"jpsi_p\", ztf.constant(jpsi_phase), floating = False)\n", "jpsi_s = zfit.Parameter(\"jpsi_s\", ztf.constant(jpsi_scale), floating = False)\n", "\n", "#psi2s\n", "\n", "psi2s_mass, psi2s_width, psi2s_phase, psi2s_scale = pdg[\"psi2s\"]\n", "\n", "psi2s_m = zfit.Parameter(\"psi2s_m\", ztf.constant(psi2s_mass), floating = False)\n", "psi2s_w = zfit.Parameter(\"psi2s_w\", ztf.constant(psi2s_width), floating = False)\n", "psi2s_p = zfit.Parameter(\"psi2s_p\", ztf.constant(psi2s_phase), floating = False)\n", "psi2s_s = zfit.Parameter(\"psi2s_s\", ztf.constant(psi2s_scale), floating = False)\n", "\n", "#cusp\n", "\n", "cusp_mass, sigma_R, sigma_L, cusp_scale = 3550, 3e-7, 200, 0\n", "\n", "cusp_m = zfit.Parameter(\"cusp_m\", ztf.constant(cusp_mass))\n", "sig_L = zfit.Parameter(\"sig_L\", ztf.constant(sigma_L))\n", "sig_R = zfit.Parameter(\"sig_R\", ztf.constant(sigma_R))\n", "cusp_s = zfit.Parameter(\"cusp_s\", ztf.constant(cusp_scale))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test if graphs actually work and compute values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def total_test_tf(xq):\n", "\n", " def jpsi_res(q):\n", " return resonance(q, jpsi_m, jpsi_s, jpsi_p, jpsi_w)\n", "\n", " def psi2s_res(q):\n", " return resonance(q, psi2s_m, psi2s_s, psi2s_p, psi2s_w)\n", "\n", " def cusp(q):\n", " return bifur_gauss(q, cusp_m, sig_L, sig_R, cusp_s)\n", "\n", " funcs = jpsi_res(xq) + psi2s_res(xq) + cusp(xq)\n", "\n", " vec_f = vec(xq, funcs)\n", "\n", " axiv_nr = axiv_nonres(xq)\n", "\n", " tot = vec_f + axiv_nr\n", " \n", " return tot\n", "\n", "calcs = zfit.run(total_test_tf(x_part))\n", "\n", "test_q = np.linspace(x_min, x_max,20000000)\n", "\n", "calcs_test = zfit.run(total_test_tf(test_q))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plt.plot(x_part, calcs, '.')\n", "plt.plot(test_q, calcs_test)\n", "plt.ylim(3.5e-7, 5.5e-7)\n", "plt.xlim(3050, 3150)\n", "plt.savefig('test.png')\n", "print(jpsi_width)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup pdf" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "total_f = total_pdf(obs=obs, jpsi_mass = jpsi_m, jpsi_scale = jpsi_s, jpsi_phase = jpsi_p, jpsi_width = jpsi_w,\n", " psi2s_mass = psi2s_m, psi2s_scale = psi2s_s, psi2s_phase = psi2s_p, psi2s_width = psi2s_w,\n", " cusp_mass = cusp_m, sigma_L = sig_L, sigma_R = sig_R, cusp_scale = cusp_s)\n", "\n", "# print(total_pdf.obs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Sampling\n", "## One sample" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nevents = 440000\n", "\n", "samp = total_f.sample(n=nevents)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sam = samp.unstack_x()\n", "\n", "sam = zfit.run(sam)\n", "\n", "bins = int((x_max-x_min)/7)\n", "\n", "calcs = zfit.run(total_test_tf(samp))\n", "\n", "plt.hist(sam, bins = bins, range = (x_min,x_max))\n", "\n", "# plt.plot(sam, calcs, '.')\n", "# plt.plot(test_q, calcs_test)\n", "# plt.ylim(0, 0.0000007)\n", "# plt.xlim(3000, 3750)\n", "\n", "plt.savefig('test.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Toys" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sampler = total_f.create_sampler(n=nevents)\n", "nll = zfit.loss.UnbinnedNLL(model=total_f, data=sampler, fit_range = (x_min, x_max))\n", "\n", "# for param in pdf.get_dependents():\n", "# param.set_value(initial_value)\n", "\n", "sampler.resample(n=nevents)\n", "\n", "# Randomise initial values\n", "# for param in pdf.get_dependents():\n", "# param.set_value(random value here)\n", "\n", "# Minimise the NLL\n", "minimizer = zfit.minimize.MinuitMinimizer(verbosity = 10)\n", "minimum = minimizer.minimize(nll)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Fitting" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nll = zfit.loss.UnbinnedNLL(model=total_f, data=data, fit_range = (x_min, x_max))\n", "\n", "minimizer = zfit.minimize.MinuitMinimizer()\n", "# minimizer._use_tfgrad = False\n", "result = minimizer.minimize(nll)\n", "\n", "param_errors = result.error()\n", "\n", "for var, errors in param_errors.items():\n", " print('{}: ^{{+{}}}_{{{}}}'.format(var.name, errors['upper'], errors['lower']))\n", "\n", "print(\"Function minimum:\", result.fmin)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }