Blame view

Modules/Timing.py 6.95 KB
f4246390   Thomas Fitoussi   Improve angular d...
1
from numpy import histogram, log10, shape, sqrt, arccos
7030f150   Thomas Fitoussi   Full reorganisati...
2
3
from matplotlib.pyplot import figure, show
from scipy.integrate import quad
f4246390   Thomas Fitoussi   Improve angular d...
4
from Read import ReadTime, ReadWeight, ReadGeneration, ReadE_source, ReadD_source
fb95bd3d   Thomas Fitoussi   Add time delay ve...
5
from Read import ReadNphot_source,ReadPosition,ReadMomentum,ReadNbLeptonsProd,ReadEnergy
f4246390   Thomas Fitoussi   Improve angular d...
6
from Constants import yr, degre
7030f150   Thomas Fitoussi   Full reorganisati...
7
from Integrand import comobileTime
fb95bd3d   Thomas Fitoussi   Add time delay ve...
8
from Analytic import Analytic_delay_vs_theta,Analytic_delay_vs_Egamma
7030f150   Thomas Fitoussi   Full reorganisati...
9

f4246390   Thomas Fitoussi   Improve angular d...
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
7030f150   Thomas Fitoussi   Full reorganisati...
29
30
31
32
33

   time=time[time>0]
   weight=weight[time>0]

   nbBins = 100
f4246390   Thomas Fitoussi   Improve angular d...
34
   time = log10(time*yr)
7030f150   Thomas Fitoussi   Full reorganisati...
35
36
37
38

   dN,dt=histogram(time,nbBins,weights=weight)
   timecenter=(dt[1:nbBins+1]+dt[0:nbBins])/2
   binSize=dt[1:nbBins+1]-dt[0:nbBins]
f4246390   Thomas Fitoussi   Improve angular d...
39
   Nmax=ReadNphot_source(fileId) # Nb photons emitted by the source
7030f150   Thomas Fitoussi   Full reorganisati...
40
   dNdt=dN/(Nmax*binSize)
f4246390   Thomas Fitoussi   Improve angular d...
41
   maxflux = ReadNphot_source(fileId)
e6f5876a   Thomas Fitoussi   Map: convolution ...
42
   dNdt /= maxflux
7030f150   Thomas Fitoussi   Full reorganisati...
43

f4246390   Thomas Fitoussi   Improve angular d...
44
   return 10**timecenter, dNdt
7030f150   Thomas Fitoussi   Full reorganisati...
45
46

def drawTiming(files):
f4246390   Thomas Fitoussi   Improve angular d...
47
48
49
50
51
   '''
      Plot flux versus time delay
      Input: list of directories
      Output: graph of flux versus time delay 
   '''
7030f150   Thomas Fitoussi   Full reorganisati...
52
53
54
55
56
57
   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)

f4246390   Thomas Fitoussi   Improve angular d...
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
   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')
7030f150   Thomas Fitoussi   Full reorganisati...
78
79
80
   ax.set_yscale('log')
   ax.grid(b=True,which='major')
   ax.legend(loc="best")
f4246390   Thomas Fitoussi   Improve angular d...
81
   ax.set_xlabel("Time delay [s]")
7030f150   Thomas Fitoussi   Full reorganisati...
82
83
84
   ax.set_ylabel("$t\ dN/dt$")

   show()
f4246390   Thomas Fitoussi   Improve angular d...
85
86
87
88
89
90
91
92
93
94
95

######## 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)
fb95bd3d   Thomas Fitoussi   Add time delay ve...
96
   energy = ReadEnergy(fileId)
f4246390   Thomas Fitoussi   Improve angular d...
97
98
   weight = ReadWeight(fileId)
   time=ReadTime(fileId)
fb95bd3d   Thomas Fitoussi   Add time delay ve...
99
   generation = ReadGeneration(fileId)
f4246390   Thomas Fitoussi   Improve angular d...
100
101
102
103
104
105
106
107

   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 :
fb95bd3d   Thomas Fitoussi   Add time delay ve...
108
      cond=(abs(costheta)<=1) & (generation==gen) & (time>0) & (energy>1)
f4246390   Thomas Fitoussi   Improve angular d...
109
   else:
fb95bd3d   Thomas Fitoussi   Add time delay ve...
110
      cond=(abs(costheta)<=1) & (time>0) & (energy>1)
f4246390   Thomas Fitoussi   Improve angular d...
111
112

   prop = float(shape(time[time<0])[0])/shape(time)[0]
fb95bd3d   Thomas Fitoussi   Add time delay ve...
113
   print "gen", int(gen), "->", shape(time[time<0])[0], "negative time events among ",shape(time)[0], "~", prop
f4246390   Thomas Fitoussi   Improve angular d...
114
115
116
   theta = arccos(costheta[cond])*degre
   weight= weight[cond]
   time=time[cond]*yr
f4246390   Thomas Fitoussi   Improve angular d...
117
   cond=(theta!=0)
fb95bd3d   Thomas Fitoussi   Add time delay ve...
118
   return theta[cond], time[cond], weight[cond], generation[cond]
f4246390   Thomas Fitoussi   Improve angular d...
119
120

def delay_vs_angle_histo(fileId,gen=-1):
fb95bd3d   Thomas Fitoussi   Add time delay ve...
121
   theta, time, weight, generation = delay_vs_angle(fileId,gen)
f4246390   Thomas Fitoussi   Improve angular d...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
   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)
   
fb95bd3d   Thomas Fitoussi   Add time delay ve...
142
143
144
145
146
   angle,delay,weight,generation = delay_vs_angle(fileId)
   #for gen in list(set(generation)):
   for gen in [2]:
      cond= (generation==gen)
      ax.plot(angle[cond],delay[cond]/weight[cond],".",label="gen="+str(int(gen)))
f4246390   Thomas Fitoussi   Improve angular d...
147
148

   angle,delay = delay_vs_angle_histo(fileId)
fb95bd3d   Thomas Fitoussi   Add time delay ve...
149
   #ax.plot(angle,delay,color='k',linestyle="steps-mid",label="gen=all")
f4246390   Thomas Fitoussi   Improve angular d...
150

fb95bd3d   Thomas Fitoussi   Add time delay ve...
151
152
   delay_fit = Analytic_delay_vs_theta(angle,fileId)
   ax.plot(angle,delay_fit,'--r',linewidth=2)
f4246390   Thomas Fitoussi   Improve angular d...
153
154
155
156
157
158
159
160
161

   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()
fb95bd3d   Thomas Fitoussi   Add time delay ve...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

######## delay versus angle ###############
def delay_vs_energy(fileId,gen=-1):
   '''
      Compute time delay versus arrival angle
      Input: directory name
      Input (optional): generation (default = all)
      Output: arrival angle [degre], time delay [sec]
   '''
   energy = ReadEnergy(fileId)
   weight = ReadWeight(fileId)
   time=ReadTime(fileId)
   generation = ReadGeneration(fileId)

   if gen != -1 :
      cond= (generation==gen) & (time>0) & (energy>1)
   else:
      cond= (time>0) & (energy>1)

   prop = float(shape(time[time<0])[0])/shape(time)[0]
   print "gen", int(gen), "->", shape(time[time<0])[0], "negative time events among ",shape(time)[0], "~", prop
   return energy[cond], time[cond]*yr, weight[cond], generation[cond]

def delay_vs_energy_histo(fileId,gen=-1):
   energy, time, weight, generation = delay_vs_energy(fileId,gen)
   nbBins = 100

   energy=log10(energy)
   dt,dE=histogram(energy,nbBins,weights=time)
   dN,dE=histogram(energy,nbBins,weights=weight)
   dE=10**dE
   Ecenter=(dE[1:nbBins+1]+dE[0:nbBins])/2
   timing = dt/dN

   return Ecenter, timing

def drawDelay_vs_energy(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)
   
   energy,delay,weight,generation = delay_vs_energy(fileId)
   #for gen in list(set(generation)):
   for gen in [2]:
      cond= (generation==gen)
      ax.plot(energy[cond],delay[cond]/weight[cond],".",label="gen="+str(int(gen)))

   energy,delay = delay_vs_energy_histo(fileId)
   #ax.plot(energy,delay,color='k',linestyle="steps-mid",label="gen=all")

   delay_fit = Analytic_delay_vs_Egamma(energy,fileId)
   ax.plot(energy,delay_fit,'--r',linewidth=2)

   ax.set_xscale('log')
   ax.set_yscale('log')
   ax.grid(b=True,which='major')
   ax.legend(loc="best")
   ax.set_xlabel("E [GeV]")
   ax.set_ylabel("Time delay [s]")

   show()