BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSScorerConversionLoader.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 "BDSDebug.hh"
20#include "BDSException.hh"
21#include "BDSScorerConversionLoader.hh"
22
23#include "G4DataVector.hh"
24#include "G4PhysicsFreeVector.hh"
25#include "G4PhysicsVector.hh"
26#include "G4String.hh"
27#include "G4Types.hh"
28
29#include <algorithm>
30#include <fstream>
31#include <iostream>
32#include <regex>
33#include <sstream>
34#include <string>
35#include <vector>
36
37#ifdef USE_GZSTREAM
38#include "src-external/gzstream/gzstream.h"
39#endif
40
41template <class T>
43{;}
44
45template <class T>
47{;}
48
49template <class T>
50G4PhysicsVector* BDSScorerConversionLoader<T>::Load(const G4String& fileName,
51 G4bool silent)
52{
53 file.open(fileName);
54
55 // test if file is valid
56#ifdef USE_GZSTREAM
57 bool validFile = file.rdbuf()->is_open();
58#else
59 bool validFile = file.is_open();
60#endif
61 if (!validFile)
62 {throw BDSException(__METHOD_NAME__, "Invalid file name or no such file named \"" + fileName + "\"");}
63 if (!silent)
64 {G4cout << "Scorer conversion factors - loading \"" << fileName << "\"" << G4endl;}
65
66 G4int lineNumber = 1;
67 std::string line;
68
69 G4DataVector energy;
70 G4DataVector conversionFactor;
71
72 while (std::getline(file, line))
73 {// read a line only if it's not a blank one
74
75 // match a line starting with #
76 std::regex comment("^\\#.*");
77
78 // Skip a line if it's only whitespace
79 if (std::all_of(line.begin(), line.end(), isspace))
80 {continue;}
81 else if (std::regex_search(line, comment))
82 {continue;} // skip lines starting with '#'
83
84 std::stringstream liness(line);
85 std::vector<G4double> numbers;
86 G4double number;
87 while (liness >> number)
88 {numbers.push_back(number);}
89
90 if (numbers.size() != 2)
91 {
92 file.close();
93 throw BDSException(__METHOD_NAME__, "Incomplete line " + std::to_string(lineNumber));
94 }
95
96 energy.emplace_back(numbers[0]);
97 conversionFactor.emplace_back(numbers[1]);
98
99 lineNumber++;
100 }
101
102 file.close();
103 // relies on energy being increasing from one value to another
104 G4PhysicsFreeVector* results = new G4PhysicsFreeVector(energy, conversionFactor);
105 return results;
106}
107
109
110#ifdef USE_GZSTREAM
112#endif
General exception with possible name of object and message.
Definition: BDSException.hh:35
Loader for scoring conversion tables as function of energy.
G4PhysicsVector * Load(const G4String &fileName, G4bool silent=false)
Load the file.