{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Calculate the Fourier coefficient $v_2$ from two-particle correlations "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from itertools import combinations\n",
    "import math as m"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Read data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "event, phi = np.loadtxt(\"dndphi_events.csv\", delimiter=',', skiprows=1, unpack=True)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Define function that calculates $v_2$ for a given event\n",
    "\n",
    "One can use [``itertools.combinations``](https://docs.python.org/3/library/itertools.html#itertools.combinations) to get all pairs for a given 1d array."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<font color=blue>\n",
    "The function we use here is\n",
    "$$ c_n\\{2\\} \\equiv\\left\\langle\\left\\langle e^{i n\\left(\\varphi_1-\\varphi_2\\right)}\\right\\rangle\\right\\rangle=\\left\\langle v_n^2\\right\\rangle $$\n",
    "whereas the ´v2´ will average over all pairs from $\\varphi$s and the main function will average over all evets\n",
    "</font>\n",
    "\n",
    "<font color=red>\n",
    "Note that only the real part is kept, which $\\exp(i2 \\varphi)$ is now $\\cos(2 \\varphi)$\n",
    "</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def v2(phi_vals):\n",
    "    temp=combinations(phi_vals,2)\n",
    "    all=0\n",
    "    num=0\n",
    "    for i in temp:\n",
    "        all+=m.cos(2*(i[0]-i[1]))\n",
    "        num+=1\n",
    "    return all/num"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Loop over all events and determine $v_2$ averaged over all events"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "v2: 0.1462940675740694\n"
     ]
    }
   ],
   "source": [
    "v2val = np.array([]) # array with v2 values for each event, can use numpy.append() to append a value\n",
    "\n",
    "nevt = 100\n",
    "for i in range(nevt):\n",
    "    phi_vals = phi[event == i]\n",
    "    v2val=np.append(v2val,v2(phi_vals))\n",
    "print('v2:',m.sqrt(sum(v2val)/nevt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "argv": [
    "/usr/bin/python3",
    "-m",
    "ipykernel",
    "--HistoryManager.enabled=False",
    "--matplotlib=inline",
    "-c",
    "%config InlineBackend.figure_formats = set(['retina'])\nimport matplotlib; matplotlib.rcParams['figure.figsize'] = (12, 7)",
    "-f",
    "{connection_file}"
   ],
   "display_name": "Python 3 (system-wide)",
   "env": {},
   "language": "python",
   "metadata": {
    "cocalc": {
     "description": "Python 3 programming language",
     "priority": 100,
     "url": "https://www.python.org/"
    }
   },
   "name": "python3",
   "resource_dir": "/ext/jupyter/kernels/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.10.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
