Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
plot-woods-saxon.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file plot-woods-saxon.py
1 #!/usr/bin/env python3
2 
3 """
4 Compile the C++ Woods-Saxon generator, run it, pipe the results into this
5 script, and plot.
6 """
7 
8 import os.path
9 import subprocess
10 
11 import matplotlib.pyplot as plt
12 import numpy as np
13 from scipy import integrate
14 
15 
16 def woods_saxon(r, R, a):
17  return r*r/(1. + np.exp((r-R)/a))
18 
19 
20 def main():
21  basename = 'generate-woods-saxon'
22  cxxname = '{}.cxx'.format(basename)
23  if not os.path.exists(basename) or (
24  os.path.getmtime(cxxname) > os.path.getmtime(basename)):
25  print('compiling C++ Woods-Saxon generator')
26  subprocess.check_call(
27  ['g++', '-std=c++11', '-Wall', '-Wextra', '-O3', '-march=native',
28  cxxname, '-o'+basename])
29 
30  R = 6.5
31  a = 0.5
32  N = int(1e7)
33 
34  print('generating Woods-Saxon numbers')
35  with subprocess.Popen(['./'+basename, str(R), str(a), str(N)],
36  stdout=subprocess.PIPE) as proc:
37  samples = np.fromiter(proc.stdout, dtype=float, count=N)
38 
39  print('plotting')
40  plt.hist(samples, bins=200, histtype='step', normed=True)
41 
42  rmax = samples.max()
43  r = np.linspace(0, rmax, 1000)
44  norm = integrate.quad(woods_saxon, 0, rmax, args=(R, a))[0]
45  plt.plot(r, woods_saxon(r, R, a)/norm)
46 
47  plt.xlim(0, rmax)
48  plt.show()
49 
50 
51 if __name__ == "__main__":
52  main()