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_root.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#include <memory>
18
19#include "marley/marley_root.hh"
20
21std::unique_ptr<marley::GridNeutrinoSource>
22 marley_root::make_root_neutrino_source(int pdg_code, const TH1* th1)
23{
24 if (!th1) {
25 throw marley::Error(std::string("Null TH1* passed")
26 + " to marley_root::make_root_neutrino_source");
27 return std::unique_ptr<marley::GridNeutrinoSource>(nullptr);
28 }
29
30 // Read in the (energy low edge, bin weight) ordered pairs. Keep the
31 // overflow bin so that we can use its left edge as the maximum energy
32 // value. Skip the unneeded underflow bin.
33 size_t n_bins = th1->GetNbinsX() + 1;
34 std::vector<double> Es(n_bins);
35 std::vector<double> PDs(n_bins);
36
37 // underflow bin is bin 0
38 for (size_t b = 1; b <= n_bins; ++b) {
39 Es.at(b - 1) = th1->GetBinLowEdge(b);
40 // the content of the overflow bin is reset to zero below
41 PDs.at(b - 1) = th1->GetBinContent(b);
42 }
43
44 // Convert the bin weights to probability densities (used by our
45 // GridNeutrinoSource object) by dividing each bin weight by the
46 // corresponding bin width.
47 for (size_t c = 0; c < n_bins - 1; ++c) {
48 double width = Es.at(c + 1) - Es.at(c);
49 if (width <= 0) throw marley::Error(std::string("Invalid bin width")
50 + std::to_string(width) + " encountered when creating a TH1 neutrino"
51 + " source");
52 PDs.at(c) /= width;
53 }
54
55 // Assign zero probability density to the overflow bin's
56 // left edge. This ensures that neutrino energies will be
57 // sampled on the half-open interval [Elow, Ehigh), where
58 // Elow is the left edge of the first bin and Ehigh is the
59 // left edge of the overflow bin.
60 PDs.back() = 0.;
61
62 // Now that we've processed grid points, create the grid neutrino
63 // source
64 auto source = std::make_unique<marley::GridNeutrinoSource>(
65 Es, PDs, pdg_code, marley::InterpolationGrid<double>
66 ::InterpolationMethod::Constant);
67 return source;
68}
69
70std::unique_ptr<marley::GridNeutrinoSource>
71 marley_root::make_root_neutrino_source(int pdg_code, const TGraph* tg)
72{
73 if (!tg) {
74 throw marley::Error(std::string("Null TGraph* passed")
75 + " to marley_root::make_root_neutrino_source");
76 return std::unique_ptr<marley::GridNeutrinoSource>(nullptr);
77 }
78
79 size_t num_points = tg->GetN();
80 std::vector<double> Es(num_points);
81 std::vector<double> PDs(num_points);
82
83 // Load the energies and PDF values into our vectors
84 for(size_t p = 0; p < num_points; ++p) {
85 tg->GetPoint(p, Es.at(p), PDs.at(p));
86 }
87
88 // Create a neutrino source based on the grid
89 auto source = std::make_unique<marley::GridNeutrinoSource>(Es, PDs,
91 ::LinearLinear);
92 return source;
93}
Base class for all exceptions thrown by MARLEY functions.
Definition Error.hh:26
One-dimensional function y(x) defined using a grid of ordered pairs (x,y) and an interpolation rule.
InterpolationMethod
Method to use for interpolating between (x,y) grid points.