Commit 074c1729389f8fae1c62b0dbb1f1245a04272783

Authored by Thomas Fitoussi
1 parent 65135318
Exists in master

Analysis.py used to pre-compute spectrum, timing and radial distribution

computing done for power source spectrum (p= 1, 1.5, 2, 2.5)
   speed-up graph drawing, no need to recompute each time everything
1 #!/bin/python 1 #!/bin/python
2 2
3 from sys import argv 3 from sys import argv
4 -from numpy import shape 4 +from numpy import append,savetxt, shape, array, newaxis, empty
  5 +from Modules.Read import ReadEnergy, ReadTime, ReadExtraFile, ReadProfile
5 from Modules.Spectrum import spectrum 6 from Modules.Spectrum import spectrum
  7 +from Modules.Angle import radial, angle_vs_energy
  8 +from Modules.Timing import timing
6 9
7 def error(error_type): 10 def error(error_type):
8 print error_type 11 print error_type
9 exit() 12 exit()
10 13
11 if shape(argv)[0] < 2: 14 if shape(argv)[0] < 2:
12 - error("not enough arguments (at least 2)") 15 + error("not enough arguments (at least 1)")
13 16
14 -# choose what to print  
15 -print "What will be printed?"  
16 -print " 1. Spectrum"  
17 -print " 2. Image"  
18 -print " 3. Radial distribution VS energy"  
19 -print " 4. Timing distribution VS arrival angle"  
20 -selection = raw_input("selection (number): ") 17 +folder = "Results/"
21 18
22 -if (selection == "1"): # draw spectrum 19 +for fileId in argv[1:]:
  20 + # read files
  21 + time = ReadTime(fileId)
  22 + energy = ReadEnergy(fileId)
  23 + weightini, generation, theta_arrival, Esource = ReadExtraFile(fileId,[2,3,4,5])
  24 + nbPhotonsEmitted=ReadProfile(fileId,[3])
  25 +
  26 + Source = []
  27 + Spectrum = []
  28 + Radial = []
  29 + Angle_Energy = []
  30 + Timing = []
  31 +
  32 + for powerlaw_index in [1,1.5,2,2.5]:
  33 + # apply source spectrum
  34 + weight = weightini*(Esource)**(1-powerlaw_index)
  35 +
  36 + #=============================================================================#
  37 + # SPECTRUM (SOURCE AND MEASURED)
  38 + #=============================================================================#
  39 + nbBins = 100
  40 + # draw source spectrum
  41 + Es=array(list(set(Esource)))
  42 + Ws= (Es)**(1-powerlaw_index)
  43 + Es,Fs = spectrum(Es,Ws,nbBins)
  44 + Es=Es[:,newaxis]
  45 + Fs=Fs[:,newaxis]
  46 + if Source==[]:
  47 + Source = Es
  48 + Source = append(Source,Fs,axis=1)
  49 + else:
  50 + Source = append(Source,Fs,axis=1)
  51 +
  52 + # primary gamma-rays contribution
  53 + cond = (generation==0)
  54 + ener,flux_0 = spectrum(energy[cond],weight[cond],nbBins)
  55 + # full spectrum
  56 + ener,flux = spectrum(energy,weight,nbBins)
  57 + ener=ener[:,newaxis]
  58 + flux=flux[:,newaxis]
  59 + flux_0=flux_0[:,newaxis]
  60 + if Spectrum==[]:
  61 + Spectrum = ener
  62 + Spectrum = append(Spectrum,flux,axis=1)
  63 + Spectrum = append(Spectrum,flux_0,axis=1)
  64 + else:
  65 + Spectrum = append(Spectrum,flux,axis=1)
  66 + Spectrum = append(Spectrum,flux_0,axis=1)
  67 +
  68 + #=============================================================================#
  69 + # RADIAL DISTRIBUTION AND ANGLE VERSUS ENERGY
  70 + #=============================================================================#
  71 + nbBins = 100
  72 + ener,angle = angle_vs_energy(theta_arrival,energy,weight,nbPhotonsEmitted)
  73 + ener=ener[:,newaxis]
  74 + angle=angle[:,newaxis]
  75 + if Angle_Energy==[]:
  76 + Angle_Energy = ener
  77 + Angle_Energy = append(Angle_Energy,angle,axis=1)
  78 + else:
  79 + Angle_Energy = append(Angle_Energy,angle,axis=1)
  80 +
  81 + theta,dndtheta2 = radial(theta_arrival,weight,nbPhotonsEmitted)
  82 + theta=theta[:,newaxis]
  83 + dndtheta2=dndtheta2[:,newaxis]
  84 + if Radial==[]:
  85 + Radial = theta
  86 + Radial = append(Radial,dndtheta2,axis=1)
  87 + else:
  88 + Radial = append(Radial,dndtheta2,axis=1)
  89 +
  90 + #=============================================================================#
  91 + # TIME DISTRIBUTION AND TIME DELAY VERSUS ANGLE
  92 + #=============================================================================#
  93 + nbBins = 100
  94 + delta_t,dNdt = timing(time,weight,nbBins)
  95 + delta_t=delta_t[:,newaxis]
  96 + dNdt=dNdt[:,newaxis]
  97 + if Timing==[]:
  98 + Timing = delta_t
  99 + Timing = append(Timing,dNdt,axis=1)
  100 + else:
  101 + Timing = append(Timing,dNdt,axis=1)
  102 +
  103 + savetxt(folder+fileId+"/Spectrum.txt",Spectrum)
  104 + savetxt(folder+fileId+"/Source_spectrum.txt",Source)
  105 + savetxt(folder+fileId+"/Angle_vs_Energy.txt",Angle_Energy)
  106 + savetxt(folder+fileId+"/Radial_distribution.txt",Radial)
  107 + savetxt(folder+fileId+"/Timing.txt",Timing)
