{"cells":[{"metadata":{"_uuid":"ba9eee5dd211fbc78d768c93f25aa1aa4f764a15"},"cell_type":"markdown","source":"\n## Now, we expect that particles that hit a detector along the detector's normal axis will traverse fewer cells, but let's see if we can prove this\n## Also, what can we learn about true vs spurious hits by looking at the cells that were hit"},{"metadata":{"_uuid":"2404bd67955f24240b69aa2aa1137933acfe3a10"},"cell_type":"markdown","source":"### load the libraries"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true,"_kg_hide-output":true,"collapsed":true},"cell_type":"code","source":"%matplotlib inline\nimport matplotlib.pyplot as plt\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport glob\nimport seaborn as sns\nplt.style.use('seaborn-deep')\nFILE_FORMAT = \"../input/train_1/event000001000-{}.csv\"","execution_count":132,"outputs":[]},{"metadata":{"_uuid":"5c82c4f10e26b24edc7a7ed12629b772306a60c8"},"cell_type":"markdown","source":"### read the first training event"},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true,"collapsed":true,"_uuid":"765d47c8dbc6fcbb2bb0eca3c55fadcf57f47870"},"cell_type":"code","source":"truth = pd.read_csv(FILE_FORMAT.format(\"truth\"))\nhits = pd.read_csv(FILE_FORMAT.format(\"hits\"))\nparticles = pd.read_csv(FILE_FORMAT.format(\"particles\"))\ncells = pd.read_csv(FILE_FORMAT.format(\"cells\"))","execution_count":133,"outputs":[]},{"metadata":{"_uuid":"946d59511896d77be878b63f01336c06ddba0344"},"cell_type":"markdown","source":"### count the number of cells per hit and the total charge deposited per hit, add this to the hits table"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"_kg_hide-input":true,"_kg_hide-output":true,"collapsed":true},"cell_type":"code","source":"cellagg = cells.groupby(\"hit_id\").agg({\"value\" : [\"sum\", \"count\"]})\ncellagg.columns = cellagg.columns.droplevel()\ncellagg.reset_index(inplace=True)\ncellagg.columns = [\"hit_id\", \"cell_sum\", \"cell_count\"]\nhits = pd.merge(hits, cellagg, on=\"hit_id\")","execution_count":134,"outputs":[]},{"metadata":{"_uuid":"db9d38d536291d3903e0b897d34008505e8571cc"},"cell_type":"markdown","source":"### compute the true momentum inclination (theta), and add this to the hits\nNote: theta = 0 implies that the particle is moving along the positive z-axis, while theta=180 implies that it's moving along the negative z-axis. Theta = 90 implies that the particle is moving perpendicular to the z-axis in a radially outward direction."},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true,"_uuid":"d0cec790fc412df11b5ea0be54a8eb262a79bbec","collapsed":true},"cell_type":"code","source":"truth['theta'] = np.degrees(np.arctan2(np.sqrt(truth.tpy.values ** 2 + truth.tpx.values ** 2), truth.tpz.values))\ntruth['phi'] = np.degrees(np.arctan2(truth.tpy.values, truth.tpx.values))\nhits = pd.merge(hits, truth, on=\"hit_id\")","execution_count":135,"outputs":[]},{"metadata":{"_uuid":"b15e29a5b14f4278cb95da0a0696423bdc330dac"},"cell_type":"markdown","source":"## let's look at the distribution of cell_count and cell_sum for real vs spurious hits"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"feff2c312337ea8fed8202edb9a215ce6a012e04","collapsed":true},"cell_type":"code","source":"plt.figure(figsize=(18,6))\nplt.subplot(121)\nbuckets = np.arange(60)\nplt.hist(hits.query(\"weight==0\")[\"cell_count\"].values, label=\"spurious\", alpha=.5, normed=True, bins=buckets)\nplt.hist(hits.query(\"weight>0\")[\"cell_count\"].values, label=\"real\", alpha=.5, normed=True, bins=buckets)\nplt.xlabel(\"cell count\")\nplt.grid()\nplt.legend()\nplt.subplot(122)\nbuckets = np.linspace(0, 10, 100)\nplt.hist(hits.query(\"weight==0\")[\"cell_sum\"].values, label=\"spurious\", alpha=.5, normed=True, bins=buckets)\nplt.hist(hits.query(\"weight>0\")[\"cell_sum\"].values, label=\"real\", alpha=.5, normed=True, bins=buckets)\nplt.xlabel(\"cell value sum\")\nplt.grid()\n_ = plt.legend()","execution_count":136,"outputs":[]},{"metadata":{"_uuid":"a7a9ab60a8a4bbce383604f97ea9b86adab53ee0"},"cell_type":"markdown","source":"## the discrete values are coming from some of the volumes, so let's plot the above figure for each volume"},{"metadata":{"_kg_hide-input":true,"trusted":true,"scrolled":false,"_uuid":"cda64ac1d3552ae6b5869bffb31c9ca82593f51b","collapsed":true},"cell_type":"code","source":"for volume_id in sorted(set(hits.volume_id.values)):\n    plt.figure(figsize=(18,6))\n    plt.suptitle(\"Volume {}\".format(volume_id))\n    plt.subplot(121)\n    buckets = np.arange(60)\n    plt.hist(hits.query(\"(volume_id=={}) and (weight==0)\".format(volume_id))[\"cell_count\"].values, label=\"spurious\", alpha=.5, normed=True, bins=buckets)\n    plt.hist(hits.query(\"(volume_id=={}) and (weight>0)\".format(volume_id))[\"cell_count\"].values, label=\"real\", alpha=.5, normed=True, bins=buckets)\n    plt.xlabel(\"cell count\")\n    plt.grid()\n    plt.legend()\n    plt.subplot(122)\n    buckets = np.linspace(0, 10, 100)\n    plt.hist(hits.query(\"(volume_id=={}) and (weight==0)\".format(volume_id))[\"cell_sum\"].values, label=\"spurious\", alpha=.5, normed=True, bins=buckets)\n    plt.hist(hits.query(\"(volume_id=={}) and (weight>0)\".format(volume_id))[\"cell_sum\"].values, label=\"real\", alpha=.5, normed=True, bins=buckets)\n    plt.xlabel(\"cell value sum\")\n    plt.grid()\n    _ = plt.legend()\n    plt.show()","execution_count":150,"outputs":[]},{"metadata":{"_uuid":"6cafc617db1b3f990f04adc9b6065fdba667c81d"},"cell_type":"markdown","source":"## let's see the distribution of cell_count for spurious hits in each volume"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"110b40b65bc733cbc530400741f2fe7dfd90ae6a","collapsed":true},"cell_type":"code","source":"plt.figure(figsize=(18,6))\nbuckets = np.arange(60)\nfor volume_id in sorted(set(hits.volume_id.values)):\n    plt.hist(hits.query(\"(volume_id=={}) and (weight==0)\".format(volume_id))[\"cell_count\"].values, label=str(volume_id), alpha=.5, normed=True, bins=buckets)\nplt.xlabel(\"cell count\")\nplt.grid()\n_ = plt.legend(title=\"volume\")","execution_count":155,"outputs":[]},{"metadata":{"_uuid":"d571187b711971192bf387b80fcb6c9254e8078b"},"cell_type":"markdown","source":"## is there any difference in the spurious hits' cell count for volume 8 across the various layers?"},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":false,"trusted":true,"_uuid":"686d1da00d38e4b5869f1b35323f8658504fad79","collapsed":true},"cell_type":"code","source":"plt.figure(figsize=(18,6))\nbuckets = np.arange(60)\nfor layer_id in sorted(set(hits.query(\"volume_id==8\").layer_id.values)):\n    plt.hist(hits.query(\"(volume_id==8) and (layer_id=={}) and (weight==0)\".format(layer_id))[\"cell_count\"].values, label=str(layer_id), alpha=.5, normed=True, bins=buckets)\nplt.xlabel(\"cell count\")\nplt.grid()\n_ = plt.legend(title=\"layer\")","execution_count":154,"outputs":[]},{"metadata":{"_uuid":"4e23843ce7e4b1727fb927f5361f415b66de55b6"},"cell_type":"markdown","source":"## Finally, let's take a look at the distribution of theta as a function of the cell_count for real hits"},{"metadata":{"trusted":true,"_uuid":"3cab2872fd7c0a5f268fc7d0291c5a1c92379513","_kg_hide-input":true,"collapsed":true},"cell_type":"code","source":"plt.figure(figsize=(18, 12))\nbuckets = np.arange(181)\nfor cnt in range(1, 10):\n    plt.hist(hits.query(\"(weight > 0) and (cell_count == {})\".format(cnt)).theta.values, normed=True, alpha=.5, label=str(cnt), bins=buckets)\nplt.hist(hits.query(\"(weight > 0) and (cell_count > 9)\").theta.values, normed=True, alpha=.25, label=\">9\", bins=buckets, fc='k')\nplt.legend(title=\"cell count\")\n_ = plt.xlabel(\"theta\")","execution_count":152,"outputs":[]},{"metadata":{"_uuid":"accdf87dcfccdc664c0ebf72109956d2b0018178"},"cell_type":"markdown","source":"## So, there is clearly some signal here; let's repeat the above analysis on a per-volume basis"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"b9d49d577a9e9f2d436c0b0e149be75d908c83e4","collapsed":true},"cell_type":"code","source":"for vol in sorted(set(hits.volume_id.values)):\n    plt.figure(figsize=(18, 12))\n    plt.title(\"Volume {}\".format(vol))\n    buckets = np.arange(181)\n    for cnt in range(1, 10):\n        plt.hist(hits.query(\"(weight > 0) and (cell_count == {} and (volume_id == {}))\".format(cnt, vol)).theta.values, normed=True, alpha=.5, label=str(cnt), bins=buckets)\n    plt.hist(hits.query(\"(weight > 0) and (cell_count > 9) and (volume_id == {})\".format(vol)).theta.values, normed=True, alpha=.25, label=\">9\", bins=buckets, fc='k')\n    plt.legend(title=\"cell count\")\n    plt.xlabel(\"theta\")\n    plt.show()","execution_count":153,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"269d3f6fbdb6c5e6c983776badb6488aa01a04b6"},"cell_type":"markdown","source":"# does the same thing hold for phi? (it shouldn't)"},{"metadata":{"trusted":true,"_uuid":"be6b33998cb85fec365333c80b701b173aa4c3bf","_kg_hide-input":true,"collapsed":true},"cell_type":"code","source":"plt.figure(figsize=(18, 12))\nbuckets = np.arange(-180, 181)\nfor cnt in range(1, 10):\n    plt.hist(hits.query(\"(weight > 0) and (cell_count == {})\".format(cnt)).phi.values, normed=True, alpha=.5, label=str(cnt), bins=buckets)\nplt.hist(hits.query(\"(weight > 0) and (cell_count > 9)\").phi.values, normed=True, alpha=.25, label=\">9\", bins=buckets, fc='k')\nplt.xlabel(\"phi\")\n_ = plt.legend(title=\"cell count\")","execution_count":151,"outputs":[]},{"metadata":{"_uuid":"ff6eb60bbcf552596845028be0449c59817dc3e6"},"cell_type":"markdown","source":"# Conclusions\n- The distribution of cell counts for false hits is very similar across all volumes and layers\n- The cell counts for real hits depends very closely on the inclination angle of the particle motion\n\n"}],"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}