BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSScorerInfo.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 "BDSMaterials.hh"
21#include "BDSScorerInfo.hh"
22#include "BDSScorerType.hh"
23#include "BDSUtilities.hh"
24
25#include "parser/scorer.h"
26
27#include "G4ParticleTable.hh"
28#include "G4String.hh"
29#include "G4Types.hh"
30
31
32#include "CLHEP/Units/SystemOfUnits.h"
33
34#include <map>
35#include <string>
36#include <vector>
37
39 G4bool upgradeTo3D):
40 particle(nullptr)
41{
42 const std::map<std::string, std::string> replacements = {
43 {"cellcharge", "cellcharge3d"},
44 {"depositeddose", "depositeddose3d"},
45 {"depositedenergy", "depositedenergy3d"},
46 {"population", "population3d"},
47 {"cellflux", "cellflux3d"},
48 {"cellfluxscaled", "cellfluxscaled3d"},
49 {"cellfluxscaledperparticle", "cellfluxscaledperparticle3d"}
50 };
51
52 std::string scorerTypeNameOriginal = scorer.type;
53 std::string scorerTypeName = scorerTypeNameOriginal; // default copy
54 if (upgradeTo3D)
55 {
56 auto search = replacements.find(scorerTypeNameOriginal);
57 if (search != replacements.end())
58 {scorerTypeName = search->second;}
59 else if (!BDS::StrContains(scorerTypeNameOriginal, "3d") && !BDS::StrContains(scorerTypeNameOriginal, "4d") )
60 {
61 G4String msg = "scorer type \"" + scorerTypeNameOriginal + "\" specified which does not";
62 msg += "\ncontain the suffix \"3d\" or \"4d\", and there is no known mapping to the required";
63 msg += "\n3d one. Check the scorer name or specify it with the suffix explicitly.";
64 msg += "\nAvailable 3d scorers are:\n";
65 for (const auto& fs : replacements)
66 {msg += "\"" + fs.first + "\" -> \"" + fs.second + "\"\n";}
67 // now check if it's even a valid scorer type
68 try
69 {BDS::DetermineScorerType(G4String(scorerTypeNameOriginal));}
70 catch (const BDSException& e)
71 {
72 msg += "\n";
73 msg += e.what();
74 }
75 throw BDSException(__METHOD_NAME__, msg);
76 }
77 }
78
79 scorerType = BDS::DetermineScorerType(G4String(scorerTypeName));
80 name = scorer.name;
81 minimumKineticEnergy = scorer.minimumKineticEnergy * CLHEP::GeV;
82 maximumKineticEnergy = scorer.maximumKineticEnergy * CLHEP::GeV;
83 filename = scorer.conversionFactorFile;
84 pathname = scorer.conversionFactorPath;
85 minimumTime = scorer.minimumTime*CLHEP::second;
86 maximumTime = scorer.maximumTime*CLHEP::second;
87 worldVolumeOnly = scorer.scoreWorldVolumeOnly;
88 primariesOnly = scorer.scorePrimariesOnly;
89
90 if (scorer.particlePDGID != 0)
91 {
92 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
93 particle = particleTable->FindParticle(scorer.particlePDGID);
95
96 }
97 if (!(scorer.particleName.empty()))
98 {
99 G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
100 particle = particleTable->FindParticle(scorer.particleName);
101 CheckParticle(particle, scorer.name);
102 }
103
104 for (const auto& mi : BDS::SplitOnWhiteSpace(scorer.materialToInclude))
105 {materialsToInclude.push_back(BDSMaterials::Instance()->GetMaterial(mi));}
106
107 for (const auto& me : BDS::SplitOnWhiteSpace(scorer.materialToExclude))
109}
110
111void BDSScorerInfo::CheckParticle(G4ParticleDefinition* particleIn,
112 const G4String& nameIn)
113{
114 if (!particleIn)
115 {
116 G4cout << __METHOD_NAME__ << "Note, only 1 particle can be specified." << G4endl;
117 throw BDSException(__METHOD_NAME__, "Particle not found for scorer " + nameIn);
118 }
119}
General exception with possible name of object and message.
const char * what() const noexcept override
Override message in std::exception.
static BDSMaterials * Instance()
Singleton pattern access.
G4Material * GetMaterial(G4String material) const
Get material by name.
BDSScorerType scorerType
Scorer type.
G4bool worldVolumeOnly
Which materials to exclude for scoring.
std::vector< G4Material * > materialsToExclude
Which materials to include for scoring.
G4String name
Scorer name.
G4String filename
Name of the conversion factor file.
G4String pathname
Path of the conversion factor file (for ambient dose)
void CheckParticle(G4ParticleDefinition *particleIn, const G4String &nameIn)
Utility function to check valid pointer and throw exception if not.
BDSScorerInfo()=delete
No default constructor as unused.
G4ParticleDefinition * particle
Particle filter.
Scorer class for parser.
Definition scorer.h:36
std::string name
Name.
Definition scorer.h:38
int particlePDGID
PDG ID code for particle.
Definition scorer.h:41
std::string type
Type of the scorer, ie forumula.
Definition scorer.h:39
std::string particleName
Particle name as a string.
Definition scorer.h:40
BDSScorerType DetermineScorerType(G4String scorerType)
function to determine the enum type of the cavity geometry (case-insensitive)
G4bool StrContains(const G4String &str, const G4String &test)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
std::vector< G4String > SplitOnWhiteSpace(const G4String &input)
Split a string on whitespace and return a vector of these 'words'.