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