MARLEY (Model of Argon Reaction Low Energy Yields) v2.0.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Loading...
Searching...
No Matches
marley::RotationMatrix Class Reference

Simple rotation matrix implementation used to reorient Particle objects based on the incident neutrino direction. More...

#include <RotationMatrix.hh>

Public Member Functions

 RotationMatrix ()
 Creates a 3×3 identity matrix.
 
 RotationMatrix (const ThreeVector &from_vec, const ThreeVector &to_vec)
 Create a 3×3 rotation matrix that rotates the 3-vector from_vec into the 3-vector to_vec.
 
ThreeVector rotate_copy (const ThreeVector &v)
 Create a rotated copy of the 3-vector v.
 
void rotate_inplace (ThreeVector &v)
 Rotate a 3-vector v in place.
 
void rotate_particle_inplace (HepMC3::GenParticle &p)
 Rotate the 3-momentum of a marley::Particle in place.
 

Static Public Member Functions

static ThreeVector normalize (const ThreeVector &v)
 

Protected Attributes

ThreeThreeMatrix matrix_
 3×3 rotation matrix
 

Detailed Description

Simple rotation matrix implementation used to reorient Particle objects based on the incident neutrino direction.

Definition at line 30 of file RotationMatrix.hh.

Constructor & Destructor Documentation

◆ RotationMatrix() [1/2]

marley::RotationMatrix::RotationMatrix ( )

Creates a 3×3 identity matrix.

Definition at line 63 of file RotationMatrix.cc.

64 : matrix_{{ {{ 1., 0., 0.}}, {{ 0., 1., 0.}}, {{ 0., 0., 1.}} }}
65{}
ThreeThreeMatrix matrix_
3×3 rotation matrix

References matrix_.

◆ RotationMatrix() [2/2]

marley::RotationMatrix::RotationMatrix ( const ThreeVector & from_vec,
const ThreeVector & to_vec )

Create a 3×3 rotation matrix that rotates the 3-vector from_vec into the 3-vector to_vec.

This function is a C++11 version of an original rotation matrix program by Möller & Hughes (see this GitHub page for details)

The vectors from_vec and to_vec do not need to be normalized, but both should be nonzero. If either vector is a null vector, then a marley::Error will be thrown.

Definition at line 122 of file RotationMatrix.cc.

124{
125 static constexpr ThreeVector null_three_vector = { 0., 0., 0. };
126
127 if (from_vec == null_three_vector)
128 throw marley::Error(std::string("Null from vector")
129 + " passed to constructor of marley::RotationMatrix");
130 else if (to_vec == null_three_vector)
131 throw marley::Error(std::string("Null to vector")
132 + " passed to constructor of marley::RotationMatrix");
133
134 // The from and to vectors must be normalized to use this method, so make any
135 // necessary adjustments now.
136 ThreeVector from = normalize(from_vec);
137 ThreeVector to = normalize(to_vec);
138
139 double e = dot_product(from, to);
140 double f = std::abs(e);
141
142 static constexpr double EPSILON = 0.000001;
143 if (f > 1.0 - EPSILON) { // "from" and "to" vectors are almost parallel
144
145 // Temporary storage vectors
146 ThreeVector v;
147 ThreeVector u;
148
149 // Find the standard unit vector x most nearly orthogonal to "from"
150 ThreeVector x;
151 x[0] = std::abs(from[0]);
152 x[1] = std::abs(from[1]);
153 x[2] = std::abs(from[2]);
154
155 if (x[0] < x[1])
156 {
157 if (x[0] < x[2])
158 {
159 x[0] = 1.0; x[1] = x[2] = 0.0;
160 }
161 else
162 {
163 x[2] = 1.0; x[0] = x[1] = 0.0;
164 }
165 }
166 else
167 {
168 if (x[1] < x[2])
169 {
170 x[1] = 1.0; x[0] = x[2] = 0.0;
171 }
172 else
173 {
174 x[2] = 1.0; x[0] = x[1] = 0.0;
175 }
176 }
177
178 // u = x - from
179 subtract(u, x, from);
180
181 // v = x - to
182 subtract(v, x, to);
183
184 // coefficients for later use
185 double c1 = 2.0 / dot_product(u, u);
186 double c2 = 2.0 / dot_product(v, v);
187 double c3 = c1 * c2 * dot_product(u, v);
188
189 for (unsigned i = 0; i < 3; ++i) {
190 for (unsigned j = 0; j < 3; ++j) {
191 matrix_[i][j] = - c1 * u[i] * u[j] - c2 * v[i] * v[j]
192 + c3 * v[i] * u[j];
193 }
194 matrix_[i][i] += 1.0;
195 }
196 }
197 else // the most common case, unless "from" = "to", or "from" = -"to"
198 {
199 ThreeVector v;
200 cross_product(v, from, to); // v = from x to
201
202 // hand-optimized version (9 mults less than original)
203 // optimization by Gottfried Chen
204 double hvx, hvz, hvxy, hvxz, hvyz;
205 double h = 1.0 / (1.0 + e);
206 hvx = h * v[0];
207 hvz = h * v[2];
208 hvxy = hvx * v[1];
209 hvxz = hvx * v[2];
210 hvyz = hvz * v[1];
211
212 matrix_[0][0] = e + hvx * v[0];
213 matrix_[0][1] = hvxy - v[2];
214 matrix_[0][2] = hvxz + v[1];
215
216 matrix_[1][0] = hvxy + v[2];
217 matrix_[1][1] = e + h * v[1] * v[1];
218 matrix_[1][2] = hvyz - v[0];
219
220 matrix_[2][0] = hvxz - v[1];
221 matrix_[2][1] = hvyz + v[0];
222 matrix_[2][2] = e + hvz * v[2];
223 }
224}

