Blame view

analysis.py 9.48 KB
7030f150   Thomas Fitoussi   Full reorganisati...
1
2
#!/bin/python

30f3bf5d   Thomas Fitoussi   Adapt reading of ...
3
import os, shutil
188a8c6a   Thomas Fitoussi   Update analysis 2...
4
5
6
7
8
from xml.dom               import minidom
from numpy                 import append, savetxt, shape, array, newaxis, zeros, arange, select, where
from modules.read          import ReadResults, ReadProfile, resultdir
from modules.spectrum      import spectrum
from modules.maps          import computeMap
4d96d628   Thomas Fitoussi   orrection in "ana...
9
from modules.arrival_angle import arrivalAngle
188a8c6a   Thomas Fitoussi   Update analysis 2...
10
from modules.timing        import timing
d88319ae   Thomas Fitoussi   Update analysis 2...
11
from modules.observables   import observables_vs_delay, observables_vs_energy
188a8c6a   Thomas Fitoussi   Update analysis 2...
12
13
14
15
16
from modules.constants     import degre, day, yr


def PSF_Taylor2011(E): # E in GeV -> PSF in degre
   return 1.7 * E**(-0.74) * (1+(E/15)**2)**0.37
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
17
18
19
20
21
22
23
24
25

xmlfile = minidom.parse("simulations.xml")
simulations = xmlfile.getElementsByTagName("simu")

for simu in simulations:
   fileId = simu.getAttribute("simulation_dir")
   output_dir = resultdir+simu.getAttribute("id")+"/"
   if not os.path.exists(output_dir):
      os.makedirs(output_dir)
188a8c6a   Thomas Fitoussi   Update analysis 2...
26
27
28

   Emin = float(simu.getAttribute("Emin")) 
   Emax = float(simu.getAttribute("Emax"))
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
29
   print "#=============================================================================#"
188a8c6a   Thomas Fitoussi   Update analysis 2...
30
   print " Reading data from ", fileId 
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
31
   print " Output directory: ", output_dir 
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
32
33
   # copy profile
   shutil.copy(fileId+"/profile.dat",output_dir+"profile.dat")
074c1729   Thomas Fitoussi   Analysis.py used ...
34
   # read files
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
35

188a8c6a   Thomas Fitoussi   Update analysis 2...
36
   n_phot = ReadProfile("../"+fileId,[4]) 
d943e502   Thomas Fitoussi   Update 'analysis'...
37
38
   results=ReadResults(fileId)
   # select photons only
188a8c6a   Thomas Fitoussi   Update analysis 2...
39
40
   cond = (results[0]%2==0) & (results[2]>=Emin) & (results[2]<=Emax)
   results = results[:,cond]
d943e502   Thomas Fitoussi   Update 'analysis'...
41
42

   generation = results[0]
188a8c6a   Thomas Fitoussi   Update analysis 2...
43
   weight = results[1]/n_phot
d943e502   Thomas Fitoussi   Update 'analysis'...
44
45
46
47
48
49
50
51
52
   energy = results[2]
   time = results[3]
   arrival_pos1 = results[4]*degre
   arrival_pos2 = results[6]*degre
   theta = (results[4]-results[6])*degre
   phi = (results[5]-results[7])*degre
   theta_arrival = results[8]*degre
   Esource =results[9]

