{
   "cells": [
      {
         "cell_type": "code",
         "execution_count": 1,
         "id": "bda7256e",
         "metadata": {},
         "outputs": [],
         "source": [
            "import numpy as np \n",
            "from scipy.stats import multivariate_normal\n",
            "import matplotlib.pyplot as plt\n",
            "from aare import ClusterFinder, calculate_eta2, Interpolator\n",
            "import boost_histogram as bh\n",
            "from aare._aare import Cluster2x2d, ClusterVector_Cluster2x2d\n",
            "import pickle\n",
            "import os\n",
            "\n",
            "from test_Interpolation import create_photon_hit_with_gaussian_distribution, create_2x2cluster_from_frame, create_3x3cluster_from_frame, calculate_eta_distribution, photon_hit_in_euclidean_space"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 2,
         "id": "9453f403",
         "metadata": {},
         "outputs": [],
         "source": [
            "### plotting functions\n",
            "\n",
            "def plot_eta_distribution(eta_distribution):\n",
            "    plt.imshow(\n",
            "    eta_distribution[:, :, 0].view().transpose(),   \n",
            "    origin='lower',                  \n",
            "    extent=[eta_distribution.axes[0].edges[0], eta_distribution.axes[0].edges[-1], eta_distribution.axes[1].edges[0], eta_distribution.axes[1].edges[-1]],\n",
            "    aspect='auto')\n",
            "    plt.colorbar()\n",
            "    plt.xlabel('eta_x')\n",
            "    plt.ylabel('eta_y')\n",
            "\n",
            "def plot_uniform_eta_distribution(uniform_etax, uniform_etay, eta_extent): \n",
            "    fig, axes = plt.subplots(1, 2, figsize=(10, 5))  # (rows, cols)\n",
            "\n",
            "    im1 = axes[0].imshow(\n",
            "    uniform_etax[:, :, 0].view().transpose(),   \n",
            "    origin='lower',                  \n",
            "    extent=[eta_extent[0], eta_extent[1], eta_extent[0], eta_extent[1]],\n",
            "    aspect='auto'\n",
            "    )\n",
            "    axes[0].set_title(\"uniform distribution of etax\")\n",
            "    fig.colorbar(im1, ax=axes[0], fraction=0.046, pad=0.04)\n",
            "    axes[0].set_xlabel(\"eta_x\")\n",
            "    axes[0].set_ylabel(\"eta_y\")\n",
            "\n",
            "    im2 = axes[1].imshow(\n",
            "    uniform_etay[:, :, 0].view().transpose(),   \n",
            "    origin='lower',                  \n",
            "    extent=[eta_extent[0], eta_extent[1], eta_extent[0], eta_extent[1]],\n",
            "    aspect='auto'\n",
            "    )\n",
            "    axes[1].set_title(\"uniform distribution of etay\")\n",
            "    fig.colorbar(im2, ax=axes[1], fraction=0.046, pad=0.04)\n",
            "    axes[1].set_xlabel(\"eta_x\")\n",
            "    axes[1].set_ylabel(\"eta_y\")\n",
            "\n",
            "    plt.tight_layout()\n",
            "    plt.show()\n",
            "\n",
            "def plot_photon_hit_distribution(eta_distribution, marg_CDF_etax, cond_CDF_etay): \n",
            "    normalized_eta_distribution = eta_distribution.values()[:,:, 0] / eta_distribution.values()[: , :, 0].sum() #normnalize \n",
            "\n",
            "    n_samples = 1000\n",
            "    rng = np.random.default_rng(42)\n",
            "\n",
            "    flat_indices = rng.choice(\n",
            "        normalized_eta_distribution.size,\n",
            "        size=n_samples,\n",
            "        p=normalized_eta_distribution.ravel())\n",
            "\n",
            "    x_indices, y_indices = np.unravel_index(flat_indices, normalized_eta_distribution.shape)\n",
            "\n",
            "    photon_position_x = marg_CDF_etax[x_indices, y_indices]\n",
            "    photon_position_y = cond_CDF_etay[x_indices, y_indices]\n",
            "\n",
            "    fig, axes = plt.subplots(1, 2, figsize=(10, 5))  # (rows, cols)\n",
            "\n",
            "    axes[0].scatter(eta_distribution.axes[0].edges[:-1][x_indices], eta_distribution.axes[1].edges[:-1][y_indices], s=1, alpha=0.5)\n",
            "    axes[0].set_title(\"Eta distribution (eta_x, eta_y)\")\n",
            "    #plt.gca().set_aspect('equal')\n",
            "    axes[0].set_xlabel(\"eta_x\")\n",
            "    axes[0].set_ylabel(\"eta_y\")\n",
            "    #axes[0].set_xlim(0,1)\n",
            "    #axes[0].set_ylim(0,1)\n",
            "\n",
            "    axes[1].scatter(photon_position_x, photon_position_y, s=1, alpha=0.5)\n",
            "    axes[1].set_title(\"uniform Photon positions\")\n",
            "    #plt.gca().set_aspect('equal')\n",
            "    axes[1].set_xlabel(\"x\")\n",
            "    axes[1].set_ylabel(\"y\")\n",
            "    #axes[1].set_xlim(0,1)\n",
            "    #axes[1].set_ylim(0,1)\n",
            "\n"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 3,
         "id": "f57feae4",
         "metadata": {},
         "outputs": [
            {
               "name": "stdout",
               "output_type": "stream",
               "text": [
                  "num pixels: (1000 x 1000)\n"
               ]
            }
         ],
         "source": [
            "pixel_width = 1e-4\n",
            "\n",
            "values = np.arange(0.5*pixel_width, 0.1, pixel_width)\n",
            "\n",
            "num_pixels = values.size\n",
            "\n",
            "print(f\"num pixels: ({values.size} x {values.size})\")\n",
            "\n",
            "X, Y = np.meshgrid(values, values)\n",
            "\n",
            "data_points = np.stack([X.ravel(), Y.ravel()], axis=1)"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 4,
         "id": "5a93b339",
         "metadata": {},
         "outputs": [
            {
               "data": {
                  "text/plain": [
                     "Text(0, 0.5, 'y_0')"
                  ]
               },
               "execution_count": 4,
               "metadata": {},
               "output_type": "execute_result"
            },
            {
               "data": {
                  "image/png": "[encoded data, 122876 characters]",
                  "text/plain": [
                     "<Figure size 640x480 with 2 Axes>"
                  ]
               },
               "metadata": {},
               "output_type": "display_data"
            }
         ],
         "source": [
            "# mean is in bottom right quadrant \n",
            "variance = 10*pixel_width\n",
            "\n",
            "covariance_matrix = np.array([[variance, 0], [0, variance]])\n",
            "\n",
            "mean = 650*pixel_width\n",
            "\n",
            "mean = np.array([mean, mean])\n",
            "\n",
            "base_frame = create_photon_hit_with_gaussian_distribution(mean, variance, data_points)\n",
            "\n",
            "pixels_per_superpixel = int(num_pixels*0.5)\n",
            "\n",
            "plt.imshow(base_frame) \n",
            "\n",
            "plt.colorbar()\n",
            "\n",
            "plt.axvline(x=pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "plt.axhline(y=pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "\n",
            "plt.xlabel('x_0')\n",
            "plt.ylabel('y_0')"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 5,
         "id": "2376390e",
         "metadata": {},
         "outputs": [],
         "source": [
            "### create eta distribution\n",
            "num_frames = 5000\n",
            "random_number_generator = np.random.default_rng(42)\n",
            "eta_distribution = calculate_eta_distribution(num_frames, pixels_per_superpixel, random_number_generator, bh.axis.Regular(100, -0.1, 0.6), bh.axis.Regular(100, -0.1, 0.6))\n",
            "\n",
            "test_data_path = os.getenv(\"AARE_TEST_DATA\") + \"/eta_distributions\"\n",
            "filename = test_data_path + \"/eta_distribution_2x2cluster_gaussian.pkl\"\n",
            "with open(filename, \"wb\") as f:\n",
            "    pickle.dump(eta_distribution, f)"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 7,
         "id": "0803d81d",
         "metadata": {},
         "outputs": [],
         "source": [
            "filename = os.getenv(\"AARE_TEST_DATA\") + \"/eta_distributions/eta_distribution_2x2cluster_gaussian.pkl\"\n",
            "with open(filename, \"rb\") as f:\n",
            "        eta_distribution = pickle.load(f)"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 8,
         "id": "778969be",
         "metadata": {},
         "outputs": [
            {
               "data": {
                  "image/png": "[encoded data, 28584 characters]",
                  "text/plain": [
                     "<Figure size 640x480 with 2 Axes>"
                  ]
               },
               "metadata": {},
               "output_type": "display_data"
            }
         ],
         "source": [
            "plot_eta_distribution(eta_distribution)"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 9,
         "id": "68f283b3",
         "metadata": {},
         "outputs": [],
         "source": [
            "interpolator = Interpolator(eta_distribution, eta_distribution.axes[0].edges, eta_distribution.axes[1].edges, eta_distribution.axes[2].edges[:-1])\n",
            "#interpolator = Interpolator(eta_distribution.axes[0].edges, eta_distribution.axes[1].edges, eta_distribution.axes[2].edges[:-1])\n",
            "\n",
            "#interpolator.rosenblatttransform(eta_distribution.values())\n"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 18,
         "id": "1108a591",
         "metadata": {},
         "outputs": [
            {
               "data": {
                  "image/png": "[encoded data, 51952 characters]",
                  "text/plain": [
                     "<Figure size 1000x500 with 2 Axes>"
                  ]
               },
               "metadata": {},
               "output_type": "display_data"
            }
         ],
         "source": [
            "marg_CDF_etax = interpolator.get_ietax()\n",
            "cond_CDF_etay = interpolator.get_ietay()\n",
            "\n",
            "plot_photon_hit_distribution(eta_distribution, marg_CDF_etax, cond_CDF_etay)\n",
            "\n"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 10,
         "id": "7170170f",
         "metadata": {},
         "outputs": [],
         "source": [
            "marg_CDF_etax = interpolator.get_ietax()\n",
            "cond_CDF_etay = interpolator.get_ietay()\n",
            "\n",
            "#plot_uniform_eta_distribution(uniform_etax, uniform_etay, [eta_distribution.axes[0].edges[0], eta_distribution.axes[0].edges[-1]])"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 11,
         "id": "2de329be",
         "metadata": {},
         "outputs": [
            {
               "name": "stdout",
               "output_type": "stream",
               "text": [
                  "cluster.x: 1\n",
                  "cluster.y: 1\n",
                  "eta:  (0.3519491547025806, 0.3519491547025806, 71553511.0)\n",
                  "distance x: 0.7083333333333334\n",
                  "distance y: 0.7564102564102564\n",
                  "[(0.78166653, 0.81284991, 71553511.)]\n"
               ]
            }
         ],
         "source": [
            "\n",
            "cluster = create_2x2cluster_from_frame(base_frame, pixels_per_superpixel)\n",
            "\n",
            "print(\"cluster.x:\", cluster.x)\n",
            "print(\"cluster.y:\", cluster.y)\n",
            "clustervec = ClusterVector_Cluster2x2d()\n",
            "clustervec.push_back(cluster)\n",
            "\n",
            "eta = calculate_eta2(cluster)\n",
            "print(\"eta: \", eta)\n",
            "bin_size = (eta_distribution.axes[0].edges[-1] - eta_distribution.axes[0].edges[0])/eta_distribution.axes[0].edges.shape[0]\n",
            "\n",
            "bin_index_x = int((eta[0] - eta_distribution.axes[0].edges[0])/bin_size)\n",
            "bin_index_y = int((eta[1] - eta_distribution.axes[1].edges[0])/bin_size)\n",
            "\n",
            "print(\"distance x:\", marg_CDF_etax[bin_index_x, bin_index_y, 0])\n",
            "print(\"distance y:\", cond_CDF_etay[bin_index_x, bin_index_y, 0])\n",
            "\n",
            "photon_hit = interpolator.interpolate(clustervec)\n",
            "\n",
            "print(photon_hit)\n",
            "\n"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 12,
         "id": "dadb07f2",
         "metadata": {},
         "outputs": [
            {
               "name": "stdout",
               "output_type": "stream",
               "text": [
                  "(0.7816665310999981, 0.8128499138725078, 71553511.0)\n",
                  "previous center: (0.07500000000000001, 0.07500000000000001)\n",
                  "interpolated center: (0.7816665310999981,0.8128499138725078)\n",
                  "scaled interpolated center: (0.03908332655499991,0.040642495693625394)\n",
                  "actual center: ([0.065 0.065],[0.065 0.065])\n"
               ]
            }
         ],
         "source": [
            "# scale to cluster pixel width \n",
            "#cluster_center = num_pixels*0.75*pixel_width\n",
            "cluster_center = (pixels_per_superpixel*pixel_width*(cluster.x + 0.5), pixels_per_superpixel*pixel_width*(cluster.y + 0.5))\n",
            "#scaled_photon_hit = photon_hit_in_euclidean_space(cluster_center, pixels_per_superpixel, photon_hit)\n",
            "\n",
            "print(photon_hit[0])\n",
            "\n",
            "scaled_photon_hit = ((photon_hit[0][0])*pixels_per_superpixel*pixel_width, (photon_hit[0][1])*pixels_per_superpixel*pixel_width)\n",
            "\n",
            "print(f\"previous center: ({cluster_center[0]}, {cluster_center[1]})\")\n",
            "print(f\"interpolated center: ({photon_hit[0][0]},{photon_hit[0][1]})\")\n",
            "print(f\"scaled interpolated center: ({scaled_photon_hit[0]},{scaled_photon_hit[1]})\")\n",
            "print(f\"actual center: ({mean},{mean})\")"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 13,
         "id": "e6968416",
         "metadata": {},
         "outputs": [
            {
               "data": {
                  "text/plain": [
                     "Text(0, 0.5, 'y_0')"
                  ]
               },
               "execution_count": 13,
               "metadata": {},
               "output_type": "execute_result"
            },
            {
               "data": {
                  "image/png": "[encoded data, 123824 characters]",
                  "text/plain": [
                     "<Figure size 640x480 with 2 Axes>"
                  ]
               },
               "metadata": {},
               "output_type": "display_data"
            }
         ],
         "source": [
            "plt.imshow(base_frame) \n",
            "\n",
            "plt.colorbar()\n",
            "\n",
            "plt.axvline(x=pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "plt.axhline(y=pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "\n",
            "plt.scatter(mean/pixel_width, mean/pixel_width, color='red', s=40, marker='x', label='Actual photon hit')\n",
            "plt.scatter(scaled_photon_hit[0]/pixel_width, scaled_photon_hit[1]/pixel_width, color = 'blue', s=40, marker='x', label='interpolated photon hit')\n",
            "\n",
            "plt.xlabel('x_0')\n",
            "plt.ylabel('y_0')"
         ]
      },