Blame view

Analysis.py 9.7 KB
7030f150   Thomas Fitoussi   Full reorganisati...
1
2
3
#!/bin/python

from sys import argv
631c85a9   Thomas Fitoussi   Ad generation com...
4
from numpy import append, savetxt, shape, array, newaxis, zeros, arange
074c1729   Thomas Fitoussi   Analysis.py used ...
5
from Modules.Read import ReadEnergy, ReadTime, ReadExtraFile, ReadProfile
49a0fe94   Thomas Fitoussi   Add direct printi...
6
from Modules.Read import ReadMomentumAngle, ReadPositionAngle, resultDirectory
65135318   Thomas Fitoussi   Reset modules
7
from Modules.Spectrum import spectrum
0b56ded5   Thomas Fitoussi   take into account...
8
from Modules.Map import computeMap
484df032   Thomas Fitoussi   Correction on the...
9
from Modules.Angle import angle_vs_energy, radial
074c1729   Thomas Fitoussi   Analysis.py used ...
10
from Modules.Timing import timing
49a0fe94   Thomas Fitoussi   Add direct printi...
11
from Modules.Constants import degre
7030f150   Thomas Fitoussi   Full reorganisati...
12

28843f23   Thomas Fitoussi   Put radial distri...
13
14
def InProgress(i,Nmax):
   print "  ", (i*100)/Nmax, "% done"
7030f150   Thomas Fitoussi   Full reorganisati...
15

6633967c   Thomas Fitoussi   Correction on the...
16
17
18
if shape(argv)[0] < 2: 
   print "not enough arguments (at least 1)"
   exit()
8ffbee93   Thomas Fitoussi   Analytic expressi...
19

6633967c   Thomas Fitoussi   Correction on the...
20
PowerSpectrum=[1,1.5,2,2.5] 
7030f150   Thomas Fitoussi   Full reorganisati...
21

074c1729   Thomas Fitoussi   Analysis.py used ...
22
for fileId in argv[1:]:
28843f23   Thomas Fitoussi   Put radial distri...
23
24
25
   print "#=============================================================================#"
   print "#    Analysis of", fileId
   print "#=============================================================================#"
074c1729   Thomas Fitoussi   Analysis.py used ...
26
   # read files
28843f23   Thomas Fitoussi   Put radial distri...
27
   print "# 1. Reading data" 
074c1729   Thomas Fitoussi   Analysis.py used ...
28
29
   time = ReadTime(fileId)
   energy = ReadEnergy(fileId)
0b56ded5   Thomas Fitoussi   take into account...
30
   weightini, generation, theta_arrival, Esource, dir_source = ReadExtraFile(fileId,[2,3,4,5,6])
138898c8   Thomas Fitoussi   Computation of ra...
31
   #weightini, generation, theta_arrival, Esource = ReadExtraFile(fileId,[2,3,4,5])
484df032   Thomas Fitoussi   Correction on the...
32
33
34
   n_phot, n_lept = ReadProfile(fileId,[3,5]) 
   ratio = n_phot#/n_lept
   weightini /= ratio
138898c8   Thomas Fitoussi   Computation of ra...
35
36
37
38
39
40
   theta_arrival*= degre

   thetaDir,phiDir = ReadMomentumAngle(fileId)*degre
   thetaPos,phiPos = ReadPositionAngle(fileId)*degre
   theta = thetaDir - thetaPos
   phi = phiDir -phiPos
074c1729   Thomas Fitoussi   Analysis.py used ...
41

631c85a9   Thomas Fitoussi   Ad generation com...
42
43
   Gen_contrib = []
   Gen_cont = zeros((int(max(generation))+1))
074c1729   Thomas Fitoussi   Analysis.py used ...
44
45
   Source = []
   Spectrum = []
074c1729   Thomas Fitoussi   Analysis.py used ...
46
   Timing = []
138898c8   Thomas Fitoussi   Computation of ra...
47
48
   Angle_Energy = []
   Radial = []
074c1729   Thomas Fitoussi   Analysis.py used ...
49

28843f23   Thomas Fitoussi   Put radial distri...
50
   print "# 2. Computing powerlaw spectrum" 
