BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSPrimaryGeneratorFile.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 "BDSBunch.hh"
20#include "BDSBunchEventGenerator.hh"
21#include "BDSBunchType.hh"
22#include "BDSDebug.hh"
23#include "BDSException.hh"
24#include "BDSGlobalConstants.hh"
25#include "BDSPrimaryGeneratorFile.hh"
26#include "BDSPrimaryGeneratorFileSampler.hh"
27#include "BDSRunAction.hh"
28#include "BDSUtilities.hh"
29
30#ifdef USE_HEPMC3
31#include "BDSPrimaryGeneratorFileHEPMC.hh"
32#endif
33
34#include "geomdefs.hh"
35#include "G4LogicalVolume.hh"
36#include "G4Navigator.hh"
37#include "G4String.hh"
38#include "G4ThreeVector.hh"
39#include "G4TransportationManager.hh"
40#include "G4Types.hh"
41#include "G4VPhysicalVolume.hh"
42
43#include "parser/beam.h"
44
45#include <string>
46
47BDSPrimaryGeneratorFile::BDSPrimaryGeneratorFile(G4bool loopFileIn,
48 BDSBunchEventGenerator* bunchIn):
49 loopFile(loopFileIn),
50 bunch(bunchIn),
51 endOfFileReached(false),
52 vertexGeneratedSuccessfully(false),
53 currentFileEventIndex(0),
54 nEventsInFile(0),
55 nEventsReadThatPassedFilters(0),
56 nEventsSkipped(0),
57 worldSolid(nullptr)
58{;}
59
60BDSPrimaryGeneratorFile::~BDSPrimaryGeneratorFile()
61{;}
62
64{
65 G4int nLoops = bunch->DistrFileLoopNTimes();
66 if (eventOffset > nEventsInFile*nLoops)
67 {
68 G4String msg = "eventOffset (" + std::to_string(eventOffset) + ") is greater than the number of valid data lines in this file.";
69 throw BDSException(__METHOD_NAME__, msg);
70 }
71}
72
74{
75 vertexGeneratedSuccessfully = false;
76 GeneratePrimaryVertex(event);//derived class must update vertexGeneratedSuccessfully
77 if (!vertexGeneratedSuccessfully && !endOfFileReached)
78 {bunch->IncrementNEventsInFileSkipped();}
79 return vertexGeneratedSuccessfully;
80}
81
83{
84 return nEventsInFile - currentFileEventIndex;
85}
86
88{
89 return nEventsReadThatPassedFilters > 0;
90}
91
93 BDSBunch* bunchIn,
94 G4bool recreate,
95 G4int eventOffset,
96 G4bool batchMode)
97{
98 BDSPrimaryGeneratorFile* generatorFromFile = nullptr;
99
100 BDSBunchType egf = BDSBunchType::eventgeneratorfile;
101 G4bool useEventGeneratorFile = BDS::StrContains(G4String(beam.distrType), egf.ToString());
102 BDSBunchType samp = BDSBunchType::bdsimsampler;
103 G4bool useSamplerLoader = BDS::StrContains(beam.distrType, samp.ToString());
104
105 if (useEventGeneratorFile || useSamplerLoader)
106 {
107 if (beam.distrFile.empty())
108 {throw BDSException(__METHOD_NAME__, "no distrFile specified for event generator beam distribution.");}
109 G4String filename = BDS::GetFullPath(beam.distrFile, false, beam.distrFileFromExecOptions);
110 BDSBunchEventGenerator* beg = dynamic_cast<BDSBunchEventGenerator*>(bunchIn);
111 if (!beg)
112 {throw BDSException(__METHOD_NAME__, "must be used with a BDSBunchEventGenerator instance");}
113 G4bool shouldLoopFile = beam.distrFileLoop || !batchMode;
114 if (useEventGeneratorFile)
115 {
116#ifdef USE_HEPMC3
117 generatorFromFile = new BDSPrimaryGeneratorFileHEPMC(beam.distrType,
118 filename,
119 beg,
120 shouldLoopFile,
123#else
124 throw BDSException(__METHOD_NAME__, "event generator file being used but BDSIM not compiled with HEPMC3");
125#endif
126 }
127 else // must be useSamplerLoader is true
128 {
129 generatorFromFile = new BDSPrimaryGeneratorFileSampler(beam.distrType,
130 filename,
131 beg,
132 shouldLoopFile,
135
136 }
137
138 // common bits
139 if (recreate)
140 {generatorFromFile->RecreateAdvanceToEvent(eventOffset);}
141 if (beam.distrFileMatchLength)
142 {
143 G4int nEventsPerLoop = (G4int)generatorFromFile->NEventsLeftInFile();
144 G4int distrFileLoopNTimes = (G4int)beam.distrFileLoopNTimes;
145 BDSGlobalConstants::Instance()->SetNumberToGenerate(nEventsPerLoop*distrFileLoopNTimes);
146 G4cout << __METHOD_NAME__ << "distrFileMatchLength is true -> simulating " << nEventsPerLoop << " events";
147 if (distrFileLoopNTimes > 1)
148 {G4cout << " " << distrFileLoopNTimes << " times";}
149 G4cout << G4endl;
150 }
151 }
152
153 return generatorFromFile; // could be nullptr
154}
155
156G4bool BDSPrimaryGeneratorFile::VertexInsideWorld(const G4ThreeVector& pos) const
157{
158 if (!worldSolid)
159 {// cache the world solid
160 G4Navigator* navigator = G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
161 G4VPhysicalVolume* world = navigator->GetWorldVolume();
162 worldSolid = world->GetLogicalVolume()->GetSolid();
163 }
164 EInside qinside = worldSolid->Inside(pos);
165 return qinside == EInside::kInside;
166}
A wrapper of BDSBunch to include a filter for the events loaded by an event generator.
G4int DistrFileLoopNTimes() const
Accessor.
The base class for bunch distribution generators.
Definition: BDSBunch.hh:47
General exception with possible name of object and message.
Definition: BDSException.hh:35
void SetNumberToGenerate(G4int number)
Setter.
static BDSGlobalConstants * Instance()
Access method.
Loader to use any HepMC3 compatible file.
Loader to read a specific sampler from a BDSIM ROOT output file.
Common interface for any primary generators that are file based.
G4bool GeneratePrimaryVertexSafe(G4Event *event)
Return false if not able to generate a primary vertex.
void ThrowExceptionIfRecreateOffsetTooHigh(G4long eventOffset) const
G4bool VertexInsideWorld(const G4ThreeVector &pos) const
Utility function for derived classes to check a position is inside the world.
virtual void RecreateAdvanceToEvent(G4int eventOffset)=0
Advance into the file as required.
static BDSPrimaryGeneratorFile * ConstructGenerator(const GMAD::Beam &beam, BDSBunch *bunchIn, G4bool recreate, G4int eventOffset, G4bool batchMode)
Improve type-safety of native enum data type in C++.
bool eventGeneratorWarnSkippedParticles
Event generator file filter.
Definition: beamBase.h:163
std::string distrFile
beam parameters
Definition: beamBase.h:52
bool distrFileFromExecOptions
Required to know how to build the absolute path properly.
Definition: beamBase.h:54
bool distrFileLoop
beam parameters
Definition: beamBase.h:56
bool removeUnstableWithoutDecay
beam parameters
Definition: beamBase.h:58
std::string distrType
beam parameters
Definition: beamBase.h:45
int distrFileLoopNTimes
beam parameters
Definition: beamBase.h:57
bool distrFileMatchLength
beam parameters
Definition: beamBase.h:55
Beam class.
Definition: beam.h:44
G4bool StrContains(const G4String &str, const G4String &test)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
Definition: BDSUtilities.cc:66
G4String GetFullPath(G4String filename, bool excludeNameFromPath=false, bool useCWDForPrefix=false)