
      {
         "cell_type": "markdown",
         "id": "ba2c856e",
         "metadata": {},
         "source": [
            "## 3x3 Cluster"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 14,
         "id": "b4e274ed",
         "metadata": {},
         "outputs": [],
         "source": [
            "from aare._aare import Cluster3x3d, ClusterVector_Cluster3x3d"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 15,
         "id": "d9b6c7ba",
         "metadata": {},
         "outputs": [
            {
               "data": {
                  "text/plain": [
                     "Text(0, 0.5, 'y_0')"
                  ]
               },
               "execution_count": 15,
               "metadata": {},
               "output_type": "execute_result"
            },
            {
               "data": {
                  "image/png": "[encoded data, 132408 characters]",
                  "text/plain": [
                     "<Figure size 640x480 with 2 Axes>"
                  ]
               },
               "metadata": {},
               "output_type": "display_data"
            }
         ],
         "source": [
            "# mean is in center quadrant \n",
            "variance = 10*pixel_width\n",
            "\n",
            "covariance_matrix = np.array([[variance, 0], [0, variance]])\n",
            "\n",
            "mean_x = (1 + 0.8)*(num_pixels/3)*pixel_width\n",
            "mean_y = (1 + 0.2)*(num_pixels/3)*pixel_width\n",
            "\n",
            "mean = np.array([mean_x, mean_y])\n",
            "\n",
            "base_frame = create_photon_hit_with_gaussian_distribution(mean, variance, data_points)\n",
            "\n",
            "pixels_per_superpixel = int(num_pixels/3)\n",
            "\n",
            "plt.imshow(base_frame) \n",
            "\n",
            "plt.colorbar()\n",
            "\n",
            "plt.axvline(x=pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "plt.axhline(y=pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "plt.axvline(x=2*pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "plt.axhline(y=2*pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "\n",
            "plt.xlabel('x_0')\n",
            "plt.ylabel('y_0')"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": null,
         "id": "87f061a6",
         "metadata": {},
         "outputs": [],
         "source": [
            "### calculate eta distribution\n",
            "\n",
            "num_frames = 5000\n",
            "\n",
            "random_number_generator = np.random.default_rng(42)\n",
            "eta_distribution = calculate_eta_distribution(num_frames, pixels_per_superpixel, random_number_generator, bh.axis.Regular(100, -0.1, 1.0), bh.axis.Regular(100, -0.1, 1.0), False)\n",
            "\n",
            "filename = os.getenv(\"AARE_TEST_DATA\") + \"/eta_distributions/eta_distribution_3x3cluster_gaussian.pkl\"\n",
            "with open(filename, \"wb\") as f:\n",
            "    pickle.dump(eta_distribution, f)\n"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": null,
         "id": "57a91e41",
         "metadata": {},
         "outputs": [],
         "source": [
            "filename = os.getenv(\"AARE_TEST_DATA\") + \"/eta_distributions/eta_distribution_3x3cluster_gaussian.pkl\"\n",
            "with open(filename, \"rb\") as f:\n",
            "        eta_distribution = pickle.load(f)"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 7,
         "id": "cd109d25",
         "metadata": {},
         "outputs": [
            {
               "data": {
                  "image/png": "[encoded data, 24264 characters]",
                  "text/plain": [
                     "<Figure size 640x480 with 2 Axes>"
                  ]
               },
               "metadata": {},
               "output_type": "display_data"
            }
         ],
         "source": [
            "plot_eta_distribution(eta_distribution)"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": null,
         "id": "1cf0e265",
         "metadata": {},
         "outputs": [],
         "source": []
      },