Blame view

src/read.py 5.44 KB
188a8c6a   Thomas Fitoussi   Update analysis 2...
1
import os
4d2bede0   Thomas Fitoussi   add script for me...
2
3
4
from numpy import loadtxt, select, log, sin, cos, tan, arctan, arccos, exp, sqrt, pi, size
from numpy import append, zeros, linspace, shape
from src.constants import degre, yr
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
5
6
7
8

resultsFile="/results.dat"
profileFile="/profile.dat"

3c65fe50   Thomas Fitoussi   Modification in t...
9
10
11
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 ...
12
# READ SIMULATIONS FILES ===================================================================
4d2bede0   Thomas Fitoussi   add script for me...
13
def select_events(simulation,Erange=[1e-3,1e6],delayrange=[-1e8,1e30],powerlaw_index=1,tjet=180.,tobs=0.,generation='all'):    
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
14
   '''
3c65fe50   Thomas Fitoussi   Modification in t...
15
16
17
18
19
20
21
      Allowed to read results file of a simulation and to apply a preselection on events. 
  
      Input:
         1. simulation directory location
         OPTIONAL:
         2. energy range [min,max] in GeV
         3. delay range in second
3c65fe50   Thomas Fitoussi   Modification in t...
22
23
         5. powerlaw index Gamma such as dN/dE = E^-Gamma
         6. jet opening angle
627ed999   Thomas Fitoussi   1. Reorganized fi...
24
25
         7. observation angle 
         8. generation desired else 'all' 
3c65fe50   Thomas Fitoussi   Modification in t...
26
27

      Output:
3c65fe50   Thomas Fitoussi   Modification in t...
28
29
30
31
32
33
            0. weight
            1. energy [GeV]
            2. time delay [s]
            3. arrival angle [degre]
            4,5. theta, phi (arrival polar and azimuthal coordinates) [degre]
            (6. generation if generation = "all")
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
34
   '''
627ed999   Thomas Fitoussi   1. Reorganized fi...
35

3c65fe50   Thomas Fitoussi   Modification in t...
36
37
38
39
   # 0 - generation
   # 1 - weight
   # 2 - energy [GeV]
   # 3 - time delay [s]
627ed999   Thomas Fitoussi   1. Reorganized fi...
40
41
   # 4,5,6 - theta_pos, theta_dir, phi_dir [rad]
   # 7 - source photon energy [GeV]
4d2bede0   Thomas Fitoussi   add script for me...
42
43
   results = loadtxt(simulation+resultsFile,unpack=True)
   Dsource,redshift,n_phot  = ReadProfile(simulation,[2,3,4]) 
3c65fe50   Thomas Fitoussi   Modification in t...
44
45
46
47
48
49
   try:
      gen_select = int(generation)
      cond = (results[0] == gen_select)
   except:
      # select photons only
      cond = (results[0]%2==0) 
627ed999   Thomas Fitoussi   1. Reorganized fi...
50
   # energy selection and time selection
4d2bede0   Thomas Fitoussi   add script for me...
51
52
   cond = cond & (results[2] >= Erange[0])     & (results[2] <= Erange[1])  
   cond = cond & (results[3] >= delayrange[0]) & (results[3] <= delayrange[1])
3c65fe50   Thomas Fitoussi   Modification in t...
53
54
55
   results = results[:,cond]

   # Apply source spectrum
627ed999   Thomas Fitoussi   1. Reorganized fi...
56
57
58
59
   results[1] /= n_phot 
   if "simple case" not in simulation: 
      # compute intrinsic luminosity 
      powerlaw_index = float(powerlaw_index) 
4d2bede0   Thomas Fitoussi   add script for me...
60
      results[1]*= results[7]**(1-powerlaw_index) * log(Erange[1]/Erange[0])
627ed999   Thomas Fitoussi   1. Reorganized fi...
61
62
63
64
      if powerlaw_index == 2:
         results[1] /= log(Erange[1]/Erange[0])
      else:
         results[1] *= (2-powerlaw_index)/(Erange[1]**(2-powerlaw_index)-Erange[0]**(2-powerlaw_index))
3c65fe50   Thomas Fitoussi   Modification in t...
65

627ed999   Thomas Fitoussi   1. Reorganized fi...
66
67
68
69
70
71
72
   # misaligned jet & jet opening angle selection 
   results = append(results,zeros((2,size(results,axis=1))),axis=0)
   phi_dj = 0
   results[8] = phi_dj
   results[9] = cos(tobs*degre)*cos(results[4])+sin(tobs*degre)*sin(results[4])*cos(phi_dj-results[6])
   tabi = results
   Nphi=100
4d2bede0   Thomas Fitoussi   add script for me...
73
   for phi_dj in linspace(-pi,pi,Nphi-1):