Modules/Angle.py
1 from numpy import log10, histogram 1 from numpy import log10, histogram
2 from matplotlib.pyplot import figure, show 2 from matplotlib.pyplot import figure, show
3 from matplotlib import gridspec 3 from matplotlib import gridspec
4 -from Read import ReadEnergy, ReadExtraFile, ReadProfile 4 +from Read import ReadRadial, ReadAngleVsEnergy
5 from Analytic import Analytic_theta 5 from Analytic import Analytic_theta
6 from Constants import degre 6 from Constants import degre
7 7
@@ -23,6 +23,24 @@ def angle_vs_energy(theta,energy,weight,nbPhotonsEmitted=1,nbBins=40): @@ -23,6 +23,24 @@ def angle_vs_energy(theta,energy,weight,nbPhotonsEmitted=1,nbBins=40):
23 23
24 return enercenter, angle 24 return enercenter, angle
25 25
  26 +def radial(theta,weight,nbPhotonsEmitted=1,nbBins=50,thetamax=90):
  27 + '''
  28 + Compute flux versus arrival angle
  29 + Input: directory name
  30 + Input (optionnal): maximal angle desired (by default full sky)
  31 + Output: arrival angle, flux (dn/dtheta)
  32 + '''
  33 +
  34 + theta=log10(theta)
  35 + dn,angle = histogram(theta,nbBins,range=[log10(1e-6),log10(thetamax)],weights=weight)
  36 + angle=10**angle
  37 + thetacenter = (angle[1:nbBins+1]+angle[0:nbBins])/2
  38 + dtheta = angle[1:nbBins+1]-angle[0:nbBins]
  39 + dndtheta = dn/dtheta*thetacenter
  40 + dndtheta /= nbPhotonsEmitted
  41 +
  42 + return thetacenter, dndtheta
  43 +
