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
Generator.hh
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#pragma once
18
19// Standard library includes
20#include <limits>
21#include <memory>
22#include <random>
23#include <sstream>
24#include <vector>
25
26// MARLEY includes
27#include "marley/GammaStrengthFunctionModel.hh"
28#include "marley/InterpolatingFunction.hh"
29#include "marley/NeutrinoSource.hh"
30#include "marley/NuclearReaction.hh"
31#include "marley/LevelDensityModel.hh"
32#include "marley/NuclearFormFactor.hh"
33#include "marley/OpticalModel.hh"
34#include "marley/Parity.hh"
35#include "marley/ProjectileDirectionRotator.hh"
36#include "marley/RotationMatrix.hh"
37#include "marley/StructureDatabase.hh"
38#include "marley/Target.hh"
39#include "marley/marley_utils.hh"
40
41namespace HepMC3 {
42 class GenEvent;
43 class GenRunInfo;
44}
45
46namespace marley {
47
49 class JSON;
50 class JSONConfig;
51 class Weighter;
52
54 class Generator {
55
56 // Allow the JSONConfig class to access the private
57 // constructors of Generator
58 friend JSONConfig;
59
60 public:
61
63 Generator();
64
69 std::shared_ptr< HepMC3::GenEvent > create_event(
70 bool attach_state = false );
71
73 inline uint_fast64_t get_seed() const;
74
76 void reseed(uint_fast64_t seed);
77
81 void seed_using_state_string(const std::string& state_string);
82
85 std::string get_state_string() const;
86
93 double uniform_random_double(double min, double max, bool inclusive);
94
104 double rejection_sample(const std::function<double(double)>& f,
105 double xmin, double xmax, double& fmax, double safety_factor = 1.01,
106 double max_search_tolerance = DEFAULT_REJECTION_SAMPLING_TOLERANCE_);
107
116 double xmin, double xmax, double bisection_tolerance = 1e-12);
117
124 double inverse_transform_sample( const std::function<double(double)>& f,
125 double xmin, double xmax, double bisection_tolerance = 1e-12 );
126
131 double sample_decay_time( double partial_width );
132
136
139 inline const std::vector< std::unique_ptr<marley::Reaction> >&
140 get_reactions() const;
141
144 void add_reaction(std::unique_ptr<marley::Reaction> reaction);
145
147 void clear_reactions();
148
153
163 double E_pdf(double E);
164
169 const marley::NeutrinoSource& get_source() const;
170
174 void set_source(std::unique_ptr<marley::NeutrinoSource> source);
175
179 void set_target(std::unique_ptr<marley::Target> target);
180
185 const marley::Target& get_target() const;
186
192 template <class RandomNumberDistribution>
193 inline auto sample_from_distribution(RandomNumberDistribution& rnd)
194 -> decltype( std::declval<RandomNumberDistribution&>().operator()(
195 std::declval<std::mt19937_64&>()) )
196 {
197 return rnd(rand_gen_);
198 }
199
204 template <class RandomNumberDistribution, typename ParamType>
205 inline auto sample_from_distribution(RandomNumberDistribution& rnd,
206 const ParamType& params) -> decltype(
207 std::declval<RandomNumberDistribution&>().operator()(
208 std::declval<std::mt19937_64&>(), std::declval<const ParamType&>() ) )
209 {
210 return rnd(rand_gen_, params);
211 }
212
220 void set_neutrino_direction(const std::array<double, 3>& dir_vec);
221
224 inline const std::array<double, 3>& neutrino_direction();
225
229 void set_weight_flux(bool should_we_weight );
230
234 inline void set_do_deexcitations( bool do_them );
235
242 double flux_averaged_total_xs() const;
243
255 double total_xs(int pdg_a, double KEa, int pdg_atom) const;
256
267 double total_xs(int pdg_a, double KEa) const;
268
282 std::shared_ptr< HepMC3::GenEvent > create_event( int pdg_a, double KEa,
283 int pdg_atom, const std::array<double, 3>& dir_vec,
284 bool attach_state = false );
285
288 { return rotator_; }
289
290 inline const std::string& json_config() const { return json_config_; }
291
292 inline const std::shared_ptr< HepMC3::GenRunInfo >& run_info() const
293 { return run_info_; }
294
296 void assign_run_info( HepMC3::GenEvent& event ) const;
297
298 inline void set_run_info( const std::shared_ptr< HepMC3::GenRunInfo >&
299 run_info ) { run_info_ = run_info; }
300
303 void set_up_run_info();
304
309 void finish_event_metadata( HepMC3::GenEvent& ev,
310 bool attach_state = false );
311
313 inline const marley::Weighter& get_weighter() const
314 { return *weighter_; }
315
317 inline marley::Weighter& get_weighter() { return *weighter_; }
318
320 inline void set_weighter(std::shared_ptr<marley::Weighter> w)
321 { weighter_ = w; }
322
325 void add_state_to_event( HepMC3::GenEvent& ev ) const;
326
329 static void add_state_to_event( HepMC3::GenEvent& ev,
330 const std::string& state );
331
332 private:
333
337 Generator(uint_fast64_t seed);
338
341 void normalize_E_pdf();
342
345 void print_logo();
346
348 uint_fast64_t seed_;
349
351 std::mt19937_64 rand_gen_;
352
354 static constexpr double DEFAULT_REJECTION_SAMPLING_TOLERANCE_ = 1e-8;
355
357 double norm_ = 1.;
358
360 std::unique_ptr<marley::NeutrinoSource> source_;
361
364 std::unique_ptr<marley::Target> target_;
365
368 std::unique_ptr<marley::StructureDatabase> structure_db_;
369
371 std::vector< std::unique_ptr<marley::Reaction> > reactions_;
372
377 std::vector<double> total_xs_values_;
378
380 std::discrete_distribution<size_t> r_index_dist_;
381
386 bool weight_flux_ = true;
387
393 double E_pdf_max_;
394
399 double E_PDF_MAX_DEFAULT_ = marley_utils::UNKNOWN_MAX;
400
405 inline void set_default_E_pdf_max( double def_max ) {
406 E_PDF_MAX_DEFAULT_ = def_max;
407 }
408
412 bool dont_normalize_E_pdf_ = false;
413
417
422 bool do_deexcitations_ = true;
423
427 std::shared_ptr< HepMC3::GenRunInfo > run_info_;
428
432 std::string json_config_;
433
453 double total_xs(int pdg_a, double KEa, int pdg_atom,
454 std::vector<size_t>* index_vec, std::vector<double>* xsec_vec) const;
455
458 void set_json_config( const marley::JSON& jc );
459
461 std::shared_ptr< Weighter > weighter_;
462 };
463
464 // Inline function definitions
465 inline uint_fast64_t Generator::get_seed() const { return seed_; }
466
467 inline const std::vector<std::unique_ptr<marley::Reaction> >&
468 Generator::get_reactions() const { return reactions_; }
469
470 inline const std::array<double, 3>& Generator::neutrino_direction()
471 { return rotator_.projectile_direction(); }
472
473 inline void Generator::set_do_deexcitations( bool do_them )
474 { do_deexcitations_ = do_them; }
475
476}
Stores event-related information.
Definition GenEvent.h:47
Stores run-related information.
Definition GenRunInfo.h:33
Approximates a 1D function using Chebyshev points.
uint_fast64_t get_seed() const
Get the seed used to initialize this Generator.
Definition Generator.hh:465
const std::array< double, 3 > & neutrino_direction()
Gets the direction of the incident neutrinos that is used when generating events.
Definition Generator.hh:470
void clear_reactions()
Clear the vector of Reaction objects owned by this Generator.
Definition Generator.cc:502
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:461
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
double inverse_transform_sample(const marley::InterpolatingFunction &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:552
void set_up_run_info()
Initializes the owned GenRunInfo object that will be used to associate run metadata with the output e...
Definition Generator.cc:769
marley::StructureDatabase & get_structure_db()
Get a reference to the StructureDatabase owned by this Generator.
Definition Generator.cc:510
const marley::NeutrinoSource & get_source() const
Get a const reference to the NeutrinoSource owned by this Generator.
Definition Generator.cc:449
marley::Weighter & get_weighter()
Get a non-const reference to the owned Weighter object.
Definition Generator.hh:317
const marley::Weighter & get_weighter() const
Get a const reference to the owned Weighter object.
Definition Generator.hh:313
double E_pdf(double E)
Probability density function that describes the distribution of reacting neutrino energies.
Definition Generator.cc:335
double sample_decay_time(double partial_width)
Sample a random decay time given a partial decay width.
Definition Generator.cc:859
marley::ProjectileDirectionRotator & get_rotator()
Provides access to the owned ProjectileDirectionRotator.
Definition Generator.hh:287
void add_state_to_event(HepMC3::GenEvent &ev) const
Attach the current random number generator state to the input event as a string attribute.
Definition Generator.cc:880
void set_weighter(std::shared_ptr< marley::Weighter > w)
Replace the owned Weighter object.
Definition Generator.hh:320
void seed_using_state_string(const std::string &state_string)
Use a string to set this Generator's internal state.
Definition Generator.cc:148
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:629
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:281
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:205
void set_weight_flux(bool should_we_weight)
Sets the value of the weight_flux flag.
Definition Generator.cc:532
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:468
void reseed(uint_fast64_t seed)
Reseeds the Generator.
Definition Generator.cc:156
Generator()
Create a Generator using default settings.
Definition Generator.cc:45
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:193
marley::Reaction & sample_reaction(double &E)
Sample a Reaction and an energy for the reacting neutrino.
Definition Generator.cc:390
std::shared_ptr< HepMC3::GenEvent > create_event(bool attach_state=false)
Create an Event using the NeutrinoSource, Target, Reaction, and StructureDatabase objects owned by th...
Definition Generator.cc:77
void assign_run_info(HepMC3::GenEvent &event) const
Associates the owned GenRunInfo object with the input event.
Definition Generator.cc:854
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:612
double flux_averaged_total_xs() const
Computes the flux-averaged total cross section for all enabled neutrino reactions,...
Definition Generator.cc:587
void add_reaction(std::unique_ptr< marley::Reaction > reaction)
Take ownership of a new Reaction.
Definition Generator.cc:479
void finish_event_metadata(HepMC3::GenEvent &ev, bool attach_state=false)
Add final pieces of metadata (e.g., the RNG state) to an otherwise complete event.
Definition Generator.cc:827
const marley::Target & get_target() const
Get a const reference to the Target owned by this Generator.
Definition Generator.cc:455
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:516
std::string get_state_string() const
Get a string that represents the current internal state of this Generator.
Definition Generator.cc:168
void set_do_deexcitations(bool do_them)
Sets the value of the do_deexcitations flag.
Definition Generator.hh:473
Abstract base class for an approximate representation of a 1D continuous function.
Abstract base class for all objects that describe the incident neutrino energy distribution.
If needed, rotates the coordinate system of a GenEvent so that the projectile 3-momentum lies along a...
Abstract base class that represents a 2 → 2 scattering reaction.
Definition Reaction.hh:46
Container for nuclear structure information organized by nuclide.
Description of a macroscopic target for scattering reactions.
Definition Target.hh:32
EventProcessor that assigns event weights.
Definition Weighter.hh:32