xboa
Variables
xboa.examples.Example_4 Namespace Reference

Example code to read a file, apply a transformation and plot before and after. More...

Variables

int target_beta = 800
 
float target_alpha = 0.0
 
tuple bunch_list = Bunch.new_list_from_read_builtin('icool_for003', sys.prefix+'/share/xboa/data/for003_test.dat')
 
list bunch = bunch_list[-1]
 
tuple emit_trans = bunch.get_emittance(['x'])
 
tuple target_ellipse = Bunch.build_ellipse_2d(target_beta, target_alpha, emit_trans, bunch.mean(['p'])['p'], Common.pdg_pid_to_mass[13], True)
 

Detailed Description

Example code to read a file, apply a transformation and plot before and after.

1 """
2 \namespace xboa::examples::Example_4
3 
4 Example code to read a file, apply a transformation and plot before and after
5 
6 \include xboa/examples/Example_4.py
7 """
8 
9 
10 #######################
11 # EXAMPLE 4 #
12 # IDEAL TRANSFER LINE #
13 #######################
14 #
15 # Load a data file; make some cuts
16 # transformation bunch train into single bunch data
17 # transform x x' distribution while keeping amplitude distribution constant
18 # make some plots; write the data file out in a new format
19 #
20 
21 from xboa import *
22 from xboa.hit import Hit
23 from xboa.bunch import Bunch
24 import xboa.common as Common
25 import operator
26 import sys
27 
28 #script starts here
29 target_beta = 800.*Common.units['mm']
30 target_alpha = 0.0
31 
32 print '========= XBOA example 4 ========='
33 
34 print
35 """
36 Now we start manipulating the data. The idea here is to take particles from
37 one system, apply a match condition so that they can be used in another system.
38 Aim is to mimic an ideal transfer line.
39 """
40 print "Loading file... "
41 bunch_list = Bunch.new_list_from_read_builtin('icool_for003', sys.prefix+'/share/xboa/data/for003_test.dat')
42 print "Loaded"
43 
44 #I only use the last bunch for this
45 bunch = bunch_list[-1]
46 
47 #let's look at the particles with 150 MeV < energy < 400 MeV
48 bunch.cut({'energy':400}, operator.ge)
49 bunch.cut({'energy':150}, operator.le)
50 #remove the outside of the beam - particles with amplitude > 90 mm (emittance > 22.5)
51 bunch.cut({'amplitude x y':90}, operator.ge)
52 bunch.root_scatter_graph('t', 'energy', 'ns', 'MeV', include_weightless=False)
53 #first we transform the bunch so that all particles sit in a single RF bucket
54 bunch.period_transformation( bunch[0]['t'], 0.20125*Common.units['GHz'])
55 bunch.root_scatter_graph('t', 'energy', 'ns', 'MeV', include_weightless=False)
56 
57 #print out some information about longitudinal phase space
58 print "Longitudinal phase space covariance matrix\n",bunch.covariance_matrix(['t','energy'])
59 print 'Longitudinal RMS beta ',bunch.get_beta(['ct']),'mm'
60 print 'Longitudinal RMS alpha ',bunch.get_alpha(['ct'])
61 print 'Longitudinal RMS gamma ',bunch.get_gamma(['ct']),'mm^{-1}'
62 print 'Longitudinal RMS emittance',bunch.get_emittance(['ct']),'mm'
63 
64 #set to use geometric variables (like x,x')
65 #default is to use momentum variables (like x,px)
66 bunch.set_geometric_momentum(True)
67 
68 #calculate Twiss parameters and emittance for x, x' space
69 emit_trans = bunch.get_emittance(['x'])
70 print '\nInput phase space (x,x\') covariance matrix\n',bunch.covariance_matrix(['x','x\''])
71 print 'Input RMS beta ',bunch.get_beta(['x']),'mm'
72 print 'Input RMS alpha ',bunch.get_alpha(['x'])
73 print 'Input RMS gamma ',bunch.get_gamma(['x']),'mm^{-1}'
74 print 'Input RMS emittance',bunch.get_emittance(['x']),'mm'
75 
76 #plot x x' and amplitude distribution
77 bunch.root_scatter_graph('x', 'x\'', 'MeV/c', 'MeV/c', include_weightless=False)
78 bunch.root_histogram('amplitude x', 'mm')
79 #transform the transverse phase space to a new set of coordinates, conserving emittance
80 #for example, to remove an x-x' correlation into the beam
81 target_ellipse = Bunch.build_ellipse_2d(target_beta, target_alpha, emit_trans, bunch.mean(['p'])['p'], Common.pdg_pid_to_mass[13], True)
82 bunch.transform_to(['x','x\''], target_ellipse, mass_shell_variable='energy')
83 
84 #print target Twiss parameters
85 print '\nNow I will make a transformation to target (x,x\') covariance matrix\n',target_ellipse
86 print 'Target RMS beta ',target_beta,'mm'
87 print 'Target RMS alpha ',target_alpha
88 print 'Target RMS gamma ',(1.+target_alpha**2)/target_beta,'mm^{-1}'
89 print 'Transformation will conserve emittance by construction'
90 print
91 
92 #print Twiss parameters of transformed distribution
93 print "Transformed phase space (x,x\') covariance matrix\n",bunch.covariance_matrix(['x','x\''])
94 print 'Transformed RMS beta ',bunch.get_beta(['x']),'mm'
95 print 'Transformed RMS alpha ',bunch.get_alpha(['x'])
96 print 'Transformed RMS gamma ',bunch.get_gamma(['x']),'mm^{-1}'
97 print 'Transformed RMS emittance',bunch.get_emittance(['x']),'mm'
98 
99 #plot x x' and amplitude distribution after - the point being that the x x' distribution is radically different,
100 #but the amplitude distribution is the same; what one would expect from an ideal transfer line
101 bunch.root_scatter_graph('x', 'x\'', 'MeV/c', 'MeV/c', include_weightless=False)
102 bunch.root_histogram('amplitude x', 'mm')
103 
104 #translate to z=0
105 print 'Mean z at start',bunch.mean(['z'])['z']
106 bunch.translate({'z':-bunch.mean(['z'])['z']})
107 print 'Mean z at end ',bunch.mean(['z'])['z']
108 
109 #write out as an ICOOL for003 file
110 bunch.hit_write_builtin('icool_for003', 'for003_test.dat', 'for003 file generated by XBOA')
111 
112 print 'Press <return> key to finish'
113 raw_input()
114 Bunch.clear_global_weights()
115 
116 
117 
118 

Variable Documentation

int target_beta = 800

Definition at line 30 of file Example_4.py.

float target_alpha = 0.0

Definition at line 31 of file Example_4.py.

tuple bunch_list = Bunch.new_list_from_read_builtin('icool_for003', sys.prefix+'/share/xboa/data/for003_test.dat')

Definition at line 42 of file Example_4.py.

list bunch = bunch_list[-1]

Definition at line 46 of file Example_4.py.

tuple emit_trans = bunch.get_emittance(['x'])

Definition at line 70 of file Example_4.py.

tuple target_ellipse = Bunch.build_ellipse_2d(target_beta, target_alpha, emit_trans, bunch.mean(['p'])['p'], Common.pdg_pid_to_mass[13], True)

Definition at line 82 of file Example_4.py.