26 def drawAngle_vs_energy(files): 44 def drawAngle_vs_energy(files):
27 ''' 45 '''
28 Plot arrival angle versus energy + analytic expression 46 Plot arrival angle versus energy + analytic expression
@@ -33,27 +51,11 @@ def drawAngle_vs_energy(files): @@ -33,27 +51,11 @@ def drawAngle_vs_energy(files):
33 ax1 = fig.add_subplot(111) 51 ax1 = fig.add_subplot(111)
34 52
35 for fileId in files: 53 for fileId in files:
36 - energy = ReadEnergy(fileId)  
37 - weightini, theta_arrival, Esource = ReadExtraFile(fileId,[2,4,5])  
38 - nbPhotonsEmitted=ReadProfile(fileId,[3])  
39 -  
40 - # apply source spectrum  
41 - powerlaw_index = 1.5  
42 - weight = weightini*(Esource/min(Esource))**(1-powerlaw_index)  
43 - ener,angle = angle_vs_energy(theta_arrival,energy,weight,nbPhotonsEmitted)  
44 - p=ax1.plot(ener,angle,'-',label="p=%.1f"%powerlaw_index)  
45 -  
46 - # apply source spectrum  
47 - powerlaw_index = 2  
48 - weight = weightini*(Esource/min(Esource))**(1-powerlaw_index)  
49 - ener,angle = angle_vs_energy(theta_arrival,energy,weight,nbPhotonsEmitted)  
50 - p=ax1.plot(ener,angle,'-',label="p=%.1f"%powerlaw_index)  
51 -  
52 - # apply source spectrum  
53 - powerlaw_index = 2.5  
54 - weight = weightini*(Esource/min(Esource))**(1-powerlaw_index)  
55 - ener,angle = angle_vs_energy(theta_arrival,energy,weight,nbPhotonsEmitted)  
56 - p=ax1.plot(ener,angle,'-',label="p=%.1f"%powerlaw_index) 54 + i=1
  55 + for powerlaw_index in [1,1.5,2,2.5]:
  56 + # apply source spectrum
  57 + ener,angle = ReadAngleVsEnergy(fileId,[0,i])
  58 + p=ax1.plot(ener,angle,'-',label="p=%.1f"%powerlaw_index)
57 59
58 yfit = Analytic_theta(ener,fileId) 60 yfit = Analytic_theta(ener,fileId)
59 ax1.plot(ener,yfit,'--',color="m",linewidth=2,label="analytic") 61 ax1.plot(ener,yfit,'--',color="m",linewidth=2,label="analytic")
@@ -68,42 +70,19 @@ def drawAngle_vs_energy(files): @@ -68,42 +70,19 @@ def drawAngle_vs_energy(files):
68 70
69 show() 71 show()
70 72
71 -def radial(theta,weight,nbPhotonsEmitted=1,nbBins=50,thetamax=90):  
72 - '''  
73 - Compute flux versus arrival angle  
74 - Input: directory name  
75 - Input (optionnal): maximal angle desired (by default full sky)  
76 - Output: arrival angle, flux (dn/dtheta)  
77 - '''  
78 -  
79 - theta=log10(theta)  
80 - dn,angle = histogram(theta,nbBins,range=[log10(1e-6),log10(thetamax)],weights=weight)  
81 - angle=10**angle  
82 - thetacenter = (angle[1:nbBins+1]+angle[0:nbBins])/2  
83 - dtheta = angle[1:nbBins+1]-angle[0:nbBins]  
84 - dndtheta = dn/dtheta*thetacenter  
85 - dndtheta /= nbPhotonsEmitted  
86 -  
87 - return thetacenter, dndtheta  
88 -  
89 -def drawRadial(files,thetamax=90): 73 +def drawRadial(files):
90 ''' 74 '''
91 Plot flux versus arrival angle 75 Plot flux versus arrival angle
92 Input: list of directories 76 Input: list of directories
93 - Input (optionnal): maximal angle desired (by default full sky)  
94 Output: graph of flux versus angle 77 Output: graph of flux versus angle
95 ''' 78 '''
96 fig = figure() 79 fig = figure()
97 ax = fig.add_subplot(111) 80 ax = fig.add_subplot(111)
98 81
99 for fileId in files: 82 for fileId in files:
100 - weightini, theta_arrival, Esource = ReadExtraFile(fileId,[2,4,5])  
101 - nbPhotonsEmitted=ReadProfile(fileId,[3])  
102 -  
103 - for powerlaw_index in [1.5,2,2.5]:  
104 - # apply source spectrum  
105 - weight = weightini*(Esource/min(Esource))**(1-powerlaw_index)  
106 - theta,dndtheta2=radial(theta_arrival,weight,nbPhotonsEmitted,thetamax) 83 + i=1
  84 + for powerlaw_index in [1,1.5,2,2.5]:
  85 + theta,dndtheta2 = ReadRadial(fileId,[0,i])