627ed999   Thomas Fitoussi   1. Reorganized fi...
74
75
76
77
78
79
80
   #for phi_dj in 2*pi*random.rand(Nphi):
      results[8] = phi_dj
      results[9] = cos(tobs*degre)*cos(results[4])+sin(tobs*degre)*sin(results[4])*cos(phi_dj-results[6])
      tabi = append(tabi,results,axis=1)
   results = tabi     
   cond = (arccos(results[9])/degre < tjet)
   results = results[:,cond]         
4d2bede0   Thomas Fitoussi   add script for me...
81
82
83
84
   #if tjet < 90:
   #    sigma = sin(tjet*degre)
   #    results[1] *= exp((sin(arccos(results[9]))/sigma)**2) 
   results[1] /= Nphi #*4*pi * Dsource**2 *(1-cos(tjet*degre))
627ed999   Thomas Fitoussi   1. Reorganized fi...
85
86
   
   if generation == "all":
4d2bede0   Thomas Fitoussi   add script for me...
87
      return results[1],results[2],results[3],results[5],results[8],results[7],results[0]
3c65fe50   Thomas Fitoussi   Modification in t...
88
   else:
4d2bede0   Thomas Fitoussi   add script for me...
89
      return results[1],results[2],results[3],results[5],results[8],results[7]
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
90
91
92
93
94
95
96
97
98
99
100
101
102
103

# READ RESULTS FILES =======================================================================
resultdir="Results/"

def ReadProfile(fileName,cols=[0,1,2,3,4,5]):
   '''
      Allowed to read simulation profile
         0 - magnetic field amplitude [Gauss]
         1 - maximum source energy [TeV]
         2 - source distance [Mpc]
         3 - source redshift
         4 - number of photons emitted
         5 - number of leptons produced during the cascade
   '''
188a8c6a   Thomas Fitoussi   Update analysis 2...
104
105
106
107
   if not os.path.exists(resultdir+fileName):
      return loadtxt(fileName+profileFile,unpack=True,usecols=cols)
   else:
      return loadtxt(resultdir+fileName+profileFile,unpack=True,usecols=cols)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
108
109
110
111

def ReadSpectrum(fileName,cols=[0,1,2,3,4,5,6,7,8]): 
   return loadtxt(resultdir+fileName+"/Spectrum.txt",unpack=True,usecols=cols)

627ed999   Thomas Fitoussi   1. Reorganized fi...
112
113
114
def ReadSourceSpectrum(fileName): 
   return loadtxt(resultdir+fileName+"/Source_spectrum.txt",unpack=True,usecols=[0,1])

30f3bf5d   Thomas Fitoussi   Adapt reading of ...
115
def ReadRadial(fileName,cols=[0,1,2,3,4]): 
d943e502   Thomas Fitoussi   Update 'analysis'...
116
   return loadtxt(resultdir+fileName+"/arrival_Angle_distribution.txt",unpack=True,usecols=cols)
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
117
118

def ReadTiming(fileName,cols=[0,1,2,3,4]): 
30f3bf5d   Thomas Fitoussi   Adapt reading of ...
119
120
121
122
123
   return loadtxt(resultdir+fileName+"/Timing.txt",unpack=True,usecols=cols)

def ReadGeneration(fileName,cols=[0,1]):
   return loadtxt(resultdir+fileName+"/Generation.txt",unpack=True,usecols=cols)

188a8c6a   Thomas Fitoussi   Update analysis 2...
124
125
126
127
128
129
def ReadAngleVsEnergy(fileName,cols=[0,1,2,3,4]): 
   return loadtxt(resultdir+fileName+"/Angle_versus_Energy.txt",unpack=True,usecols=cols)

def ReadDelayVsEnergy(fileName,cols=[0,1,2,3,4]): 
   return loadtxt(resultdir+fileName+"/Delay_versus_Energy.txt",unpack=True,usecols=cols)

30f3bf5d   Thomas Fitoussi   Adapt reading of ...
130
131
def ReadDelayVsAngle(fileName,cols=[0,1,2,3,4]): 
   return loadtxt(resultdir+fileName+"/Delay_versus_angle.txt",unpack=True,usecols=cols)
188a8c6a   Thomas Fitoussi   Update analysis 2...
132
133
134
135
136
137
138
139
140
141
142

def ReadElmag(fileName):
   E,E2dNdE = loadtxt(resultdir+fileName+"/Elmag.dat",unpack=True,usecols=[0,1])
   E_PSF,E2dNdE_PSF = loadtxt(resultdir+fileName+"/Elmag_PSF.dat",unpack=True,usecols=[0,1])
   return  E*1e-9, E2dNdE*1e-9, E_PSF*1e-9, E2dNdE_PSF*1e-9

def ReadTaylor(fileName):
   E,E2dNdE = loadtxt(resultdir+fileName+"/Taylor2011-fig1.csv",delimiter=",",unpack=True,usecols=[0,1])
   E *= 1e-9
   E2dNdE *= 1e-9 
   return E, E2dNdE