analysis.py
6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/python
import os, shutil
from xml.dom import minidom
from numpy import append, savetxt, shape, array, newaxis, zeros, arange, select
from numpy import where, size, log
from modules.read import select_events, ReadProfile, resultdir
from modules.spectrum import spectrum
from modules.maps import computeMap
from modules.arrival_angle import arrivalAngle
from modules.timing import timing
from modules.observables import observables_vs_delay, observables_vs_energy
from modules.constants import degre, day, yr
xmlfile = minidom.parse("simulations.xml")
simulations = xmlfile.getElementsByTagName("simu")
for simu in simulations:
print "#=============================================================================#"
fileId = simu.getAttribute("simulation_dir")
print " Reading data from ", fileId
output_dir = resultdir+simu.getAttribute("id")+"/"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# copy profile
shutil.copy(fileId+"/profile.dat",output_dir+"profile.dat")
print " Output directory: ", output_dir
print " Applying selection:"
Emin = float(simu.getAttribute("Emin"))
Emax = float(simu.getAttribute("Emax"))
print " > Energy range: [", Emin,"GeV,",Emax*1e-3,"TeV]"
powerlaw_index = simu.getAttribute("powerlaw_index")
if "simple case" not in fileId:
print " > Source spectrum:", powerlaw_index
PSF = simu.getAttribute("PSF")
if PSF == "Taylor2011":
print " > PSF:", PSF
theta_range = [5e-7,90]
else:
PSF = float(PSF)
print " > PSF:", PSF, "degre"
theta_range = [5e-7,PSF]
# read files
weight, energy, time, theta_arrival, theta, phi, generation = select_events(fileId,
Erange=[Emin,Emax], PSF=PSF, powerlaw_index=powerlaw_index)
theta_arrival /= degre
theta /= degre
phi /= degre
NbTotEvents = sum(weight)
print " ----------------------------------------------------------------------------- "
#=============================================================================#
# NO SELECTION
#=============================================================================#
print " > No selection of events ..."
# IMAGING ===================================================================#
nbBins = 300
computeMap(theta,phi,weight,energy,output_dir,nbBins,borne=[PSF,PSF])
# PARAMETER SPACE (theta, dt, E) ============================================#
nbBins = 50
ener,angle,dt = observables_vs_energy(energy,theta_arrival,time,weight)
Angle_Energy = ener[:,newaxis]
Angle_Energy = append(Angle_Energy,angle[:,newaxis],axis=1)
Delay_Energy = ener[:,newaxis]
Delay_Energy = append(Delay_Energy,dt[:,newaxis],axis=1)
dt,angle,ener = observables_vs_delay(energy,theta_arrival,time,weight)
Delay_vs_angle = angle[:,newaxis]
Delay_vs_angle = append(Delay_vs_angle,dt[:,newaxis],axis=1)
# TIME AND ARRIVAL ANGLE DISTRIBUTION =======================================#
nbBins = 200
theta2,dndtheta = arrivalAngle(theta_arrival,weight,nbBins,theta_range)
arrival_Angle = theta2[:,newaxis]
arrival_Angle = append(arrival_Angle,dndtheta[:,newaxis],axis=1)
delta_t,dNdt = timing(time,weight,nbBins)#,dt_range=[])
Timing = delta_t[:,newaxis]
Timing = append(Timing,dNdt[:,newaxis],axis=1)
# SPECTRUM (MEASURED) =======================================================#
nbBins = 200
ener,flux = spectrum(energy,weight,nbBins,E_range=[Emin,Emax])
Spectrum = ener[:,newaxis]
Spectrum = append(Spectrum,flux[:,newaxis],axis=1)
#=============================================================================#
# BY GENERATION
#=============================================================================#
print " > Selection by generation ..."
gen_tab =[0,2,4,6,8]#list(set(generation))
Gen_cont = zeros([shape(gen_tab)[0],2])
for gen in gen_tab:
cond = (generation==gen)
contrib =sum(weight[cond])/NbTotEvents*100
i = gen_tab.index(gen)
Gen_cont[i,0]=int(gen)
Gen_cont[i,1]=contrib
print " ... gen=",int(gen),"-> contribution:",int(contrib),"%"
# PARAMETER SPACE (theta, dt, E) =========================================#
ener,angle,dt = observables_vs_energy(energy[cond],theta_arrival[cond],time[cond],weight[cond])
Angle_Energy = append(Angle_Energy,angle[:,newaxis],axis=1)
Delay_Energy = append(Delay_Energy,dt[:,newaxis],axis=1)
dt,angle,ener = observables_vs_delay(energy[cond],theta_arrival[cond],time[cond],weight[cond])
Delay_vs_angle = append(Delay_vs_angle,dt[:,newaxis],axis=1)
# TIME AND ARRIVAL ANGLE DISTRIBUTION ==================================#
nbBins = 200
theta2,dndtheta = arrivalAngle(theta_arrival[cond],weight[cond],nbBins,theta_range)
arrival_Angle = append(arrival_Angle,dndtheta[:,newaxis],axis=1)
delta_t,dNdt = timing(time[cond],weight[cond],nbBins)
Timing = append(Timing,dNdt[:,newaxis],axis=1)
# SPECTRUM (MEASURED) ====================================================#
nbBins = 200
ener,flux = spectrum(energy[cond],weight[cond],nbBins,E_range=[Emin,Emax])
Spectrum = append(Spectrum,flux[:,newaxis],axis=1)
#=============================================================================#
# BY TIME RANGE
#=============================================================================#
print " > Selection by time range ..."
tmax = [day, 10*day, 30*day, yr] # yr
for n in arange(0,4,1):
cond= (time<tmax[n])
contrib =sum(weight[cond])/NbTotEvents*100
print " ... integration time =",float(tmax[n]),"s\t-> contribution:",int(contrib),"%"
# ARRIVAL ANGLE DISTRIBUTION ============================================#
nbBins = 200
theta2,dndtheta = arrivalAngle(theta_arrival[cond],weight[cond],nbBins,theta_range)
arrival_Angle = append(arrival_Angle,dndtheta[:,newaxis],axis=1)
# SPECTRUM (MEASURED) ====================================================#
nbBins = 200
ener,flux = spectrum(energy[cond],weight[cond],nbBins,E_range=[Emin,Emax])
Spectrum = append(Spectrum,flux[:,newaxis],axis=1)
#=============================================================================#
print " > writing files"
savetxt(output_dir+"/Spectrum.txt",Spectrum)
savetxt(output_dir+"/arrival_Angle_distribution.txt",arrival_Angle)
savetxt(output_dir+"/Timing.txt",Timing)
savetxt(output_dir+"/Angle_versus_Energy.txt",Angle_Energy)
savetxt(output_dir+"/Delay_versus_Angle.txt",Delay_vs_angle)
savetxt(output_dir+"/Delay_versus_Energy.txt",Delay_Energy)
savetxt(output_dir+"/Generation.txt",Gen_cont)
print "#=============================================================================#"