Blame view

compute_distrib_and_obs.py 6.96 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
from xml.dom               import minidom
3c65fe50   Thomas Fitoussi   Modification in t...
5
from numpy                 import append, savetxt, shape, array, newaxis, zeros, arange, select
627ed999   Thomas Fitoussi   1. Reorganized fi...
6
from numpy                 import where, size, log10, ones
4d2bede0   Thomas Fitoussi   add script for me...
7
8
9
10
11
from src.read              import select_events, ReadProfile, resultdir
from src.distribution      import distribution
from src.image             import image
from src.observables       import observables_vs_delay, observables_vs_energy
from src.constants         import degre, day, yr
188a8c6a   Thomas Fitoussi   Update analysis 2...
12

30f3bf5d   Thomas Fitoussi   Adapt reading of ...
13
14
15
16
xmlfile = minidom.parse("simulations.xml")
simulations = xmlfile.getElementsByTagName("simu")

for simu in simulations:
3c65fe50   Thomas Fitoussi   Modification in t...
17
   print "#=============================================================================#"
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
18
   fileId = simu.getAttribute("simulation_dir")
3c65fe50   Thomas Fitoussi   Modification in t...
19
20
   print " Reading data from ", fileId 

30f3bf5d   Thomas Fitoussi   Adapt reading of ...
21
22
23
   output_dir = resultdir+simu.getAttribute("id")+"/"
   if not os.path.exists(output_dir):
      os.makedirs(output_dir)
3c65fe50   Thomas Fitoussi   Modification in t...
24
25
26
   # copy profile
   shutil.copy(fileId+"/profile.dat",output_dir+"profile.dat")
   print " Output directory: ", output_dir 
188a8c6a   Thomas Fitoussi   Update analysis 2...
27

3c65fe50   Thomas Fitoussi   Modification in t...
28
   print " Applying selection:"
188a8c6a   Thomas Fitoussi   Update analysis 2...
29
30
   Emin = float(simu.getAttribute("Emin")) 
   Emax = float(simu.getAttribute("Emax"))
627ed999   Thomas Fitoussi   1. Reorganized fi...
31
32
   NbinsE=int(log10(Emax)-log10(Emin))*15
   print "    > Energy range: [", Emin,"GeV,",Emax*1e-3,"TeV]" 
3c65fe50   Thomas Fitoussi   Modification in t...
33
34
35
   powerlaw_index = simu.getAttribute("powerlaw_index") 
   if "simple case" not in fileId:
      print "    > Source spectrum:", powerlaw_index
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
36

627ed999   Thomas Fitoussi   1. Reorganized fi...
37
38
39
40
41
   tjet = float(simu.getAttribute("tjet"))
   print "    > jet opening angle:", tjet, "degre" 
   tobs = float(simu.getAttribute("tobs"))
   print "    > observation angle:", tobs, "degre" 

4d2bede0   Thomas Fitoussi   add script for me...
42
43
   theta_range = [5e-7*degre,90*degre] # degre
   dt_range = [1e-4,1e17] # s
627ed999   Thomas Fitoussi   1. Reorganized fi...
44
   E_range= [Emin,Emax] # GeV
188a8c6a   Thomas Fitoussi   Update analysis 2...
45

4d2bede0   Thomas Fitoussi   add script for me...
46
47
48
49
50
51
   print "# Simulation parameters "
   EGMF,E0,Dsource,redshift,n_phot,n_lept  = ReadProfile(fileId) 
   print "    > Source redshift: ", redshift, "(eq.: ",Dsource,"Mpc)"
   print "    > EGMF:            ", EGMF, "G"
   print "    > run over         ", int(n_phot), " photons"

3c65fe50   Thomas Fitoussi   Modification in t...
52
   # read files
4d2bede0   Thomas Fitoussi   add script for me...
53
   weight, energy, time, theta_d, phi_d, Esource, generation =  select_events(fileId,
627ed999   Thomas Fitoussi   1. Reorganized fi...
54
         Erange=[Emin,Emax],powerlaw_index=powerlaw_index,tjet=tjet,tobs=tobs)
188a8c6a   Thomas Fitoussi   Update analysis 2...
55
56
   NbTotEvents = sum(weight)
   print " ----------------------------------------------------------------------------- "
28843f23   Thomas Fitoussi   Put radial distri...
57

