Analysis.py
8.27 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/python
from sys import argv
from numpy import append, savetxt, shape, array, newaxis, zeros, arange
from Modules.Read import ReadEnergy, ReadTime, ReadExtraFile, ReadProfile
from Modules.Read import ReadMomentumAngle, ReadPositionAngle, resultDirectory
from Modules.Spectrum import spectrum
from Modules.Map import computeMap
from Modules.Angle import angle_vs_energy, radial
from Modules.Timing import timing
from Modules.Constants import degre
def InProgress(i,Nmax):
print " ", (i*100)/Nmax, "% done"
if shape(argv)[0] < 2:
print "not enough arguments (at least 1)"
exit()
PowerSpectrum=[1,1.5,2,2.5]
for fileId in argv[1:]:
print "#=============================================================================#"
print "# Analysis of", fileId
print "#=============================================================================#"
# read files
print "# 1. Reading data"
time = ReadTime(fileId)
energy = ReadEnergy(fileId)
weightini, generation, theta_arrival, Esource, dir_source = ReadExtraFile(fileId,[2,3,4,5,6])
#weightini, generation, theta_arrival, Esource = ReadExtraFile(fileId,[2,3,4,5])
n_phot, n_lept = ReadProfile(fileId,[3,5])
ratio = n_phot#/n_lept
weightini /= ratio
theta_arrival*= degre
thetaDir,phiDir = ReadMomentumAngle(fileId)*degre
thetaPos,phiPos = ReadPositionAngle(fileId)*degre
theta = thetaDir - thetaPos
phi = phiDir -phiPos
Gen_contrib = []
Gen_cont = zeros((int(max(generation))+1))
Source = []
Spectrum = []
Timing = []
Angle_Energy = []
Radial = []
print "# 2. Computing powerlaw spectrum"
#=============================================================================#
# VERSUS SOURCE SPECTRUM
#=============================================================================#
for powerlaw_index in PowerSpectrum:
# apply source spectrum
weight_source = (Esource/min(Esource))**(1-powerlaw_index)
weight = weightini* weight_source
# GENERATION CONTRIBUTION ====================================================#
NbTotEvents = sum(weight)
if Gen_contrib==[]:
Gen_contrib = arange(0,max(generation)+1,1)[:,newaxis]
for gen in list(set(generation)):
Gen_cont[int(gen)] = sum(weight[generation==gen])/NbTotEvents *100
Gen_contrib = append(Gen_contrib,Gen_cont[:,newaxis],axis=1)
# SPECTRUM (SOURCE AND MEASURED) ============================================#
nbBins = 100
# draw source spectrum
Es=array(list(set(Esource)))
Ws= (Es/min(Es))**(1-powerlaw_index) / ratio
Es,Fs = spectrum(Es,Ws,nbBins=nbBins)
Es=Es[:,newaxis]
Fs=Fs[:,newaxis]
if Source==[]:
Source = Es
Source = append(Source,Fs,axis=1)
else:
Source = append(Source,Fs,axis=1)
# primary gamma-rays contribution and full spectrum
ener,flux,flux_0 = spectrum(energy,weight,generation,nbBins)
ener=ener[:,newaxis]
flux=flux[:,newaxis]
flux_0=flux_0[:,newaxis]
if Spectrum==[]:
Spectrum = ener
Spectrum = append(Spectrum,flux,axis=1)
Spectrum = append(Spectrum,flux_0,axis=1)
else:
Spectrum = append(Spectrum,flux,axis=1)
Spectrum = append(Spectrum,flux_0,axis=1)
# IMAGING, RADIAL DISTRIBUTION AND ANGLE VERSUS ENERGY ======================#
nbBins = 50
cond = energy>1e-3
ener,angle = angle_vs_energy(theta_arrival[cond],energy[cond],weight[cond],nbBins)
theta2,dndtheta2 = radial(theta[cond],weight[cond],nbBins)
theta2=theta2[:,newaxis]
dndtheta2=dndtheta2[:,newaxis]
ener=ener[:,newaxis]
angle=angle[:,newaxis]
if Radial==[]:
Radial = theta2
Radial = append(Radial,dndtheta2,axis=1)
Angle_Energy = ener
Angle_Energy = append(Angle_Energy,angle,axis=1)
else:
Radial = append(Radial,dndtheta2,axis=1)
Angle_Energy = append(Angle_Energy,angle,axis=1)
# TIME DISTRIBUTION AND TIME DELAY VERSUS ANGLE =============================#
nbBins = 100
delta_t,dNdt = timing(time,weight,nbBins)
delta_t=delta_t[:,newaxis]
dNdt=dNdt[:,newaxis]
if Timing==[]:
Timing = delta_t
Timing = append(Timing,dNdt,axis=1)
else:
Timing = append(Timing,dNdt,axis=1)
InProgress(PowerSpectrum.index(powerlaw_index)+1,shape(PowerSpectrum)[0])
#=============================================================================#
# VERSUS JET OPENING ANGLE
#=============================================================================#
Jet_Opening=[180,60,30] # degre (180 <=> isotrop)
powerlaw_index = 2
Elim = 1e-1 # GeV
thetalim = 20 # degre
# apply source spectrum
weight_source = (Esource/min(Esource))**(1-powerlaw_index)
weight = weightini* weight_source
print "# 3. compute images and radial distribution"
for jet_opening_angle in Jet_Opening:
# apply selection ( /!\ USE DECREASING VALUES OF jet_opening_angle )
cond = (dir_source*degre <= jet_opening_angle) & (energy>Elim) #& (abs(theta)<thetalim) & (abs(phi)<thetalim)
dir_source = dir_source[cond]
theta_arrival = theta_arrival[cond]
theta = theta[cond]
phi = phi[cond]
weight = weight[cond]
energy = energy[cond]
time = time[cond]
generation = generation[cond]
# GENERATION CONTRIBUTION ====================================================#
NbTotEvents = sum(weight)
if Gen_contrib==[]:
Gen_contrib = arange(0,max(generation)+1,1)[:,newaxis]
for gen in list(set(generation)):
Gen_cont[int(gen)] = sum(weight[generation==gen])/NbTotEvents *100
Gen_contrib = append(Gen_contrib,Gen_cont[:,newaxis],axis=1)
# SPECTRUM (SOURCE AND MEASURED) ============================================#
nbBins = 100
# draw source spectrum
Es=array(list(set(Esource)))
Ws= (Es/min(Es))**(1-powerlaw_index) / ratio
Es,Fs = spectrum(Es,Ws,nbBins=nbBins)
Es=Es[:,newaxis]
Fs=Fs[:,newaxis]
if Source==[]:
Source = Es
Source = append(Source,Fs,axis=1)
else:
Source = append(Source,Fs,axis=1)
# primary gamma-rays contribution and full spectrum
ener,flux,flux_0 = spectrum(energy,weight,generation,nbBins)
ener=ener[:,newaxis]
flux=flux[:,newaxis]
flux_0=flux_0[:,newaxis]
if Spectrum==[]:
Spectrum = ener
Spectrum = append(Spectrum,flux,axis=1)
Spectrum = append(Spectrum,flux_0,axis=1)
else:
Spectrum = append(Spectrum,flux,axis=1)
Spectrum = append(Spectrum,flux_0,axis=1)
# IMAGING, RADIAL DISTRIBUTION AND ANGLE VERSUS ENERGY ======================#
nbBins = 50
computeMap(theta,phi,weight,energy,fileId,jet_opening_angle,nbBins,borne=[thetalim,thetalim])
ener,angle = angle_vs_energy(theta_arrival,energy,weight,nbBins)
theta2,dndtheta2 = radial(theta_arrival,weight,nbBins)
theta2=theta2[:,newaxis]
dndtheta2=dndtheta2[:,newaxis]
ener=ener[:,newaxis]
angle=angle[:,newaxis]
if Radial==[]:
Radial = theta2
Radial = append(Radial,dndtheta2,axis=1)
Angle_Energy = ener
Angle_Energy = append(Angle_Energy,angle,axis=1)
else:
Radial = append(Radial,dndtheta2,axis=1)
Angle_Energy = append(Angle_Energy,angle,axis=1)
# TIME DISTRIBUTION AND TIME DELAY VERSUS ANGLE =============================#
nbBins = 100
delta_t,dNdt = timing(time,weight,nbBins)
delta_t=delta_t[:,newaxis]
dNdt=dNdt[:,newaxis]
Timing = append(Timing,dNdt,axis=1)
InProgress(Jet_Opening.index(jet_opening_angle)+1,shape(Jet_Opening)[0])
#=============================================================================#
print "# 4. writing files"
savetxt(resultDirectory+fileId+"/Generation.txt",Gen_contrib)
savetxt(resultDirectory+fileId+"/Spectrum.txt",Spectrum)
savetxt(resultDirectory+fileId+"/Source_spectrum.txt",Source)
savetxt(resultDirectory+fileId+"/Angle_vs_Energy.txt",Angle_Energy)
savetxt(resultDirectory+fileId+"/Radial_distribution.txt",Radial)
savetxt(resultDirectory+fileId+"/Timing.txt",Timing)
print "#=============================================================================#"