xboa
Variables
xboa.examples.Example_3 Namespace Reference

Example code to read a file, make some cuts and then plot. More...

Variables

tuple bunch_list = Bunch.new_list_from_read_builtin('icool_for009', sys.prefix+'/share/xboa/data/for009.dat')
 
tuple n_stations = len(bunch_list)
 

Detailed Description

Example code to read a file, make some cuts and then plot.

1 """
2 \namespace xboa::examples::Example_3
3 
4 Example code to read a file, make some cuts and then plot
5 
6 \include xboa/examples/Example_3.py
7 """
8 
9 #############
10 # EXAMPLE 3 #
11 # CUTS #
12 #############
13 #
14 # Load a data file; make some cuts; make some plots
15 # For this example, I require the ROOT library or matplotlib library
16 #
17 
18 import xboa.hit
19 from xboa.hit import Hit
20 import xboa.bunch
21 from xboa.bunch import Bunch
22 
23 import operator
24 import sys
25 
26 print '========= XBOA example 3 ========='
27 
28 #load data file
29 print "This example shows how to make cuts"
30 print "Loading file... "
31 bunch_list = Bunch.new_list_from_read_builtin('icool_for009', sys.prefix+'/share/xboa/data/for009.dat')
32 print "Loaded"
33 
34 #make px histogram
35 bunch_list[0].root_histogram('px', 'MeV/c')
36 #throw away particles with px > 30 MeV/c OR < -30 MeV/c
37 #here, operator.ge is a function that returns TRUE if x1 >= x2
38 #so throw away particles with operator.ge(value, test) is TRUE
39 bunch_list[0].cut({'px':30.}, operator.ge)
40 #now use operator.le, which is a function that returns TRUE if x1 <= x2
41 bunch_list[0].cut({'px':-30.}, operator.le)
42 #make px histogram again; note that axis range is modified to fit values of the data
43 bunch_list[0].root_histogram('px','MeV/c')
44 
45 #reset all weights to one
46 #note that if you loaded a file with some weights in, these will be cleared
47 bunch_list[0].clear_weights()
48 #cut on transverse amplitude (in 4d x-y phase space)
49 #note global_cut=True => I make the cut for all bunches (based on event_number)
50 bunch_list[0].cut({'amplitude x y':30.}, operator.ge, global_cut=True)
51 n_stations = len(bunch_list)
52 #plot transverse amplitude
53 #note that the plot doesn't quite stop at 30 mm; this is because the
54 #amplitudes have been recalculated with the new distribution and have
55 #moved slightly
56 bunch_list[0].root_histogram('amplitude x y','mm')
57 
58 #plot transverse amplitude at a few points in the linac; look to see how much the amplitude blurs
59 bunch_list[n_stations/3].root_histogram('amplitude x y','mm')
60 bunch_list[2*n_stations/3].root_histogram('amplitude x y','mm')
61 bunch_list[-1].root_histogram('amplitude x y','mm')
62 
63 #clear weights
64 for bunch in bunch_list:
65  bunch.clear_weights()
66 
67 #plot transmission
68 Bunch.root_graph(bunch_list, 'mean', ['z'], 'bunch_weight', '')
69 
70 #cut on transmission in middle plane
71 #looks through all particles in bunch_list[0]; if it can't find the event_number in
72 #bunch_list[-1], set weight to 0
73 bunch_list[0].transmission_cut(bunch_list[-1], global_cut=True)
74 #plot transmission
75 Bunch.root_graph(bunch_list, 'mean', ['z'], 'bunch_weight', '')
76 
77 #now wait for user to review plots before ending
78 print 'Press <return> key to finish'
79 raw_input()
80 Bunch.clear_global_weights()
81 

Variable Documentation

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

Definition at line 32 of file Example_3.py.

tuple n_stations = len(bunch_list)

Definition at line 52 of file Example_3.py.