{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"},"cell_type":"markdown","source":"## Simple  Events EDA  - TrackML Particle Tracking \n\n\nIn this notebook, I have explored the dataset provied in the [TrackML Particle Tracking Challenge](https://www.kaggle.com/c/trackml-particle-identification). \n\nThe dataset comprises of multiple independent events, where each event contains simulated measurements (essentially 3D points) of particles generated in a collision between proton bunches at the Large Hadron Collider at CERN. The goal of this challenge is to group the recorded measurements or hits for each event into tracks, sets of hits that belong to the same initial particle. A solution must uniquely associate each hit to one track. The training dataset contains the recorded hits, their ground truth counterpart and their association to particles, and the initial parameters of those particles. \n\n**Contents **  \n\n1. Read Event Files \n2. Read - Cells, Particles, Truth, Hits Files  \n3. Exploring Cells Dataset   \n  3.1 Distribution of cells.ch0  \n  3.2 Distribution of cells.ch1  \n  3.3 Distribution of cells.value   \n  3.4 mean of \"cells.value\" by ch0 and ch1  \n4. Exploring Hits Data  \n  4.1 X, Y, Z global coordinates of particles   \n  4.2 Layer Id for every hit   \n  4.3 Module Id for every hit  \n  4.4 Volume Id for every hit   \n  4.5 Distance of particles from Origin  \n  4.6 Initial Position of particles in 3d space  \n5. Exploring Particles Data   \n  5.1 Animated trajectory of a positively charged particle   \n  5.2 Animated trajectory of a negatively charged particle  \n  5.3 Plotting the initial x, y, and z coordinates of particles  \n  5.4 Distribution of positively and negatively charged particles   \n  5.5 Distribution of number of hits for different particles  \n6. Exploring Truths Data  \n  6.1 Truth Data weight per id  "},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"from IPython.display import display\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pandas as pd\nimport os \n\nfrom IPython.display import HTML\nfrom matplotlib import animation\nimport io, base64\n# plt.style.use('fivethirtyeight')\n\nfrom plotly.offline import init_notebook_mode, iplot\nfrom matplotlib.pyplot import pie, show\nimport plotly.graph_objs as go\nimport numpy as np\n\n# from trackml.dataset import load_event, load_dataset\n# from trackml.score import score_event\n\nsns.set_style(\"whitegrid\", {'axes.grid' : False})\ninit_notebook_mode(connected=True)","execution_count":1,"outputs":[]},{"metadata":{"_uuid":"4ddc9b968c78271c32ce28278c3da9e44130ac32","_cell_guid":"ff74a135-9463-4361-ade9-b566aa8971be"},"cell_type":"markdown","source":"## 1. Read the Event Files\n\nThe full training dataset with 8850 events split into 5 files, In this notebook I am only using train_1 file."},{"metadata":{"_uuid":"5a71e633ac240ead2024105f495c844ff7861737","_cell_guid":"88c20f62-0495-4790-895b-f60751ea06a3","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"train_path = '../input/train_1/'\ntrain_files = os.listdir(train_path)\ntrain_files[:10]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f8423e3fe3822f25db4e43adf6c4016b01f4eaf8","_cell_guid":"ae8fd6c1-e3e9-46a5-938d-9aaded470e6b"},"cell_type":"markdown","source":"### Total events in train_1 file"},{"metadata":{"_uuid":"17529e5a2798cc9f2dd457fd33f60d4ec5319471","_cell_guid":"2b0413b1-a3d8-4c64-872e-ffd732601401","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"len(train_files)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c1dc2d2c9b2044e0eedf582d716d52730c280129","_cell_guid":"45734414-b475-4637-a123-2f81a7fc287c"},"cell_type":"markdown","source":"## 2. Read - Cells, Particles, Truth, Hits Files\n\nIn train_1, there are four different files for every event: \n\n1. Event Hits - Details about where particles hit and the position in the 3 dimentional space\n2. Event Hit Cells  - The cells file contains the constituent active detector cells that comprise each hit. The cells can be used to refine the hit to track association.   \n3. Event Particles  - The truth file contains the mapping between hits and generating particles and the true particle state at each measured hit. Each entry maps one hit to one particle.  \n4. Event Truth  - All entries in thie file contain the generated information or ground truth.  "},{"metadata":{"_uuid":"3e578e9132ca84f564e3cdbdcf7ea1db8611dd3a","_cell_guid":"0b6c9a93-a8ae-4a06-b6f3-1826237b303f","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"cells_df, truth_df, particles_df, hits_df = [], [], [], []\nfor filename in sorted(train_files)[:20]:\n    if \"cells\" in filename:\n        cell_df = pd.read_csv(train_path+filename)\n        cells_df.append(cell_df)\n    elif \"hits\" in filename:\n        hit_df = pd.read_csv(train_path+filename)\n        hits_df.append(hit_df)\n    elif \"particles\" in filename:\n        particle_df = pd.read_csv(train_path+filename)\n        particles_df.append(particle_df)\n    elif \"truth\" in filename:\n        trut_df = pd.read_csv(train_path+filename)\n        truth_df.append(trut_df)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dfd25e626eb37d8be71d82d01a959b5e6bc2d72f","_cell_guid":"051622fa-dc5d-4c02-b78c-4698c2c3229e"},"cell_type":"markdown","source":"## 3. Exploring Cells Dataset\n\nThe cells file contains the constituent active detector cells that comprise each hit. The cells can be used to refine the hit to track association. A cell is the smallest granularity inside each detector module, much like a pixel on a screen, except that depending on the volume_id a cell can be a square or a long rectangle. It is identified by two channel identifiers that are unique within each detector module and encode the position, much like column/row numbers of a matrix. A cell can provide signal information that the detector module has recorded in addition to the position. Depending on the detector type only one of the channel identifiers is valid, e.g. for the strip detectors, and the value might have different resolution. \n\nhit_id: numerical identifier of the hit as defined in the hits file.  \nch0, ch1: channel identifier/coordinates unique within one module.  \nvalue: signal value information, e.g. how much charge a particle has deposited.  "},{"metadata":{"_uuid":"238a3d071d2d1b9e85cf26d075af8d36cd510048","_cell_guid":"9e756875-b340-47f6-a7c6-c4537eb94009","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"# cells_df[0].shape\ncells_df[0].head(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"90cd4466cfc20e63f9e49a38cb6344fe0a0b186c","_cell_guid":"5dbc063c-4029-4516-8c44-937531c02654"},"cell_type":"markdown","source":"There are a total of 664996 entries in this file.  \n\n### 3.1 cells.value for single event\n\nLets observe the cells.value column for a single event. "},{"metadata":{"_uuid":"892105e2cf426c4ae52d0fd966652d0cd47ef7ea","_cell_guid":"b60ca651-382b-4aff-bc71-8abd91d7e13e","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"def dist(df, col, bins, color, title, kde=False):\n    plt.figure(figsize=(15,3))\n    sns.distplot(df[col].values, bins=bins, color=color, kde=kde)\n    plt.title(title, fontsize=14);\n    plt.show();\n\ndef mdist(df, col, bins, color, kde=False):\n    f, axes = plt.subplots(1, 2, figsize=(15,3))\n    sns.distplot(df[1][col].values, bins=bins, color=color, rug=False, ax=axes[0], kde=kde)\n    sns.distplot(df[2][col].values, bins=bins, color=color, rug=False, ax=axes[1], kde=kde)\n\n    f, axes = plt.subplots(1, 2, figsize=(15,3))\n    sns.distplot(df[3][col].values, bins=bins, color=color, rug=False, ax=axes[0], kde=kde)\n    sns.distplot(df[4][col].values, bins=bins, color=color, rug=False, ax=axes[1], kde=kde)\n\ndist(cells_df[0], 'value', 10, 'red', 'cells.value')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3ceaff0b2a3aa957138ba89fa47e6e2e3fa00900","_cell_guid":"4296d010-5bb1-4cc0-8308-bbf190516b56"},"cell_type":"markdown","source":"The cell.value takes values between 0 and 1, where most of the cells hit have value on the lower end. \n\nIs it true for some over events ? Lets check by plotting the same variable for other events. \n\n### Distribution of cells.value for some other events"},{"metadata":{"_uuid":"04f1a80f8050869c383f14b63527196c34c5f1ca","_cell_guid":"9b934600-9f70-4b7f-a46b-920a72b4b190","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"mdist(cells_df, 'value', 10, 'green', kde=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bf08552a40be5e3b968eba8d075f25ad17e6a914","_cell_guid":"d0a77f30-4dce-4ad3-af8f-36f5418de6dc"},"cell_type":"markdown","source":"### 3.2 cells.ch0 for single event"},{"metadata":{"_uuid":"4ec1e3a1268515b53023a1ed2e3cdc550b2ef244","_cell_guid":"c415e7dd-2876-4d14-8513-aafa9fe19e47","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"dist(cells_df[0], 'ch0', 100, 'red', 'cells.ch0')\n# cells_df[0].ch0.describe()\n# cells_df[0].ch0.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d8d44ec7b9cfa5238129feebee9ac38b62107445","_cell_guid":"0083fe5e-d93a-45f5-8e8f-52f656152c02"},"cell_type":"markdown","source":"The variable cells.ch0 takes values between 0 to 1195 for this event\n\n### cells.ch0 for some other events"},{"metadata":{"_uuid":"056c38a956fa3963f3b268db99893b710c723647","_cell_guid":"6b6e91ee-2ab8-4365-8c67-5a83993cecfb","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"mdist(cells_df, 'ch0', 10, 'green', kde=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"147e4f0dcfce0159c4233255ea057c0a4781ca7a","_cell_guid":"8f55ec4a-1336-43ed-a7d0-dc1aed02c712"},"cell_type":"markdown","source":"### 3.3 cells.ch1 for single event"},{"metadata":{"_uuid":"c380a40b0777842ebbed2eee5eed88be9d2dc8cf","_cell_guid":"e4fdd3fd-24d7-48cc-a236-3e87d525b0ed","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"dist(cells_df[0], 'ch1', 100, 'red', 'cells.ch1')\n# cells_df[0].ch1.describe()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"95cbb1c82aad3be15794d538117243568de1bc43","_cell_guid":"f599b1bb-2dc6-4712-b1fa-d74ce632a254"},"cell_type":"markdown","source":"This variable takes values from 0 to 1275, and by looking at above graph it seems that most of the entries have ch1 = 0\n\n### cells.ch1 for some other events"},{"metadata":{"_uuid":"463b9b2ed067cb1d48e13751f8d6b244984cd6da","_cell_guid":"b30c7093-a971-46b5-9b00-53f1afb60576","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"mdist(cells_df, 'ch1', 10, 'green', kde=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6cfa710b2d0f0454d2f1b48e2df1c59f078fc8de","_cell_guid":"2b46eb99-1786-47f7-b9a4-8096bc5fc825"},"cell_type":"markdown","source":"### 3.4 mean of \"value\" for ch0 and ch1"},{"metadata":{"_uuid":"1f82983207ab5472cb68cc192d6225ef5c098e32","_cell_guid":"057d5f07-6578-49f6-8afe-3bddc8b973d0","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"ch0df = cells_df[0].groupby('ch0').agg({'value' : 'mean'}).reset_index()\nch1df = cells_df[0].groupby('ch1').agg({'value' : 'mean'}).reset_index()\n\nf, axes = plt.subplots(2, 1, figsize=(15,10))\nsns.regplot(x='ch0', y='value', data=ch0df, fit_reg=False, color='#ff4c64', ax=axes[0])\nsns.regplot(x='ch1', y='value', data=ch1df, fit_reg=False, color='#89ea7c', ax=axes[1])\naxes[0].spines['right'].set_visible(False)\naxes[0].spines['top'].set_visible(False)\naxes[1].spines['right'].set_visible(False)\naxes[1].spines['top'].set_visible(False)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cd8c6662e5ebc025ca4bbc7ba7ae9fe3bc739628","_cell_guid":"8d361444-efa1-4524-b8db-4268bc3453a1"},"cell_type":"markdown","source":"Mean of value variable is close to zero for many values of ch1 (greater than 180), while the mean value is higher, while for ch0 it is close to zero for smaller values of ch0 and close to 1 for higher values of ch0.\n\n### 4. hits data\n\nHit data contains following variables: \n\nhit_id: numerical identifier of the hit inside the event.  \nx, y, z: measured x, y, z position (in millimeter) of the hit in global coordinates.  \nvolume_id: numerical identifier of the detector group.  \nlayer_id: numerical identifier of the detector layer inside the group.  \nmodule_id: numerical identifier of the detector module inside the layer.  "},{"metadata":{"_uuid":"9d964d0c6cecdb1a41f543eb3a7e74f4fb9853b2","_cell_guid":"9b0be81c-0196-40a1-aeb0-c2d2d4eea4c5","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"hits_df[0].head(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8d682c765877b94671c1d482f35c9838d77987b2","_cell_guid":"42701485-59c1-4cb4-bfe6-756f6ff8c13f"},"cell_type":"markdown","source":"\n### 4.1 values of x,y,z for hit in global coordinates"},{"metadata":{"_uuid":"a873dc591cbbb240f76c1f17a9f6489c94ecae6a","_cell_guid":"34358c27-0f49-4c3e-9f37-7baceb266c34","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"# f, axes = plt.subplots(1, 2, figsize=(15,5));\n# sns.distplot(hits_df[0].x.values, color='red', rug=False, ax=axes[0])\n# sns.distplot(hits_df[0].y.values, color='red', rug=False, ax=axes[1])\n# axes[0].set_title(\"distribution of x coordinate of particles\");\n# axes[1].set_title(\"distribution of y coordinate of particles\");\n\n# f, axes = plt.subplots(1, 2, figsize=(15,5));\n# sns.distplot(hits_df[0].z.values, color='red', rug=False, ax=axes[0])\n# sns.regplot(x=hits_df[0][:2000].x.values, y=hits_df[0][:2000].y.values, fit_reg=False, color='#ff4c64', ax=axes[1])\n# axes[0].set_title(\"distribution of z coordinate of particles\");\n# axes[1].set_title(\"plotting of x and y of particles\");\n# axes[1].set(xlabel='x', ylabel='y');\n\nhits_small = hits_df[0][['x','y','z']]\nsns.pairplot(hits_small, palette='husl', size=6)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"06251e5d9bc78018920adaf05b5e07339fe3f705","_cell_guid":"996a64bd-f849-4a8c-aee9-251566f6bc7b"},"cell_type":"markdown","source":"### 4.2 hits.volume_id for single event"},{"metadata":{"_uuid":"d56e88b1a521af4fbb77a01ca2b44c5639066e3d","_cell_guid":"360d14f0-facb-4bf3-8e86-e886586d5ad2","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"dist(hits_df[0], 'volume_id', 20, 'red', 'hits.volume_id distribution', kde=True)\n# hits_df[0].volume_id.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0f64bf7f057670973f9c12259ac7e7e2bf1b1279","_cell_guid":"6de55d91-981a-4971-bf49-ebd09a2690bf"},"cell_type":"markdown","source":"volume_id takes these values - 7,8,9,12,13,14,16,17,18\n\n### hits.volume_id for some other events"},{"metadata":{"_uuid":"269a08d564c1a513134e33cc8afaa1b08243cbaa","_cell_guid":"6f1ed3e2-6cfb-4a2b-9607-ad2a59e6a35a","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"mdist(hits_df, 'volume_id', 20, 'green')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3157e605b1d56fb8c4f2bd0fc061108b42be7f7f","_cell_guid":"335224cc-504d-4eb8-94b5-7a5e22816432"},"cell_type":"markdown","source":"### 4.3 hits.layer_id for single event"},{"metadata":{"_uuid":"50316687f1a56d468fe724bfc345f94a33825b2f","_cell_guid":"8a4c2fb5-f480-4349-a819-065d7a1f7e19","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"dist(hits_df[0], 'layer_id', 20, 'red', 'hits.layer_id distribution', kde=False)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a55110688c8d595dfcf3cc4e7b5661808b218cf6","_cell_guid":"d5562b38-d6c3-424c-966e-de636e1953b4"},"cell_type":"markdown","source":"layer_id distinct values : 2,4,6,8,10,12,14\n\n### 4.4 hits.module_id for single event"},{"metadata":{"_uuid":"3b9f54341bf99a17bee7e12e0b83a9fb785dd0c6","_cell_guid":"93a7dba2-2d29-481b-a96a-0797736798da","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"dist(hits_df[0], 'module_id', 100, 'green', 'hits.module_id distribution')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"49728fc6ff8d652fbb9abe93d55c30fad3dbfe13","_cell_guid":"d5b426bb-ee1f-4af8-8869-a77ff9e000f9"},"cell_type":"markdown","source":"### 4.5 Distance of particles from origin\n\nlets compute the distance of particles from the origin"},{"metadata":{"_uuid":"c8b7df11a4a0646538ac7b9bc0e4c8d7d113d6e0","_cell_guid":"7e9e77c1-58ca-456b-a1eb-d6633600c478","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"hits_df[0]['origin_dis'] = np.sqrt(np.square(hits_df[0].x) + np.square(hits_df[0].y) + np.square(hits_df[0].z))\ndist(hits_df[0], 'origin_dis', 100, 'red', 'hits.origin_dis distribution')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2367a16c4fa288fbe79d29870f980998909cb3f4","_cell_guid":"2019c27c-cc78-4493-950e-286af3a8be92","_kg_hide-input":true},"cell_type":"markdown","source":"### 4.5 Initial Position of particles "},{"metadata":{"_uuid":"b018b47f41af0c1e4ab01704d1aee55c3f601400","_cell_guid":"51483b0a-8d35-447e-ad49-7ead04433e01","collapsed":true,"trusted":false},"cell_type":"code","source":"from matplotlib import pyplot\nfrom mpl_toolkits.mplot3d import Axes3D\n\nfig = pyplot.figure(figsize=(10,8))\n# plt.figure();\n\nax = Axes3D(fig)\n\nhits_sample = hits_df[0].sample(3000)\nax.scatter(hits_sample.x, hits_sample.y, hits_sample.z)\npyplot.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d79c80909bea01c80fc8c207811ad1c061d75541","_cell_guid":"07761cf8-5103-4a23-b3c8-c9d88541b385"},"cell_type":"markdown","source":"### 5. particles data\n\nParticles data consists of following variables:  \n\n\nparticle_id: numerical identifier of the particle inside the event.  \nvx, vy, vz: initial position or vertex (in millimeters) in global coordinates.  \npx, py, pz: initial momentum (in GeV/c) along each global axis.  \nq: particle charge (as multiple of the absolute electron charge).  \nnhits: number of hits generated by this particle.  \n"},{"metadata":{"_uuid":"daefbd42588d4cd8d6bf2c624a30ba471c620538","_kg_hide-output":true,"_cell_guid":"b9853fa1-eb2a-4372-a420-13c275e25773","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"# truth_df[0][truth_df[0]['particle_id'] == 414345390649769984]\n# particle_df.sort_values(['nhits'])[particle_df.nhits == 12]\n# 4513357793067008, 112595763120308224\n\nvals = list(truth_df[0][truth_df[0]['particle_id'] == 112595763120308224].hit_id.values)\ntempdf = hits_df[0][hits_df[0]['hit_id'].isin(vals)]\ntempdf\nfig = plt.figure(figsize = (10,10))\nax = Axes3D(fig)\nplt.style.use('fivethirtyeight')\ndef animate(hit_id):\n    ax.set_xlim([0,900])\n    ax.set_ylim([-600,300])\n    ax.set_zlim([-300,-10])\n    ax.set_xlabel('x')\n    ax.set_ylabel('y')\n    ax.set_zlabel('z')\n    \n#     x = tempdf.x[tempdf.hit_id == hit_id]\n#     y = tempdf.y[tempdf.hit_id == hit_id]\n#     z = tempdf.z[tempdf.hit_id == hit_id]\n    \n    \n    x = tempdf.x[tempdf.hit_id == hit_id]\n    y = tempdf.y[tempdf.hit_id == hit_id]\n    z = tempdf.z[tempdf.hit_id == hit_id]\n    s = tempdf.module_id[tempdf.hit_id == hit_id] \n#     c = tempdf.layer_id[tempdf.hit_id == hit_id]\n    ax.scatter(x, y, z, s=300)# 'o', color = 'r', markersize = 10, alpha = 0.5) \n    \nani = animation.FuncAnimation(fig, animate, tempdf.hit_id.unique().tolist())\nani.save('animation1.gif', writer='imagemagick', fps=2)\n\n\n# truth_df[0][truth_df[0]['particle_id'] == 414345390649769984]\n# particle_df.sort_values(['nhits'])[particle_df.nhits == 12]\n# 4513357793067008, 112595763120308224\n\nvals = list(truth_df[0][truth_df[0]['particle_id'] == 968275088115761152].hit_id.values)\ntempdf = hits_df[0][hits_df[0]['hit_id'].isin(vals)]\n# tempdf\nfig = plt.figure(figsize = (10,10))\nax = Axes3D(fig)\nplt.style.use('fivethirtyeight')\ndef animate(hit_id):\n#     ax.set_xlim([-60,20])\n#     ax.set_ylim([0,800])\n#     ax.set_zlim([-100,-3000])\n\n    ax.set_xlim([20,1100])\n    ax.set_ylim([-110,0])\n    ax.set_zlim([0,1300])\n\n    ax.set_xlabel('x')\n    ax.set_ylabel('y')\n    ax.set_zlabel('z')\n    \n#     x = tempdf.x[tempdf.hit_id == hit_id]\n#     y = tempdf.y[tempdf.hit_id == hit_id]\n#     z = tempdf.z[tempdf.hit_id == hit_id]\n    \n    \n    x = tempdf.x[tempdf.hit_id == hit_id]\n    y = tempdf.y[tempdf.hit_id == hit_id]\n    z = tempdf.z[tempdf.hit_id == hit_id]\n    ax.scatter(x, y, z, s=300)# 'o', color = 'r', markersize = 10, alpha = 0.5)\n    \nani = animation.FuncAnimation(fig, animate, tempdf.hit_id.unique().tolist())\nani.save('animation2.gif', writer='imagemagick', fps=2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"82a0fa8f8867a4f36e429eaa4757fdc2f5afebdc","_cell_guid":"2521d3aa-e148-4933-a0b3-59243e261b91","collapsed":true,"trusted":false},"cell_type":"code","source":"particles_df[0].head(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cb293ff93f37d3bfab8d712713d9e8d70bba34e1","_cell_guid":"80d00cbd-7a53-48dc-b8e3-bf3f1ef640a7"},"cell_type":"markdown","source":"### 5.1 Lets see the animated trajectory of a Positively Charged Particle "},{"metadata":{"_uuid":"c86ee6f40854c5a43948fe33a7ad3600a6e42cd1","_cell_guid":"0c075805-2f05-4f38-a0b8-cad5438db6be","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"filename = 'animation1.gif'\nvideo = io.open(filename, 'r+b').read()\nencoded = base64.b64encode(video)\nHTML(data='''<img src=\"data:image/gif;base64,{0}\" type=\"gif\" />'''.format(encoded.decode('ascii')))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"67670417559b3fe4bb6e02f110c434bcf3cca081","_cell_guid":"4a68de61-d4ed-460f-8b3e-55d399d34066"},"cell_type":"markdown","source":"### 5.2 Animated Trajectory of a negatively charged particle "},{"metadata":{"_uuid":"809d7ba58903484b813fa8910af2bc374913a151","_cell_guid":"8db86051-f7bd-4e01-8a51-a94dd0c4ee9d","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"filename = 'animation2.gif'\nvideo = io.open(filename, 'r+b').read()\nencoded = base64.b64encode(video)\nHTML(data='''<img src=\"data:image/gif;base64,{0}\" type=\"gif\" />'''.format(encoded.decode('ascii')))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f647b207c6219073b290e9d7947698a3af42d67d","_cell_guid":"42844b13-1be1-44bf-8b7c-e36b1cae8016"},"cell_type":"markdown","source":"### 5.3 Plotting the initial x,y,z coordinates of particles and initial momentum values for the particles"},{"metadata":{"_uuid":"bd87f6b482934739919ae87d36221110935f1e32","_cell_guid":"98dc9bf7-2b2c-4cc5-a40d-1ce49ae27793","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"f, axes = plt.subplots(1, 2, figsize=(15,3));\nsns.distplot(particles_df[0].vx.values, color='green', rug=False, ax=axes[0])\nsns.distplot(particles_df[0].px.values, color='green', rug=False, ax=axes[1])\naxes[0].set_title(\"x coordinate of particles\");\naxes[1].set_title(\"momentum of particle in x direction \");\n\nf, axes = plt.subplots(1, 2, figsize=(15,3));\nsns.distplot(particles_df[0].vy.values, color='green', rug=False, ax=axes[0])\nsns.distplot(particles_df[0].py.values, color='green', rug=False, ax=axes[1])\naxes[0].set_title(\"y coordinate of particles\");\naxes[1].set_title(\"momentum of particle in y direction \");\n\nf, axes = plt.subplots(1, 2, figsize=(15,3));\nsns.distplot(particles_df[0].vz.values, color='green', rug=False, ax=axes[0])\nsns.distplot(particles_df[0].pz.values, color='green', rug=False, ax=axes[1])\naxes[0].set_title(\"z coordinate of particles\");\naxes[1].set_title(\"momentum of particle in z direction \");\n\nf, axes = plt.subplots(1, 2, figsize=(15,5));\nsns.regplot(x=particles_df[0][:12000].vx.values, y=particles_df[0][:12000].vy.values, fit_reg=False, color='#ff4c64', ax=axes[0])\nsns.regplot(x=particles_df[0][:12000].vx.values, y=particles_df[0][:12000].vz.values, fit_reg=False, color='#ff4c64', ax=axes[1])\naxes[0].set_title(\"x and y position of particles\");\naxes[0].set(xlabel='x', ylabel='y');\naxes[1].set_title(\"x and z position of particles\");\naxes[1].set(xlabel='x', ylabel='z');","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f566fdfe7d97933e35aaadac06b07e344aceaf3f","_cell_guid":"01b5c2d4-e6b2-4fbb-91c9-2114ded11e16"},"cell_type":"markdown","source":"### 5.4 particles charge"},{"metadata":{"_uuid":"d9f60adaf16b3c9436cdc3f310d79b44ece6b375","_cell_guid":"85a9a105-6c74-4944-8475-e45e1288f1e0","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"plt.figure(figsize=(5, 4))\ncnts = particles_df[0]['q'].value_counts()\npie(cnts.values, labels=cnts.index, colors=['#8ded82', '#f45342']);\nshow()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f2e2d9363df568a1e2daa1e518623801edbf3d16","_cell_guid":"6a790d61-c671-4633-ab4b-d5d81c5dbd8a"},"cell_type":"markdown","source":"More than half particles have positive charge in this event as compared to negatively charged particles \n\n### 5.5 particles.nhits"},{"metadata":{"_uuid":"9a097f652af8b3856c099eb4eb1ea3678ec203d6","_cell_guid":"e92c3626-dd3f-4ded-bd85-4e4a30ee9041","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"dist(particles_df[0], 'nhits', 10, 'red', 'particles.nhits distribution', kde=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e97c2917ce9c793443ec2ad05c42b955ce374dd3","_cell_guid":"e1243cc8-5b1b-4494-8f1e-cb39e955d184"},"cell_type":"markdown","source":"### 5.6 Pair plotting of initial positions of the particles"},{"metadata":{"_uuid":"ea4b4a7d87430444f5aa4ad58e57de1bffe3b639","_cell_guid":"b6e93d92-44b2-42ba-a967-8a4fee87f29c","collapsed":true,"trusted":false},"cell_type":"code","source":"psmall = particles_df[0][['vx','vy','vz']]\nsns.pairplot(psmall, palette='husl', size=6)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b7b1be35990b6637dae93a6fbf2a3f8d41dfd52a","_cell_guid":"270f6cb8-9e71-4255-affd-551fb3c90bd2"},"cell_type":"markdown","source":"### 6 Truths Data\n\ntruths data consists of following variables:  \n\nhit_id: numerical identifier of the hit as defined in the hits file.  \nparticle_id: numerical identifier of the generating particle as defined in the particles file.   \ntx, ty, tz true intersection point in global coordinates (in millimeters) between the particle trajectory and the sensitive surface.  \ntpx, tpy, tpz true particle momentum (in GeV/c) in the global coordinate system at the intersection point.  \nweight per-hit weight used for the scoring metric; total sum of weights within one event equals to one.  "},{"metadata":{"_uuid":"001e0c0cd1c98d6ff6955a1b288428882e303789","_cell_guid":"67d9babc-d890-41e9-8343-b105a02cf679","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"truth_df[0].head(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5b616f56a85d3970374167cd90e4aafb8fba57a1","_cell_guid":"81afbb72-b530-41cd-a8b3-4b108f6f3e31"},"cell_type":"markdown","source":"### 6.1 Truth data Weight per hit"},{"metadata":{"_uuid":"ac113ea69b6b0ef73c2582c90be79b6b9b5919bb","_cell_guid":"d12bd737-0b55-413e-a3b4-95ae589a86f5","collapsed":true,"_kg_hide-input":true,"trusted":false},"cell_type":"code","source":"dist(truth_df[0], 'weight', 10, 'red', 'truth.weight distribution', kde=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"519291a702f6d99058e4e104388e70577fd025b8","_cell_guid":"482342fc-41d0-4cc4-915c-9d7144f08622","collapsed":true},"cell_type":"markdown","source":"Thanks for viewing the notebook. An upvote wi"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}