Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.55 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Mar 22 15:23:25 2016
  4.  
  5. @author: Martian
  6. """
  7. import matplotlib.pyplot as plt
  8. from numpy import genfromtxt,exp,sqrt,diag
  9. import numpy as np
  10. import scipy as sp
  11. from scipy.optimize import curve_fit
  12.  
  13.  
  14. def ProcessData(filename):
  15. with open(filename,"r") as data:
  16. for line in data:
  17. if line.startswith("&END"):
  18. break
  19. else:
  20. print "Oooops,failed to read data"
  21. stored_data=genfromtxt(data,skip_header=1)
  22.  
  23. raw_data=np.array(stored_data,dtype=float)
  24. ch1_5keV=raw_data[raw_data[:,1]==1,0]
  25. ch2_5keV=raw_data[raw_data[:,1]==2,0]
  26. ch1_10keV=raw_data[raw_data[:,3]==1,2]
  27. ch2_10keV=raw_data[raw_data[:,3]==2,2]
  28. ch1_15keV=raw_data[raw_data[:,5]==1,4]
  29. ch2_15keV=raw_data[raw_data[:,5]==2,4]
  30. ch1_20keV=raw_data[raw_data[:,7]==1,6]
  31. ch2_20keV=raw_data[raw_data[:,7]==2,6]
  32. ch1_25keV=raw_data[raw_data[:,9]==1,8]
  33. ch2_25keV=raw_data[raw_data[:,9]==2,8]
  34.  
  35. return (ch1_5keV,ch2_5keV,ch1_10keV,ch2_10keV,ch1_15keV,ch2_15keV,ch1_20keV,ch2_20keV,ch1_25keV,ch2_25keV)
  36.  
  37.  
  38. def Histograms(ch1_10keV,ch2_10keV):
  39.  
  40. t=np.linspace(0,10,500)
  41. t2=(t[:-1]+t[1:])/2.0
  42. plt.figure()
  43. plt.hist(ch1_10keV,bins=t)
  44. plt.title("py14mz Channel Left 10 keV")
  45. plt.xlabel("Time(us)")
  46. plt.ylabel("Counts(s-1)")
  47.  
  48. plt.figure()
  49. plt.hist(ch2_10keV,bins=t)
  50. plt.title("py14mz Channel Right 10 keV")
  51. plt.xlabel("Time(us)")
  52. plt.ylabel("Channel Counts(s-1)")
  53. plt.show()
  54.  
  55. return (t2)
  56.  
  57. def Probabilities(ch1_5keV,ch2_5keV,ch1_10keV,ch2_10keV,ch1_15keV,ch2_15keV,ch1_20keV,ch2_20keV,ch1_25keV,ch2_25keV):
  58.  
  59. t=np.linspace(0,10,500)
  60.  
  61. counts_left_5keV=np.array((plt.hist(ch1_5keV,bins=t))[0],dtype=float)
  62. counts_right_5keV=np.array((plt.hist(ch2_5keV,bins=t))[0],dtype=float)
  63. counts_left_10keV=np.array((plt.hist(ch1_10keV,bins=t))[0],dtype=float)
  64. counts_right_10keV=np.array((plt.hist(ch2_10keV,bins=t))[0],dtype=float)
  65. counts_left_15keV=np.array((plt.hist(ch1_15keV,bins=t))[0],dtype=float)
  66. counts_right_15keV=np.array((plt.hist(ch2_15keV,bins=t))[0],dtype=float)
  67. counts_left_20keV=np.array((plt.hist(ch1_20keV,bins=t))[0],dtype=float)
  68. counts_right_20keV=np.array((plt.hist(ch2_20keV,bins=t))[0],dtype=float)
  69. counts_left_25keV=np.array((plt.hist(ch1_25keV,bins=t))[0],dtype=float)
  70. counts_right_25keV=np.array((plt.hist(ch2_25keV,bins=t))[0],dtype=float)
  71.  
  72. P_L_5keV=(counts_left_5keV)/(counts_right_10keV.sum()+counts_left_10keV.sum())
  73. P_R_5keV=(counts_right_5keV)/(counts_right_10keV.sum()+counts_left_10keV.sum())
  74. P_L_10keV=(counts_left_10keV)/(counts_right_10keV.sum()+counts_left_10keV.sum())
  75. P_R_10keV=(counts_right_10keV)/(counts_right_10keV.sum()+counts_left_10keV.sum())
  76. P_L_15keV=(counts_left_15keV)/(counts_right_10keV.sum()+counts_left_10keV.sum())
  77. P_R_15keV=(counts_right_15keV)/(counts_right_10keV.sum()+counts_left_10keV.sum())
  78. P_L_20keV=(counts_left_20keV)/(counts_right_10keV.sum()+counts_left_10keV.sum())
  79. P_R_20keV=(counts_right_20keV)/(counts_right_10keV.sum()+counts_left_10keV.sum())
  80. P_L_25keV=(counts_left_25keV)/(counts_right_10keV.sum()+counts_left_10keV.sum())
  81. P_R_25keV=(counts_right_25keV)/(counts_right_10keV.sum()+counts_left_10keV.sum())
  82.  
  83. return (counts_left_5keV,counts_right_5keV,counts_left_10keV,counts_right_10keV,counts_left_15keV,counts_right_15keV,counts_left_20keV,counts_right_20keV,counts_left_25keV,counts_right_25keV,P_L_5keV,P_R_5keV,P_L_10keV,P_R_10keV,P_L_15keV,P_R_15keV,P_L_20keV,P_R_20keV,P_L_25keV,P_R_25keV,)
  84.  
  85.  
  86. def Assymetry_data(P_L_5keV, P_R_5keV,P_L_10keV,P_R_10keV,P_L_15keV,P_R_15keV,P_L_20keV,P_R_20keV,P_L_25keV,P_R_25keV):
  87.  
  88. A_5keV=(P_L_5keV-P_R_5keV)/(P_L_5keV+P_R_5keV)
  89. A_10keV=(P_L_10keV-P_R_10keV)/(P_L_10keV+P_R_10keV)
  90. A_15keV=(P_L_15keV-P_R_15keV)/(P_L_15keV+P_R_15keV)
  91. A_20keV=(P_L_20keV-P_R_20keV)/(P_L_20keV+P_R_20keV)
  92. A_25keV=(P_L_25keV-P_R_25keV)/(P_L_25keV+P_R_25keV)
  93.  
  94. return (A_5keV,A_10keV,A_15keV,A_20keV,A_25keV)
  95.  
  96.  
  97. def GuessB (A_5keV,A_10keV,A_15keV,A_20keV,A_25keV):
  98. B_10keV=np.argmax(A_10keV)
  99.  
  100. return(B_10keV)
  101.  
  102.  
  103. def A (t2, beta,B):
  104.  
  105. gamma=851.616
  106. return (-1.0/3.0)*((sp.sin(gamma*B*t2 - beta)-sp.sin(gamma*B*t2 + beta))/(2.0*beta))
  107.  
  108. def AssymetryErr (counts_left_5keV,counts_right_5keV,counts_left_10keV,counts_right_10keV,counts_left_15keV,counts_right_15keV,counts_left_20keV,counts_right_20keV,counts_left_25keV,counts_right_25keV):
  109.  
  110. delta_right_5keV=sqrt(counts_right_5keV)
  111. delta_left_5keV=sqrt(counts_left_5keV)
  112. delta_right_10keV=sqrt(counts_right_10keV)
  113. delta_left_10keV=sqrt(counts_left_10keV)
  114. delta_right_15keV=sqrt(counts_right_15keV)
  115. delta_left_15keV=sqrt(counts_left_15keV)
  116. delta_right_20keV=sqrt(counts_right_20keV)
  117. delta_left_20keV=sqrt(counts_left_20keV)
  118. delta_right_25keV=sqrt(counts_right_25keV)
  119. delta_left_25keV=sqrt(counts_left_25keV)
  120.  
  121. err_5keV = 2*sqrt(1/(counts_left_5keV+counts_right_5keV)**4*(counts_left_5keV**2*delta_right_5keV**2+counts_right_5keV**2*delta_left_5keV**2))
  122. err_10keV = 2*sqrt(1/(counts_left_10keV+counts_right_10keV)**4*(counts_left_10keV**2*delta_right_10keV**2+counts_right_10keV**2*delta_left_10keV**2))
  123. err_15keV = 2*sqrt(1/(counts_left_15keV+counts_right_15keV)**4*(counts_left_15keV**2*delta_right_15keV**2+counts_right_15keV**2*delta_left_15keV**2))
  124. err_20keV = 2*sqrt(1/(counts_left_20keV+counts_right_20keV)**4*(counts_left_20keV**2*delta_right_20keV**2+counts_right_20keV**2*delta_left_20keV**2))
  125. err_25keV = 2*sqrt(1/(counts_left_25keV+counts_right_25keV)**4*(counts_left_25keV**2*delta_right_25keV**2+counts_right_25keV**2*delta_left_25keV**2))
  126.  
  127. return (err_5keV,err_10keV,err_15keV,err_20keV,err_25keV)
  128.  
  129. def Assymetry_Plot(t2,A_5keV,A_10keV,A_15keV,A_20keV,A_25keV,err_5keV,err_10keV,err_15keV,err_20keV,err_25keV):
  130.  
  131. plt.figure()
  132. plt.plot(t2,A_10keV)
  133. plt.title("py14mz Asymmetry Function 10 keV")
  134. plt.xlabel("Time(us)")
  135. plt.ylabel("Channel Counts(s-1)")
  136. plt.show
  137.  
  138. fitParams_5keV, fitCovariance_5keV = curve_fit(A,t2,A_5keV,po=(1.116,0.008),absolute_sigma=True,sigma=err_5keV)
  139. fitParams_10keV, fitCovariance_10keV = curve_fit(A,t2,A_10keV,po=(1.116,0.008),absolute_sigma=True,sigma=err_10keV)
  140. fitParams_15keV, fitCovariance_15keV = curve_fit(A,t2,A_15keV,po=(1.116,0.008),absolute_sigma=True,sigma=err_15keV)
  141. fitParams_20keV, fitCovariance_20keV = curve_fit(A,t2,A_20keV,po=(1.116,0.008),absolute_sigma=True,sigma=err_20keV)
  142. fitParams_25keV, fitCovariance_25keV = curve_fit(A,t2,A_25keV,po=(1.116,0.008),absolute_sigma=True,sigma=err_25keV)
  143.  
  144. plt.plot(t2, A(t2,fitParams_10keV[0],fitParams_10keV[1]), 'r-', label='fit')
  145. plt.errorbar(t2,A_10keV,yerr=err_10keV,fmt='',color='g',alpha=0.4)
  146.  
  147. return (fitParams_5keV,fitParams_10keV,fitParams_15keV,fitParams_20keV,fitParams_25keV)
  148.  
  149. ProcessData("pr3.dat")
  150. #Histograms(ch1_10keV,ch2_10keV)
  151. Probabilities(ch1_5keV,ch2_5keV,ch1_10keV,ch2_10keV,ch1_15keV,ch2_15keV,ch1_20keV,ch2_20keV,ch1_25keV,ch2_25keV)
  152. Assymetry_data(P_L_5keV,P_R_5keV,P_L_10keV,P_R_10keV,P_L_15keV,P_R_15keV,P_L_20keV,P_R_20keV,P_L_25keV,P_R_25keV)
  153. AssymetryErr(counts_left_5keV,counts_right_5keV,counts_left_10keV,counts_right_10keV,counts_left_15keV,counts_right_15keV,counts_left_20keV,counts_right_20keV,counts_left_25keV,counts_right_25keV)
  154. Assymetry_Plot(A_10keV)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement