19 Set of algorithms for transforming between different file formats and bunch structures. Reads in input command file, by default file_convert.inp, and uses information to read beam in a user specified file format and output in a different user specified file format. Command line options are:
20 -c=<filename> <filename> is the input control file (default is 'file_convert.inp')
21 e.g. ./File_Convert.py -c=my_control_data.inp
23 Input file cav contain the following control variables and data, one on each line. Hash (#) or exclamation mark (!) symbols are comments.
25 Mandatory data to control input and output formats:
27 format_in <format_string>
29 format_out <format_string>
31 The rest of the control variables are optional:
32 If you need to define a station or region from the input file:
34 Otherwise the routine will add all stations/regions to the output
36 To perform a transformation in phase space, about some origin, to a desired covariance matrix:
37 rotate <variable1> <variable2> ... one label for each axis in the rotation
38 origin <variable1> <value>
39 origin <variable2> <value>
41 covariance <variable1> <variable2> <value>
42 If not defined, values default to 0. The covariance matrix should be symmetric and positive definite. The rotation will be scaled such that emittance is conserved. If called as a python script, this function requires a correct installation of NumPy on the user's PC
44 To perform a translation in phase space:
45 translate <variable1> <value1> <variable2> <value2> ... one variable/value pair for each variable to be translated
47 To enforce E^2 + p^2 = m^2:
48 mass_shell_condition <momentum_variable>
49 <momentum_variable> should be one of energy, p, px, py, pz. This is the variable that is changed to maintain the mass shell condition
51 To make 1d histograms or 2d scatter plots of either input or output data:
52 plot_format <format> (default is png)
53 scatter_in <x-axis_variable> <x-units> <y-axis_variable> <y-units>
54 ... one line for each histogram
55 scatter_out <x-axis_variable> <x-units> <y-axis_variable> <y-units>
56 ... one line for each histogram
57 histogram_in <x-axis_variable> <x-units>
58 ... one line for each histogram
59 histogram_out <x-axis_variable> <x-units>
60 ... one line for each histogram
61 You can do as many of these as you like. If called as python script, this function requires a correct PyRoot installation on the user's PC
63 The routine goes like:
67 (iv) apply translation
69 (vi) write output data
74 g4beamline_bl_track_file
77 Possible variables are:
140 Obviously feel free to take this python script as a basis to hack with...
148 from bunch
import Bunch
150 print 'Error during x-boa import. Check your x-boa installation is in the PYTHONPATH environment variable'
160 print 'Error during python import. Check your python installation.'
168 plots(lines, bunch,
True)
172 plots(lines, bunch,
False)
177 lines = fh.readlines()
179 for i
in range( len(lines) ):
180 (line,dummy,dummy) = lines[i].partition(
'#')
181 (line,dummy,dummy) = line.partition(
'!')
183 if len(line) > 0: lines_read.append(line)
191 if line[0] ==
'file_in': file_in = line[1]
192 if line[0] ==
'format_in': format_in = line[1]
193 if line[0] ==
'station_in': station = int(line[1])
195 print 'Reading file \''+file_in+
'\' of type \''+format_in+
'\''
196 bunch = Bunch.new_from_read_builtin(format_in, file_in)
198 print 'Reading station',station,
'from file \''+file_in+
'\' of type \''+format_in+
'\''
199 bunch = Bunch.new_dict_from_read_builtin(format_in, file_in)[station]
207 if line[0] ==
'file_out': file_out = line[1]
208 if line[0] ==
'format_out': format_out = line[1]
209 bunch.hit_write_builtin(format_out, file_out)
210 print 'Written a',format_out,
'file to',file_out
216 if line[0] ==
'rotate':
217 for i
in range(1,len(line)): variables.append(line[i])
218 if line[0] ==
'origin': origin[line[1]] = float(line[2])
219 if len(variables) <= 1:
return
221 target_matrix = numpy.zeros( (len(variables),len(variables)))
223 if line[0] ==
'covariance':
224 i1 = variables.index(line[1])
225 i2 = variables.index(line[2])
226 target_matrix[i1,i2] = float(line[3])
227 target_matrix[i2,i1] = float(line[3])
228 print 'Rotation in variables\n',variables,
'\nto matrix\n',target_matrix,
'\nabout origin\n',origin
229 bunch.transform_to(variables, target_matrix, origin)
230 print 'gives\n',bunch.covariance_matrix(variables, origin)
235 if line[0] ==
'translate':
236 for i
in range(1,len(line),2):
237 translation[ line[i] ] = float(line[i+1])
238 print 'Translating through\n',translation
239 bunch.translate(translation)
243 if line[0] ==
'mass_shell_condition':
244 print 'Changing momentum variable',line[1],
'to force E^2 = p^2 + m^2'
246 hit.mass_shell_condition(line[1])
249 def plots(lines, bunch, do_in):
250 histogram_format =
'png'
253 if line[0] ==
'histogram_format': histogram_format = line[0]
255 if line[0] ==
'histogram_in' and do_in: bunch.root_histogram(line[1], line[2]).Print(line[1]+
'_in.'+histogram_format)
256 if line[0] ==
'histogram_out' and not do_in: bunch.root_histogram(line[1], line[2]).Print(line[1]+
'_out.'+histogram_format)
257 if line[0] ==
'scatter_in' and do_in: bunch.root_scatter_graph(line[1], line[3], line[2], line[4])\
258 .Print(line[1]+
'_vs_'+line[3]+
'_in.'+histogram_format)
259 if line[0] ==
'scatter_out' and not do_in: bunch.root_scatter_graph(line[1], line[3], line[2], line[4])\
260 .Print(line[1]+
'_vs_'+line[3]+
'_out.'+histogram_format)
263 contname =
'file_convert.inp'
264 if len(sys.argv) > 1:
265 (arg_name,dummy,argument) = sys.argv[1].partition(
'=')
266 if arg_name ==
'-c': contname = argument
267 else:
print 'Warning - didnt recognise command line argument',arg
269 if sys.argv[0].find(
'File_Convert' ) > -1:
273 except Exception, error:
274 print 'There was a problem during execution. Error was:\n',error
276 if __name__ ==
"__main__":