138898c8   Thomas Fitoussi   Computation of ra...
51
52
53
   #=============================================================================#
   #  VERSUS SOURCE SPECTRUM
   #=============================================================================#
6633967c   Thomas Fitoussi   Correction on the...
54
   for powerlaw_index in PowerSpectrum:
074c1729   Thomas Fitoussi   Analysis.py used ...
55
      # apply source spectrum
49a0fe94   Thomas Fitoussi   Add direct printi...
56
      weight_source = (Esource/min(Esource))**(1-powerlaw_index)
6633967c   Thomas Fitoussi   Correction on the...
57
58
      weight = weightini* weight_source

138898c8   Thomas Fitoussi   Computation of ra...
59
      # GENERATION CONTRIBUTION ====================================================#
631c85a9   Thomas Fitoussi   Ad generation com...
60
61
62
63
64
65
66
67
      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)

138898c8   Thomas Fitoussi   Computation of ra...
68
69
      #  SPECTRUM (SOURCE AND MEASURED) ============================================#
      nbBins = 100
074c1729   Thomas Fitoussi   Analysis.py used ...
70
71
      # draw source spectrum
      Es=array(list(set(Esource)))
484df032   Thomas Fitoussi   Correction on the...
72
      Ws= (Es/min(Es))**(1-powerlaw_index)  / ratio
6633967c   Thomas Fitoussi   Correction on the...
73
      Es,Fs = spectrum(Es,Ws,nbBins=nbBins)
074c1729   Thomas Fitoussi   Analysis.py used ...
74
75
76
77
78
79
80
81
      Es=Es[:,newaxis]
      Fs=Fs[:,newaxis]
      if Source==[]:
         Source = Es
         Source = append(Source,Fs,axis=1)
      else: 
         Source = append(Source,Fs,axis=1)

6633967c   Thomas Fitoussi   Correction on the...
82
83
      # primary gamma-rays contribution and full spectrum
      ener,flux,flux_0 = spectrum(energy,weight,generation,nbBins)
074c1729   Thomas Fitoussi   Analysis.py used ...
84
85
86
87
88
89
90
91
92
93
94
      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)

138898c8   Thomas Fitoussi   Computation of ra...
95
      #  IMAGING, RADIAL DISTRIBUTION AND ANGLE VERSUS ENERGY ======================#
484df032   Thomas Fitoussi   Correction on the...
96
97
98
99
      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)
138898c8   Thomas Fitoussi   Computation of ra...
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
      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 =============================#
074c1729   Thomas Fitoussi   Analysis.py used ...
116
117
118
119
120
121
122
123
124
125
      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)

28843f23   Thomas Fitoussi   Put radial distri...
126
127
128
      InProgress(PowerSpectrum.index(powerlaw_index)+1,shape(PowerSpectrum)[0])

   #=============================================================================#
138898c8   Thomas Fitoussi   Computation of ra...
129
   #  VERSUS JET OPENING ANGLE
28843f23   Thomas Fitoussi   Put radial distri...
130
   #=============================================================================#
138898c8   Thomas Fitoussi   Computation of ra...
131
132
133
   Jet_Opening=[180,60,30] # degre (180 <=> isotrop)
   powerlaw_index = 2
   Elim = 1e-1 # GeV
484df032   Thomas Fitoussi   Correction on the...
134
   thetalim = 20 # degre
138898c8   Thomas Fitoussi   Computation of ra...
135
136
137
138
139

   # apply source spectrum
   weight_source = (Esource/min(Esource))**(1-powerlaw_index)
   weight = weightini* weight_source

0b56ded5   Thomas Fitoussi   take into account...
140
141
   print "# 3. compute images and radial distribution" 
   for jet_opening_angle in Jet_Opening:
138898c8   Thomas Fitoussi   Computation of ra...
142
      # apply selection ( /!\ USE DECREASING VALUES OF jet_opening_angle )
a44230b7   Thomas Fitoussi   Improve approxima...
143
      cond = (dir_source*degre <= jet_opening_angle) #& (energy>Elim) #& (abs(theta)<thetalim) & (abs(phi)<thetalim)
138898c8   Thomas Fitoussi   Computation of ra...
144
145
146
147
148
149
150
151
152
153
154
155
156
      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]
