BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSTrajectory.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 "BDSTrajectory.hh"
20#include "BDSTrajectoryPoint.hh"
21
22#include "globals.hh" // geant4 globals / types
23#include "G4Allocator.hh"
24#include "G4Step.hh"
25#include "G4Track.hh"
26#include "G4VProcess.hh"
27#include "G4TrajectoryContainer.hh" // also provides TrajectoryVector type(def)
28
29#include <map>
30#include <ostream>
31
32G4Allocator<BDSTrajectory> bdsTrajectoryAllocator;
33
34BDSTrajectory::BDSTrajectory(const G4Track* aTrack,
35 G4bool interactiveIn,
36 const BDS::TrajectoryOptions& storageOptionsIn):
37 G4Trajectory(aTrack),
38 interactive(interactiveIn),
39 storageOptions(storageOptionsIn),
40 parent(nullptr),
41 trajIndex(0),
42 parentIndex(0),
43 parentStepIndex(0),
44 depth(-1)
45{
46 suppressTransportationAndNotInteractive = storageOptionsIn.suppressTransportationSteps && !interactiveIn;
47 const G4VProcess* proc = aTrack->GetCreatorProcess();
48 if (proc)
49 {
50 creatorProcessType = aTrack->GetCreatorProcess()->GetProcessType();
51 creatorProcessSubType = aTrack->GetCreatorProcess()->GetProcessSubType();
52 }
53 else
54 {
55 creatorProcessType = -1;
56 creatorProcessSubType = -1;
57 }
58 weight = aTrack->GetWeight();
59
60 parentIndex = -1;
61 fpBDSPointsContainer = new BDSTrajectoryPointsContainer();
62 // this is for the first point of the track
63 (*fpBDSPointsContainer).push_back(new BDSTrajectoryPoint(aTrack,
64 storageOptions.storeLocal,
65 storageOptions.storeLinks,
66 storageOptions.storeIon));
67}
68
69BDSTrajectory::~BDSTrajectory()
70{
71 // clean points container
72 for (auto i : *fpBDSPointsContainer)
73 {delete i;}
74 fpBDSPointsContainer->clear();
76}
77
79{
80 if (suppressTransportationAndNotInteractive)
81 {
82 if (pointIn->NotTransportationLimitedStep())
83 {
84 auto r = new BDSTrajectoryPoint(*pointIn);
85 if (fpBDSPointsContainer->size() == 1)
86 {(*fpBDSPointsContainer)[0]->SetMaterial(pointIn->GetMaterial());}
87 CleanPoint(r);
88 fpBDSPointsContainer->push_back(r);
89 }
90 }
91 else
92 {
93 auto r = new BDSTrajectoryPoint(*pointIn);
94 if (fpBDSPointsContainer->size() == 1)
95 {(*fpBDSPointsContainer)[0]->SetMaterial(pointIn->GetMaterial());}
96 CleanPoint(r);
97 fpBDSPointsContainer->push_back(r);
98 }
99}
100
102{
103 if (!storageOptions.storeIon)
104 {point->DeleteExtraIon();}
105 if (!storageOptions.storeLinks)
106 {point->DeleteExtraLinks();}
107 if (!storageOptions.storeLocal)
108 {point->DeleteExtraLocal();}
109}
110
111void BDSTrajectory::AppendStep(const G4Step* aStep)
112{
113 // we do not use G4Trajectory::AppendStep here as that would
114 // duplicate position information in its own vector of positions
115 // which we prevent access to be overriding GetPoint
116
117 // if the first step, we update the material of the 0th point which was
118 // constructed from the track before geometry tracking and we didn't know
119 // the material
120 if (fpBDSPointsContainer->size() == 1)
121 {(*fpBDSPointsContainer)[0]->SetMaterial(aStep->GetTrack()->GetMaterial());}
122 if (suppressTransportationAndNotInteractive)
123 {
124 // note for a first step of a track, the prestep point process
125 // may be nullptr, but if we're appending a step we really care
126 // about what the post process is - test on that
127 auto postStepPoint = aStep->GetPostStepPoint();
128 const G4VProcess* postProcess = postStepPoint->GetProcessDefinedStep();
129 if (postProcess)
130 {
131 G4int postProcessType = postProcess->GetProcessType();
132 if(postProcessType != 1 /* transportation */ &&
133 postProcessType != 10 /* parallel world */ )
134 {
135 fpBDSPointsContainer->push_back(new BDSTrajectoryPoint(aStep,
136 storageOptions.storeLocal,
137 storageOptions.storeLinks,
138 storageOptions.storeIon));
139 }
140 }
141 }
142 else
143 {
144 fpBDSPointsContainer->push_back(new BDSTrajectoryPoint(aStep,
145 storageOptions.storeLocal,
146 storageOptions.storeLinks,
147 storageOptions.storeIon));
148 }
149}
150
151void BDSTrajectory::MergeTrajectory(G4VTrajectory* secondTrajectory)
152{
153 if(!secondTrajectory)
154 {return;}
155
156 BDSTrajectory* second = (BDSTrajectory*)secondTrajectory;
157 G4int ent = second->GetPointEntries();
158 // initial point of the second trajectory should not be merged
159 for (G4int i = 1; i < ent; ++i)
160 {fpBDSPointsContainer->push_back((*(second->fpBDSPointsContainer))[i]);}
161 delete (*second->fpBDSPointsContainer)[0];
162 second->fpBDSPointsContainer->clear();
163}
164
166{
167 // loop over trajectory to find non transportation step
168 for (G4int i = 0; i < GetPointEntries(); ++i)
169 {
170 BDSTrajectoryPoint* point = static_cast<BDSTrajectoryPoint*>(GetPoint(i));
171 if (point->IsScatteringPoint())
172 {return point;}
173 }
174#ifdef BDSDEBUG
175 G4cout << __METHOD_NAME__ << "no interaction" << G4endl;
176#endif
177 return static_cast<BDSTrajectoryPoint*>(GetPoint(0));
178}
179
180BDSTrajectoryPoint* BDSTrajectory::LastInteraction()const
181{
182 // loop over trajectory backwards to find non transportation step
183 for (G4int i = GetPointEntries()-1; i >= 0; --i)
184 {
185 BDSTrajectoryPoint* point = static_cast<BDSTrajectoryPoint*>(GetPoint(i));
186 if (point->IsScatteringPoint())
187 {return point;}
188 }
189#ifdef BDSDEBUG
190 G4cout << __METHOD_NAME__ << "no interaction" << G4endl;
191#endif
192 return static_cast<BDSTrajectoryPoint*>(GetPoint(GetPointEntries()-1));
193}
194
195std::ostream& operator<< (std::ostream& out, BDSTrajectory const& t)
196{
197 for (G4int i = 0; i < t.GetPointEntries(); i++)
198 {out << *(static_cast<BDSTrajectoryPoint*>(t.GetPoint(i))) << G4endl;}
199 return out;
200}
201
A Point in a trajectory with extra information.
G4bool NotTransportationLimitedStep() const
Return true if step isn't defined by transportation processes.
G4bool IsScatteringPoint() const
G4Material * GetMaterial() const
Accessor.
Trajectory information from track including last scatter etc.
virtual int GetPointEntries() const
Get number of trajectory points in this trajectory.
BDSTrajectoryPoint * FirstInteraction() const
virtual void MergeTrajectory(G4VTrajectory *secondTrajectory)
Merge another trajectory into this one.
virtual G4VTrajectoryPoint * GetPoint(G4int i) const
Access a point - use this class's container.
void CleanPoint(BDSTrajectoryPoint *point) const
virtual void AppendStep(const G4Step *aStep)
BDSTrajectoryPointsContainer * fpBDSPointsContainer