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