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