
      {
         "cell_type": "code",
         "execution_count": 14,
         "id": "c38b2b9e",
         "metadata": {},
         "outputs": [],
         "source": [
            "#interpolator = Interpolator(eta_distribution, eta_distribution.axes[0].edges, eta_distribution.axes[1].edges, eta_distribution.axes[2].edges[:-1])\n",
            "\n",
            "interpolator = Interpolator(eta_distribution.axes[0].edges, eta_distribution.axes[1].edges, eta_distribution.axes[2].edges[:-1])\n",
            "interpolator.rosenblatttransform(eta_distribution.values())\n",
            "marg_CDF_etax = interpolator.get_ietax()\n",
            "cond_CDF_etay = interpolator.get_ietay()\n"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 15,
         "id": "0458f9e6",
         "metadata": {},
         "outputs": [
            {
               "data": {
                  "image/png": "[encoded data, 38632 characters]",
                  "text/plain": [
                     "<Figure size 1000x500 with 2 Axes>"
                  ]
               },
               "metadata": {},
               "output_type": "display_data"
            }
         ],
         "source": [
            "\n",
            "plot_photon_hit_distribution(eta_distribution, marg_CDF_etax, cond_CDF_etay)"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": null,
         "id": "04a9a588",
         "metadata": {},
         "outputs": [
            {
               "data": {
                  "image/png": "[encoded data, 34272 characters]",
                  "text/plain": [
                     "<Figure size 1000x500 with 4 Axes>"
                  ]
               },
               "metadata": {},
               "output_type": "display_data"
            }
         ],
         "source": [
            "\n",
            "\n",
            "plot_uniform_eta_distribution(uniform_etax, uniform_etay, [eta_distribution.axes[0].edges[0], eta_distribution.axes[0].edges[-1]])"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 16,
         "id": "9e00f659",
         "metadata": {},
         "outputs": [
            {
               "name": "stdout",
               "output_type": "stream",
               "text": [
                  "(0.5499154381017892, 0.44933336606613455, 48591988.0)\n",
                  "[-0.1   -0.093 -0.086 -0.079 -0.072 -0.065 -0.058 -0.051 -0.044 -0.037\n",
                  " -0.03  -0.023 -0.016 -0.009 -0.002  0.005  0.012  0.019  0.026  0.033\n",
                  "  0.04   0.047  0.054  0.061  0.068  0.075  0.082  0.089  0.096  0.103\n",
                  "  0.11   0.117  0.124  0.131  0.138  0.145  0.152  0.159  0.166  0.173\n",
                  "  0.18   0.187  0.194  0.201  0.208  0.215  0.222  0.229  0.236  0.243\n",
                  "  0.25   0.257  0.264  0.271  0.278  0.285  0.292  0.299  0.306  0.313\n",
                  "  0.32   0.327  0.334  0.341  0.348  0.355  0.362  0.369  0.376  0.383\n",
                  "  0.39   0.397  0.404  0.411  0.418  0.425  0.432  0.439  0.446  0.453\n",
                  "  0.46   0.467  0.474  0.481  0.488  0.495  0.502  0.509  0.516  0.523\n",
                  "  0.53   0.537  0.544  0.551  0.558  0.565  0.572  0.579  0.586  0.593\n",
                  "  0.6  ]\n",
                  "0.9999999999999998\n",
                  "dX: 1\n",
                  "dY: 0\n",
                  "u: 1\n",
                  "v: 0\n"
               ]
            }
         ],
         "source": [
            "\n",
            "cluster = create_3x3cluster_from_frame(base_frame, pixels_per_superpixel)\n",
            "\n",
            "eta = calculate_eta2(cluster)\n",
            "print(eta)\n",
            "print(eta_distribution.axes[0].edges)\n",
            "clustervec = ClusterVector_Cluster3x3d()\n",
            "#print(marg_CDF_etax)\n",
            "print(marg_CDF_etax[93, 0,0])\n",
            "clustervec.push_back(cluster)\n",
            "\n",
            "\n",
            "\n",
            "photon_hit = interpolator.interpolate(clustervec)"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 17,
         "id": "2f878b6c",
         "metadata": {},
         "outputs": [
            {
               "name": "stdout",
               "output_type": "stream",
               "text": [
                  "(1.5000000000000002, 1.5, 48591988.0)\n",
                  "previous center: (0.04995000000000001, 0.04995000000000001)\n",
                  "interpolated center: (1.5000000000000002,1.5)\n",
                  "scaled interpolated center: (0.04995000000000001,0.04995)\n",
                  "actual center: ([0.06 0.04],[0.06 0.04])\n"
               ]
            }
         ],
         "source": [
            "## scale to cluster pixel width \n",
            "#cluster_center = (1+0.5)*pixels_per_superpixel*pixel_width\n",
            "\n",
            "cluster_center = (pixels_per_superpixel*pixel_width*(cluster.x + 0.5), pixels_per_superpixel*pixel_width*(cluster.y + 0.5))\n",
            "\n",
            "print(photon_hit[0])\n",
            "\n",
            "scaled_photon_hit = (photon_hit[0][0]*pixels_per_superpixel*pixel_width, photon_hit[0][1]*pixels_per_superpixel*pixel_width)\n",
            "\n",
            "print(f\"previous center: ({cluster_center[0]}, {cluster_center[1]})\")\n",
            "print(f\"interpolated center: ({photon_hit[0][0]},{photon_hit[0][1]})\")\n",
            "print(f\"scaled interpolated center: ({scaled_photon_hit[0]},{scaled_photon_hit[1]})\")\n",
            "print(f\"actual center: ({mean},{mean})\")"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 18,
         "id": "1b9a9823",
         "metadata": {},
         "outputs": [
            {
               "data": {
                  "text/plain": [
                     "Text(0, 0.5, 'y_0')"
                  ]
               },
               "execution_count": 18,
               "metadata": {},
               "output_type": "execute_result"
            },
            {
               "data": {
                  "image/png": "[encoded data, 133136 characters]",
                  "text/plain": [
                     "<Figure size 640x480 with 2 Axes>"
                  ]
               },
               "metadata": {},
               "output_type": "display_data"
            }
         ],
         "source": [
            "plt.imshow(base_frame) \n",
            "\n",
            "plt.colorbar()\n",
            "\n",
            "plt.axvline(x=pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "plt.axhline(y=pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "plt.axvline(x=2*pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "plt.axhline(y=2*pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "\n",
            "plt.scatter(mean_x/pixel_width, mean_y/pixel_width, color='red', s=40, marker='x', label='Actual photon hit')\n",
            "plt.scatter(scaled_photon_hit[0]/pixel_width, scaled_photon_hit[1]/pixel_width, color = 'blue', s=40, marker='x', label='interpolated photon hit')\n",
            "\n",
            "plt.xlabel('x_0')\n",
            "plt.ylabel('y_0')"
         ]
      },