0b56ded5   Thomas Fitoussi   take into account...
157

138898c8   Thomas Fitoussi   Computation of ra...
158
159
160
      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)
0b56ded5   Thomas Fitoussi   take into account...
161

138898c8   Thomas Fitoussi   Computation of ra...
162
163
164
165
      #  SPECTRUM (SOURCE AND MEASURED) ============================================#
      nbBins = 100
      # draw source spectrum
      Es=array(list(set(Esource)))
484df032   Thomas Fitoussi   Correction on the...
166
      Ws= (Es/min(Es))**(1-powerlaw_index) / ratio
138898c8   Thomas Fitoussi   Computation of ra...
167
168
169
170
171
172
173
174
      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)
0b56ded5   Thomas Fitoussi   take into account...
175

138898c8   Thomas Fitoussi   Computation of ra...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
      # 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 ======================#
484df032   Thomas Fitoussi   Correction on the...
190
      nbBins = 50
138898c8   Thomas Fitoussi   Computation of ra...
191
      computeMap(theta,phi,weight,energy,fileId,jet_opening_angle,nbBins,borne=[thetalim,thetalim])
484df032   Thomas Fitoussi   Correction on the...
192
193
      ener,angle = angle_vs_energy(theta_arrival,energy,weight,nbBins)
      theta2,dndtheta2 = radial(theta_arrival,weight,nbBins)
0b56ded5   Thomas Fitoussi   take into account...
194
195
196
197
198
199
200
201
202
203
204
205
206
      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)
28843f23   Thomas Fitoussi   Put radial distri...
207

484df032   Thomas Fitoussi   Correction on the...
208
      #  TIME DISTRIBUTION AND TIME DELAY VERSUS ANGLE =============================#
138898c8   Thomas Fitoussi   Computation of ra...
209
210
211
212
213
214
      nbBins = 100
      delta_t,dNdt = timing(time,weight,nbBins)
      delta_t=delta_t[:,newaxis]
      dNdt=dNdt[:,newaxis]
      Timing = append(Timing,dNdt,axis=1)

0b56ded5   Thomas Fitoussi   take into account...
215
      InProgress(Jet_Opening.index(jet_opening_angle)+1,shape(Jet_Opening)[0])
28843f23   Thomas Fitoussi   Put radial distri...
216
217

   #=============================================================================#
a44230b7   Thomas Fitoussi   Improve approxima...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
   #  BY ENERGY RANGE
   #=============================================================================#
   print "# 3. By energy range" 
   powerlaw_index = 2
   weight_source = (Esource/min(Esource))**(1-powerlaw_index)
   weight = weightini* weight_source

   Emin = [1e-3,1e0,1e3] #GeV
   Emax = [1e0,1e3,1e5] #GeV
   for n in arange(0,3,1):
      cond= (energy>Emin[n]) & (energy<Emax[n])

      #  TIME DISTRIBUTION AND TIME DELAY VERSUS ANGLE =============================#
      nbBins = 100
      delta_t,dNdt = timing(time[cond],weight[cond],nbBins)
      delta_t=delta_t[:,newaxis]
      dNdt=dNdt[:,newaxis]
      Timing = append(Timing,dNdt,axis=1)

   #=============================================================================#
   #  BY GENERATION
   #=============================================================================#
   print "# 3. By generation" 
   gen_tab =list(set(generation))
   powerlaw_index = 2
   weight_source = (Esource/min(Esource))**(1-powerlaw_index)
   weight = weightini* weight_source

   for gen in gen_tab:
      cond = generation==gen
      print shape(time[cond])

      #  TIME DISTRIBUTION AND TIME DELAY VERSUS ANGLE =============================#
      nbBins = 100
      delta_t,dNdt = timing(time[cond],weight[cond],nbBins)
      delta_t=delta_t[:,newaxis]
      dNdt=dNdt[:,newaxis]
      Timing = append(Timing,dNdt,axis=1)

   #=============================================================================#
28843f23   Thomas Fitoussi   Put radial distri...
258
   print "# 4. writing files" 
49a0fe94   Thomas Fitoussi   Add direct printi...
259
260
261
262
263
264
   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)
28843f23   Thomas Fitoussi   Put radial distri...
265
   print "#=============================================================================#"