BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BinGeneration.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BinGeneration.hh"
19#include "RBDSException.hh"
20
21#include <algorithm>
22#include <cmath>
23#include <limits>
24#include <string>
25#include <vector>
26
27std::vector<double> RBDS::LogSpace(double start,
28 double stop,
29 int nBins,
30 double base,
31 bool includeLastPoint)
32{
33 if (start <= std::numeric_limits<double>::min_exponent10 + 1)
34 {throw RBDSException("Lower limit of log binning (" + std::to_string(start) + ") is too low to represent as a double in C++");}
35 if (stop >= std::numeric_limits<double>::max_exponent10 - 1)
36 {throw RBDSException("Upper limit of log binning (" + std::to_string(stop) + ") is too high to represent as a double in C++");}
37
38 double realStart = std::pow(base, start);
39 double realBase = std::pow(base, (stop-start)/nBins);
40
41 std::vector<double> result;
42 int n = includeLastPoint ? nBins+1 : nBins;
43 result.reserve(n);
44 std::generate_n(std::back_inserter(result), n, RBDS::Logspace<double>(realStart,realBase));
45 return result;
46}
47
48std::vector<double> RBDS::LinSpace(double start,
49 double stop,
50 int nBins,
51 bool includeLastPoint)
52{
53 std::vector<double> result;
54 int n = includeLastPoint ? nBins+1 : nBins;
55 result.reserve(n);
56 double step = (stop - start) / (double)nBins;
57 int i = 0; // counter
58 for (double value = start; i < n; value += step, i++)
59 {result.push_back(value);}
60 return result;
61}
General exception with possible name of object and message.
std::vector< double > LinSpace(double start, double stop, int nBins, bool includeLastPoint=true)
Linear range of values.