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) 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 "BDSBunch.hh"
19#include "BDSBunchEventGenerator.hh"
20#include "BDSBunchType.hh"
21#include "BDSDebug.hh"
22#include "BDSException.hh"
23#include "BDSGlobalConstants.hh"
24#include "BDSPrimaryGeneratorFile.hh"
25#include "BDSPrimaryGeneratorFileSampler.hh"
26#include "BDSRunAction.hh"
27#include "BDSUtilities.hh"
28
29#ifdef USE_HEPMC3
30#include "BDSPrimaryGeneratorFileHEPMC.hh"
31#endif
32
33#include "geomdefs.hh"
34#include "G4LogicalVolume.hh"
35#include "G4Navigator.hh"
36#include "G4String.hh"
37#include "G4ThreeVector.hh"
38#include "G4TransportationManager.hh"
39#include "G4Types.hh"
40#include "G4VPhysicalVolume.hh"
41
42#include "parser/beam.h"
43
44#include <string>
45
46BDSPrimaryGeneratorFile::BDSPrimaryGeneratorFile(G4bool loopFileIn,
47 BDSBunchEventGenerator* bunchIn):
48 loopFile(loopFileIn),
49 bunch(bunchIn),
50 recreate(false),
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 || recreate;
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 || beam.distrFileLoopNTimes > 1;
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 {
141 generatorFromFile->RecreateAdvanceToEvent(eventOffset);
142 generatorFromFile->SetRecreationOn();
143 }
144 if (beam.distrFileMatchLength)
145 {
146 G4int nEventsPerLoop = (G4int)generatorFromFile->NEventsLeftInFile();
147 G4int distrFileLoopNTimes = (G4int)beam.distrFileLoopNTimes;
148 BDSGlobalConstants::Instance()->SetNumberToGenerate(nEventsPerLoop*distrFileLoopNTimes);
149 G4cout << __METHOD_NAME__ << "distrFileMatchLength is true -> simulating " << nEventsPerLoop << " events";
150 if (distrFileLoopNTimes > 1)
151 {G4cout << " " << distrFileLoopNTimes << " times";}
152 G4cout << G4endl;
153 }
154 }
155
156 return generatorFromFile; // could be nullptr
157}
158
159G4bool BDSPrimaryGeneratorFile::VertexInsideWorld(const G4ThreeVector& pos) const
160{
161 if (!worldSolid)
162 {// cache the world solid
163 G4Navigator* navigator = G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
164 G4VPhysicalVolume* world = navigator->GetWorldVolume();
165 worldSolid = world->GetLogicalVolume()->GetSolid();
166 }
167 EInside qinside = worldSolid->Inside(pos);
168 return qinside == EInside::kInside;
169}
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:46
General exception with possible name of object and message.
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
fo event generator file filter
Definition beamBase.h:163
std::string distrFile
beam parameters
Definition beamBase.h:51
bool distrFileFromExecOptions
Required to know how to build the absolute path properly.
Definition beamBase.h:53
bool distrFileLoop
beam parameters
Definition beamBase.h:55
bool removeUnstableWithoutDecay
beam parameters
Definition beamBase.h:57
std::string distrType
beam parameters
Definition beamBase.h:44
int distrFileLoopNTimes
beam parameters
Definition beamBase.h:56
bool distrFileMatchLength
beam parameters
Definition beamBase.h:54
Beam class.
Definition beam.h:43
G4bool StrContains(const G4String &str, const G4String &test)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
G4String GetFullPath(G4String filename, bool excludeNameFromPath=false, bool useCWDForPrefix=false)