{"cells":[{"metadata":{"_uuid":"a9efa4476913717daf0c4430b2d3137d76c2edbe"},"cell_type":"markdown","source":"# CERN Particle Collision Data Visualizer in Python with Plotly"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\nprint(os.listdir(\"../input/train_1\")[:5])\n\n# Any results you write to the current directory are saved as output.","execution_count":1,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"collapsed":true},"cell_type":"code","source":"files=os.listdir(\"../input/train_1\")\ndf=pd.DataFrame(files).sort_values(0)","execution_count":3,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"16608929b7e03f0ecc5bcf25512db83f324a26ce","collapsed":true},"cell_type":"code","source":"df['eventID'],df['infoType']=df[0].str.split('-',1).str\ndf['infoType'],_=df['infoType'].str.split('.',1).str","execution_count":4,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df8d2eb326d8f0140c78a88ca4d4a09e8101829a","scrolled":true,"collapsed":true},"cell_type":"code","source":"event_data_types=df['infoType'].unique()\nevent_data_ids=pd.DataFrame(data=df['eventID'].unique(),columns=['eventId'])","execution_count":5,"outputs":[]},{"metadata":{"_uuid":"8d388597384af8696c1daacf89d39e85a1acf522"},"cell_type":"markdown","source":"# We will start with 1st sample"},{"metadata":{"trusted":true,"_uuid":"0aa802726bb9b814ec5216af7e48498022128d29","collapsed":true},"cell_type":"code","source":"df_cells=pd.read_csv(\"../input/train_1/\"+event_data_ids.loc[0]['eventId']+'-cells.csv')\ndf_hits=pd.read_csv(\"../input/train_1/\"+event_data_ids.loc[0]['eventId']+'-hits.csv')\ndf_particles=pd.read_csv(\"../input/train_1/\"+event_data_ids.loc[0]['eventId']+'-particles.csv')\ndf_truth=pd.read_csv(\"../input/train_1/\"+event_data_ids.loc[0]['eventId']+'-truth.csv')\n    ","execution_count":6,"outputs":[]},{"metadata":{"_uuid":"ad5ab11363c001df4cc5f7a8852650f6571d42b3"},"cell_type":"markdown","source":"# Appending more samples"},{"metadata":{"trusted":true,"_uuid":"cec319f97108c6326b0af476fef46cb2bc3f7353","collapsed":true},"cell_type":"code","source":"sample_csv_size=10\nfor i in range(1,sample_csv_size):\n    df_cells.append(pd.read_csv(\"../input/train_1/\"+event_data_ids.loc[i]['eventId']+'-cells.csv'), ignore_index=True)\n    df_hits.append(pd.read_csv(\"../input/train_1/\"+event_data_ids.loc[i]['eventId']+'-hits.csv') , ignore_index=True)\n    df_particles.append(pd.read_csv(\"../input/train_1/\"+event_data_ids.loc[i]['eventId']+'-particles.csv'), ignore_index=True)\n    df_truth.append(pd.read_csv(\"../input/train_1/\"+event_data_ids.loc[i]['eventId']+'-truth.csv'), ignore_index=True)","execution_count":7,"outputs":[]},{"metadata":{"_uuid":"71aa8a6196300303f48d9c78d9ff3ed10cbc6eee"},"cell_type":"markdown","source":"# Creating a helper function for 3d Scatter plot"},{"metadata":{"trusted":true,"_uuid":"549bf08ac48adcf6309541b0b9d4da67d3b78fa4","collapsed":true},"cell_type":"code","source":"def scatter3D(x,y,z,color,name='Undefiend 3d Scatter Plot'):\n    from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot\n    import plotly.graph_objs as go\n    init_notebook_mode(connected=True)\n    trace1 = go.Scatter3d( x=x, y=y, z=z, mode='markers',\n        marker=dict(\n            size=12,\n            color=color,           # set color to an array/list of desired values\n            colorscale='Viridis',   # choose a colorscale\n            opacity=0.8,\n            showscale=True\n        ),\n        name=name\n    )\n\n    data = [trace1]\n    layout = go.Layout( margin=dict(l=0,r=0,b=0,t=0), showlegend=True, legend=dict(x=0,y=1))\n\n    fig = go.Figure(data=data, layout=layout)\n    iplot(fig, filename=name)\n    ","execution_count":8,"outputs":[]},{"metadata":{"_uuid":"2767495de462c37f4ba03426899196019705be33"},"cell_type":"markdown","source":"# Lets Plot the Intersection point of hits in the detecters "},{"metadata":{"trusted":true,"_uuid":"545f52bfec58e88ff7f659481b984882d2fca3c5","scrolled":true,"_kg_hide-input":false,"_kg_hide-output":false},"cell_type":"code","source":"sample_size=5000\nx=df_truth['tx'].head(sample_size)\ny=df_truth['ty'].head(sample_size)\nz=df_truth['tz'].head(sample_size)\n\nweight=df_truth['weight'].head(sample_size).apply(lambda x: x*100)\n\nscatter3D(x,y,z,weight, 'Detecter hits location plot')\n","execution_count":9,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d65ff116252cf5659936c4c621da02729b5805b6","collapsed":true},"cell_type":"code","source":"df_truth.head()","execution_count":16,"outputs":[]},{"metadata":{"_uuid":"b9b9495c98c7564e87aa13a01c59840f25012d60"},"cell_type":"markdown","source":"# Visualizing the Initial Particle positions in 3d\n### chagres in the color dimention "},{"metadata":{"trusted":true,"_uuid":"5ac805d24c38c7e67c1c2da21d54e3a9befd6223","scrolled":false,"collapsed":true},"cell_type":"code","source":"sample_size=200000\nx=df_particles['vx'].head(sample_size)\ny=df_particles['vy'].head(sample_size)\nz=df_particles['vz'].head(sample_size)\n\ncharge=df_particles['q'].head(sample_size)\n\nscatter3D(x,y,z,charge,\"Particle's Initial locations\")","execution_count":19,"outputs":[]},{"metadata":{"_uuid":"eca4ea7620fa6e6ef10fa1baa228a52063fa2973"},"cell_type":"markdown","source":"# Lets Draw Points From Single Particle"},{"metadata":{"trusted":true,"_uuid":"cbbeb823de03bb83a9cba5f684b0c97b0284bb84","collapsed":true},"cell_type":"code","source":"sample_particle_df=df_truth.loc[df_truth['particle_id']==418835796137607168]","execution_count":17,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"102b3a4741dd9d3fbc82db3d1e9afd215df59d70","collapsed":true},"cell_type":"code","source":"sample_particle_df","execution_count":18,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03f902f80eda350673358631576ed9d0aae5ec48","scrolled":false,"collapsed":true},"cell_type":"code","source":"x=sample_particle_df['tx']\ny=sample_particle_df['ty']\nz=sample_particle_df['tz']\n\nweight=sample_particle_df['weight'].apply(lambda x: x*100)\n\nscatter3D(x,y,z,weight, 'Single particle hit location plot')","execution_count":1,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"68d03515c220272e3c0e5b247966ef3c1abafa7d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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}