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27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
{
 "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
}