POST SIMULATION PROCESS AND BEYOND …

Alexander S. Aryshev, Ph.. Assistant Professor KEK: High Energy Accelerator Research Organization, 1-1 Oho, Tsukuba 305-0801, Ibaraki-ken, Japan. TEL: +81-298-64-5715, FAX: +81-298-64-0321. e-mail: [email protected]

3rd international school on Beam Dynamics and Accelerator technology, 25 Feb. – 3 Mar. 2021 Outline • EPICS: – Introduction, History, Usage and Examples – Channel Access, its clients – IOC, Records, Process variables • KEK LUCX experience • Python: – Introduction, Examples, Links, Versions, Development environment • Matplotlib: – Different ways, Examples • GUI programming: – Basic concepts, GUI in python: PyQt • Python non-linear fitting • KEK LUCX ASTRA simulation • ASTRA post-processing: – ASTRA output file – ASTRA particle tracker output processing – Examples • Summary 3rd international school on Beam Dynamics 02 March 2021 2 and Accelerator technology What is EPICS?

EPICS is an abbreviation for: Experimental Physics and Industrial Control System EPICS is: EPICS • A collaboration • A software tool kit • A control system architecture

EPICS: • supports distributed control systems for large research facilities like accelerators • uses Client/Server and Publish/Subscribe methods • uses the Channel Access (CA) network protocol

3rd international school on Beam Dynamics 02 March 2021 3 and Accelerator technology GTA: Ground Test Accelerator The History APS: Advanced Photon Source –In1989 started a collaboration between Los Alamos National Laboratory and Argonne National Laboratory (Jeff Hill, Bob Dalesio & Marty Kraimer) –More than 150 licenses agreements were signed, before EPICS became Open Source in 2004 –Team work on problems, for example over “Tech Talk” mailing list –Two collaboration meetings every year (spring and autumn) in turns in America, Asia, and Europe –Collaborative efforts vary • Assistance in finding bugs • Share tools, schemes, and advice

3rd international school on Beam Dynamics 02 March 2021 4 and Accelerator technology What does EPICS Control?

For example Swiss Light Source (SLS at PSI) Accelerator components to control: ca. 200 computers ca. 600 magnets (+power supplies) 300 vacuum pumps 9 cavity structures ca. 150 beam position monitors

21 beamlines (together) ca. 300 computers 10 undulator magnets more than 1200 motors

Distances between components (storage ring with 130 m diameter): 50 km power cable and more than 500 km signal cable

3rd international school on Beam Dynamics 02 March 2021 5 and Accelerator technology EPICS – Who is Using That?

Some members of the collaboration (not complete!): – ANL (APS Accelerator, APS Beamlines, IPNS) in Chicago, USA – LANL in Los Alamos, USA – ORNL (SNS) in Oak Ridge, USA – SLAC (SSRL, LCLS) in Standford, USA – JLAB (CEBAF) in Newport, USA – DESY in Hamburg, Deutschland – BESSY in Berlin, Deutschland – PSI (SLS, HIPA, PROSCAN, SwissFEL) in Villigen, Schweiz – KEK in Tsukuba, Japan – DIAMOND Light Source (Rutherford Appleton Laboratory) in Oxfordshire, England – Iter (Test Fusion Power Plant), South France – ESS (European Spallation Source), Lund, Sweden – … and many other

3rd international school on Beam Dynamics 02 March 2021 6 and Accelerator technology ITER-FPSC- ITER vBetaVersionPanelsOPI -UserManual.doc, Nadine Utzel, 2011

3rd international school on Beam Dynamics 02 March 2021 7 and Accelerator technology ORNL ‘CG-1D’ Beam Line

Neutron Tomography, EPICS/CSS since Jan. 2013

3rd international school on Beam Dynamics 02 March 2021 8 and Accelerator technology ORNL SNS ‘VULCAN’ Beam Line

Engineering Diffractometer, EPICS/CSS since March 2014 3rd international school on Beam Dynamics 02 March 2021 9 and Accelerator technology Are There Alternatives to EPICS?