188a8c6a   Thomas Fitoussi   Update analysis 2...
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
   print " [", Emin,"GeV,",Emax,"GeV] band ->",shape(results)[1],"events selected"
   theta_range = [1e-5,90]
   time_range=[1e0,1e9]#max(time)]
   
   PSF  = simu.getAttribute("PSF")
   if PSF == "Taylor2011":
      print " PSF:", PSF
   else:
      PSF = float(PSF)
      print " PSF:", PSF, "degre"

   #  SPECTRUM (SOURCE) =========================================================#
   if "simple case" not in fileId:
      powerlaw_index = simu.getAttribute("powerlaw_index") 
      print " Applying source spectrum:", powerlaw_index
      weightini = weight
      Es=array(list(set(Esource)))
      if powerlaw_index == "BL Lacertae*":
         Eth = 300
         gamaVHE = 2.94
         gamaHE  = 1.86
         condlist = [Esource>Eth,Esource<=Eth]
         spect = [(Esource/Eth)**(1-gamaVHE),(Esource/Eth)**(1-gamaHE)]
         weight = weightini*select(condlist,spect)
         Ws = select([Es>Eth,Es<=Eth],[(Es/Eth)**(1-gamaVHE),(Es/Eth)**(1-gamaVHE)])
      elif powerlaw_index == "1ES 0229+200":
         powerlaw_index = 5/3.
         weight = weightini*(Esource/min(Esource))**(1-powerlaw_index) 
         Ws= (Es/min(Es))**(1-powerlaw_index)  / n_phot
      else: 
         powerlaw_index = float(simu.getAttribute("powerlaw_index")) 
         weight = weightini*(Esource/min(Esource))**(1-powerlaw_index) 
         Ws= (Es/min(Es))**(1-powerlaw_index)  / n_phot

      nbBins = 100
      Es,Fs = spectrum(Es,Ws,nbBins=nbBins,E_range=[Emin,Emax])
      Source = Es[:,newaxis]
      Source = append(Source,Fs[:,newaxis],axis=1)
      if shape(Ws!=0)[0]>1:
          weight /= (max(Es)-min(Es))*(max(Fs)+min(Fs))/2
      savetxt(output_dir+"/Source_spectrum.txt",Source)

   NbTotEvents = sum(weight)
   print " ----------------------------------------------------------------------------- "
28843f23   Thomas Fitoussi   Put radial distri...
97
98

   #=============================================================================#
f5a75019   Thomas Fitoussi   Rewrite Analysis....
99
   #  NO SELECTION
28843f23   Thomas Fitoussi   Put radial distri...
100
   #=============================================================================#
f5a75019   Thomas Fitoussi   Rewrite Analysis....
101
   print "   > No selection of events ..." 
f5a75019   Thomas Fitoussi   Rewrite Analysis....
102
   #  IMAGING ===================================================================#
188a8c6a   Thomas Fitoussi   Update analysis 2...
103
104
   nbBins = 300
   computeMap(theta,phi,weight,energy,output_dir,nbBins,borne=[theta_range[1],theta_range[1]])
f5a75019   Thomas Fitoussi   Rewrite Analysis....
105

188a8c6a   Thomas Fitoussi   Update analysis 2...
106
107
   #  PARAMETER SPACE (theta, dt, E) ============================================#
   nbBins = 50
d88319ae   Thomas Fitoussi   Update analysis 2...
108
   ener,angle,dt = observables_vs_energy(energy,theta_arrival,time/yr,weight)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
109
110
   Angle_Energy = ener[:,newaxis]
   Angle_Energy = append(Angle_Energy,angle[:,newaxis],axis=1)
188a8c6a   Thomas Fitoussi   Update analysis 2...
111
112
   Delay_Energy = ener[:,newaxis]
   Delay_Energy = append(Delay_Energy,dt[:,newaxis],axis=1)
d88319ae   Thomas Fitoussi   Update analysis 2...
113
   dt,angle,ener = observables_vs_delay(energy,theta_arrival,time/yr,weight)
188a8c6a   Thomas Fitoussi   Update analysis 2...
114
115
116
117
118
119
   Delay_vs_angle = angle[:,newaxis]
   Delay_vs_angle = append(Delay_vs_angle,dt[:,newaxis],axis=1)
   if PSF == "Taylor2011":
      cond = (theta_arrival<PSF_Taylor2011(energy))
   else:
      cond = (theta_arrival<PSF)
d88319ae   Thomas Fitoussi   Update analysis 2...
120
   ener,angle,dt = observables_vs_energy(energy[cond],theta_arrival[cond],time[cond]/yr,weight[cond])
188a8c6a   Thomas Fitoussi   Update analysis 2...
121
   Angle_Energy = append(Angle_Energy,angle[:,newaxis],axis=1)