107 ax.plot(theta,dndtheta2,drawstyle='steps-mid',label="p=%.1f"%powerlaw_index) 86 ax.plot(theta,dndtheta2,drawstyle='steps-mid',label="p=%.1f"%powerlaw_index)
108 87
109 ax.legend(loc="best") 88 ax.legend(loc="best")
Modules/Angle.pyc
No preview for this file type
Modules/Generation.py
1 from numpy import shape, arange 1 from numpy import shape, arange
2 from matplotlib.pyplot import figure, show, hist 2 from matplotlib.pyplot import figure, show, hist
3 -from Read import ReadGeneration, ReadWeight 3 +from Read import ReadExtraFile
4 4
5 5
6 def drawGeneration(files): 6 def drawGeneration(files):
@@ -13,8 +13,7 @@ def drawGeneration(files): @@ -13,8 +13,7 @@ def drawGeneration(files):
13 ax = fig.add_subplot(111) 13 ax = fig.add_subplot(111)
14 nbBins = 7 14 nbBins = 7
15 for fileId in files: 15 for fileId in files:
16 - weight = ReadWeight(fileId)  
17 - generation = ReadGeneration(fileId) 16 + weight, generation = ReadExtraFile(fileId,[2,3])
18 print "file", fileId, "->", shape(generation)[0], "events" 17 print "file", fileId, "->", shape(generation)[0], "events"
19 hist(generation,nbBins,range=[2,9],log=1,alpha=.5, weights=weight,label=fileId) 18 hist(generation,nbBins,range=[2,9],log=1,alpha=.5, weights=weight,label=fileId)
20 19
@@ -26,3 +25,15 @@ def drawGeneration(files): @@ -26,3 +25,15 @@ def drawGeneration(files):
26 ax.set_ylabel("Number of events") 25 ax.set_ylabel("Number of events")
27 26
28 show() 27 show()
  28 +
  29 +def GenContribution(fileId):
  30 + '''
  31 + compute the contribution to the final events of each
  32 + generation
  33 + '''
  34 + weight, generation = ReadExtraFile(fileId,[2,3])
  35 +
  36 + NbTotEvents = sum(weight)
  37 +
  38 + for gen in list(set(generation)):
  39 + print "Generation", gen, " -> ", sum(weight[generation==gen])/NbTotEvents *100, "\percent"
Modules/Generation.pyc
No preview for this file type
Modules/Read.py
@@ -59,3 +59,18 @@ def ReadElmag(fileName): @@ -59,3 +59,18 @@ def ReadElmag(fileName):
59 energy *= 10**(-9) #GeV 59 energy *= 10**(-9) #GeV
60 flux=(fluxGamma+fluxLepton)*1e-9 #GeV normalized to 1 initial photon 60 flux=(fluxGamma+fluxLepton)*1e-9 #GeV normalized to 1 initial photon
61 return energy,flux 61 return energy,flux
  62 +
  63 +def ReadSpectrum(fileName,cols=[0,1,2,3,4,5,6,7,8]):
  64 + return loadtxt(resultDirectory+fileName+"/Spectrum.txt",unpack=True,usecols=cols)
  65 +
  66 +def ReadSourceSpectrum(fileName,cols=[0,1,2,3,4]):
  67 + return loadtxt(resultDirectory+fileName+"/Source_spectrum.txt",unpack=True,usecols=cols)
  68 +
  69 +def ReadAngleVsEnergy(fileName,cols=[0,1,2,3,4]):
  70 + return loadtxt(resultDirectory+fileName+"/Angle_vs_Energy.txt",unpack=True,usecols=cols)
  71 +
  72 +def ReadRadial(fileName,cols=[0,1,2,3,4]):
  73 + return loadtxt(resultDirectory+fileName+"/Radial_distribution.txt",unpack=True,usecols=cols)
  74 +
  75 +def ReadTiming(fileName,cols=[0,1,2,3,4]):
  76 + return loadtxt(resultDirectory+fileName+"/Timing.txt",unpack=True,usecols=cols)