System Name: • EPICS Collaborations: Used at more than one Lab • TANGO Pro: Contra: Bugs are already found Complicated to adapt to your problems • DOOCS Single Site Systems: Developed and used in one Lab • Tine Pro: Contra: Your problems solved You are on your own perfectly (no one can help) • PVSS Commercial System (CERN) Pro: Contra: Outsource your problems Expensive

3rd international school on Beam Dynamics 02 March 2021 10 and Accelerator technology The Architecture of EPICS

Network based Client/Server Model (hence the EPICS logo):

A Server provides information and EPICS service CA CA Client Client A Client uses the service or asks for the CA CA information Server Server

• For EPICS, client and server refer to their Channel Access role: i.e. Channel Access Client and Channel Access Server • This standard control system architecture is often called 3 tier architecture or 3 layer model

3rd international school on Beam Dynamics 02 March 2021 11 and Accelerator technology What is EPICS? *

My Special • Any tool/program/application Data that abides by the Channel Access Collection protocol could be described as Program “EPICS Compliant”. StripTool MEDM

CAC CAC CAC

. EPICS can be viewed as a “toolkit” of EPICS

compliant programs. One CAS CAS CAC CAS CAC can select the appropriate My tool for their need or Accelerator iocCore LabView Simulator (PVs) (PVs) develop their own. Code (PVs)

3rd international school on Beam Dynamics 12 and Accelerator technology What is Channel Access?

