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