particle.py 2.62 KB
Newer Older
1 2
# -*- coding: utf-8 -*-
# Sample Particle class and function that creates N particles, returning a list of particles
3 4
import math
import random
5

6
# Basic particle class to store bascic information
7
class Particle:
8
    def __init__(self, index, x = 0, y = 0, z = 0, charge = 1, mass=0.94):
9 10 11 12
        self.iDx=index
        self.x=x
        self.y=y
        self.z=z
13 14 15 16 17 18
        self.charge=charge
        self.mass = mass
    def PrintPosition(self):
        print(str(self.x) + " ; " + str(self.y) + " ; " + str(self.z))
    def GetPosition(self):
        return self.x, self.y, self.z
19

20 21
#Function that creates N particles and return them in a list
def createNparticles(N_particles, x = 0, y = 0, z = 0):  # Create particles at given position and return them in a list
22
    particles=[]
23
    #loop over particles
24 25 26 27
    for i in range(0, N_particles):
        part = Particle(i,x,y,z)
        particles.append(part)
    return particles;
28 29 30

# Derived class to computes the time evolution particle positions
class ParticlePropagator(Particle):
31
    def SetMagneticField(self, B = 0.5*10):
32
        self.B = B
33 34
    def SetProperties(self, Px, Py, Pz, charge = 1):
        self.charge = charge
35 36 37 38 39 40 41 42 43 44 45 46 47
        self.Px = Px # unit: Gev/c
        self.Py = Py # unit: Gev/c
        self.Pz = Pz # unit: Gev/c
        self.Velocity = 1/math.sqrt(1+self.mass*self.mass/(Px*Px+Py*Py+Pz*Pz)) # unit: []c
        self.LorentzFactor = 1 / math.sqrt( 1 - self.Velocity * self.Velocity )
        self.Vz = 300 * Pz / self.LorentzFactor / self.mass # unit: meters/micro seconds
    def Propagate(self, time):
        Rx = self.Px / (self.charge * self.B) * 3.335641 # unit conversion to meters
        Ry = self.Py / (self.charge * self.B) * 3.335641 # unit conversion to meters
        omega = self.charge * self.B / ( self.LorentzFactor * self.mass ) * 89.876 # Angular frequency (unit: radians/micro seconds)
        Xprop = self.x + Rx * math.sin(omega*time) - Ry * ( math.cos(omega*time) - 1 )
        Yprop = self.y + Ry * math.sin(omega*time) + Rx * ( math.cos(omega*time) - 1 )
        Zprop = self.z + self.Vz * time
48 49 50 51
        return Xprop, Yprop, Zprop

# Function that creates N particle propagators
#   Momentum values to be obtained from real data
52
def createNparticlesPropGaussian(N_particles, x = 0, y = 0, z = 0):  # Create particles at given position and return them in a list
53 54 55
    particles=[]
    #loop over particles
    for i in range(0, N_particles):
56
        charge =  random.choice([+1,-1])
57
        part = ParticlePropagator(i,x,y,z)
58
        part.SetMagneticField()
59
        part.SetProperties(random.gauss(0,1),random.gauss(0,1),random.gauss(0,1),charge)
60 61
        particles.append(part)
    return particles;