xboa
Example_2.py
Go to the documentation of this file.
1 """
2 \namespace xboa::examples::Example_2
3 
4 Example code to read a file and make some plots
5 
6 \include xboa/examples/Example_2.py
7 """
8 
9 #############
10 # EXAMPLE 2 #
11 # PLOTTING #
12 #############
13 #
14 # Load a data file; make some plots
15 #
16 
17 #try to import the XBOA library - if this fails, review the installation procedure
18 from xboa import *
19 from xboa.Hit import Hit
20 from xboa.Bunch import Bunch
21 import sys
22 
23 #some input data
24 filename = sys.prefix+'/share/xboa/data/for009.dat'
25 filetype = "icool_for009" #some other options are "g4mice_virtual_hit", "icool_for003", "g4mice_special_hit"
26 
27 print '========= XBOA example 2 ========='
28 
29 #try to load an input file
30 #this will load the for009 file, and make a list of "bunches", one for each region
31 #a list is a python version of an array
32 print "This example shows how to make plots\nYou will need to have access to either ROOT library or the matplotlib library to make plots"
33 print "First loading the data... "
34 bunch_list = Bunch.new_list_from_read_builtin(filetype, filename)
35 print "Loaded"
36 
37 #make some plots
38 #first try to make plots with ROOT if PyROOT exists
39 try:
40  print 'Trying to make some plots using PyROOT plotting package'
41  Common.has_root() #check for PyRoot library
42  #momentum distribution at start and end
43  bunch_list[0] .root_histogram('p', 'MeV/c')
44  bunch_list[-1].root_histogram('p', 'MeV/c')
45 
46  #energy-time scatter plot at start
47  bunch_list[0].root_scatter_graph('t', 'energy', 'ns', 'MeV/c')
48 
49  #histogram at start; note that in the first instance it *looks* like a scatter plot
50  #but it is not; hence I draw the same histogram twice, once as a pseudo-scatter
51  #and once as a contour plot
52  bunch_list[0].root_histogram('t', 'ns', 'energy', 'MeV/c')
53  (canvas, hist) = bunch_list[0] .root_histogram('t', 'ns', 'energy', 'MeV/c')
54  hist.Draw('CONT')
55  canvas.Update()
56 
57  #evolution of RMS emittance in x along the beamline
58  Bunch.root_graph(bunch_list, 'mean', ['z'], 'emittance', ['x'], 'm', 'mm')
59  Bunch.root_graph(bunch_list, 'mean', ['z'], 'emittance', ['x','y'], 'm', 'mm')
60 except ImportError:
61  print "PyROOT not detected - skipping PyROOT graphics"
62 
63 #now try to make plots with matplotlib if matplotlib exists
64 try:
65  print 'Trying to make some plots using matplotlib plotting package'
66  Common.has_matplot() #check for matplot library
67  #momentum distribution at start and end
68  bunch_list[0] .matplot_histogram('p', 'MeV/c')
69  bunch_list[-1].matplot_histogram('p', 'MeV/c')
70 
71  #energy-time scatter plot at start
72  bunch_list[0].matplot_scatter_graph('t', 'energy', 'ns', 'MeV/c')
73 
74  #histogram at start; note that in the first instance it *looks* like a scatter plot
75  #but it is not; hence I draw the same histogram twice, once as a pseudo-scatter
76  #and once as a contour plot
77  bunch_list[0].matplot_histogram('t', 'ns', 'energy', 'MeV/c')
78 
79  #evolution of RMS emittance in x along the beamline
80  Bunch.matplot_graph(bunch_list, 'mean', ['z'], 'emittance', ['x'], 'm', 'mm')
81  Bunch.matplot_graph(bunch_list, 'mean', ['z'], 'emittance', ['x','y'], 'm', 'mm')
82 
83  try:
84  Common.show_matplot_and_continue()
85  except: #bug - if you have PyROOT and use python < 2.6, show_matplot_and_continue() fails
86  try:
87  print 'Press ctrl-C to continue'
88  Common.wait_for_matplot()
89  except:
90  pass
91 except ImportError:
92  print "Matplotlib not detected - skipping matplotlib graphics"
93 
94 
95 #now wait for user to review plots before ending
96 print 'Press <return> key to finish'
97 raw_input()
98 
99 
100 
101 
102