188a8c6a   Thomas Fitoussi   Update analysis 2...
122
   Delay_Energy = append(Delay_Energy,dt[:,newaxis],axis=1)
d88319ae   Thomas Fitoussi   Update analysis 2...
123
   dt,angle,ener = observables_vs_delay(energy[cond],theta_arrival[cond],time[cond]/yr,weight[cond])
188a8c6a   Thomas Fitoussi   Update analysis 2...
124
   Delay_vs_angle = append(Delay_vs_angle,dt[:,newaxis],axis=1)
f5a75019   Thomas Fitoussi   Rewrite Analysis....
125

188a8c6a   Thomas Fitoussi   Update analysis 2...
126
127
128
   #  TIME AND ARRIVAL ANGLE DISTRIBUTION =======================================#
   nbBins = 200
   theta2,dndtheta = arrivalAngle(theta_arrival,weight,nbBins,theta_range)
4d96d628   Thomas Fitoussi   orrection in "ana...
129
130
   arrival_Angle = theta2[:,newaxis]
   arrival_Angle = append(arrival_Angle,dndtheta[:,newaxis],axis=1)
b1415bc6   Thomas Fitoussi   Correction of spe...
131
   delta_t,dNdt = timing(time,weight,nbBins)#,dt_range=[])
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
132
133
   Timing = delta_t[:,newaxis]
   Timing = append(Timing,dNdt[:,newaxis],axis=1)
a44230b7   Thomas Fitoussi   Improve approxima...
134

188a8c6a   Thomas Fitoussi   Update analysis 2...
135
136
137
138
139
140
141
142
143
144
145
   #  SPECTRUM (MEASURED) =======================================================#
   ener,flux = spectrum(energy,weight,nbBins,E_range=[Emin,Emax])
   Spectrum = ener[:,newaxis]
   Spectrum = append(Spectrum,flux[:,newaxis],axis=1)
   nbBins = 200
   if PSF == "Taylor2011":
      cond = (theta_arrival<PSF_Taylor2011(energy))
   else:
      cond = (theta_arrival<PSF)
   ener,flux = spectrum(energy[cond],weight[cond],nbBins,E_range=[Emin,Emax])
   Spectrum = append(Spectrum,flux[:,newaxis],axis=1)
f5a75019   Thomas Fitoussi   Rewrite Analysis....
146

a44230b7   Thomas Fitoussi   Improve approxima...
147
   #=============================================================================#
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
148
149
150
   #  BY TIME RANGE
   #=============================================================================#
   print "   > Selection by time range ..." 
188a8c6a   Thomas Fitoussi   Update analysis 2...
151
   tmax = [day,10*day,30*day] # seconds (1 month, 1 year, 5 years)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
152
153
154

   for n in arange(0,3,1):
      cond= (time<tmax[n])
188a8c6a   Thomas Fitoussi   Update analysis 2...
155
156
      contrib =sum(weight[cond])/NbTotEvents*100 
      print "     ... integration time =",float(tmax[n]),"s\t-> contribution:",int(contrib),"%"
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
157

188a8c6a   Thomas Fitoussi   Update analysis 2...
158
159
160
      #  ARRIVAL ANGLE DISTRIBUTION  ============================================#
      nbBins = 200
      theta2,dndtheta = arrivalAngle(theta_arrival[cond],weight[cond],nbBins,theta_range)
d943e502   Thomas Fitoussi   Update 'analysis'...
161
162
      arrival_Angle = append(arrival_Angle,dndtheta[:,newaxis],axis=1)

188a8c6a   Thomas Fitoussi   Update analysis 2...
163
164
165
166
167
168
169
170
      #  SPECTRUM (MEASURED) ====================================================#
      nbBins = 200
      if PSF == "Taylor2011":
         cond = (theta_arrival<PSF_Taylor2011(energy)) & (time<tmax[n]) 
      else:
         cond = (theta_arrival<PSF) & (time<tmax[n]) 
      ener,flux = spectrum(energy[cond],weight[cond],nbBins,E_range=[Emin,Emax])
      Spectrum = append(Spectrum,flux[:,newaxis],axis=1)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
