Project

General

Profile

Support #1254 » Xboa_Example2_TimEdit.txt

Carlisle, Timothy, 16 April 2013 13:13

 
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

    
25
if (len(sys.argv) < 2):
26
    print "No input data supplied. Fail. "
27
    raise SystemExit
28
#do nothing
29
    
30
filename = sys.argv[1]
31
filetype = "maus_root_virtual_hit"
32

    
33

    
34

    
35
print '========= XBOA example 2 ========='
36

    
37
#try to load an input file
38
#this will load the for009 file, and make a list of "bunches", one for each region
39
#a list is a python version of an array
40
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"
41
print "First loading the data... "
42
bunch_list = Bunch.new_list_from_read_builtin(filetype, filename)
43
print "Loaded"
44

    
45
#make some plots
46
#first try to make plots with ROOT if PyROOT exists
47
try:
48
  print 'Trying to make some plots using PyROOT plotting package'
49
  Common.has_root() #check for PyRoot library
50
  #momentum distribution at start and end
51
  bunch_list[0] .root_histogram('px', 'MeV/c')
52
  bunch_list[-1].root_histogram('px', 'MeV/c')
53

    
54
  #energy-time scatter plot at start
55
  bunch_list[0].root_scatter_graph('t', 'energy', 'ns', 'MeV/c')
56

    
57
  #histogram at start; note that in the first instance it *looks* like a scatter plot
58
  #but it is not; hence I draw the same histogram twice, once as a pseudo-scatter
59
  #and once as a contour plot
60
  bunch_list[0].root_histogram('t', 'ns', 'energy', 'MeV/c')
61
  (canvas, hist) = bunch_list[0] .root_histogram('t', 'ns', 'energy', 'MeV/c')
62
  hist.Draw('CONT')
63
  canvas.Update()
64

    
65
  #evolution of RMS emittance in x along the beamline
66
  Bunch.root_graph(bunch_list, 'mean', ['z'], 'mean', ['x'],     'm', 'mm')
67
  Bunch.root_graph(bunch_list, 'mean', ['z'], 'mean', ['pz'], 'm', 'mm')
68
*********************
69
## added by Tim **************	
70
*************************
71
  Bunch.root_graph(bunch_list, 'mean', ['z'], 'beta', ['x','y'],     'm', 'mm')
72
  Bunch.root_graph(bunch_list, 'mean', ['z'], 'emittance', ['x','y'], 'm', 'mm')
73
***********************************
74
except ImportError:
75
  print "PyROOT not detected - skipping PyROOT graphics"
76

    
77

    
78
#now wait for user to review plots before ending
79
print 'Press <return> key to finish'
80
raw_input()
81

    
82

    
83

    
84

    
85

    
(1-1/3)