6 views
Intensity Limitations in Hadron Beams CAS 2025 - Wire Scanner Case Study ================================ # TL;DR :::danger **This part explains how to install the necessary packages for the Hands-on in less than a coffee break. Please proceed with that part if you don't have a lot of time. Otherwise, go [here](https://codimd.web.cern.ch/sKxGrJvDRdG2EM8OT3kYHw?view#Introduction) for more details.** ::: **0. You'll need your own laptop, you'll be paired with someone if you don't have one.** **1. Install a `Python3` distribution, we recommend [Anaconda](https://www.anaconda.com/download) or Python>=3.9.** **2. Open the `Anaconda Navigator` and run `Jupyter Notebook`** ![](https://codimd.web.cern.ch/uploads/upload_d7b8ac073922273e55d81e28f609b98a.png) **3. Installation is done! Just download the notebook and support functions into your folder from the [Indico](https://indico.cern.ch/event/1466612/contributions/6448899/) Note: the install command is required only once and doesn't need to be called for every notebook.** **4. To verify the setup, run the first cell of `main.ipynb` after downloading the notebook and support_functions.py script.** ![](https://codimd.web.cern.ch/uploads/upload_aea17b569ec162d79039dccb347c37d2.gif) :::success **You are now set and ready to go. You can still read the text below for more information (longer than a coffee break).** **In particular, give a look to the following [Python Cheat sheet](https://indico.cern.ch/event/1466612/contributions/6448899/) that covers all relevant python aspects for the hands-on.** ::: # Introduction During the [Intensity Limitations in Hadron Beams CAS 2025 in Borovets, Bulgaria](https://indico.cern.ch/event/1466612/), we will use Python as scripting language for the *Beam Induced Power in the SPS Wire Scanner Case Study*. We kindly ask you to make some homework before coming to **prepare yourself (and your laptop) for the course**. :::warning **We strongly suggest to use Python for the course.** ::: A basic knowledge of Python is assumed, therefore if you are not familiar with it you can find, in the following sections, few resources to fill the gap. During the school, feel free to ask questions to the tutors outside of the hands-on sessions. Should you require some help, we will be pleased to discuss over coffee or in the evening after the courses. During the course we will use **Python3** in a **Jupyter notebook** for the development. The following packages and dependacies will be needed for the hands on: **numpy**, **scipy** and **matplotlib** (as well as **jupyter** and **ipympl** for the notebook). We will explain in the following sections how to install this software on your laptops. After a short introduction where we provided some useful links to get familiar with Python, we will focus on the software setup. The layout of this document is the following: 1. Installation instructions 2. An introduction to Python 3. More information to go beyond # 1. Installation instructions ## With Anaconda **0. You'll need your own laptop, you'll be paired with someone if you don't have one.** **1. Install a `Python3` distribution, we recommend [Anaconda](https://www.anaconda.com/download) or Python>=3.9.** **2. Open the `Anaconda Navigator` and run `Jupyter Notebook`** ![](https://codimd.web.cern.ch/uploads/upload_d7b8ac073922273e55d81e28f609b98a.png) **3. Installation is done! Just download the notebook and support functions into your folder from the [Indico](https://indico.cern.ch/event/1466612/contributions/6448899/) Note: the install command is required only once and doesn't need to be called for every notebook.** **4. To verify the setup, run the first cell of `main.ipynb` after downloading the notebook and support_functions.py script.** ![](https://codimd.web.cern.ch/uploads/upload_aea17b569ec162d79039dccb347c37d2.gif) ## With a default python installation 0. Install `Python` from the [official page](https://www.python.org/downloads/) 1. Open a terminal and go to your favorite folder 2. Create an python environment specific for the tracking hands-on, and activate it ```bash python -m venv env source env/bin/activate ``` 3. Install the necessary packages and dependencies ``` pip install numpy scipy matplotlib ipympl jupyter ``` 4. Run a jupyter notebook instance ``` jupyter notebook . ``` # 2. An introduction to Python ### Please have a look to the following [Python Cheat sheet](https://indico.cern.ch/event/1466612/contributions/6448899/). Getting experienced with the content of the notebook will make you efficient during the hands-on. ### Indexing Generate a random array and select specific elements: ```python= import numpy as np # Create an array array1d = np.random.uniform(size=10) # Print selected elements print("Entire array: " + str(array1d) + "\n") print("Specific element: " + str(array1d[5]) + "\n") print("Last element: " + str(array1d[-1]) + "\n") print("Specific elements: " + str(array1d[3:7]) + "\n") print("First 5 elements array: " + str(array1d[:5]) + "\n") print("Last 5 elements array: " + str(array1d[5:]) + "\n") ``` will result in, e.g.: ```bash Entire array: [0.09402447 0.05647033 0.79670378 0.60573004 0.81588777 0.97863634 0.51376609 0.19763518 0.7649532 0.59285346] Specific element: 0.9786363385079204 Last element: 0.5928534616865488 Specific elements: [0.60573004 0.81588777 0.97863634 0.51376609] First 5 elements array: [0.09402447 0.05647033 0.79670378 0.60573004 0.81588777] Last 5 elements array: [0.97863634 0.51376609 0.19763518 0.7649532 0.59285346] ``` ### Implicit loops In contrast to programming languages like C++, Python can handle vectors. No loop is required, e.g. to multiply each element with a constant or squaring it: ```python= import numpy as np # Create an array array1d = np.random.uniform(size=10) print("Entire array: " + str(array1d) + "\n") print("Each element muliplied by 5: " + str(5 * array1d) + "\n") print("Each element squared: " + str(array1d**2) + "\n") print("Square root of each element: " + str(np.sqrt(array1d)) + "\n") ``` will result in, e.g.: ```bash Entire array: [0.2240143 0.35153156 0.68864907 0.14062298 0.77280195 0.26872206 0.9135403 0.8776261 0.26158576 0.93883652] Each element muliplied by 5: [1.12007151 1.75765782 3.44324537 0.70311488 3.86400975 1.34361029 4.56770149 4.3881305 1.30792879 4.69418259] Each element squared: [0.05018241 0.12357444 0.47423755 0.01977482 0.59722285 0.07221154 0.83455588 0.77022757 0.06842711 0.88141401] Square root of each element: [0.47330149 0.59290097 0.82984883 0.3749973 0.87909155 0.51838408 0.95579302 0.936817 0.51145455 0.96893577] ``` ### Plotting Or you can test a simple plot: ```python= import matplotlib.pyplot as plt # check a simple plot %matplotlib inline plt.plot([0,10],[0,10],'ob-') plt.xlabel('My x-label [arb. units]') plt.ylabel('My y-label [arb. units]') plt.title('My title') ``` ![](https://codimd.web.cern.ch/uploads/upload_b7610a37c41729a79bc4b0a5d863594b.png) # 3. More information to go beyond ### Python packages You can leverage python's capability by exploring a galaxy of packages. Below you can find the most useful for our course (focus mostly on `numpy`, `scipy` and `matplotlib`) and some very popular ones. ### The *numpy* package To get familiar with the *numpy* package have a look at the following [summary poster](https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Numpy_Python_Cheat_Sheet.pdf). ![](https://codimd.web.cern.ch/uploads/upload_6ffb4d07b1ebb895528f2a34aae41ec6.png) You can google many other resources, but the one presented of the poster covers the set of instructions you should familiar with. ### The *scipy* package To get familiar with the *scipy* package have a look at the following [summary poster](https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python_SciPy_Cheat_Sheet_Linear_Algebra.pdf). ![](https://codimd.web.cern.ch/uploads/upload_4360a12d1ac256b45d5453e543839607.png) You can google many other resources, but the one presented of the poster covers the set of instructions you should familiar with. ### The *matplotlib* package To get familiar with the *matplotlib* package have a look at the following [summary poster](https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python_Matplotlib_Cheat_Sheet.pdf). ![](https://codimd.web.cern.ch/uploads/upload_4b54812812e21978b600b860ba1ddf5b.png) # Final words :::success **Looking forward to see you in Borovets!** :::