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 xboa.common as common
22 import sys
23 
24 #some input data
25 filename = sys.prefix+'/share/xboa/data/for009.dat'
26 filetype = "icool_for009" #some other options are "g4mice_virtual_hit", "icool_for003", "g4mice_special_hit"
27 
28 print '========= XBOA example 2 ========='
29 
30 #try to load an input file
31 #this will load the for009 file, and make a list of "bunches", one for each region
32 #a list is a python version of an array
33 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"
34 print "First loading the data... "
35 bunch_list = Bunch.new_list_from_read_builtin(filetype, filename)
36 print "Loaded"
37 
38 #make some plots
39 #first try to make plots with ROOT if PyROOT exists
40 try:
41  print 'Trying to make some plots using PyROOT plotting package'
42  common.has_root() #check for PyRoot library
43  #momentum distribution at start and end
44  bunch_list[0] .root_histogram('p', 'MeV/c')
45  bunch_list[-1].root_histogram('p', 'MeV/c')
46 
47  #energy-time scatter plot at start
48  bunch_list[0].root_scatter_graph('t', 'energy', 'ns', 'MeV/c')
49 
50  #histogram at start; note that in the first instance it *looks* like a scatter plot
51  #but it is not; hence I draw the same histogram twice, once as a pseudo-scatter
52  #and once as a contour plot
53  bunch_list[0].root_histogram('t', 'ns', 'energy', 'MeV/c')
54  (canvas, hist) = bunch_list[0] .root_histogram('t', 'ns', 'energy', 'MeV/c')
55  hist.Draw('CONT')
56  canvas.Update()
57 
58  #evolution of RMS emittance in x along the beamline
59  Bunch.root_graph(bunch_list, 'mean', ['z'], 'emittance', ['x'], 'm', 'mm')
60  Bunch.root_graph(bunch_list, 'mean', ['z'], 'emittance', ['x','y'], 'm', 'mm')
61 except ImportError:
62  print "PyROOT not detected - skipping PyROOT graphics"
63 
64 #now try to make plots with matplotlib if matplotlib exists
65 try:
66  print 'Trying to make some plots using matplotlib plotting package'
67  common.has_matplot() #check for matplot library
68  #momentum distribution at start and end
69  bunch_list[0] .matplot_histogram('p', 'MeV/c')
70  bunch_list[-1].matplot_histogram('p', 'MeV/c')
71 
72  #energy-time scatter plot at start
73  bunch_list[0].matplot_scatter_graph('t', 'energy', 'ns', 'MeV/c')
74 
75  #histogram at start; note that in the first instance it *looks* like a scatter plot
76  #but it is not; hence I draw the same histogram twice, once as a pseudo-scatter
77  #and once as a contour plot
78  bunch_list[0].matplot_histogram('t', 'ns', 'energy', 'MeV/c')
79 
80  #evolution of RMS emittance in x along the beamline
81  Bunch.matplot_graph(bunch_list, 'mean', ['z'], 'emittance', ['x'], 'm', 'mm')
82  Bunch.matplot_graph(bunch_list, 'mean', ['z'], 'emittance', ['x','y'], 'm', 'mm')
83 
84  try:
85  common.show_matplot_and_continue()
86  except: #bug - if you have PyROOT and use python < 2.6, show_matplot_and_continue() fails
87  pass # give up on matplot
88 except ImportError:
89  print "Matplotlib not detected - skipping matplotlib graphics"
90 
91 
92 #now wait for user to review plots before ending
93 print 'Press <return> key to finish'
94 raw_input()
95 
96 
97 
98 
99 
Implemented within this module:
Definition: __init__.py:1
common module defines common utility data and functions that are used elsewhere
Definition: __init__.py:1
Implemented within this module:
Definition: __init__.py:1