3
# This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
5
# MAUS is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
10
# MAUS is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with MAUS. If not, see <http://www.gnu.org/licenses/>.
21
from xboa.Bunch import Bunch
34
upstream_reference = 14
35
downstream_reference = 50
37
#inspection_plane = 56
38
inspection_plane = -1 # Negative means don't write a file
40
write_refernce_planes = False
41
print_all_planes = False
42
print_all_plots_all_planes = False
52
if len( sys.argv ) <= 1 :
53
print "No File Supplied."
56
filename = sys.argv[1]
62
bunch_list_tmp = Bunch.new_list_from_read_builtin('maus_root_virtual_hit', filename)
64
print "Read", len(bunch_list_tmp), "bunches"
66
for bun in bunch_list_tmp :
67
bun.cut( {'pid':-13}, operator.ne, global_cut = True)
68
if bun.bunch_weight() < 1.e-9 :
73
for bun in bunch_list_tmp :
74
if bun.bunch_weight() < 1.e-9 :
76
mean = bun.mean( ['pz'] )['pz']
77
print "PZ =", ( mean - pz_tail ), mean, ( mean + pz_tail )
78
bun.cut( { 'pz' : mean + pz_tail }, operator.ge, global_cut = False )
79
bun.cut( { 'pz' : mean - pz_tail }, operator.le, global_cut = False )
83
for bun in bunch_list_tmp :
84
if bun.bunch_weight() < 1.e-9 :
86
bun.cut( {'pid':-13}, operator.ne, global_cut = True)
87
if bun.bunch_weight() < 1.e-9 :
89
bun.cut( {'r' : radial_cut}, operator.ge, global_cut = True)
90
if bun.bunch_weight() < 1.e-9 :
92
bun.transmission_cut( bunch_list_tmp[ downstream_reference ], True )
95
bunch_list = [ bun for bun in bunch_list_tmp if len( bun ) > 1 and bun.bunch_weight()>1.0 ]
97
outfile = ROOT.TFile( "emittance_from_virtualPlanes.root", "RECREATE" )
99
# canvas, hist, graph = Bunch.root_graph(bunch_list, 'mean', ['z'], 'standard_deviation', ['pz'], 'MeV/c')
101
# canvas.Print( "RMS_pz.eps", "eps")
103
canvas, hist, graph = Bunch.root_graph(bunch_list, 'mean', ['z'], 'bunch_weight', [''], 'm')
105
canvas.Print( "BunchWeight.eps", "eps")
107
canvas, hist, graph = Bunch.root_graph(bunch_list, 'mean', ['z'], 'emittance', ['x', 'y'], 'm', 'm')
109
canvas.Print( "Emittance.eps", "eps")
111
canvas, hist, graph = Bunch.root_graph(bunch_list, 'mean', ['z'], 'beta', ['x', 'y'], 'm', 'm')
113
canvas.Print( "BetaFunc.eps", "eps")
115
canvas, hist, graph = Bunch.root_graph(bunch_list, 'mean', ['z'], 'mean', ['energy'], 'm', 'MeV')
117
canvas.Print( "Energy.eps", "eps")
119
canvas, hist, graph = Bunch.root_graph(bunch_list, 'mean', ['z'], 'mean', ['bz'], 'm', 'T')
121
canvas.Print( "MagneticField.eps", "eps")
123
canvas, hist, graph = Bunch.root_graph(bunch_list, 'mean', ['z'], 'mean', ['pid'], 'm')
125
canvas.Print( "PID.eps", "eps")
128
bunch_up = bunch_list[ upstream_reference ]
129
bunch_down = bunch_list[ downstream_reference ]
131
up_means = bunch_up.mean( ['x','y','z','px','py','pz','pt','energy','bz'] )
132
down_means = bunch_down.mean( ['x','y','z','px','py','pz','pt','energy','bz'] )
134
if write_refernce_planes is True :
136
print "Writing Raw Output Files"
139
with open( "upstream_virtual_hits.txt", 'w' ) as outfile :
140
outfile.write( '# Position (x,y,z) | Momentum (px,py,pz) | Magnetic File (bz) \n\n' )
142
for hit in bunch_up :
143
outfile.write( str(hit.get('x')) + ' '
144
+ str(hit.get( 'y' )) + ' '
145
+ str(hit.get( 'z' )) + ' '
146
+ str(hit.get( 'px' )) + ' '
147
+ str(hit.get( 'py' )) + ' '
148
+ str(hit.get( 'pz' )) + ' '
149
+ str(hit.get( 'bz' )) + '\n' )
151
with open( "downstream_virtual_hits.txt", 'w' ) as outfile :
152
outfile.write( '# Position (x,y,z) | Momentum (px,py,pz) | Magnetic File (bz) \n\n' )
154
for hit in bunch_down :
155
outfile.write( str(hit.get('x')) + ' '
156
+ str(hit.get( 'y' )) + ' '
157
+ str(hit.get( 'z' )) + ' '
158
+ str(hit.get( 'px' )) + ' '
159
+ str(hit.get( 'py' )) + ' '
160
+ str(hit.get( 'pz' )) + ' '
161
+ str(hit.get( 'bz' )) + '\n' )
165
print " === Upstream Results === "
167
print " Collective Properties"
168
print "Emittance : ", bunch_up.get_emittance( ['x','y'] )
169
print "Beta : ", bunch_up.get_beta( ['x','y'] )
170
print "Weight : ", bunch_up.bunch_weight()
173
print "X Position : ", up_means['x']
174
print "Y Position : ", up_means['y']
175
print "Z Position : ", up_means['z']
176
print "X Momentum : ", up_means['px']
177
print "Y Momentum : ", up_means['py']
178
print "Z Momentum : ", up_means['pz']
179
print "Transverese Momentum : ", up_means['pt']
180
print "Energy : ", up_means['energy']
181
print "Z Magnetic Field : ", up_means['bz']
184
print " === Downstream Results === "
186
print " Collective Properties"
187
print "Emittance : ", bunch_down.get_emittance( ['x','y'] )
188
print "Beta : ", bunch_down.get_beta( ['x','y'] )
189
print "Weight : ", bunch_down.bunch_weight()
192
print "X Position : ", down_means['x']
193
print "Y Position : ", down_means['y']
194
print "Z Position : ", down_means['z']
195
print "X Momentum : ", down_means['px']
196
print "Y Momentum : ", down_means['py']
197
print "Z Momentum : ", down_means['pz']
198
print "Transverese Momentum : ", down_means['pt']
199
print "Energy : ", down_means['energy']
200
print "Z Magnetic Field : ", down_means['bz']
202
if inspection_plane > 0 :
204
print "Saving Inspection Plane"
208
with open( 'inspection_plane.txt', 'w' ) as outfile :
209
outfile.write( '# X Y Z Px Py Pz\n' )
210
for hit in bunch_list[ inspection_plane ] :
211
outfile.write( str( hit.get( 'x' ) ) + ' ' +
212
str( hit.get( 'y' ) ) + ' ' +
213
str( hit.get( 'z' ) ) + ' ' +
214
str( hit.get( 'px' ) ) + ' ' +
215
str( hit.get( 'py' ) ) + ' ' +
216
str( hit.get( 'pz' ) ) + ' ' +
217
str( hit.get( 'pid' ) ) + ' ' +
218
str( hit.get_weight() ) + '\n' )
219
print 'inspection_plane.txt Created'
221
if print_all_planes is True :
223
print "Saving Plane Data"
226
with open( 'plane_data.txt', 'w' ) as outfile :
227
outfile.write( '# Mean X Mean Y Mean Z Mean Px Mean Py Mean Pz Mean Pt Mean Bz Energy Beta Emittance Weight \n' )
229
for bun in bunch_list :
230
means = bun.mean( ['x','y','z','px','py','pz','pt','energy','bz'] )
231
outfile.write( str( bunch_count ) + ' ' +
232
str( means[ 'x' ] ) + ' ' +
233
str( means[ 'y' ] ) + ' ' +
234
str( means[ 'z' ] ) + ' ' +
235
str( means[ 'px' ] ) + ' ' +
236
str( means[ 'py' ] ) + ' ' +
237
str( means[ 'pz' ] ) + ' ' +
238
str( means[ 'pt' ] ) + ' ' +
239
str( means[ 'bz' ] ) + ' ' +
240
str( means[ 'energy' ] ) + ' ' +
241
str( bun.get_emittance( [ 'x','y' ] ) ) + ' ' +
242
str( bun.get_beta( ['x','y'] ) ) + ' ' +
243
str( bun.bunch_weight() ) + "\n" )
245
print 'plane_data.txt Created'
247
if print_all_plots_all_planes is True :
249
print "Saving plots for every plane. Theres going to be a lot of them!"
253
for num in range( len( bunch_list ) ) :
254
bunch = bunch_list[num]
255
prefix = 'Virtual_plane_' + str( num ) + '_'
257
canvas, hist = bunch.root_histogram( 'x', 'mm', 'y', 'mm' )
258
canvas.Print( prefix+'X-Y.eps', 'eps' )
260
canvas, hist = bunch.root_histogram( 'px', 'MeV/c', 'py', 'MeV/c' )
261
canvas.Print( prefix+'PX-PY.eps', 'eps' )
263
canvas, hist = bunch.root_histogram( 'x', 'mm', 'px', 'MeV/c' )
264
canvas.Print( prefix+'X-PX.eps', 'eps' )
266
canvas, hist = bunch.root_histogram( 'y', 'mm', 'py', 'MeV/c' )
267
canvas.Print( prefix+'Y-PY.eps', 'eps' )
269
canvas, hist = bunch.root_histogram( 'pz', 'MeV/c' )
270
canvas.Print( prefix+'PZ.eps', 'eps' )
272
canvas, hist = bunch.root_histogram( 'pt', 'MeV/c' )
273
canvas.Print( prefix+'PT.eps', 'eps' )
277
if __name__ == "__main__":