References matrix_.

Member Function Documentation

◆ normalize()

ThreeVector marley::RotationMatrix::normalize ( const ThreeVector & v)
static

Definition at line 68 of file RotationMatrix.cc.

69{
70 static ThreeVector nv({0., 0., 0.});
71 double norm_factor = std::sqrt(std::pow(v[0], 2) + std::pow(v[1], 2)
72 + std::pow(v[2], 2));
73 if (norm_factor <= 0.) throw marley::Error(std::string("Invalid vector")
74 + " magnitude encountered in marley::RotationMatrix::normalize()");
75 else norm_factor = 1. / norm_factor;
76 nv[0] = norm_factor * v[0];
77 nv[1] = norm_factor * v[1];
78 nv[2] = norm_factor * v[2];
79 return nv;
80}

◆ rotate_copy()

ThreeVector marley::RotationMatrix::rotate_copy ( const ThreeVector & v)

Create a rotated copy of the 3-vector v.

Definition at line 83 of file RotationMatrix.cc.

84{
85 ThreeVector rv = {0., 0., 0.};
86 for (unsigned i = 0; i < 3; ++i) rv[i] = dot_product(matrix_[i], v);
87 return rv;
88}

References matrix_.

◆ rotate_inplace()

void marley::RotationMatrix::rotate_inplace ( ThreeVector & v)

Rotate a 3-vector v in place.

Definition at line 91 of file RotationMatrix.cc.

92{
93 ThreeVector rv = {0., 0., 0.};
94 for (unsigned i = 0; i < 3; ++i) rv[i] = dot_product(matrix_[i], v);
95 v = rv;
96}

References matrix_.

◆ rotate_particle_inplace()

void marley::RotationMatrix::rotate_particle_inplace ( HepMC3::GenParticle & p)

Rotate the 3-momentum of a marley::Particle in place.

Definition at line 99 of file RotationMatrix.cc.

100{
101 HepMC3::FourVector mom4 = p.momentum();
102
103 ThreeVector rv = { 0., 0., 0. };
104 ThreeVector three_momentum = { mom4.px(), mom4.py(), mom4.pz() };
105
106 for (unsigned i = 0; i < 3; ++i)
107 rv[i] = dot_product( matrix_[i], three_momentum );
108
109 mom4.set_px( rv[0] );
110 mom4.set_py( rv[1] );
111 mom4.set_pz( rv[2] );
112
113 p.set_momentum( mom4 );
114}
void set_py(double pyy)
Set y-component of momentum.
Definition FourVector.h:123
void set_px(double pxx)
Set x-component of momentum.
Definition FourVector.h:116
double px() const
x-component of momentum
Definition FourVector.h:114
double py() const
y-component of momentum
Definition FourVector.h:121
double pz() const
z-component of momentum
Definition FourVector.h:128
void set_pz(double pzz)
Set z-component of momentum.
Definition FourVector.h:130
void set_momentum(const FourVector &momentum)
Set momentum.
const FourVector & momentum() const
Get momentum.
Definition GenParticle.h:97

References matrix_, HepMC3::GenParticle::momentum(), HepMC3::FourVector::px(), HepMC3::FourVector::py(), HepMC3::FourVector::pz(), HepMC3::GenParticle::set_momentum(), HepMC3::FourVector::set_px(), HepMC3::FourVector::set_py(), and HepMC3::FourVector::set_pz().

Member Data Documentation

◆ matrix_

ThreeThreeMatrix marley::RotationMatrix::matrix_
protected

3×3 rotation matrix

Definition at line 59 of file RotationMatrix.hh.

Referenced by RotationMatrix(), RotationMatrix(), rotate_copy(), rotate_inplace(), and rotate_particle_inplace().


The documentation for this class was generated from the following files: