xboa
_matrix_tracking.py
Go to the documentation of this file.
1 #This file is a part of xboa
2 #
3 #xboa is free software: you can redistribute it and/or modify
4 #it under the terms of the GNU General Public License as published by
5 #the Free Software Foundation, either version 3 of the License, or
6 #(at your option) any later version.
7 #
8 #xboa is distributed in the hope that it will be useful,
9 #but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 #GNU General Public License for more details.
12 #
13 #You should have received a copy of the GNU General Public License
14 #along with xboa in the doc folder. If not, see
15 #<http://www.gnu.org/licenses/>.
16 
17 
18 """
19 \namespace xboa::tracking::_matrix_tracking
20 
21 Should be imported directly from the xboa::tracking namespace
22 """
23 
24 import numpy
25 
26 from xboa import common as Common
27 from xboa.hit import Hit
28 from _tracking_base import TrackingBase
29 
30 class MatrixTracking(TrackingBase):
31  """
32  Class to mimic tracking using simple, user-supplied transfer matrices
33  Each transfer matrix M_i0 must be of type numpy.matrix, where M is defined
34  by u_i = M_i*(u_in-v_in) + v_i and u, v are matrices with shape (1, 6)
35  going like (x, px, y, py, t, energy)
36  """
37  def __init__(self, list_of_transfer_matrices, list_of_offsets, offset_in):
38  """
39  Initialisation
40  - list_of_transfer_matrices list of transfer matrices. Each element
41  should be a numpy matrix of shape (6,6)
42  - list_of_offsets list of offsets v_i. Each element should be a numpy
43  matrix of shape (1,6)
44  - offset_in offset v_in. Should be a numpy matrix of shape (1,6)
45  """
46  TrackingBase.__init__(self)
47  if len(list_of_offsets) != len(list_of_transfer_matrices):
48  raise IndexError(
49  "list_of_offsets must be same length as list_of_tranfer_matrices")
50  mat_type = type(numpy.matrix([0.]))
51  for offset in list_of_offsets+[offset_in]:
52  if type(offset) != mat_type:
53  raise TypeError("Offset should be of type "+str(mat_type)+\
54  " not "+str(type(offset)))
55  if offset.shape != (1, 6):
56  raise TypeError("Offset should be of shape (1,6) not "+\
57  str(offset.shape))
58  for tm in list_of_transfer_matrices:
59  if type(tm) != mat_type:
60  raise TypeError("Matrix should be of type "+str(mat_type)+\
61  " not "+str(type(tm)))
62  if tm.shape != (6, 6):
63  raise TypeError("Matrix should be of shape (6,6) not "+\
64  str(tm.shape))
65  self.tm_list = list_of_transfer_matrices
66  self.offset_list = list_of_offsets
67  self.offset_in = offset_in
68 
69  def track_one(self, hit):
70  """
71  Track a hit and return a list of output hits
72  - hit initial particle coordinates to be tracked
73 
74  Return a list of hits, with the first hit being equal to the input hit
75  and subsequent hits given by u_i = M_i0 * u_0 with
76  u = (x, px, y, py, t, energy)
77  """
78  hit_list = [hit.deepcopy()]
79  for i, tm in enumerate(self.tm_list):
80  offset_out = self.offset_list[i]
81  vec_in = numpy.matrix([hit['x'], hit['px'],
82  hit['y'], hit['py'],
83  hit['t'], hit['energy']])
84  vec_in -= self.offset_in
85  vec_out = tm * vec_in.transpose() + offset_out.transpose()
86  hit_out = hit.deepcopy()
87  for i, key in enumerate(["x", "px", "y", "py", "t", "energy"]):
88  hit_out[key] = float(vec_out[i])
89  hit_out.mass_shell_condition("pz")
90  hit_list.append(hit_out)
91  return hit_list
92 
def track_one
Track a hit and return a list of output hits.
Class to mimic tracking using simple, user-supplied transfer matrices Each transfer matrix M_i0 must ...
Implemented within this module:
Definition: __init__.py:1