BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSPSCellFluxScaledPerParticle3D.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 "BDSHistBinMapper.hh"
21#include "BDSScorerConversionLoader.hh"
22#include "BDSPSCellFluxScaledPerParticle3D.hh"
23#include "BDSUtilities.hh"
24
25#include "globals.hh"
26#include "G4PhysicsVector.hh"
27#include "G4SystemOfUnits.hh"
28#include "G4VSolid.hh"
29#include "G4VPhysicalVolume.hh"
30#include "G4VPVParameterisation.hh"
31
32#include <fstream>
33#include <map>
34#include <string>
35
36#ifdef USE_GZSTREAM
37#include "src-external/gzstream/gzstream.h"
38#endif
39
40BDSPSCellFluxScaledPerParticle3D::BDSPSCellFluxScaledPerParticle3D(const G4String& scorerName,
41 const BDSHistBinMapper* mapperIn,
42 const G4String& pathname,
43 const G4String& unitIn,
44 G4int ni,
45 G4int nj,
46 G4int nk,
47 G4int depi,
48 G4int depj,
49 G4int depk):
50 BDSPSCellFluxScaled3D(scorerName, mapperIn, unitIn, ni, nj, nk, depi, depj, depk)
51{
52 G4String filePath = BDS::GetFullPath(pathname);
53 if (filePath.back() != '/')
54 {filePath += '/';}
55
56 const std::map<std::string, G4int> files = {{"protons.dat", 2212},
57 {"neutrons.dat", 2112},
58 {"photons.dat", 22},
59 {"electrons.dat", 11},
60 {"positrons.dat", -11}};
61
62 G4cout << "Scorer \"" << GetName() << "\" - adding conversionFiles:" << G4endl;
64 for (const auto& filePDG : files)
65 {
66 G4String uncompressedFile = filePath + filePDG.first;
67 G4String compressedFile = filePath + filePDG.first+".gz";
68 if (BDS::FileExists(uncompressedFile))
69 {
70 conversionFactors[filePDG.second] = loader.Load(uncompressedFile);
71 G4cout << "Adding: " << uncompressedFile << G4endl;
72 }
73 else if (BDS::FileExists(compressedFile))
74 {
75#ifdef USE_GZSTREAM
77 conversionFactors[filePDG.second] = loaderC.Load(compressedFile);
78 G4cout << "Adding: " << compressedFile << G4endl;
79#else
80 throw BDSException(__METHOD_NAME__, "Compressed file loading - but BDSIM not compiled with ZLIB.");
81#endif
82 }
83 }
84 if (conversionFactors.empty())
85 {
86 G4String message = "no conversion files for scorer \"" + scorerName + "\" found. Please specify one of:\n";
87 for (const auto &kv : files)
88 {message += kv.first + "\n";}
89 message += "In the specified directory \"" + pathname + "\"\n";
90 message += "Would result in a factor of 0 for all particles and all energies.";
91 throw BDSException(__METHOD_NAME__, message);
92 }
93}
94
95BDSPSCellFluxScaledPerParticle3D::~BDSPSCellFluxScaledPerParticle3D()
96{
97 for (auto cf : conversionFactors)
98 {delete cf.second;}
99}
100
101G4double BDSPSCellFluxScaledPerParticle3D::GetConversionFactor(G4int particleID, G4double kineticEnergy) const
102{
103 auto search = conversionFactors.find(particleID);
104 if (search != conversionFactors.end())
105 {return search->second->Value(kineticEnergy);}
106 else
107 {return 0;}
108}
General exception with possible name of object and message.
Mapping from axis indices to 1D index.
Primitive scorer for cell flux in a 3D mesh with a conversion factor.
Loader for scoring conversion tables as function of energy.
G4PhysicsVector * Load(const G4String &fileName, G4bool silent=false)
Load the file.
G4String GetFullPath(G4String filename, bool excludeNameFromPath=false, bool useCWDForPrefix=false)
G4bool FileExists(const G4String &filename)
Checks if filename exists.
const char * GetName(int)
Name of element.
Definition parserROOT.cc:37