
      {
         "cell_type": "markdown",
         "id": "c2c6f5a7",
         "metadata": {},
         "source": [
            "## Interpolation with Eta3"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 6,
         "id": "1698728e",
         "metadata": {},
         "outputs": [],
         "source": [
            "from aare._aare import Cluster3x3d, ClusterVector_Cluster3x3d, calculate_eta3, calculate_cross_eta3"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 4,
         "id": "019d93c7",
         "metadata": {},
         "outputs": [
            {
               "data": {
                  "text/plain": [
                     "Text(0, 0.5, 'y_0')"
                  ]
               },
               "execution_count": 4,
               "metadata": {},
               "output_type": "execute_result"
            },
            {
               "data": {
                  "image/png": "[encoded data, 134912 characters]",
                  "text/plain": [
                     "<Figure size 640x480 with 2 Axes>"
                  ]
               },
               "metadata": {},
               "output_type": "display_data"
            }
         ],
         "source": [
            "# mean is in center quadrant \n",
            "variance = 10*pixel_width\n",
            "\n",
            "covariance_matrix = np.array([[variance, 0], [0, variance]])\n",
            "\n",
            "mean = (1 + 0.3)*(num_pixels/3)*pixel_width\n",
            "\n",
            "mean = np.array([mean, mean])\n",
            "\n",
            "base_frame = create_photon_hit_with_gaussian_distribution(mean, variance, data_points)\n",
            "\n",
            "pixels_per_superpixel = int(num_pixels/3)\n",
            "\n",
            "plt.imshow(base_frame) \n",
            "\n",
            "plt.colorbar()\n",
            "\n",
            "plt.axvline(x=pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "plt.axhline(y=pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "plt.axvline(x=2*pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "plt.axhline(y=2*pixels_per_superpixel, color='black', linestyle='--', linewidth=0.5)\n",
            "\n",
            "plt.xlabel('x_0')\n",
            "plt.ylabel('y_0')"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 7,
         "id": "14da0d16",
         "metadata": {},
         "outputs": [],
         "source": [
            "num_frames = 2000\n",
            "\n",
            "random_number_generator = np.random.default_rng(42)\n",
            "eta_distribution = calculate_eta_distribution(num_frames, pixels_per_superpixel, random_number_generator, calculate_eta3, False)\n"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 8,
         "id": "e056ffc3",
         "metadata": {},
         "outputs": [
            {
               "data": {
                  "image/png": "[encoded data, 41844 characters]",
                  "text/plain": [
                     "<Figure size 640x480 with 2 Axes>"
                  ]
               },
               "metadata": {},
               "output_type": "display_data"
            }
         ],
         "source": [
            "plot_eta_distribution(eta_distribution)"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 9,
         "id": "bfb92aba",
         "metadata": {},
         "outputs": [],
         "source": [
            "num_frames = 2000\n",
            "\n",
            "random_number_generator = np.random.default_rng(42)\n",
            "eta_distribution = calculate_eta_distribution(num_frames, pixels_per_superpixel, random_number_generator, calculate_cross_eta3, False)\n"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": 10,
         "id": "90f910a2",
         "metadata": {},
         "outputs": [
            {
               "data": {
                  "image/png": "[encoded data, 41844 characters]",
                  "text/plain": [
                     "<Figure size 640x480 with 2 Axes>"
                  ]
               },
               "metadata": {},
               "output_type": "display_data"
            }
         ],
         "source": [
            "plot_eta_distribution(eta_distribution)"
         ]
      },
      {
         "cell_type": "code",
         "execution_count": null,
         "id": "fb161fb9",
         "metadata": {},
         "outputs": [],
         "source": []
      }
   ],
   "metadata": {
      "kernelspec": {
         "display_name": "base",
         "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.11.11"
      }
   },
   "nbformat": 4,
   "nbformat_minor": 5
}
