# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # What is CMS?

# <img src="images/cern_cms_endon.jpg" width="500"/>

# $$pp \rightarrow \chi \chi$$

# The <a href="http://en.wikipedia.org/wiki/Compact_Muon_Solenoid">CMS detector</a> is designed to measure the properties of particles produced in the collisions of high energy protons at the LHC. 
#
# The CMS detector is made of many. many sub-detectors and when the particles are created in each proton-proton collision, they fly through these detectors and we are able to measure in which direction all these particles went.

# <img src="images/small_cms1.png" width="500"/>

# These types of displays of the detector can be challenging to understand and at the end of the day, it's not what we scientists actually analyze. We use this information to get the electric charge, energy, and momentum of these particles, and that's about it.
#
# <b>Let's go take a look at some of <i>that</i> data!</b>

# The first step is to import some helper functions. One is to get the collisions data out of the files, and the other is to display the particles that are produced in these collisions. 

import tools.cms as cms

# Next, we will open the file and pull out the collision data. This will return a Python list of all the collisions in that file.
#
# You can use these data to visualize individual collisions or to perform a data analysis on <i>all</i> the collisions.

# +
filehandler = open('./data/small_cms_test_file.dat', 'r')
collisions = cms.get_collisions(filehandler)
number_of_collisions = len(collisions)

print("# of proton-proton collisions: {0}".format(number_of_collisions))
# -

# Let's take a look at some of these collisions!

# %matplotlib inline

cms.display_collision3D(collisions[3])

cms.display_collision3D(collisions[3])

cms.display_collision3D(collisions[6])

# Animate - NOT WORKING RIGHT NOW...
'''
from IPython.display import clear_output,display
import time

fig = plt.figure(figsize=(6,4))

for i in range(0,10):
    cms.display_collision3D(collisions[i],fig=fig)
    time.sleep(0.01)
    clear_output(wait=True)
    display(fig)
    fig.clear()
''';

# What are we looking at here?
#
# * The red lines represent the protons colliding.
# * The other lines represent particles created in the collisions. The length of these lines tell us how much momentum (or energy) they have. The colors are different particles/object.
#     * Blue - <a href="http://en.wikipedia.org/wiki/Muon">muons</a>
#     * Orange - <a href="http://en.wikipedia.org/wiki/Jet_(particle_physics)">jets</a>
#     * Green - <a href="http://en.wikipedia.org/wiki/Jet_(particle_physics)">electrons</a>
#     * Gray - <a href="http://en.wikipedia.org/wiki/Photon">photons</a> 

# You can also make plots of the properties of the particles.

# +
energies = []

for collision in collisions:
    
    jets,muons,electrons,photons,met = collision
    
    for jet in jets:
        energy,px,py,pz,btag = jet
        energies.append(energy)
    
plt.figure(figsize=(4,4))
h = plt.hist(energies)
# -

# So now you know how to play around with data from the CMS experiment at the Large Hadron Collider. What do you want to do next?  :)



StarcoderdataJupyterScriptsDedupFiltered