BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSImportanceFileLoader.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 "BDSImportanceFileLoader.hh"
22
23#include "globals.hh"
24#include "G4String.hh"
25
26#include <algorithm>
27#include <fstream>
28#include <iostream>
29#include <exception>
30#include <map>
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>
49std::map<G4String, G4double> BDSImportanceFileLoader<T>::Load(const G4String& fileName)
50{
51 T file;
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
62 if (!validFile)
63 {throw BDSException(__METHOD_NAME__, "Cannot open file \"" + fileName + "\"");}
64 else
65 {G4cout << "BDSImportanceFileLoader::Load> loading \"" << fileName << "\"" << G4endl;}
66
67 std::string line;
68 std::map<G4String, G4double> importance;
69 G4int lineNum = 1;
70 while (std::getline(file, line))
71 { // read a line only if it's not a blank one
72 std::istringstream liness(line);
73 std::string volume;
74 std::string importanceValueString;
75
76 // skip a line if it's only whitespace
77 if (std::all_of(line.begin(), line.end(), isspace))
78 {continue;}
79
80 liness >> volume >> importanceValueString;
81
82 // exit if anything after the importance value
83 if (!liness.eof())
84 {
85 std::string remainder;
86 liness >> remainder;
87 G4String message = "Error: Unknown value \"" + remainder + "\" in line " + std::to_string(lineNum) + " of the importanceMapFile";
88 throw BDSException(message);
89 }
90
91 // exit if no importance value is supplied
92 if (importanceValueString.empty())
93 {
94 G4String message = "No importance value was found for cell \"" + volume + "\" in the importanceMapFile.";
95 throw BDSException(message);
96 }
97
98 G4double importanceValue = 0;
99 try
100 {importanceValue = std::stod(importanceValueString);}
101 catch (...)
102 {
103 G4String message = "Error: Cell \"" + volume + "\" has importance value \"" + importanceValueString + "\",";
104 message += " importance value must be numeric.";
105 throw BDSException(message);
106 }
107
108 importance[volume] = importanceValue;
109
110 lineNum += 1;
111 }
112
113 file.close();
114
115 G4cout << "BDSImportanceFileLoader::Load> loaded " << importance.size() << " importance entries" << G4endl;
116
117 return importance;
118}
119
121
122#ifdef USE_GZSTREAM
124#endif
General exception with possible name of object and message.
Definition: BDSException.hh:35
A loader for importance values used in importance sampling.