MARLEY (Model of Argon Reaction Low Energy Yields)  v1.2.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Generator.hh
1 //
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 #pragma once
18 #include <limits>
19 #include <memory>
20 #include <random>
21 #include <sstream>
22 #include <vector>
23 
24 #include "marley/GammaStrengthFunctionModel.hh"
25 #include "marley/NeutrinoSource.hh"
26 #include "marley/NuclearReaction.hh"
27 #include "marley/LevelDensityModel.hh"
28 #include "marley/OpticalModel.hh"
29 #include "marley/Parity.hh"
30 #include "marley/ProjectileDirectionRotator.hh"
31 #include "marley/RotationMatrix.hh"
32 #include "marley/StructureDatabase.hh"
33 #include "marley/Target.hh"
34 #include "marley/marley_utils.hh"
35 
36 namespace marley {
37 
38  class ChebyshevInterpolatingFunction;
39  class JSONConfig;
40 
42  class Generator {
43 
44  // Allow the JSONConfig class to access the private
45  // constructors of Generator
46  friend JSONConfig;
47 
48  public:
49 
51  Generator();
52 
56 
58  inline uint_fast64_t get_seed() const;
59 
61  void reseed(uint_fast64_t seed);
62 
66  void seed_using_state_string(const std::string& state_string);
67 
70  std::string get_state_string() const;
71 
78  double uniform_random_double(double min, double max, bool inclusive);
79 
89  double rejection_sample(const std::function<double(double)>& f,
90  double xmin, double xmax, double& fmax, double safety_factor = 1.01,
91  double max_search_tolerance = DEFAULT_REJECTION_SAMPLING_TOLERANCE_);
92 
101  double xmin, double xmax, double bisection_tolerance = 1e-12);
102 
109  double inverse_transform_sample(const std::function<double(double)>& f,
110  double xmin, double xmax, double bisection_tolerance = 1e-12);
111 
115 
118  inline const std::vector< std::unique_ptr<marley::Reaction> >&
119  get_reactions() const;
120 
123  void add_reaction(std::unique_ptr<marley::Reaction> reaction);
124 
126  void clear_reactions();
127 
131  marley::Reaction& sample_reaction(double& E);
132 
142  double E_pdf(double E);
143 
148  const marley::NeutrinoSource& get_source() const;
149 
153  void set_source(std::unique_ptr<marley::NeutrinoSource> source);
154 
158  void set_target(std::unique_ptr<marley::Target> target);
159 
164  const marley::Target& get_target() const;
165 
171  template <class RandomNumberDistribution>
172  inline auto sample_from_distribution(RandomNumberDistribution& rnd)
173  -> decltype( std::declval<RandomNumberDistribution&>().operator()(
174  std::declval<std::mt19937_64&>()) )
175  {
176  return rnd(rand_gen_);
177  }
178 
183  template <class RandomNumberDistribution, typename ParamType>
184  inline auto sample_from_distribution(RandomNumberDistribution& rnd,
185  const ParamType& params) -> decltype(
186  std::declval<RandomNumberDistribution&>().operator()(
187  std::declval<std::mt19937_64&>(), std::declval<const ParamType&>() ) )
188  {
189  return rnd(rand_gen_, params);
190  }
191 
199  void set_neutrino_direction(const std::array<double, 3>& dir_vec);
200 
203  inline const std::array<double, 3>& neutrino_direction();
204 
208  void set_weight_flux(bool should_we_weight );
209 
213  inline void set_do_deexcitations( bool do_them );
214 
221  double flux_averaged_total_xs() const;
222 
234  double total_xs(int pdg_a, double KEa, int pdg_atom) const;
235 
247  double total_xs(int pdg_a, double KEa) const;
248 
260  marley::Event create_event( int pdg_a, double KEa, int pdg_atom,
261  const std::array<double, 3>& dir_vec );
262 
265  { return rotator_; }
266 
267  private:
268 
272  Generator(uint_fast64_t seed);
273 
276  void normalize_E_pdf();
277 
280  void print_logo();
281 
283  uint_fast64_t seed_;
284 
286  std::mt19937_64 rand_gen_;
287 
289  static constexpr double DEFAULT_REJECTION_SAMPLING_TOLERANCE_ = 1e-8;
290 
292  double norm_ = 1.;
293 
295  std::unique_ptr<marley::NeutrinoSource> source_;
296 
299  std::unique_ptr<marley::Target> target_;
300 
303  std::unique_ptr<marley::StructureDatabase> structure_db_;
304 
306  std::vector< std::unique_ptr<marley::Reaction> > reactions_;
307 
312  std::vector<double> total_xs_values_;
313 
315  std::discrete_distribution<size_t> r_index_dist_;
316 
321  bool weight_flux_ = true;
322 
328  double E_pdf_max_;
329 
334  double E_PDF_MAX_DEFAULT_ = marley_utils::UNKNOWN_MAX;
335 
340  inline void set_default_E_pdf_max( double def_max ) {
341  E_PDF_MAX_DEFAULT_ = def_max;
342  }
343 
347  bool dont_normalize_E_pdf_ = false;
348 
352 
357  bool do_deexcitations_ = true;
358 
378  double total_xs(int pdg_a, double KEa, int pdg_atom,
379  std::vector<size_t>* index_vec, std::vector<double>* xsec_vec) const;
380  };
381 
382  // Inline function definitions
383  inline uint_fast64_t Generator::get_seed() const { return seed_; }
384 
385  inline const std::vector<std::unique_ptr<marley::Reaction> >&
386  Generator::get_reactions() const { return reactions_; }
387 
388  inline const std::array<double, 3>& Generator::neutrino_direction()
389  { return rotator_.projectile_direction(); }
390 
391  inline void Generator::set_do_deexcitations( bool do_them )
392  { do_deexcitations_ = do_them; }
393 }
Approximate representation of a 1D continuous function.
Definition: ChebyshevInterpolatingFunction.hh:29
Container for ingoing and outgoing momentum 4-vectors from a reaction.
Definition: Event.hh:66
The MARLEY Event generator.
Definition: Generator.hh:42
uint_fast64_t get_seed() const
Get the seed used to initialize this Generator.
Definition: Generator.hh:383
marley::Event create_event()
Create an Event using the NeutrinoSource, Target, Reaction, and StructureDatabase objects owned by th...
Definition: Generator.cc:67
const std::array< double, 3 > & neutrino_direction()
Gets the direction of the incident neutrinos that is used when generating events.
Definition: Generator.hh:388
void clear_reactions()
Clear the vector of Reaction objects owned by this Generator.
Definition: Generator.cc:428
void set_source(std::unique_ptr< marley::NeutrinoSource > source)
Take ownership of a new NeutrinoSource, replacing any existing source owned by this Generator.
Definition: Generator.cc:388
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:173
marley::StructureDatabase & get_structure_db()
Get a reference to the StructureDatabase owned by this Generator.
Definition: Generator.cc:436
const marley::NeutrinoSource & get_source() const
Get a const reference to the NeutrinoSource owned by this Generator.
Definition: Generator.cc:376
double E_pdf(double E)
Probability density function that describes the distribution of reacting neutrino energies.
Definition: Generator.cc:267
double inverse_transform_sample(const marley::ChebyshevInterpolatingFunction &cdf, double xmin, double xmax, double bisection_tolerance=1e-12)
Sample from a given 1D cumulative density function cdf(x) on the interval [xmin, xmax] using bisectio...
Definition: Generator.cc:478
void seed_using_state_string(const std::string &state_string)
Use a string to set this Generator's internal state.
Definition: Generator.cc:91
double total_xs(int pdg_a, double KEa, int pdg_atom) const
Computes the total cross section at fixed energy for all configured reactions involving a particular ...
Definition: Generator.cc:555
double rejection_sample(const std::function< double(double)> &f, double xmin, double xmax, double &fmax, double safety_factor=1.01, double max_search_tolerance=DEFAULT_REJECTION_SAMPLING_TOLERANCE_)
Sample from a given 1D probability density function f(x) on the interval [xmin, xmax] using a simple ...
Definition: Generator.cc:219
auto sample_from_distribution(RandomNumberDistribution &rnd, const ParamType &params) -> decltype(std::declval< RandomNumberDistribution & >().operator()(std::declval< std::mt19937_64 & >(), std::declval< const ParamType & >()))
Sample from an arbitrary probability distribution (defined here as any object that implements an oper...
Definition: Generator.hh:184
void set_weight_flux(bool should_we_weight)
Sets the value of the weight_flux flag.
Definition: Generator.cc:458
const std::vector< std::unique_ptr< marley::Reaction > > & get_reactions() const
Get a const reference to the vector of Reaction objects owned by this Generator.
Definition: Generator.hh:386
void reseed(uint_fast64_t seed)
Reseeds the Generator.
Definition: Generator.cc:99
Generator()
Create a Generator using default settings.
Definition: Generator.cc:35
auto sample_from_distribution(RandomNumberDistribution &rnd) -> decltype(std::declval< RandomNumberDistribution & >().operator()(std::declval< std::mt19937_64 & >()))
Sample from an arbitrary probability distribution (defined here as any object that implements an oper...
Definition: Generator.hh:172
marley::Reaction & sample_reaction(double &E)
Sample a Reaction and an energy for the reacting neutrino.
Definition: Generator.cc:322
marley::ProjectileDirectionRotator & get_rotator()
Provides access to the owned ProjectileDirectionRotator.
Definition: Generator.hh:264
void set_target(std::unique_ptr< marley::Target > target)
Take ownership of a new Target, replacing any existing target owned by this Generator.
Definition: Generator.cc:538
double flux_averaged_total_xs() const
Computes the flux-averaged total cross section for all enabled neutrino reactions,...
Definition: Generator.cc:513
void add_reaction(std::unique_ptr< marley::Reaction > reaction)
Take ownership of a new Reaction.
Definition: Generator.cc:406
const marley::Target & get_target() const
Get a const reference to the Target owned by this Generator.
Definition: Generator.cc:382
void set_neutrino_direction(const std::array< double, 3 > &dir_vec)
Sets the direction of the incident neutrinos to use when generating events.
Definition: Generator.cc:442
std::string get_state_string() const
Get a string that represents the current internal state of this Generator.
Definition: Generator.cc:110
void set_do_deexcitations(bool do_them)
Sets the value of the do_deexcitations flag.
Definition: Generator.hh:391
Definition: JSONConfig.hh:29
Abstract base class for all objects that describe the incident neutrino energy distribution.
Definition: NeutrinoSource.hh:38
If needed, rotates the coordinate system of an Event so that the projectile 3-momentum lies along a d...
Definition: ProjectileDirectionRotator.hh:27
Abstract base class that represents a two-two scattering reaction.
Definition: Reaction.hh:38
Container for nuclear structure information organized by nuclide.
Definition: StructureDatabase.hh:39
Description of a macroscopic target for scattering reactions.
Definition: Target.hh:32