627ed999   Thomas Fitoussi   1. Reorganized fi...
58
59
60
61
   if "simple case" not in fileId:
      print "   > Source spectrum ..." 
      Es=array(list(set(Esource)))
      Ws= (Es/min(Es))**(1-float(powerlaw_index))  
4d2bede0   Thomas Fitoussi   add script for me...
62
      ener,flux = distribution(Es,Ws,NbinsE,E_range)
627ed999   Thomas Fitoussi   1. Reorganized fi...
63
64
65
66
      Spectrum = ener[:,newaxis]
      Spectrum = append(Spectrum,flux[:,newaxis],axis=1)
      savetxt(output_dir+"/Source_spectrum.txt",Spectrum)

28843f23   Thomas Fitoussi   Put radial distri...
67
   #=============================================================================#
f5a75019   Thomas Fitoussi   Rewrite Analysis....
68
   #  NO SELECTION
28843f23   Thomas Fitoussi   Put radial distri...
69
   #=============================================================================#
f5a75019   Thomas Fitoussi   Rewrite Analysis....
70
   print "   > No selection of events ..." 
f5a75019   Thomas Fitoussi   Rewrite Analysis....
71
   #  IMAGING ===================================================================#
4d2bede0   Thomas Fitoussi   add script for me...
72
73
74
   step = 6
   PSF = 20
   image(theta_d,phi_d,weight,output_dir,step,borne=[PSF,PSF])
f5a75019   Thomas Fitoussi   Rewrite Analysis....
75

188a8c6a   Thomas Fitoussi   Update analysis 2...
76
77
   #  PARAMETER SPACE (theta, dt, E) ============================================#
   nbBins = 50
627ed999   Thomas Fitoussi   1. Reorganized fi...
78
79
   ener,angle,dt  = observables_vs_energy(energy,theta_d,time,weight)
   Angle_Energy   = ener[:,newaxis]
4d2bede0   Thomas Fitoussi   add script for me...
80
   Angle_Energy   = append(Angle_Energy,angle[:,newaxis]/degre,axis=1)
627ed999   Thomas Fitoussi   1. Reorganized fi...
81
82
83
   Delay_Energy   = ener[:,newaxis]
   Delay_Energy   = append(Delay_Energy,dt[:,newaxis],axis=1)
   dt,angle,ener  = observables_vs_delay(energy,theta_d,time,weight)
4d2bede0   Thomas Fitoussi   add script for me...
84
   Delay_vs_angle = angle[:,newaxis]/degre
188a8c6a   Thomas Fitoussi   Update analysis 2...
85
   Delay_vs_angle = append(Delay_vs_angle,dt[:,newaxis],axis=1)
f5a75019   Thomas Fitoussi   Rewrite Analysis....
86

627ed999   Thomas Fitoussi   1. Reorganized fi...
87
88
89
   #  DISTRIBUTIONS ============================================================#
   nbBins = 80
   theta2,dndtheta = distribution(theta_d,weight,nbBins,theta_range)
4d96d628   Thomas Fitoussi   orrection in "ana...
90
91
   arrival_Angle = theta2[:,newaxis]
   arrival_Angle = append(arrival_Angle,dndtheta[:,newaxis],axis=1)
627ed999   Thomas Fitoussi   1. Reorganized fi...
92
   delta_t,dNdt = distribution(time,weight,nbBins,dt_range)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
93
94
   Timing = delta_t[:,newaxis]
   Timing = append(Timing,dNdt[:,newaxis],axis=1)
627ed999   Thomas Fitoussi   1. Reorganized fi...
95
   ener,flux = distribution(energy,weight,NbinsE,E_range)
188a8c6a   Thomas Fitoussi   Update analysis 2...
96
97
   Spectrum = ener[:,newaxis]
   Spectrum = append(Spectrum,flux[:,newaxis],axis=1)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
98
99

   #=============================================================================#
a44230b7   Thomas Fitoussi   Improve approxima...
100
101
   #  BY GENERATION
   #=============================================================================#
f5a75019   Thomas Fitoussi   Rewrite Analysis....
102
   print "   > Selection by generation ..." 
188a8c6a   Thomas Fitoussi   Update analysis 2...
103
   gen_tab =[0,2,4,6,8]#list(set(generation))
f5a75019   Thomas Fitoussi   Rewrite Analysis....
104
   Gen_cont = zeros([shape(gen_tab)[0],2])
a44230b7   Thomas Fitoussi   Improve approxima...
105
   for gen in gen_tab:
