<jupyter_start><jupyter_text>Importing a CMS open data file and creating overlaid histograms to analyze data This activity will help you practice with Jupyter Notebook. It is recommended that you select *Kernel $\rightarrow$ Restart & Clear Output* before going through the notebook. This way you may start fresh without previous outputs.Select a data file to analyze from [opendata.cern.ch](http://opendata.cern.ch/record/545).For the example, download Dimuon_DoubleMu.cvs and save it to a folder named *Data*. Note, there are other files available, but the file must be comma separated values (.csv). If the file name has spaces between the words replace them with underscores.Boxes, like this, that give instruction but not code, can be entered by pressing "b" and selecting "Markdown" from the dropdown menu above. To enter code, select "Code" from the dropdown menu. Cell content may be copied from other sources, pasted and edited.First, you will need to import the packages pandas and matplotlib.pyplot to be able to read files and plot histogram.<jupyter_code>import pandas
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>After entering a code box, press *Ctrl + Enter* at the same time to run the code. An asterisk will appear in the *In[ ]* while the command is being processed. Wait until a number appears in that location before proceeding. Possible error messages will appear in pink and will indicate information regarding the error. To use the file you have downloaded, it must be saved into a variable. Type the file location and name exactly as it appears on your computer. In this example, the file is located one directory up in a folder named *Data*. The path is therefore  *..Data/Dimuon_DoubleMu.csv*.  Save the data into variable *dataset* and check the contents of the first 5 rows.<jupyter_code>dataset = pandas.read_csv('../Data/Dimuon_DoubleMu.csv')

dataset.head()<jupyter_output><empty_output><jupyter_text>For the given example, the invariant mass is of particular interest. Save the invariant mass column from the *dataset* into a variable *invariant_mass* by using the column heading as it appears in the table, e.g. *M*. If your data set does not contain invariant masses by default you must first [calculate them](https://github.com/cms-opendata-education/cms-jupyter-materials-english/blob/master/Exercises-with-open-data/Calculate-invariant-mass.ipynb). Plot a histogram, stating which variable to plot, setting the number of bins and range.In the example below, we use 50 bins and plot from 0 to 200 GeV.<jupyter_code>invariant_mass = dataset['M']

plt.hist(invariant_mass, bins=50, range=(0,200))
plt.show()<jupyter_output><empty_output><jupyter_text>The number of bins and range can be varied in order to analyze the data more clearly. Below are commands to replot the graph with a title and axis labels.<jupyter_code>plt.xlabel('Invariant mass [GeV]')
plt.ylabel('Number of events')
plt.title('The histogram of the invariant masses of two muons \n')

plt.hist(invariant_mass, bins=50, range=(0,200))
plt.show()<jupyter_output><empty_output><jupyter_text>The graph above demonstrates background events below approximately 50 GeV and a peak at approximately 90 GeV.Mathematical operations, such as addition or subtraction, may be performed on the data by defining new variables which allows the data to be further sorted. In the example below, the original data is divided into two new data sets based on the energy of the collision. Each data set is given a name and organized in this case by high energy (> 150 GeV), and low energy (< 150 GeV).<jupyter_code>newsethighE = dataset[dataset.E1+dataset.E2>150]
newsetlowE = dataset[dataset.E1+dataset.E2<150]<jupyter_output><empty_output><jupyter_text>The new data sets can be plotted separately as was done previously or on one plot. The two histograms can be overlaid by adjusting the transparency using the alpha command. Labels for each data set are included in the legend located in the upper right corner. We can also change the range to focus on the event of interest.<jupyter_code>plt.xlabel('Invariant mass [GeV]')
plt.ylabel('Number of events')
plt.title('The invariant masses of two muons comparing high and low energy\n')
plt.hist(newsetlowE ['M'], bins=50, range=(80,100),alpha=0.5, label='Low E')
plt.hist(newsethighE ['M'], bins=50, range=(80,100),alpha=0.5, label='High E')
plt.legend (loc='upper right')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07611840640997107, 'fraction_numerical': 0.013354106387714223, 'mean_word_length': 5.3835227272727275, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '208776', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1207, 'n_words': 587, 'path': 'Exercises-with-open-data/Overlaid-histograms.ipynb', 'repo_name': 'ShaliniPandurangan/cms-jupyter-materials-english-1.0', 'size': '4493'}