Modules/Read.pyc
No preview for this file type
Modules/Spectrum.py
1 from numpy import histogram, log10, sqrt, array 1 from numpy import histogram, log10, sqrt, array
2 from matplotlib.pyplot import figure, show, setp 2 from matplotlib.pyplot import figure, show, setp
3 from matplotlib import gridspec 3 from matplotlib import gridspec
4 -from Read import ReadEnergy, ReadExtraFile, ReadProfile 4 +from Read import ReadSpectrum, ReadSourceSpectrum
5 from Constants import degre 5 from Constants import degre
6 from Analytic import Ethreshold_gg 6 from Analytic import Ethreshold_gg
7 7
8 -def spectrum(energy,weight,nbPhotonsEmitted=1,nbBins=100): 8 +def spectrum(energy,weight,nbBins=100):
9 ''' 9 '''
10 Compute flux versus energy 10 Compute flux versus energy
11 Input: events energy and weight (optional: number of bins) 11 Input: events energy and weight (optional: number of bins)
@@ -17,7 +17,7 @@ def spectrum(energy,weight,nbPhotonsEmitted=1,nbBins=100): @@ -17,7 +17,7 @@ def spectrum(energy,weight,nbPhotonsEmitted=1,nbBins=100):
17 enercenter=(ener[1:nbBins+1]+ener[0:nbBins])/2 17 enercenter=(ener[1:nbBins+1]+ener[0:nbBins])/2
18 binSize=ener[1:nbBins+1]-ener[0:nbBins] 18 binSize=ener[1:nbBins+1]-ener[0:nbBins]
19 flux = enercenter**2 * flux/binSize 19 flux = enercenter**2 * flux/binSize
20 - flux /= nbPhotonsEmitted 20 + flux /= max(flux) #nbPhotonsEmitted
21 return enercenter,flux 21 return enercenter,flux
22 22
23 def drawSpectrum(files,PlotAnalytic=False): 23 def drawSpectrum(files,PlotAnalytic=False):
@@ -34,23 +34,22 @@ def drawSpectrum(files,PlotAnalytic=False): @@ -34,23 +34,22 @@ def drawSpectrum(files,PlotAnalytic=False):
34 ax2 = fig.add_subplot(gs[1],sharex=ax1) 34 ax2 = fig.add_subplot(gs[1],sharex=ax1)
35 35
36 for fileId in files: 36 for fileId in files:
37 - # read files  
38 - energy = ReadEnergy(fileId)  
39 - weightini, generation, theta_arrival, Esource = ReadExtraFile(fileId,[2,3,4,5])  
40 - nbPhotonsEmitted=ReadProfile(fileId,[3])  
41 -  
42 - for powerlaw_index in [1.5,2,2.5]:  
43 - # apply source spectrum  
44 - weight = weightini*(Esource/min(Esource))**(1-powerlaw_index) 37 + i=1
  38 + j=1
  39 + for powerlaw_index in [1,1.5,2,2.5]:
  40 + # read files
  41 + Es,Fs = ReadSourceSpectrum(fileId,[0,i])
  42 + p = ax1.plot(Es,Fs,drawstyle='--')
45 # draw full spectrum 43 # draw full spectrum
46 - ener,flux = spectrum(energy,weight,nbPhotonsEmitted)  
47 - p = ax1.plot(ener,flux,drawstyle='-',label="p=%.1f"%powerlaw_index) 44 + ener,flux,flux_0 = ReadSpectrum(fileId,[0,j,j+1])
  45 + ax1.plot(ener,flux,color=p[0].get_color(),drawstyle='-',label="p=%.1f"%powerlaw_index)
48 # primary gamma-rays contribution 46 # primary gamma-rays contribution
49 - cond = (generation==0)  
50 - ener,flux_cond = spectrum(energy[cond],weight[cond],nbPhotonsEmitted)  
51 - ax1.plot(ener,flux_cond,color=p[0].get_color(),linestyle='-.')  
52 - contrib=flux_cond/flux 47 + ax1.plot(ener,flux_0,color=p[0].get_color(),linestyle='-.')
  48 + contrib=flux_0/flux