171
172

   #=============================================================================#
a44230b7   Thomas Fitoussi   Improve approxima...
173
174
   #  BY GENERATION
   #=============================================================================#
f5a75019   Thomas Fitoussi   Rewrite Analysis....
175
   print "   > Selection by generation ..." 
188a8c6a   Thomas Fitoussi   Update analysis 2...
176
   gen_tab =[0,2,4,6,8]#list(set(generation))
f5a75019   Thomas Fitoussi   Rewrite Analysis....
177
   Gen_cont = zeros([shape(gen_tab)[0],2])
a44230b7   Thomas Fitoussi   Improve approxima...
178
   for gen in gen_tab:
188a8c6a   Thomas Fitoussi   Update analysis 2...
179
      cond = (generation==gen) 
f5a75019   Thomas Fitoussi   Rewrite Analysis....
180
181
182
183
184
185
      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),"%"

188a8c6a   Thomas Fitoussi   Update analysis 2...
186
      #  PARAMETER SPACE (theta, dt, E) =========================================#
d88319ae   Thomas Fitoussi   Update analysis 2...
187
      ener,angle,dt = observables_vs_energy(energy[cond],theta_arrival[cond],time[cond]/yr,weight[cond])
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
188
      Angle_Energy = append(Angle_Energy,angle[:,newaxis],axis=1)
188a8c6a   Thomas Fitoussi   Update analysis 2...
189
      Delay_Energy = append(Delay_Energy,dt[:,newaxis],axis=1)
d88319ae   Thomas Fitoussi   Update analysis 2...
190
      dt,angle,ener = observables_vs_delay(energy[cond],theta_arrival[cond],time[cond]/yr,weight[cond])
188a8c6a   Thomas Fitoussi   Update analysis 2...
191
      Delay_vs_angle = append(Delay_vs_angle,dt[:,newaxis],axis=1)
f5a75019   Thomas Fitoussi   Rewrite Analysis....
192

188a8c6a   Thomas Fitoussi   Update analysis 2...
193
      #  TIME AND ARRIVAL ANGLE DISTRIBUTION ==================================#
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
194
      nbBins = 200
188a8c6a   Thomas Fitoussi   Update analysis 2...
195
196
      theta2,dndtheta = arrivalAngle(theta_arrival[cond],weight[cond],nbBins,theta_range)
      arrival_Angle = append(arrival_Angle,dndtheta[:,newaxis],axis=1)
a44230b7   Thomas Fitoussi   Improve approxima...
197
      delta_t,dNdt = timing(time[cond],weight[cond],nbBins)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
198
      Timing = append(Timing,dNdt[:,newaxis],axis=1)
188a8c6a   Thomas Fitoussi   Update analysis 2...
199
200
201
202
203
204
205
206
207
208
209
210

      #  SPECTRUM (MEASURED) ====================================================#
      nbBins = 200
      if PSF == "Taylor2011":
         cond = (theta_arrival<PSF_Taylor2011(energy)) & (generation==gen) 
      else:
         cond = (theta_arrival<PSF) & (generation==gen) 
      ener,flux = spectrum(energy[cond],weight[cond],nbBins,E_range=[Emin,Emax])
      Spectrum = append(Spectrum,flux[:,newaxis],axis=1)
      
    
      print shape(Spectrum)
a44230b7   Thomas Fitoussi   Improve approxima...
211
212

   #=============================================================================#
f5a75019   Thomas Fitoussi   Rewrite Analysis....
213
   print "   > writing files" 
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
214
   savetxt(output_dir+"/Spectrum.txt",Spectrum)
4d96d628   Thomas Fitoussi   orrection in "ana...
215
   savetxt(output_dir+"/arrival_Angle_distribution.txt",arrival_Angle)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
216
   savetxt(output_dir+"/Timing.txt",Timing)
188a8c6a   Thomas Fitoussi   Update analysis 2...
217
218
219
   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)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
220
221
   savetxt(output_dir+"/Generation.txt",Gen_cont)
   print "#=============================================================================#"