• A protocol how to transfer data over network • A single data unit is called Process Variable • A Process Variable has a unique name, which is used to refer to the data (Naming convention needed!) • The detailed operation of Channel Access is unimportant for most (it already works…) • Channel Access is not dependent on a single programming language (native library in /C++, , and C# available) • The entire set of Process Variables establish a Distributed Real-time Database of machine status, information and control parameters

EPICS Database CAS CAS CAS CAS Process Process Process Process Variables Variables Variables Variables 3rd international school on Beam Dynamics 02 March 2021 13 and Accelerator technology Channel Access Network Flow

1. Query: Broadcast

CA Client 2. Answer: direct connection 3. All further queries and answers work directly (Point-To-Point)

Network

CA Server 1 CA Server 2 CA Server 3

3rd international school on Beam Dynamics 02 March 2021 14 and Accelerator technology Channel Access Commands

“connection “get” “put” or “set a monitor” request” or or “caput” “search request” “caget” Change its Notify me when the value Who has a PV named What is its value to changes Channel Access Client “S1A:H1:CurrentAO”? value? 30.5 CA Client CA Server I do. 25.5 OK, it is It is It is It is Channel Access Server AMPS now 30.5 now now now 20.5 10.5 0.0023 Process Variables: AMPS AMPS AMPS “put S1A:H1:CurrentAO complete” 30.5 is too high. It “post an event” S1:P1:x is now set to the or maximum value or S1:P1:y of 27.5. “post a monitor” You are not authorized to S1:G1:vacuum or change this value

3rd international school on Beam Dynamics 02 March 2021 15 and Accelerator technology How is it Done?

Channel Access Client

Channel Access Client Channel Access Client Operator Network (Channel Access Protocol) Channel Access Server

Process Variables: Computer Power S1A:H1:CurrentAO Interface Supply

Beam S1:P1:x Computer Interface Position S1:P1:y Monitor

Vacuum S1:G1:vacuum Computer Interface Gauge Machine

3rd international school on Beam Dynamics 02 March 2021 16 and Accelerator technology 2015 How is it Done? KEK LUCX case General schema Overhead monitors

fstb.rfgtb.kek.jp FSTB LTL (Laser

(Ti:Sa laser tunnel LUCX system) Transpo rt Line) Motion controls

THz detector Screens EPICS FCT Data taking & analysis beta.rfgtb.kek.jp ICT Scripts PD

thz.rfgtb.kek.jp CAMAC Qt + qcustomplot + epicsqt PyQt + numpy, Scipy + pyepics

3rd international school on Beam Dynamics 02 March 2021 17 and Accelerator technology How is it Done? KEK LUCX case2019 Laser Test Bench (LTB) fs Test Bench (FSTB) LUCX control room LUCX Laser hut above ATF-II Ti:Sa hut Wall monitors LUCX PC’s tunnel LabView Motion fstb.rfgtb.kek.jp LTL controls Ti:Sa laser beta.rfgtb.kek.jp System (Laser Diagnostics Transpo Soft Developments ltb-.rfgtb.kek.jp rt Att. Line) Control alpha.rfgtb.kek.j ltl-win.rfgtb.kek.jp RPI p

EPICS

RPI’s

Yb fiber x Yb fiber laser Scripts and GUI laser RF. Motion Timing System monitor spectra controls ltb- win.rfgtb.kek.jp thz.rfgtb.kek.jp THz detector fstb-linux.rfgtb.kek.jp CAMAC Yb fiber Yb fiber Yb fiber Ti:Sa laser FCT laser laser laser Opt. System Power Power spectra Diagnostics ICT Supplies meters and 3rd internationalControl school on Beam Dynamics PD 02 March 2021 18 and Accelerator technology KEK LUCX, Labview EPICS clients

3rd international school on Beam Dynamics 02 March 2021 19 and Accelerator technology What is EPICS - Conclusion

• EPICS is a control system architecture • EPICS is the connection between accelerator and operator • EPICS is based on a transfer protocol named Channel Access • Channel Access works with data sets called Process Variables • EPICS is used by many institutes all over the world 3rd international school on Beam Dynamics 02 March 2021 20 and Accelerator technology IOC means What is an IOC? Input Output Controller

• A special CA Server and CA Client (IOCs can “talk” to each other) • A computer running the “IOC Core” program (at least once) • This computer may be: - VME based, vxWorks (only possibility for old EPICS Versions up to 3.14), Linux, RTEMS - PC, operating system Windows, Linux, RTEMS - Apple, operating system OSX - UNIX Workstation, operating system Solaris • An IOC normally is connected to input and/or output hardware (otherwise called softIOC) • An IOC runs a record database, which defines what this IOC is doing

3rd international school on Beam Dynamics 02 March 2021 21 and Accelerator technology Some CA Clients

(from the EPICS Website - incomplete) •ALH: Alarm Handler •BURT: Backup and Restore Tool •CASR: Host-based Save/Restore •CAU: Channel Access Utility •Channel Archiver (SNS) •Channel Watcher (SLAC) •EDM: Extensible Display Manager (ORNL) •JoiMint: Java Operator Interface and Management INtegration Toolkit (DESY) •Knobs: Knob Manager und KnobConfig, eine Schnittstelle zu SunDials •MEDM: Motif Editor und Display Manager •StripTool: Strip-chart Plotting Tool •Command line tools (caget, caput, …) •and many more …

3rd international school on Beam Dynamics 02 March 2021 22 and Accelerator technology Client Program: Command Line Tools

• Read a PV named >caget ARIDI-PCT:CURRENT ARIDI-PCT:CURRENT 350.56 caget NAME >

>caput ARIDI-PCT:CURRENT 401.5 • Write a PV named Old: ARIDI-PCT:CURRENT 350.56 New: ARIDI-PCT:CURRENT 401.5 caput NAME value >

>cainfo ARIDI-PCT:CURRENT • Get information about that ARIDI-PCT:CURRENT Record State: connected Host: sls-cagw-1 cainfo NAME Access: read, no write Data type: DBR_DOUBLE Element count: 1 • Start a monitor >camonitor ARIDI-PCT:CURRENT ARIDI> -PCT:CURRENT 250.542 camonitor NAME ARIDI-PCT:CURRENT 250.537 ARIDI-PCT:CURRENT 250.533 (Cancel with [Ctrl] + [c]) ARIDI-PCT:CURRENT 250.525 >

3rd international school on Beam Dynamics 02 March 2021 23 and Accelerator technology Some CA Clients

(from the EPICS Website - incomplete) •ALH: Alarm Handler •BURT: Backup and Restore Tool •CASR: Host-based Save/Restore •CAU: Channel Access Utility •Channel Archiver (SNS) •Channel Watcher (SLAC) •EDM: Extensible Display Manager (ORNL) •JoiMint: Java Operator Interface and Management INtegration Toolkit (DESY) •Knobs: Knob Manager und KnobConfig, eine Schnittstelle zu SunDials •MEDM: Motif Editor und Display Manager •StripTool: Strip-chart Plotting Tool •Command line tools (caget, caput, …) •and many more …

3rd international school on Beam Dynamics 02 March 2021 24 and Accelerator technology Alarms and Colours

NO_ALARM Green Everything ok

MINOR Yellow Warning

Error MAJOR Red (no Controls Problem)

INVALID White Device not reachable

White block Not connected Record not known or Pink

3rd international school on Beam Dynamics 02 March 2021 25 and Accelerator technology Some CA Clients

(from the EPICS Website - incomplete) •ALH: Alarm Handler •BURT: Backup and Restore Tool •CASR: Host-based Save/Restore •CAU: Channel Access Utility •Channel Archiver (SNS) •Channel Watcher (SLAC) •EDM: Extensible Display Manager (ORNL) •JoiMint: Java Operator Interface and Management INtegration Toolkit (DESY) •Knobs: Knob Manager und KnobConfig, eine Schnittstelle zu SunDials •MEDM: Motif Editor und Display Manager •StripTool: Strip-chart Plotting Tool •Command line tools (caget, caput, …) •and many more …

3rd international school on Beam Dynamics 02 March 2021 26 and Accelerator technology Examples: StripTool

channel values

time

3rd international school on Beam Dynamics 02 March 2021 27 and Accelerator technology Some CA Clients

(from the EPICS Website - incomplete) •ALH: Alarm Handler •BURT: Backup and Restore Tool •CASR: Host-based Save/Restore •CAU: Channel Access Utility •Channel Archiver (SNS) •Channel Watcher (SLAC) •EDM: Extensible Display Manager (ORNL) •JoiMint: Java Operator Interface and Management INtegration Toolkit (DESY) •Knobs: Knob Manager und KnobConfig, eine Schnittstelle zu SunDials •MEDM: Motif Editor und Display Manager •StripTool: Strip-chart Plotting Tool •Command line tools (caget, caput, …)caQtDM: Display Manager •and many more …

3rd international school on Beam Dynamics 02 March 2021 28 and Accelerator technology Examples for caQtDM

3rd international school on Beam Dynamics 02 March 2021 29 and Accelerator technology Close Look at a Measured Value Value with a data type 295,5 with a unit mA with a time stamp 17.2.2005 14:21:16 with a severity (alarm state) NO_ALARM with technical limits 0 to 400 with graphical limits 0 to 370 with a description „Beam current in SR“

A measured value is an object with multiple related information that have different data types

3rd international school on Beam Dynamics 02 March 2021 30 and Accelerator technology What are Records?

A Record is an object with – a unique name – properties (fields) that contain information (data) different data types can appear in different fields – the ability to perform actions on that data unique name XYZ1234 Employee : James Bond Badge Number: 007 Address : Whitehall, London Salary : £70070.07

fields data

3rd international school on Beam Dynamics 02 March 2021 31 and Accelerator technology A Process Variable Name

• A PV name is comprised of two parts – The record name, and – the name of a field belonging to that record • For example…

ARIDI-PCT:CURRENT . EGU A Process variable name

A record name A dot to join them A field name • Note that if no field name is given, Channel Access will default to using the .VAL field “ARIDI-PCT:CURRENT” is interpreted as “ARIDI-PCT:CURRENT.VAL”

3rd international school on Beam Dynamics 02 March 2021 32 and Accelerator technology View of a Record

record(ao,"DemandTemp") { field(DRVH,"100") field(DESC,"Temperature") field(DRVL,"0") field(ASG,"") field(HOPR,"80") field(SCAN,"Passive") field(LOPR,"10") field(PINI,"NO") field(HIHI,"0.0e+00") field(PHAS,"0") field(LOLO,"0.0e+00") field(EVNT,"0") field(HIGH,"0.0e+00") field(DTYP,"VMIC 4100") field(LOW,"0.0e+00") field(DISV,"1") field(HHSV,"NO_ALARM") field(SDIS,"") field(LLSV,"NO_ALARM") field(DISS,"NO_ALARM") field(HSV,"NO_ALARM") field(PRIO,"LOW") field(LSV,"NO_ALARM") field(FLNK,"") field(HYST,"0.0e+00") field(OUT,"#C0 S0") field(ADEL,"0.0e+00") field(OROC,"0.0e+00") field(MDEL,"0.0e+00") field(DOL,"") field(SIOL,"") field(OMSL,"supervisory") field(SIML,"") field(OIF,"Full") field(SIMS,"NO_ALARM") field(PREC,"1") field(IVOA,"Continue normally") field(LINR,"NO CONVERSION") field(IVOV,"0.0e+00") field(EGUF,"100") } field(EGUL,"0") field(EGU,"Celcius")

3rd international school on Beam Dynamics 02 March 2021 33 and Accelerator technology What do Records do?

• Depends on •Analog in •Analog out 1. Record Type •Binary in •Binary out 2. Field values •Calculation •Calculation out • Records are active, they do things •Compression •Data fanout – Get data from other records or from •Event •Fanout hardware •Histogram •Motor – Perform calculations •Multi bit binary input – Check values are in range and raise •Multi bit binary output •PID control alarms •Pulse counter •Pulse delay – Put data to other records or to •Scan hardware •Select •Sequence – Activate or disable other records •String in •String out – Wait for hardware signals (interrupts) •Subarray •Subroutine • record do nothing until they are processed •Waveform

3rd international school on Beam Dynamics 02 March 2021 34 and Accelerator technology Record Processing

Record processing can be periodic or event driven:

•Periodic: Standard scan rates are… – 10, 5, 2, 1, 0.5, 0.2 and 0.1 seconds – Custom scan rates can be configured up to speeds allowed by operating system and hardware – No precise timing! •Event driven: Events include – Hardware interrupts – Request from another record via links – EPICS Events – Channel Access Puts

3rd international school on Beam Dynamics 35 and Accelerator technology The Record Reference Manual

• Explains records from the EPICS base release • Explains database concepts, record types, and record fields

• New Version as Wiki: http://www.aps.anl.gov/epics/wiki/index.php/RRM_3-14 • Old Version (R3.13): : http://www.aps.anl.gov/epics/EpicsDocumentation/AppDevManuals/RecordRef/Recordref.pdf

• More Records exist. For example Motor Record (part of synApps package)

3rd international school on Beam Dynamics 02 March 2021 36 and Accelerator technology Example Record

IOC Database Process Temperature Analog to Sensor Digital record (ai, “MTRT1-TEMP:READ”) Converter { field (EGU, “deg C”) field (EGUL, “0”) field (EGUF, “100”) field (HSV, “MAJOR”) field (HIGH, “51”) field (SCAN, “.1 second”) field (DTYP, “Hy8401”) field (INP, “#C3 S0 @”) Normal Operation 0 – 100°C 8 bit ADC } 5 - 50°C 0 – 10V 0 – 10V 0 – 255 bits

3rd international school on Beam Dynamics 02 March 2021 37 and Accelerator technology Example Record

caQtDM sets a CA monitor CA Client: caQtDM

every time the value changes record triggers callback

IOC Record Support record (ai, “MTRT1-TEMP:READ”) { … field (SCAN, “.1 second”) field (DTYP, “Hy8401”) field (INP, “#C3 S0 @”) Device Support field (VAL, “34”) … }

every 0.1 seconds the record Driver asks the driver for a value

Value (34 °C) and the driver reports back

3rd international school on Beam Dynamics 02 March 2021 38 and Accelerator technology Really Neat Things About EPICS

1. It is free 2. It is Open Source 3. There are lots of users 4. All a client needs to know to access data is a PV name 5. You can pick the best tools out there … 6. … or build your own 7. The boring stuff is already done 8. There is a lot of expertise available close by 9. A good contribution becomes internationally known 10. It does not matter, if you need 10 or 10 million PVs 11. It is fun (don’t tell the management)

3rd international school on Beam Dynamics 02 March 2021 39 and Accelerator technology EPICS Web Page

The central site for EPICS information - Documentation - CA Clients - Device support (driver) - Tech-talk (mailing list and archive)

http://www.aps.anl.gov/epics

3rd international school on Beam Dynamics 02 March 2021 40 and Accelerator technology KEK LUCX experience • EPICS base 3.14.16 (PC, Linux Mint) • EPICS clients (pyepics, Labview, epicsqt) • Python – PyQt, https://riverbankcomputing.com/software/pyqt/download – Matplotlib – pyepics • Labview (low level h/w I/O) – LabVIEW EPICS Client I/O Server • Qt5 (Monitoring) – epicsqt

3rd international school on Beam Dynamics 02 March 2021 41 and Accelerator technology Introduction to Python • Python is a high-level programming language • Open source and community driven • “Batteries Included” – a standard distribution includes many modules • Dynamic typed • Source can be compiled or run just-in-time • Similar to , tcl, ruby

3rd international school on Beam Dynamics 02 March 2021 42 and Accelerator technology Why Python? • Unlike AML and Avenue, there is a considerable base of developers already using the language • “Tried and true” language that has been in development since 1991 • Can interface with the Component Object Model (COM) used by Windows • Can interface with Open Source GIS toolsets

3rd international school on Beam Dynamics 02 March 2021 43 and Accelerator technology Python Interfaces • IDLE – a cross-platform Python development environment • PythonWin – a Windows only interface to Python • Python – running 'python' from the Command Line opens this interactive shell • For the exercises, we'll use IDLE, but you can try them all and pick a favorite

3rd international school on Beam Dynamics 02 March 2021 44 and Accelerator technology Example Python

• Hello World print “hello world” • Prints hello world to standard out

3rd international school on Beam Dynamics 02 March 2021 45 and Accelerator technology For More Information? http://python.org/ - documentation, tutorials, beginners guide, core distribution, ... Books include: • Learning Python by Mark Lutz • Python Essential Reference by David Beazley • Python Cookbook, ed. by Martelli, Ravenscroft and Ascher • (online at http://code.activestate.com/recipes/langs/python/) • http://wiki.python.org/moin/PythonBooks

3rd international school on Beam Dynamics 02 March 2021 46 and Accelerator technology Python Videos http://showmedo.com/videotutorials/python • “5 Minute Overview (What Does Python Look Like?)” • “Introducing the PyDev IDE for ” • “Linear Algebra with Numpy” • And many more

3rd international school on Beam Dynamics 02 March 2021 47 and Accelerator technology 4 Major Versions of Python • “Python” or “CPython” is written in C/C++ - Version 2.7 came out in mid-2010 - Version 3.1.2 came out in early 2010

• “Jython” is written in Java for the JVM • “IronPython” is written in C# for the .Net environment

3rd international school on Beam Dynamics 02 March 2021 48 and Accelerator technology Development Environments what IDE to use? http://stackoverflow.com/questions/81584 1. PyDev with Eclipse 2. Komodo 3. 4. 5. TextMate 6. 7. Idle 8. PIDA (Linux)(VIM Based) 9. NotePad++ (Windows) 10. (Linux) 11. SciTe (Linux)

3rd international school on Beam Dynamics 02 March 2021 49 and Accelerator technology Python Interactive Shell

% python Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>

You can type things directly into a running Python session >>> 2+3*4 14 >>> name = "Andrew" >>> name 'Andrew' >>> print "Hello", name Hello Andrew >>>

3rd international school on Beam Dynamics 02 March 2021 50 and Accelerator technology

scientific visualisation for python

John Hunter Tradelink Chicago [email protected]

3rd international school on Beam Dynamics 02 March 2021 51 and Accelerator technology Matplotlib: 3 ways of interaction • Embedding in a webserver or GUI (Tk, GTK, WX, Cocoa, Qt, PyQt) • Production scripting • Interactive shell (pylab / pyplot)

3rd international school on Beam Dynamics 02 March 2021 52 and Accelerator technology Used in MATLAB as well

3rd international school on Beam Dynamics 02 March 2021 53 and Accelerator technology Subplot demo

3rd international school on Beam Dynamics 02 March 2021 54 and Accelerator technology Histogram demo LaTex

3rd international school on Beam Dynamics 02 March 2021 55 and Accelerator technology Basic GUI Programming Concepts

• A graphical user interface (GUI) presents a user-friendly

mechanism for interacting with an application. output – Pronounced “GOO-ee” Application – Gives an application a distinctive “look-and-feel.” input • Conventional programming: – sequence of operations is determined Application by the program draw callbacks

– what you want to happen, happens when you want it Widgets • Event-driven programming: output input – sequence of operations is determined The User by the user’s interaction with the application’s interface – anything that can happen, happens at any time

3rd international school on Beam Dynamics 02 March 2021 56 and Accelerator technology GUI in Python, PyQt5 • PyQt is a multi-platform GUI toolkit. It has approximately ~1000 classes divided into a set of ~38 modules. – QtCore contains the core classes, including the event loop and Qt’s signal and slot mechanism. – QtGui contains classes for windowing system integration, event handling, 2D graphics, imaging, fonts and text. – QtWidgets contains classes that provide a set of UI elements to create classic desktop-style user interfaces.

3rd international school on Beam Dynamics 02 March 2021 57 and Accelerator technology PyQt5, a Simple Example import sys from PyQt5.QtCore import * def b1_clicked(): from PyQt5.QtGui import * print ("Button 1 from PyQt5.QtWidgets import * clicked") def b2_clicked(): def window(): print ("Button 2 app = QApplication(sys.argv) clicked") win = QDialog() b1= QPushButton(win) b1.setText("Button1") b1.move(50,20) b1.clicked.connect(b1_clicked)

b2=QPushButton(win) b2.setText("Button2") b2.move(50,50) b2.clicked.connect(b2_clicked) win.setGeometry(100,100,200,100) win.setWindowTitle("PyQt") win.show() 3rd international school on Beam Dynamics 02 March 2021 58 sys.exit(app.exec_()) and Accelerator technology PyQt5, Creating an application def main(): app = QApplication(sys.argv) w = MyWindow() w.showMaximized() w.resize(400, 200) • Step 1: sys.exit(app.exec_())

– Create an application if __name__ == "__main__": main() – Create and show the main window – Run the event loop then exit • Step 2:

class MyWindow(QWidget): – Create the main window def __init__(self, *args): QWidget.__init__(self, *args) – Extends QWidget ... – Write the constructor: def __init__(self, *args): – Creating a child widget and place it in a layout 3rd international school on Beam Dynamics 02 March 2021 59 and Accelerator technology PyQt5 , Widgets • QLabel • QPushButton • QLineEdit: a widget is a one-line . • QTextEdit: a widget that is used to edit and display both plain and rich text. • QMessageBox okButton = QPushButton("OK")

cancelButton = QPushButton("Cancel")

self.nameLabel = QLabel("Name:") self.nameEdit = QLineEdit() 3rd international school on Beam Dynamics 02 March 2021 60 and Accelerator technology PyQt5, Using Layouts • Layouts manage child widgets and are responsible for: – Updating their sizes and positions – Providing default and minimum sizes • Horizontal, vertical and grid layouts:

oneButton = QPushButton("One") – QHBoxLayout() twoButton = QPushButton("Two") threeButton = QPushButton("Three") layout = QHBoxLayout(self) – QVBoxLayout() layout.addWidget(oneButton) layout.addWidget(twoButton) – QGridLayout() layout.addWidget(threeButton)

3rd international school on Beam Dynamics 02 March 2021 61 and Accelerator technology PyQt5, Event Handling

• Widgets used to build the GUI interface act as the source of such events. Each PyQt widget, which is derived from QObject class, is designed to emit ‘signal’ in response to one or more events. oneButton.clicked.connect(self.show_one) def show_one(self): self.messageEdit.setText("ONE") – The signal on its own does not perform any action. Instead, it is ‘connected’ to a ‘slot’. The slot can be any callable Python function. – In PyQt, connection between a signal and a slot can be achieved in different ways. Following are most commonly used techniques: 3rd international school on Beam Dynamics 02 March 2021 62 and Accelerator technology KEK LUCX scanning GUI

3rd international school on Beam Dynamics 02 March 2021 63 and Accelerator technology KEK LUCX analysis GUI

3rd international school on Beam Dynamics 02 March 2021 64 and Accelerator technology Python non-linear fitting example

3rd international school on Beam Dynamics 02 March 2021 65 and Accelerator technology LUCX ASTRA simulation

Beam Size minimum: 0.70 mm at 2 m Normalized Emittance: 0.68 -mm-mrad

3rd international school on Beam Dynamics 02 March 2021 66 and Accelerator technology Longitudinal Profile at 2 m No space Charge with Space Charge @ 25 pC

Beam Size minimum: 0.6120 mm at 2 m Beam Size minimum: 0.28 mm at 2 m Normalized Emittance: 0.632 -mm-mrad Normalized Emittance: 0.65 -mm-mrad

3rd international school on Beam Dynamics 02 March 2021 67 and Accelerator technology Beam size reduced to 0.68 mm; develops fan structure at 2m z: 2.0 m Beam Size minimum: 0.70 mm at 2 m Normalized Emittance: 0.68 -mm-mrad

z: 1.8 m

RF Gun

z: 1.5 m z: 1.0 m z: 0.17 m

3rd international school on Beam Dynamics 02 March 2021 68 and Accelerator technology RF zero-crossing

3rd international school on Beam Dynamics 02 March 2021 69 and Accelerator technology ASTRA output file format (particles)

3rd international school on Beam Dynamics 02 March 2021 70 and Accelerator technology ASTRA particle tracker output processing

Initial ASTRA output XY plot 2D re-binning (effectively histogram)

2D plot Rescale (and add pads) to match a standard 640x480 pixels image using dynamic magnification factor

3rd international school on Beam Dynamics 02 March 2021 71 and Accelerator technology ASTRA output file opening with Python

3rd international school on Beam Dynamics 02 March 2021 72 and Accelerator technology ASTRA comparison with Experimental data

Experimental data ASTRA Simulation data

3rd international school on Beam Dynamics 02 March 2021 73 and Accelerator technology KEK LUCX, 4-micro bunch generation

A. Aryshev, M. Shevelev, Y. Honda, N. Terunuma, J. Urakawa, Measured Cs2Te photocathode Femtosecond response time measurements of a Cs2Te photocathode, Appl. Phys. Lett. 111, 033508 (2017). peak-to-peak response time 369.48 ± 27 fs.

“Kly1 off” “zero-cross”

YAG YAG

3rd international school on Beam Dynamics 02 March 2021 74 and Accelerator technology Space-charge force suppression ASTRA simulation, LUCX RF gun l. Serafini, et.al. NIMA 387 (1997) 305-314

• Acceleration gradient, up -> limited by discharge • Charge, down -> limited by detector’s sensitivity • UV spot size, up -> limited by off-axis dynamics • UV Pulse length, !!! -> limited by THG • Multi-bunch -> limited only by beam-loading

3rd international school on Beam Dynamics 02 March 2021 75 and Accelerator technology ASTRA 16 micro-bunch simulation

3rd international school on Beam Dynamics 02 March 2021 76 and Accelerator technology ASTRA 32 micro-bunch simulation

3rd international school on Beam Dynamics 02 March 2021 77 and Accelerator technology Summary • Knowledge of data acquisition and data analysis is crucial for experimentalists. • For these who do just simulations, it is also important as makes things clearer… • Data representation is already a part of the “deal” in science. • The ability to automate your research (faster analysis, plotting, universal GUI, or even a custom data taking) will definitely make you more efficient in every sense. • ASTRA post processing is proven to be extremely useful in case of micro-bunch simulation. 3rd international school on Beam Dynamics 02 March 2021 78 and Accelerator technology Materials • In this presentation I used materials from: – Kay Kasemir (SNS/ORNL) – Ned Arnold (APS) – John Maclean (PSI) – Sharon Lackey (Fermilab) – Guido van Rossum (Director of PythonLabs) – John Hunter (Tradelink Chicago) – Chen Lin (Brandeis University) – M. Shevelev (TPU)

3rd international school on Beam Dynamics 02 March 2021 79 and Accelerator technology THANK YOU FOR YOUR ATTENTION

3rd international school on Beam Dynamics 02 March 2021 80 and Accelerator technology