{ "cells": [ { "cell_type": "markdown", "id": "37b4fe8e-f16b-4d86-b06c-2d6782b50de0", "metadata": {}, "source": [ "# TPCpp-10M Dataset\n", "\n", "This notebook provides an example for dataloading and simple visualization of the **TPCpp-10M dataset**, a large-scale particle physics dataset designed for machine learning applications in high-energy physics research. The definitions of the plotting functions are defined in `plot.py`.\n", "\n", "## Dataset Overview\n", "\n", "The TPCpp-10M dataset contains simulated particle collision events from a Time Projection Chamber (TPC) detector, commonly used in particle physics experiments. This dataset is specifically designed for:\n", "\n", "- **Foundation model training** for particle and nuclear physics applications\n", "- **Particle track reconstruction**\n", "- **Particle identification** \n", "- **Signal vs. noise discrimination**\n", "\n", "## Dataset Characteristics\n", "\n", "- **Total Events**: over 10 million simulated proton-proton collision events at 200GeV \n", "- **Input Data Format**: Structured spacepoint data with 4D coordinates (energy + $x,y,z$-coordiantes)\n", "- **Labels**: including track IDs, particle types, and noise classification\n", "- **Split**: 10M unlabeled events, 70k/13k/7k labeled events for training/validation/testing\n", "\n", "## Data Components\n", "\n", "1. **Spacepoints**: 4D spacepoints with energy and spatial coordinates\n", "2. **Track IDs**: Unique identifiers for particle trajectories \n", "3. **Particle ID Labels**: Classification labels for different particle types\n", "4. **Noise Tags**: Binary labels distinguishing signal from detector noise\n", "\n", "## Notebook Contents\n", "\n", "This demonstration notebook includes:\n", "- Data loading.\n", "- Distribution of numbers of spacepoints, number of tracks, and class ratios.\n", "- Interactive 3D scatter plots color-coded by track, particle type, and signal/noise classification." ] }, { "cell_type": "markdown", "id": "45bcca32-9eaa-4bdb-a979-eac5dc5178e3", "metadata": {}, "source": [ "## Install and run `Plotly`\n", "The notebook uses the library `Plotly` for plotting. If it is not installed, please use the following command to install it:\n", "- **Conda**:\n", " ```\n", " conda install -c conda-forge plotly\n", " ```\n", "- **Pip**:\n", " ```\n", " pip install plotly\n", " ```\n", "\n", "For running `Plotly` in Jupyter Notebook or Jupyter Lab and saving the plots, please check out this webpage: [https://plotly.com/python/getting-started/](https://plotly.com/python/getting-started/)." ] }, { "cell_type": "code", "execution_count": null, "id": "2244df27-c924-4369-9c05-4a2eba8c6c04", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "import numpy as np\n", "\n", "# custom plotting functions\n", "from plot import (plot_histogram, \n", " plot_pie, \n", " plot_scatter)" ] }, { "cell_type": "code", "execution_count": null, "id": "cff4a872-84c1-4a40-acb8-5970c3c24f91", "metadata": {}, "outputs": [], "source": [ "# if you want to save the plots\n", "plot_folder = Path('./plots')\n", "plot_folder.mkdir(exist_ok=True)" ] }, { "cell_type": "markdown", "id": "a90837ff-4828-4853-8c68-3b23bc31f064", "metadata": {}, "source": [ "## Load all events saved in one `.npz` file \n", "together with labels for downstream tasks" ] }, { "cell_type": "code", "execution_count": null, "id": "da3b9642-de59-4906-ac8c-5a6cde7864e2", "metadata": {}, "outputs": [], "source": [ "folder = Path('labeled/test/')\n", "\n", "with np.load(folder/'spacepoints.npz') as handle:\n", " spacepoints = handle['data']\n", " event_sizes = handle['size']\n", "\n", "# although track_ids, pid_labels, and noise_tags\n", "# all have 'size' containing the sizes of each \n", "# individual events, they are the same as that\n", "# from the spacepoints, so we don't have to\n", "# load them.\n", "with np.load(folder/'track_ids.npz') as handle:\n", " # track_ids are of type np.uint32\n", " track_ids = handle['data']\n", "\n", "with np.load(folder/'pid_labels.npz') as handle:\n", " # pid_labels are of type np.uint8\n", " pid_labels = handle['data']\n", "\n", "with np.load(folder/'noise_tags.npz') as handle:\n", " # noise_tags are boolean\n", " noise_tags = handle['data']\n", "\n", "num_events = len(event_sizes)\n", "print(f'Loaded {num_events} events.')" ] }, { "cell_type": "markdown", "id": "9f0cf560-7999-4b37-9608-ebb584ac496a", "metadata": {}, "source": [ "## Load one event" ] }, { "cell_type": "code", "execution_count": null, "id": "dd7dffb7-50ee-433a-9824-0a4574d31259", "metadata": {}, "outputs": [], "source": [ "# we can use event sizes to determine the start\n", "# and end indices of each event.\n", "indices = np.cumsum(event_sizes)" ] }, { "cell_type": "code", "execution_count": null, "id": "b6084963-43ae-4120-bb52-16bdec74aff6", "metadata": {}, "outputs": [], "source": [ "# specify an event id\n", "event_id = 20\n", "\n", "# get the start and end indices from the event sizes\n", "start_id = 0 if event_id == 0 else indices[event_id - 1]\n", "end_id = indices[event_id]\n", "\n", "# get a single event\n", "event_spacepoints = spacepoints[start_id: end_id]\n", "event_track_ids = track_ids[start_id: end_id]\n", "event_pid_labels = pid_labels[start_id: end_id]\n", "event_noise_tags = noise_tags[start_id: end_id]\n", "\n", "print(f'event {event_id}:')\n", "print(f'\\tspacepoints shape = {event_spacepoints.shape}')\n", "print(f'\\ttrack_ids[:50] = {event_track_ids[:50]}')\n", "print(f'\\tpid_labels[:50] = {pid_labels[:50]}')\n", "print(f'\\tnoise_tags[:50] = {noise_tags[:50]}')" ] }, { "cell_type": "markdown", "id": "03ba25bd-1d06-40ef-a6b5-288c140d9763", "metadata": {}, "source": [ "## Statistics" ] }, { "cell_type": "markdown", "id": "04c53af3-c80d-4804-8c60-20d4e2eb1ce6", "metadata": {}, "source": [ "### Distribution of number of spacepoints" ] }, { "cell_type": "code", "execution_count": null, "id": "e377d98d-3d70-4103-be0b-8156be12db36", "metadata": {}, "outputs": [], "source": [ "start_id = 0\n", "num_sp = [] # \n", "for end_id in indices:\n", " event_spacepoints = spacepoints[start_id: end_id]\n", " num_sp.append(len(event_spacepoints))\n", " start_id = end_id\n", "\n", "fig = plot_histogram(num_sp, xaxis_title='Number of Spacepoints')\n", "fig.show()\n", "fig.write_image(plot_folder/'distribution_spacepoints.png', scale=2)\n", "fig.write_html(plot_folder/'distribution_spacepoints.html')" ] }, { "cell_type": "markdown", "id": "e922c119-2325-473c-a120-7a6446fa374e", "metadata": {}, "source": [ "### Distribution of number of tracks" ] }, { "cell_type": "code", "execution_count": null, "id": "9eb713e2-8432-43b5-bf6e-6ebd0ea2d362", "metadata": {}, "outputs": [], "source": [ "start_id = 0\n", "num_tracks = []\n", "for end_id in indices:\n", " event_track_ids = track_ids[start_id: end_id]\n", " event_num_tracks = len(np.unique(event_track_ids))\n", " num_tracks.append(event_num_tracks)\n", " start_id = end_id\n", "\n", "fig = plot_histogram(num_tracks, xaxis_title='Number of Tracks')\n", "fig.show()\n", "fig.write_image(plot_folder/'distribution_tracks.png', scale=2)\n", "fig.write_html(plot_folder/'distribution_tracks.html')" ] }, { "cell_type": "markdown", "id": "8d08a18c-40c6-47b7-8fec-0bc7218e6341", "metadata": {}, "source": [ "### Distribution of noise fraction" ] }, { "cell_type": "code", "execution_count": null, "id": "9bb092f2-0ed4-4034-880f-f948b9144692", "metadata": {}, "outputs": [], "source": [ "start_id = 0\n", "noise_fraction = []\n", "for end_id in indices:\n", " event_noise_tags = noise_tags[start_id: end_id]\n", " \n", " total = len(event_noise_tags)\n", " noise = event_noise_tags.sum()\n", " \n", " noise_fraction.append(noise / total)\n", " start_id = end_id\n", "\n", "fig = plot_histogram(noise_fraction, xaxis_title='Fraction of Noise')\n", "fig.show()\n", "fig.write_image(plot_folder/'distribution_noise.png', scale=2)\n", "fig.write_html(plot_folder/'distribution_noise.html')" ] }, { "cell_type": "markdown", "id": "24436b6a-018f-4478-8472-40ce36863263", "metadata": {}, "source": [ "### Distribution of particle ids" ] }, { "cell_type": "code", "execution_count": null, "id": "6b688bab-eb73-4517-a44e-6292edcf6d8d", "metadata": {}, "outputs": [], "source": [ "_, counts = np.unique(pid_labels, return_counts=True)\n", "total = sum(counts)\n", "fractions = [count / total for count in counts]\n", "names = ['others', 'pion', 'kaon', 'proton', 'electron']\n", "\n", "fig = plot_pie(names, \n", " fractions, \n", " title=\"Particle IDs\", \n", " width=450, \n", " height=450)\n", "fig.show()\n", "fig.write_image(plot_folder/'distribution_particle_ids.png', scale=2)\n", "fig.write_html(plot_folder/'distribution_particle_ids.html')" ] }, { "cell_type": "markdown", "id": "5642fa73-3202-4904-8a5a-0421a7a2d52e", "metadata": {}, "source": [ "## Visualization" ] }, { "cell_type": "markdown", "id": "124df5df-36e0-41e9-8816-dda272091cfd", "metadata": {}, "source": [ "### Load one event" ] }, { "cell_type": "code", "execution_count": null, "id": "61c58913-2fdc-467e-82f5-67765a9fb16a", "metadata": {}, "outputs": [], "source": [ "# specify an event id\n", "event_id = 20\n", "\n", "# get the start and end indices from the event sizes\n", "start_id = 0 if event_id == 0 else indices[event_id - 1]\n", "end_id = indices[event_id]\n", "\n", "# get a single event\n", "event_spacepoints = spacepoints[start_id: end_id]\n", "event_track_ids = track_ids[start_id: end_id]\n", "event_pid_labels = pid_labels[start_id: end_id]\n", "event_noise_tags = noise_tags[start_id: end_id]" ] }, { "cell_type": "code", "execution_count": null, "id": "436a9cfb-86c3-444d-b512-f900c5f21451", "metadata": {}, "outputs": [], "source": [ "# spacepoints contains 4 columns: energy, x, y, z.\n", "# x, y, z are the coordinates (cm) in the Euclidean space.\n", "energy, x, y, z = event_spacepoints.T" ] }, { "cell_type": "markdown", "id": "dec8a4d8-ad87-4a68-b429-b0c1c9caffa1", "metadata": {}, "source": [ "### Spacepoints" ] }, { "cell_type": "code", "execution_count": null, "id": "aef788d5-07b3-42aa-9301-08ec4d998726", "metadata": {}, "outputs": [], "source": [ "fig = plot_scatter(x, y, z)\n", "fig.show()\n", "fig.write_image(plot_folder/'scatter_spacepoints.png', scale=2)\n", "fig.write_html(plot_folder/'scatter_spacepoints.html')" ] }, { "cell_type": "markdown", "id": "03c33b61-91b1-4e0d-9530-a66e60600e8f", "metadata": {}, "source": [ "### Tracks" ] }, { "cell_type": "code", "execution_count": null, "id": "4256ffdf-3dd6-41df-96cd-a50b9d0abaf8", "metadata": {}, "outputs": [], "source": [ "fig = plot_scatter(x, y, z, labels=event_track_ids)\n", "fig.show()\n", "fig.write_image(plot_folder/'scatter_tracks.png', scale=2)\n", "fig.write_html(plot_folder/'scatter_tracks.html')" ] }, { "cell_type": "markdown", "id": "ef38d5a9-87d0-4430-9bf5-d3f3377fc209", "metadata": {}, "source": [ "### Signal vs. Noise" ] }, { "cell_type": "code", "execution_count": null, "id": "901c521e-9cbe-47c7-beab-f566d8b74322", "metadata": {}, "outputs": [], "source": [ "# Let us give all possible labels since an event may \n", "# not contain noise/signal spacepoints.\n", "fig = plot_scatter(x, y, z, \n", " labels = event_noise_tags, \n", " color_cycle = ['skyblue', 'orange'],\n", " unique_labels = [0, 1],\n", " names = ['signal', 'noise'])\n", "fig.show()\n", "fig.write_image(plot_folder/'scatter_signal-vs-noise.png', scale=2)\n", "fig.write_html(plot_folder/'scatter_signal-vs-noise.html')" ] }, { "cell_type": "markdown", "id": "058e79cc-c122-4110-ad74-cc5d8c8d079e", "metadata": {}, "source": [ "### Particle IDs" ] }, { "cell_type": "code", "execution_count": null, "id": "e186396a-12da-494f-82e4-2d4f377fcc3a", "metadata": {}, "outputs": [], "source": [ "# Let us give all possible labels since an event may \n", "# not contain all types of particles\n", "fig = plot_scatter(x, y, z, \n", " labels = event_pid_labels, \n", " unique_labels = [1, 2, 3, 4, 0],\n", " names = ['charged pion', # instead of π±\n", " 'charged kaon', # instead of K±\n", " 'proton/antiproton',\n", " 'electron/positron',\n", " 'others'])\n", "fig.show()\n", "fig.write_image(plot_folder/'scatter_particle-ids.png', scale=2)\n", "fig.write_html(plot_folder/'scatter_particle-ids.html')" ] } ], "metadata": { "kernelspec": { "display_name": "pyg", "language": "python", "name": "pyg" }, "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.12.6" } }, "nbformat": 4, "nbformat_minor": 5 }