53 ax2.plot(ener,contrib,'-',color=p[0].get_color()) 49 ax2.plot(ener,contrib,'-',color=p[0].get_color())
  50 +
  51 + i+=1
  52 + j+=2
54 53
55 # add spectrum for Fermi/LAT and CTA (FoV?) 54 # add spectrum for Fermi/LAT and CTA (FoV?)
56 55
Modules/Spectrum.pyc
No preview for this file type
Modules/Timing.py
1 from numpy import histogram, log10, shape, arange, pi 1 from numpy import histogram, log10, shape, arange, pi
2 from matplotlib.pyplot import figure, show 2 from matplotlib.pyplot import figure, show
3 -from Read import ReadTime, ReadExtraFile, ReadProfile, ReadEnergy 3 +from Read import ReadTime, ReadTiming, ReadExtraFile, ReadProfile, ReadEnergy
4 from Constants import yr, degre 4 from Constants import yr, degre
5 from Analytic import Analytic_delay_vs_theta 5 from Analytic import Analytic_delay_vs_theta
6 6
7 -def timing(time,weight,NbPhotonsEmitted=1,nbBins=100): 7 +def timing(time,weight,nbBins=100):
8 ''' 8 '''
9 Compute flux versus time delay 9 Compute flux versus time delay
10 Input: directory name 10 Input: directory name
@@ -23,7 +23,7 @@ def timing(time,weight,NbPhotonsEmitted=1,nbBins=100): @@ -23,7 +23,7 @@ def timing(time,weight,NbPhotonsEmitted=1,nbBins=100):
23 dN,dt=histogram(time,nbBins,weights=weight) 23 dN,dt=histogram(time,nbBins,weights=weight)
24 timecenter=(dt[1:nbBins+1]+dt[0:nbBins])/2 24 timecenter=(dt[1:nbBins+1]+dt[0:nbBins])/2
25 binSize=dt[1:nbBins+1]-dt[0:nbBins] 25 binSize=dt[1:nbBins+1]-dt[0:nbBins]
26 - dNdt=dN/(NbPhotonsEmitted*binSize) 26 + dNdt=dN/(max(dN)*binSize)
27 27
28 return 10**timecenter, dNdt 28 return 10**timecenter, dNdt
29 29
@@ -36,14 +36,9 @@ def drawTiming(files): @@ -36,14 +36,9 @@ def drawTiming(files):
36 fig = figure() 36 fig = figure()
37 ax = fig.add_subplot(111) 37 ax = fig.add_subplot(111)
38 for fileId in files: 38 for fileId in files:
39 - time = ReadTime(fileId)  
40 - weightini, Esource = ReadExtraFile(fileId,[2,5])  
41 - nbPhotonsEmitted=ReadProfile(fileId,[3])  
42 -  
43 - for powerlaw_index in [1.5,2,2.5]:  
44 - # apply source spectrum  
45 - weight = weightini*(Esource/min(Esource))**(1-powerlaw_index)  
46 - delta_t,dNdt = timing(time,weight,nbPhotonsEmitted) 39 + i=1
  40 + for powerlaw_index in [1,1.5,2,2.5]:
  41 + delta_t,dNdt = ReadTiming(fileId,[0,i])
47 ax.plot(delta_t,dNdt,linestyle="steps-mid",label="p=%.1f"%powerlaw_index) 42 ax.plot(delta_t,dNdt,linestyle="steps-mid",label="p=%.1f"%powerlaw_index)
48 43
49 ax.set_xscale('log') 44 ax.set_xscale('log')
@@ -51,7 +46,7 @@ def drawTiming(files): @@ -51,7 +46,7 @@ def drawTiming(files):
51 ax.grid(b=True,which='major') 46 ax.grid(b=True,which='major')
52 ax.legend(loc="best") 47 ax.legend(loc="best")
53 ax.set_xlabel("Time delay [s]") 48 ax.set_xlabel("Time delay [s]")
54 - ax.set_ylabel("$t\ dN/dt$") 49 + ax.set_ylabel("$t\ dN/dt$ [arbitrary units]")
55 50
56 show() 51 show()
57 52
Modules/Timing.pyc
No preview for this file type