Timing.py
5.04 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
from numpy import histogram, log10, shape, sqrt, arccos
from matplotlib.pyplot import figure, show
from scipy.integrate import quad
from Read import ReadTime, ReadWeight, ReadGeneration, ReadE_source, ReadD_source
from Read import ReadNphot_source,ReadPosition,ReadMomentum,ReadNbLeptonsProd
from Constants import yr, degre
from Integrand import comobileTime
from Analytic import Analytic_delay
def timing(fileId,gen=-1):
'''
Compute flux versus time delay
Input: directory name
Input (optional): generation (default = all)
Output: time delay (sec), flux
'''
time=ReadTime(fileId)
weight=ReadWeight(fileId)
if gen != -1:
generation = ReadGeneration(fileId)
time=time[generation==gen]
weight=weight[generation==gen]
prop = float(shape(time[time<0])[0])/shape(time)[0]
print "gen", int(gen), "->", shape(time)[0], "events", "negative time:",shape(time[time<0])[0], "~", prop
else:
prop = float(shape(time[time<0])[0])/shape(time)[0]
print "file", fileId, "->", shape(time)[0], "events", "negative time:",shape(time[time<0])[0], "~", prop
time=time[time>0]
weight=weight[time>0]
nbBins = 100
time = log10(time*yr)
dN,dt=histogram(time,nbBins,weights=weight)
timecenter=(dt[1:nbBins+1]+dt[0:nbBins])/2
binSize=dt[1:nbBins+1]-dt[0:nbBins]
Nmax=ReadNphot_source(fileId) # Nb photons emitted by the source
dNdt=dN/(Nmax*binSize)
maxflux = ReadNphot_source(fileId)
dNdt /= maxflux
return 10**timecenter, dNdt
def drawTiming(files):
'''
Plot flux versus time delay
Input: list of directories
Output: graph of flux versus time delay
'''
fig = figure()
ax = fig.add_subplot(111)
for fileId in files:
time,dNdt = timing(fileId)
ax.plot(time[dNdt!=0],dNdt[dNdt!=0],linestyle="steps-mid",label=fileId)
ax.set_xscale('log')
ax.set_yscale('log')
ax.grid(b=True,which='major')
ax.legend(loc="best")
ax.set_xlabel("Time delay [s]")
ax.set_ylabel("$t\ dN/dt$")
show()
def drawTimingGen(fileId):
fig = figure()
ax = fig.add_subplot(111)
for gen in list(set(ReadGeneration(fileId))):
time,dNdt = timing(fileId,gen)
ax.plot(time[dNdt!=0],dNdt[dNdt!=0],linestyle="steps-mid",label="gen="+str(int(gen)))
time,dNdt = timing(fileId)
ax.plot(time[dNdt!=0],dNdt[dNdt!=0],color='k',linestyle="steps-mid",label="gen=all")
ax.set_xscale('log')
ax.set_yscale('log')
ax.grid(b=True,which='major')
ax.legend(loc="best")
ax.set_xlabel("Time delay [s]")
ax.set_ylabel("$t\ dN/dt$")
show()
######## delay versus angle ###############
def delay_vs_angle(fileId,gen=-1):
'''
Compute time delay versus arrival angle
Input: directory name
Input (optional): generation (default = all)
Output: arrival angle [degre], time delay [sec]
'''
position = ReadPosition(fileId)
momentum = ReadMomentum(fileId)
weight = ReadWeight(fileId)
time=ReadTime(fileId)
hyp=sqrt(position[0]**2+position[1]**2+position[2]**2)
position /= hyp
hyp=sqrt(momentum[0]**2+momentum[1]**2+momentum[2]**2)
momentum /= hyp
costheta=position[0]*momentum[0]+position[1]*momentum[1]+position[2]*momentum[2]
if gen != -1 :
generation = ReadGeneration(fileId)
cond=(abs(costheta)<=1) & (generation==gen) & (time>0)
else:
cond=(abs(costheta)<=1) & (time>0)
prop = float(shape(time[time<0])[0])/shape(time)[0]
theta = arccos(costheta[cond])*degre
weight= weight[cond]
time=time[cond]*yr
print "gen", int(gen), "->", shape(time)[0], "events", "negative time:",shape(time[time<0])[0], "~", prop
cond=(theta!=0)
return theta[cond], time[cond], weight[cond]
def delay_vs_angle_histo(fileId,gen=-1):
theta, time, weight = delay_vs_angle(fileId,gen)
nbBins = 100
theta=log10(theta)
dt,dtheta=histogram(theta,nbBins,weights=time)
dN,dtheta=histogram(theta,nbBins,weights=weight)
dtheta=10**dtheta
thetacenter=(dtheta[1:nbBins+1]+dtheta[0:nbBins])/2
timing = dt/dN
return thetacenter, timing
def drawDelay_vs_angle(fileId):
'''
Plot angle versus time delay, generation by generation
Input: directory name
Output: graph of angle versus time delay, generation by generation
'''
fig = figure()
ax = fig.add_subplot(111)
Nmax = ReadNphot_source(fileId)
for gen in list(set(ReadGeneration(fileId))):
angle,delay,weight = delay_vs_angle(fileId,gen)
ax.plot(angle,delay/weight/Nmax,".",label="gen="+str(int(gen)))
#angle,delay = delay_vs_angle_histo(fileId,gen)
#ax.plot(angle,delay,linestyle="steps-mid",label="gen="+str(int(gen)))
angle,delay = delay_vs_angle_histo(fileId)
ax.plot(angle,delay/Nmax,color='k',linestyle="steps-mid",label="gen=all")
Esource = ReadE_source(fileId)
dSource = ReadD_source(fileId)
yfit = Analytic_delay(angle,Esource,dSource)
ax.plot(angle,yfit,'--k',linewidth=2)
ax.set_xscale('log')
ax.set_yscale('log')
ax.grid(b=True,which='major')
ax.legend(loc="best")
ax.set_xlabel("$\\theta $[deg]")
ax.set_ylabel("Time delay [s]")
show()