{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "77660c10-3099-44a8-bf26-f6e602ca8d8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from tqdm import tqdm\n",
    "\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c710b940-8bb5-4964-ac58-6300679b7ee6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_random_unit_vector():\n",
    "    x = np.random.uniform(low=-1., high=1.)\n",
    "    y = np.random.uniform(low=-1., high=1.)\n",
    "    z = np.random.uniform(low=-1., high=1.)\n",
    "    \n",
    "    #print(\"generating random\")\n",
    "    #print(x,y,z)\n",
    "    #print(np.sqrt(x*x + y*y + z*z))\n",
    "    #print(np.array([x, y, z]) / np.sqrt(x*x + y*y + z*z))\n",
    "    \n",
    "    if x*x + y*y + z*z > 1.:\n",
    "        return generate_random_unit_vector()\n",
    "    return np.array([x, y, z]) / np.sqrt(x*x + y*y + z*z)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c5226261-ffd1-4e5c-bcfa-eb56b47f2c76",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████████████| 100/100 [00:01<00:00, 83.87it/s]\n"
     ]
    }
   ],
   "source": [
    "min_energy = 50. # MeV\n",
    "max_energy = 1000. # MeV\n",
    "\n",
    "num_events = 100\n",
    "all_space_points = []\n",
    "all_angles = []\n",
    "all_e_dirs = []\n",
    "all_p_dirs = []\n",
    "all_e_energies = []\n",
    "all_p_energies = []\n",
    "all_angles = []\n",
    "\n",
    "for i in tqdm(range(num_events)):\n",
    "\n",
    "    electron_energy = np.random.uniform(low=min_energy, high=max_energy)\n",
    "    positron_energy = np.random.uniform(low=min_energy, high=max_energy)\n",
    "    \n",
    "    all_e_energies.append(electron_energy)\n",
    "    all_p_energies.append(positron_energy)\n",
    "\n",
    "    electron_dir = generate_random_unit_vector()\n",
    "    positron_dir = generate_random_unit_vector()\n",
    "    \n",
    "    all_e_dirs.append(electron_dir)\n",
    "    all_p_dirs.append(positron_dir)\n",
    "    \n",
    "    all_angles.append(np.arccos(electron_dir[0] * positron_dir[0]\n",
    "                              + electron_dir[1] * positron_dir[1]\n",
    "                              + electron_dir[2] * positron_dir[2]) * 180. / np.pi)\n",
    "\n",
    "    space_points = []\n",
    "    curr_e_e = electron_energy\n",
    "    curr_p_e = positron_energy\n",
    "    \n",
    "    starting_offset = np.random.uniform(low=0., high=1000.)\n",
    "    \n",
    "    curr_e_pos = np.array([0.,0.,0.])\n",
    "    curr_p_pos = np.array([0.,0.,0.])\n",
    "\n",
    "    while curr_e_e > 0:\n",
    "        #print(space_points)\n",
    "        space_points.append(np.copy(curr_e_pos))\n",
    "        curr_e_pos += electron_dir # trend in initial direction\n",
    "        curr_e_pos += 2 * generate_random_unit_vector() # noise in random direction\n",
    "        curr_e_e -= 2. # approximating 2 MeV/cm, and approximating about 1 cm of travel from above sums\n",
    "    \n",
    "    while curr_p_e > 0:\n",
    "        #print(space_points)\n",
    "        space_points.append(np.copy(curr_p_pos))        \n",
    "        curr_p_pos += positron_dir\n",
    "        curr_p_pos += 2 * generate_random_unit_vector()\n",
    "        curr_p_e -= 2.\n",
    "        \n",
    "    all_space_points.append(np.array(space_points))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "145166c2-70ec-453b-964b-3bc122f9f1a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "saved!\n"
     ]
    }
   ],
   "source": [
    "with open('toy_data_v1.pkl', 'wb') as f:\n",
    "    pickle.dump((all_space_points, all_angles, all_e_dirs, all_p_dirs, all_e_energies, all_p_energies), f)\n",
    "    \n",
    "print(\"saved!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "22ecab62-3e4e-4456-a57c-3b023702cdaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "#all_space_points"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31afae5d-b3cc-433b-a449-331af853a9be",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5efe29a-8a62-4567-bd72-877279b414f9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14996008-b715-4468-8f62-2742ffbb8afa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
