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
ProjectileDirectionRotator.cc
1
4//
5// This file is part of MARLEY (Model of Argon Reaction Low Energy Yields)
6//
7// MARLEY is free software: you can redistribute it and/or modify it under the
8// terms of version 3 of the GNU General Public License as published by the
9// Free Software Foundation.
10//
11// For the full text of the license please see COPYING or
12// visit http://opensource.org/licenses/GPL-3.0
13//
14// Please respect the MCnet academic usage guidelines. See GUIDELINES
15// or visit https://www.montecarlonet.org/GUIDELINES for details.
16
17// HepMC3 includes
18#include "HepMC3/FourVector.h"
19#include "HepMC3/GenEvent.h"
20#include "HepMC3/GenParticle.h"
21
22// MARLEY includes
23#include "marley/hepmc3_utils.hh"
24#include "marley/Error.hh"
25#include "marley/Generator.hh"
26#include "marley/ProjectileDirectionRotator.hh"
27
29 const std::array<double, 3>& dir ) : EventProcessor(),
30 dir_vec_( dir )
31{
32 constexpr ThreeVector null_three_vector = { 0., 0., 0. };
33 if ( dir_vec_ == null_three_vector ) throw marley::Error( "Null 3-vector"
34 " passed to constructor of marley::ProjectileDirectionRotator" );
35
36 dir_vec_ = marley::RotationMatrix::normalize( dir_vec_ );
37}
38
41{
42 // First check that the projectile 3-momentum is not a null vector.
43 // If it is, don't bother to rotate coordinates. Also don't bother if
44 // (somehow) the magnitude of the projectile momentum is negative.
45 const auto projectile = marley_hepmc3::get_projectile( ev );
46 HepMC3::FourVector mom4 = projectile->momentum();
47 double pmom = mom4.p3mod();
48 if ( pmom <= 0. ) return;
49
50 ThreeVector pdir = { mom4.px() / pmom, mom4.py() / pmom, mom4.pz() / pmom };
51
52 // If random projectile directions have been requested, then sample
53 // a new one isotropically for this event
55 ThreeVector random_dir = sample_isotropic_direction( gen );
56 this->set_projectile_direction( random_dir );
57 }
58
59 // If the (unrotated) projectile direction from the event exactly matches the
60 // desired direction, then we can skip the coordinate rotation.
61 if ( pdir == dir_vec_ ) return;
62
63 // If the (unrotated) projectile direction from the event differs from
64 // the last one that was processed, then we need to recompute the
65 // rotation matrix. Do so and save the unrotated projectile direction
66 // to repeat this check next time.
67 if ( pdir != last_pdir_ ) {
68 last_pdir_ = pdir;
70 }
71
72 // Do the actual rotation of the Particle 3-momenta in the event
73 this->rotate_event( ev );
74}
75
77
78 for ( auto& p : ev.particles() ) {
79 rot_matrix_.rotate_particle_inplace( *p );
80 }
81
82}
83
84marley::ProjectileDirectionRotator::ThreeVector
85 marley::ProjectileDirectionRotator::sample_isotropic_direction(
86 marley::Generator& gen ) const
87{
88 // Sample a polar cosine on the interval [-1, 1]
89 double cos_theta = gen.uniform_random_double( -1., 1., true );
90
91 // Sample an azimuthal angle on the interval [0, 2*pi)
92 double phi = gen.uniform_random_double( 0., marley_utils::two_pi, false );
93
94 // Compute direction unit vector components
95 double sin_theta = marley_utils::real_sqrt( 1. - std::pow(cos_theta, 2) );
96 double ux = sin_theta * std::cos( phi );
97 double uy = sin_theta * std::sin( phi );
98 double uz = cos_theta;
99
100 ThreeVector direction = { ux, uy, uz };
101 return direction;
102}
103
104void marley::ProjectileDirectionRotator::set_randomize_directions(
105 bool do_sampling )
106{
107 randomize_projectile_direction_ = do_sampling;
108}
Generic 4-vector.
Definition FourVector.h:36
double px() const
x-component of momentum
Definition FourVector.h:114
double py() const
y-component of momentum
Definition FourVector.h:121
double p3mod() const
Magnitude of p3 = (px, py, pz) vector.
Definition FourVector.h:163
double pz() const
z-component of momentum
Definition FourVector.h:128
Stores event-related information.
Definition GenEvent.h:47
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
Base class for all exceptions thrown by MARLEY functions.
Definition Error.hh:26
The MARLEY Event generator.
Definition Generator.hh:54
double uniform_random_double(double min, double max, bool inclusive)
Sample a random number uniformly on either [min, max) or [min, max].
Definition Generator.cc:235
ThreeVector last_pdir_
Stores the direction 3-vector for the last projectile that triggered a recalculation of the rotation ...
virtual void process_event(HepMC3::GenEvent &ev, marley::Generator &gen) override
Rotates all 3-momenta in the input event so that the projectile 3-momentum lies along dir_vec_ in the...
bool randomize_projectile_direction_
Flag that indicates whether the (rotated) projectile direction should be sampled isotropically for ea...
ThreeVector dir_vec_
3-vector that points in the desired direction of the projectile
ProjectileDirectionRotator(const ThreeVector &dir={0., 0., 1.})
marley::RotationMatrix rot_matrix_
RotationMatrix used to rotate the coordinate system of the input GenEvent.
void rotate_event(HepMC3::GenEvent &ev)
Helper function that does the coordinate system rotation.
Simple rotation matrix implementation used to reorient Particle objects based on the incident neutrin...