00001
00002
00003
00004
00005 class Spectra(object):
00006 '''
00007 Look at Beta decay Spectra
00008 '''
00009
00010 def __init__(self,z,a):
00011 '''
00012 Create Spectra for given ground state isotope of given Z and A.
00013 '''
00014 import random
00015 self.random = random.Random()
00016
00017 from g4data import G4RadData
00018 rd = G4RadData()
00019 rd = G4RadData()
00020 self.nuc = rd.nuclideData(z,a)
00021 state = self.nuc.excitedState()
00022
00023 betas = []
00024 for dk in state.decays:
00025 if dk.type[0:4] == 'Beta':
00026 betas.append(dk)
00027 continue
00028 continue
00029
00030 if not betas:
00031 print 'Warning: no beta decays for: %s'%nuc
00032 return
00033
00034 self.betas = betas
00035 return
00036
00037 def modeledSpectra(self,nbins=100):
00038 total_fraction = sum([dk.fraction for dk in self.betas])
00039 maximum = max([dk.radiation.endpoint for dk in self.betas])
00040
00041 ret = []
00042 from numpy import array
00043 total = array([0.0 for x in range(nbins)])
00044 bins = [(x+0.5)*maximum/nbins for x in range(nbins)]
00045 for dk in self.betas:
00046 ratio = dk.fraction/total_fraction
00047 Sm = []
00048 Sm = map(dk.radiation.dnde,bins)
00049 scaled = [ratio*x for x in Sm]
00050 total += scaled
00051 ret.append(scaled)
00052 continue
00053 return (ret,bins,total)
00054
00055 def sampledSpectra(self,ndecays=1000,nbins=100):
00056 '''Return a list of tuples (decay.BetaDecay,numpy.histogram).
00057 Each histogram has the given number of bins and gives the
00058 sampled energy spectrum. The total number of entries of all
00059 histograms is as given. The branching fractions of the decay
00060 are used to divide up the total number of decays when sampling
00061 each branch. Histograms are normalized so their sum is 1 and
00062 keeping the branching ratios intact'''
00063 from numpy import histogram, array
00064
00065 total_fraction = float(sum([dk.fraction for dk in self.betas]))
00066 maximum = max([dk.radiation.endpoint for dk in self.betas])
00067 ret = []
00068
00069 binwidth = maximum/nbins
00070
00071 for dk in self.betas:
00072 ratio = dk.fraction/total_fraction
00073 energies = []
00074 dk_number = ndecays*ratio
00075 for count in range(0,int(dk_number)):
00076 energies.append(dk.radiation.sampleEnergy())
00077 h = histogram(energies,nbins,(0,maximum))
00078 norm = ratio/(binwidth*sum(h[0]))
00079 scaled = array([ bin*norm for bin in h[0] ])
00080 ret.append((dk,(scaled,h[1])))
00081 continue
00082 return ret
00083
00084
00085 pass
00086
00087 def Bi210():
00088 return plot(83,210)
00089
00090 def plot(z,a,nbins=100):
00091 spec = Spectra(z,a)
00092
00093 dk_h = spec.sampledSpectra(ndecays=10000,nbins=nbins)
00094
00095 from numpy import array
00096
00097 firstDK,firstH = dk_h[0]
00098 total = array([0.0 for x in range(len(firstH[0]))])
00099 for dk,h in dk_h:
00100 total += h[0]
00101 continue
00102
00103 mods,bins,modTotal = spec.modeledSpectra(nbins=nbins)
00104
00105 import matplotlib.pyplot as plt
00106 for dk,h in dk_h:
00107 plt.plot(h[1],h[0])
00108 continue
00109 plt.plot(firstH[1],total)
00110
00111 plt.plot(bins,modTotal)
00112 for mod in mods:
00113 plt.plot(bins,mod)
00114 continue
00115
00116 import elements
00117 plt.title('%d%s'%(a,elements.symbol(z)))
00118
00119 plt.show()
00120