2 \namespace xboa::examples::Example_1
4 Example code to read a file and perform some data access operations
6 \include xboa/examples/Example_1.py
24 print '========= XBOA example 1 ========='
25 print "In this example, I will do some basic file i/o and data manipulation"
28 filename = sys.prefix+
'/share/xboa/data/for009.dat'
29 filetype =
"icool_for009"
34 print "Data can be loaded using new_list_from_read_builtin(filetype, filename) for builtin types"
35 print 'List of builtin types:'
36 print Hit.file_types()
37 print 'For non-builtin types, you would use new_from_read_user(format_list, format_units_dict, filehandle, number_of_skip_lines)'
38 print 'I\'ll try to load some example data that came with your installation...'
40 bunch_list = Bunch.new_list_from_read_builtin(filetype, filename)
42 'Oh dear, I couldn\'t load the example data - I wonder if it was installed in the default place ('+filename+
')?\nTry inputting a new location:'
43 filename = raw_input()
44 bunch_list = Bunch.new_list_from_read_builtin(filetype, filename)
47 print '\n====== HIT ======'
48 print 'A hit is the intersection of a particle trajectory with a detector or output plane'
49 my_hit = bunch_list[0][10]
50 print 'Normally you would access hit data using my_hit[get_variable], for example'
51 print 'my_hit[\'x\']: ',my_hit[
'x']
52 print 'my_hit[\'px\'] (x component of momentum vector):',my_hit[
'px']
53 print 'my_hit[\'l_kin\'] (kinetic angular momentum):',my_hit[
'l_kin']
54 print 'Possible get_variables are:'
55 print my_hit.get_variables()
56 print 't is time, r is radius, x\' is dx/dz, bx is magnetic field in x direction, ex is electric field in x direction'
57 print 'You can modify data in a hit in the way you would expect, i.e. my_hit[\'px\'] = 1.'
59 print 'my_hit[\'px\']: ',my_hit[
'px'],
'\n'
60 print 'If you change the momentum, you will need to readjust the hit to make sure that it obeys E^2 = p^2 + m^2'
61 print 'Do this using mass_shell_condition(some_variable), e.g. my_hit.mass_shell_condition(\'energy\')'
62 print '(px,py,pz),energy,mass,E^2-p^2 before:\n',(my_hit[
'px'],my_hit[
'py'],my_hit[
'pz']),my_hit[
'energy'],my_hit[
'mass'],(my_hit[
'energy']**2-my_hit[
'p']**2)**0.5
63 my_hit.mass_shell_condition(
'energy')
64 print '(px,py,pz),energy,mass,E^2-p^2 after:\n',(my_hit[
'px'],my_hit[
'py'],my_hit[
'pz']),my_hit[
'energy'],my_hit[
'mass'],(my_hit[
'energy']**2-my_hit[
'p']**2)**0.5
66 print '\n====== BUNCH ======'
67 print 'A bunch is a collection of individual hits'
68 my_bunch = bunch_list[0]
69 print 'Here\'s some example data that can be calculated by Bunch'
70 print 'Number of hits in bunch: ',len(my_bunch)
71 print 'Statistical weight of bunch: ',my_bunch.bunch_weight()
72 print 'mean (x,px): ',my_bunch.mean([
'x',
'px'])
73 print 'standard deviation in x ',my_bunch.moment([
'x',
'x'])**0.5
74 print 'Twiss beta_x ',my_bunch.get_beta([
'x'])
75 print 'x emittance ',my_bunch.get_emittance([
'x'])
76 print 'x-y emittance ',my_bunch.get_emittance([
'x',
'y']),
' (for coupled systems e.g. solenoids)'
77 print 'x-px covariance matrix ',my_bunch.covariance_matrix([
'x',
'px'])
78 print 'Another way of accessing the same data is using the get(variable, axes) function:'
79 print 'my_bunch.get(\'gamma\', [\'x\',\'y\']) ',my_bunch.get(
'gamma',[
'x',
'y']),
' (Penn gamma for coupled systems)'
80 print 'Possible data types:\n',my_bunch.get_variables()
81 print 'Possible axes:\n',my_bunch.get_axes()
82 print 'To loop over e.g. the first 10 hits in a bunch, do something like \nfor hit in my_bunch[0:10]:\n print hit[\'event_number\'],'
83 for hit
in my_bunch[0:10]:
84 print hit[
'event_number'],
85 print '\nTo loop over all hits in a bunch, do something like \nfor hit in my_bunch:\n print hit[\'event_number\'],'
87 print '\nThere\'s lots more out there... to get documentation on a module, try using inline help. So on the python command line, do something like\n import xboa.Hit\n help(xboa.Hit)\n'
92 print 'Press <return> key to finish'
Implemented within this module:
Implemented within this module: