-
Notifications
You must be signed in to change notification settings - Fork 3
/
Simulate.py
312 lines (229 loc) · 11.6 KB
/
Simulate.py
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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from Transform import *
import math
#TODO : inefficient if small pitch, probably the hit retrieving phase. Also do not need both track array and hit
#from yetkin rotate 2D vector in place
def rotate_vector(v, deltaphi):
c, s = math.cos(deltaphi), math.sin(deltaphi)
xr=c*v[0]-s*v[1]
yr=s*v[0]+c*v[1]
v[0]=xr
v[1]=yr
return
#from Thomas Boser : intersections between two circles. FIXME only one intersection is enough
def pt_dist(p1, p2):
""" distance between two points described by a list """
return math.sqrt(abs((p1[0] - p2[0])**2) + abs((p1[1] - p2[1])**2))
def circ_intersect(v0, v1, r0, r1):
""" return intersection points of two circles """
dist = pt_dist(v0, v1) #calculate distance between
if dist > (r0 + r1): return [] #out of range
if dist < abs(r0 - r1): return [] #circle contained
if dist == 0: return [] #same origin
a = (r0**2 - r1**2 + dist**2) / (2*dist)
b = dist - a
h = math.sqrt(r0**2 - a**2)
v2x = v0[0] + a*(v1[0] - v0[0])/dist
v2y = v0[1] + a*(v1[1] - v0[1])/dist
x3p = v2x + h*(v1[1] - v0[1])/dist
y3p = v2y - h*(v1[0] - v0[0])/dist
x3n = v2x - h*(v1[1] - v0[1])/dist
y3n = v2y + h*(v1[0] - v0[0])/dist
return np.array([[x3p, y3p,0.], [x3n, y3n,0.]])
def unit_vector(v): #normalise in place a vector
v=v/np.linalg.norm(v)
return v
def to02pi(x): # map [-pi,pi] to [0,2pi]
return (x+2*math.pi)%(2*math.pi)
class Particle(object):
def __init__(self, x = [], vx = [], charge=0,id = 0, irho=-1,iphi=-1):
self.history = pd.DataFrame()
self.position = np.zeros(3);
self.momentum = np.zeros(3);
self.charge=charge
self.id = id
self.position[0] = x[0]
self.position[1] = x[1]
self.layer=irho # layer index, -1 if origin
self.iphi=iphi # layer index, -1 if origin
self.momentum[0] = vx[0]
self.momentum[1] = vx[1]
self.magmom=np.linalg.norm(self.momentum)
self.traceMin = 0.1
self.history = pd.DataFrame({'particle':[self.id],'hit':[0], 'layer':irho, 'iphi':iphi, 'x':self.position[0], 'y':self.position[1]})
self.history= self.history.drop(self.history.index[[0]])
# print self.history
pass
def update(self,acceleration, time, detector, precision, stephit = 1):
self.momentum += acceleration;
self.position += self.momentum/precision;
deflect = detector.deposit(self.position, self.id)
if(np.fabs(deflect) > 0) : self.momentum = rotate(self.momentum,deflect)
if((time % stephit == 0) & (np.linalg.norm(self.position) > self.traceMin)):
self.history = self.history.append(pd.DataFrame({'particle':[self.id],'hit':[time], 'layer':self.layer, 'iphi':iphi, 'x':[self.position[0]], 'y':[self.position[1]]}), ignore_index=True)
pass
def __str__(self):
return "Particle id=%s at layer %s iphi %s (x,y)=(%s,%s) (px,py)=(%s,%s) ch=%s" %(self.id,self.layer,self.iphi,self.position[0],self.position[1],self.momentum[0],self.momentum[1],self.charge)
class Detector(object):
def __init__(self):
self.inefficiency=0.03 # probability for a hit to not be recorded
self.stoppingprobability=0.01 #probability for a track to stop (each layer)
self.Nrho = 9
self.Npipe = 2
self.range = 5.
self.sigmaMS = 13.6*math.sqrt(0.02) #FIXME to be divided by P (in MeV)
#from ATLAS (https://cds.cern.ch/record/2239573 atlas restricted unfortunately) section 2.4 Table 1 and 4.
# 4 layers of pixel at radii (in mm) 39 85 155 213 271 and 5 strip layers : 405 562 762 1000
#self.cells_r = np.array(range(self.Npipe,self.Nrho+self.Npipe)) * self.range / self.Nrho;
self.cells_r = np.array([39,85,155,213,271,405,562,762,1000])
#self.Nphi = [180] * self.Nrho
self.Nphi=[]
for i in range(self.Nrho):
if i<5:
pitch=0.025 # pitch pixel
else:
pitch=0.050 # pitch strip (divided by sqrt(2) given double layer)
self.Nphi += [int(self.cells_r[i]*2*math.pi/pitch)+1]
# print self.Nphi
self.cells_phi = np.zeros((self.Nrho, np.max(self.Nphi)))
self.cells_x = np.zeros((self.Nrho, np.max(self.Nphi)))
self.cells_y = np.zeros((self.Nrho, np.max(self.Nphi)))
self.dphi = np.zeros(self.Nrho)
self.detsize = np.zeros(self.Nrho)
for irho in range(0, self.Nrho):
self.dphi[irho] = 2.*np.pi / self.Nphi[irho]
self.detsize[irho] = self.cells_r[irho] * 2.*np.pi/self.Nphi[irho]
for iphi in range(0,self.Nphi[irho]):
self.cells_phi[irho,iphi] = 2.*np.pi*iphi/self.Nphi[irho]
rho = self.cells_r[irho]
phi = self.cells_phi[irho,iphi]
self.cells_x[irho,iphi] = rho*np.cos(phi)
self.cells_y[irho,iphi] = rho*np.sin(phi)
self.thickness = 0.02
self.hit_particle = np.zeros((self.Nrho, np.max(self.Nphi)))
self.cells_width = np.zeros((self.Nrho, np.max(self.Nphi)))
self.cells_hit = np.zeros((self.Nrho, np.max(self.Nphi)))
self.history = pd.DataFrame({'particle':[0],'hit':[0], 'layer':[0],'iphi':[0], 'x':[0], 'y':[0]})
self.history= self.history.drop(self.history.index[[0]])
def reset(self):
self.cells_hit = np.zeros((self.Nrho, np.max(self.Nphi)))
self.history = pd.DataFrame({'particle':[0], 'hit':[0], 'layer':[0],'iphi':[0], 'x':[0.], 'y':[0.]})
self.history = self.history.drop(self.history.index[[0]])
def deposit(self,position, particle):
deflect=0.
for irho in range(0, self.Nrho):
for iphi in range(0,self.Nphi[irho]):
if(
(np.fabs(np.linalg.norm(position) - self.cells_r[irho]) < self.thickness)
&
(deltaPhiAbs(np.arctan2(position[1],position[0]),self.cells_phi[irho,iphi]) < self.dphi[irho])
):
#think about overlap
self.hit_particle[irho,iphi] = particle
self.cells_hit[irho,iphi] = 1
deflect = np.random.normal(0.,self.sigmaMS/1000) #multiple scattering, should divide by particle momentum
return deflect
def getHits(self):
ihit=0
for irho in range(0, self.Nrho):
for iphi in range(0,self.Nphi[irho]):
if(self.cells_hit[irho,iphi] == 1):
self.history = self.history.append(pd.DataFrame({'particle':self.hit_particle[irho,iphi], 'hit':[ihit], 'layer':[irho],'iphi':[iphi], 'x':self.cells_x[irho,iphi], 'y':self.cells_y[irho,iphi]}), ignore_index=True)
ihit+=1
self.history=self.history.sort(['particle','layer','hit'])
return self.history
class Simulator(object):
def __init__(self):
self.p = Particle([0,0,0], [0,0,0])
self.detector = Detector()
self.precision = 100
self.bmag=1./0.3 #ATLAS R(mm)=PT(MeV)/0.3 CMS PT/0.6
self.hitid=0 # hit counter
def force(self,position, momentum):
# g = 0.5
# acc = -g * position / pow(np.linalg.norm(position),3) # gravitational force
b = 1./self.precision
# This is non relativistic!
acc = - np.cross(momentum, [0,0,b])
return acc
def propagate_numeric(self,x=[], v=[], step = 1, id = 0):
# print "New planet"
self.p = Particle(x,v,id)
for t in range(0,self.precision):
acceleration = self.force(self.p.position,self.p.momentum)
self.p.update(acceleration, t, self.detector, self.precision, stephit = step)
# if(t % 10 == 0):
# print t, p.position
return self.p.history
def propagate(self,x=[], v=[],charge=1,irhostart=-1 ,id = 0):
debug=False
self.p = Particle(x,v,int(charge),id,irhostart)
if abs(self.p.charge)!=1:
print "Detector::propagate abs(charge)!=1 not possible !",charge
exit() # very brutal
if debug: print self.p
for irho in range(self.p.layer+1,self.detector.Nrho):
# direct extrapolation to next detector
# this can certainly be improved to reduce angles calculation
# coordinates of the center of rotation
tocenter = - charge*np.cross(self.p.momentum, [0,0,self.bmag]) # vector from position to center of rotation
radius=np.linalg.norm(tocenter)
if debug : print "tocenter=",tocenter," radius=",radius
rotcenter=self.p.position+tocenter
if debug : print "rotcenter=",rotcenter
nextrho=self.detector.cells_r[irho]
nextrhocenter=np.zeros(3)
#could be done more efficiently
vintersect=circ_intersect(rotcenter, nextrhocenter, radius, nextrho)
if debug:print "nextrho= ",nextrho," vintersect= ", vintersect
if len(vintersect)==0:
break
if self.p.charge==1:
newposition=vintersect[1] #FIXME, first or second
else:
newposition=vintersect[0] #FIXME, first or second
newphipos=math.atan2(newposition[1],newposition[0])
poschange=newposition-self.p.position
phichange=math.atan2(poschange[1],poschange[0])
deltaphimom=2*(phichange-math.atan2(self.p.momentum[1],self.p.momentum[0])) # rotation of momentum vector
# insert MS there
newmomentum=np.copy(self.p.momentum)
newmagmom=np.linalg.norm(self.p.momentum)
deflect = np.random.normal(0.,self.detector.sigmaMS/newmagmom)
#now rotate momentum vector
rotate_vector(newmomentum,deltaphimom+deflect)
# update particle internal state
self.p.position=newposition
self.p.momentum=newmomentum
self.p.magmom=newmagmom
self.p.layer=irho
#now determine which detector element has been hit
iphi=int(to02pi(newphipos)/(2*math.pi)*self.detector.Nphi[irho]) # FIXME there should a Detector function for this
if debug: print "newphipos=",newphipos,"iphi=",iphi,"cell phi",self.detector.cells_phi[irho,iphi]
self.p.iphi=iphi
#if inefficient do not record the hit
rnd=np.random.random()
if rnd >self.detector.inefficiency:
self.p.history = self.p.history.append(pd.DataFrame({'particle':[self.p.id],'hit':[self.hitid], 'layer':[self.p.layer], 'x':[self.p.position[0]], 'y':[self.p.position[1]]}), ignore_index=True)
#have to think about overlap
self.detector.hit_particle[irho,iphi] = id
self.detector.cells_hit[irho,iphi] = 1
self.hitid+=1
if debug : print self.p
#if track stop, stop here
rnd=np.random.random()
if rnd<self.detector.stoppingprobability:
break
return self.p.history
def plot(self):
x=np.dstack((self.p.history['x'].values))[0][0]
y=np.dstack((self.p.history['y'].values))[0][0]
plt.plot(x,y)
plt.show()
#s = Simulator()
#print s.generate(50.0, 0.0, 0.02,0.05)
#s.generate(30.0, 0.0, 0.02,0.05)
#s.generate(30.0, 10.0, 0.02,0.05)
#s.generate(30.0, 15.0, 0.02,0.05)