188a8c6a   Thomas Fitoussi   Update analysis 2...
106
      cond = (generation==gen) 
f5a75019   Thomas Fitoussi   Rewrite Analysis....
107
108
109
110
111
112
      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...
113
      #  PARAMETER SPACE (theta, dt, E) =========================================#
627ed999   Thomas Fitoussi   1. Reorganized fi...
114
      ener,angle,dt = observables_vs_energy(energy[cond],theta_d[cond],time[cond],weight[cond])
4d2bede0   Thomas Fitoussi   add script for me...
115
      Angle_Energy = append(Angle_Energy,angle[:,newaxis]/degre,axis=1)
188a8c6a   Thomas Fitoussi   Update analysis 2...
116
      Delay_Energy = append(Delay_Energy,dt[:,newaxis],axis=1)
627ed999   Thomas Fitoussi   1. Reorganized fi...
117
      dt,angle,ener = observables_vs_delay(energy[cond],theta_d[cond],time[cond],weight[cond])
188a8c6a   Thomas Fitoussi   Update analysis 2...
118
      Delay_vs_angle = append(Delay_vs_angle,dt[:,newaxis],axis=1)
f5a75019   Thomas Fitoussi   Rewrite Analysis....
119

627ed999   Thomas Fitoussi   1. Reorganized fi...
120
121
       #  DISTRIBUTIONS =========================================================#
      theta2,dndtheta = distribution(theta_d[cond],weight[cond],nbBins,theta_range)
188a8c6a   Thomas Fitoussi   Update analysis 2...
122
      arrival_Angle = append(arrival_Angle,dndtheta[:,newaxis],axis=1)
627ed999   Thomas Fitoussi   1. Reorganized fi...
123
      delta_t,dNdt = distribution(time[cond],weight[cond],nbBins,dt_range)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
124
      Timing = append(Timing,dNdt[:,newaxis],axis=1)
4d2bede0   Thomas Fitoussi   add script for me...
125
      ener,flux = distribution(energy[cond],weight[cond],NbinsE,E_range)
188a8c6a   Thomas Fitoussi   Update analysis 2...
126
      Spectrum = append(Spectrum,flux[:,newaxis],axis=1)
3c65fe50   Thomas Fitoussi   Modification in t...
127
128
129
130
131

   #=============================================================================#
   #  BY TIME RANGE
   #=============================================================================#
   print "   > Selection by time range ..." 
627ed999   Thomas Fitoussi   1. Reorganized fi...
132
   tmax = [day, 30*day, yr, 1e3*yr, 1e6*yr] # yr 
3c65fe50   Thomas Fitoussi   Modification in t...
133

627ed999   Thomas Fitoussi   1. Reorganized fi...
134
   for n in arange(0,size(tmax),1):
3c65fe50   Thomas Fitoussi   Modification in t...
135
136
137
138
      cond= (time<tmax[n])
      contrib =sum(weight[cond])/NbTotEvents*100 
      print "     ... integration time =",float(tmax[n]),"s\t-> contribution:",int(contrib),"%"

627ed999   Thomas Fitoussi   1. Reorganized fi...
139
      theta2,dndtheta = distribution(theta_d[cond],weight[cond],nbBins,theta_range)
3c65fe50   Thomas Fitoussi   Modification in t...
140
      arrival_Angle = append(arrival_Angle,dndtheta[:,newaxis],axis=1)
4d2bede0   Thomas Fitoussi   add script for me...
141
      ener,flux = distribution(energy[cond],weight[cond],NbinsE,E_range)
3c65fe50   Thomas Fitoussi   Modification in t...
142
      Spectrum = append(Spectrum,flux[:,newaxis],axis=1)
a44230b7   Thomas Fitoussi   Improve approxima...
143
144

   #=============================================================================#
f5a75019   Thomas Fitoussi   Rewrite Analysis....
145
   print "   > writing files" 
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
146
   savetxt(output_dir+"/Spectrum.txt",Spectrum)
4d96d628   Thomas Fitoussi   orrection in "ana...
147
   savetxt(output_dir+"/arrival_Angle_distribution.txt",arrival_Angle)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
148
   savetxt(output_dir+"/Timing.txt",Timing)
188a8c6a   Thomas Fitoussi   Update analysis 2...
149
150
151
   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 ...
152
153
   savetxt(output_dir+"/Generation.txt",Gen_cont)
   print "#=============================================================================#"