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