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

Variable Documentation

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

Definition at line 33 of file Example_3.py.

tuple n_stations = len(bunch_list)

Definition at line 53 of file Example_3.py.