BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSTrajectoryPrimary.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 "BDSBeamline.hh"
20#include "BDSBeamlineElement.hh"
21#include "BDSTrajectoryPoint.hh"
22#include "BDSTrajectoryPrimary.hh"
23
24#include "globals.hh" // geant4 globals / types
25#include "G4Allocator.hh"
26#include "G4Step.hh"
27#include "G4Track.hh"
28#include "G4TrajectoryContainer.hh"
29
30#include <ostream>
31#include <set>
32
33G4Allocator<BDSTrajectoryPrimary> bdsTrajectoryPrimaryAllocator;
35
37 G4bool interactiveIn,
38 const BDS::TrajectoryOptions& storageOptionsIn,
39 G4bool storeTrajectoryPointsIn):
40 BDSTrajectory(aTrack,
41 interactiveIn,
42 storageOptionsIn),
43 firstHit(nullptr),
44 lastPoint(nullptr),
45 storeTrajectoryPoints(storeTrajectoryPointsIn)
46{;}
47
48BDSTrajectoryPrimary::~BDSTrajectoryPrimary()
49{
50 delete firstHit;
51 delete lastPoint;
52}
53
54void BDSTrajectoryPrimary::AppendStep(const G4Step* aStep)
55{
56 if (aStep->GetTrack()->GetTrackStatus() != G4TrackStatus::fAlive)
57 {
58 // particle is being killed, ie end of track. update last point
59 delete lastPoint;
60 lastPoint = new BDSTrajectoryPoint(aStep, true, true, true);
61 }
62
63 G4bool isScatteringPoint = BDSTrajectoryPoint::IsScatteringPoint(aStep);
64
65 // if we don't have a first hit already and it's a scattering point, record it
66 if (!firstHit && isScatteringPoint)
67 {
68 firstHit = new BDSTrajectoryPoint(aStep, true, true, true);
70 }
71 else if (isScatteringPoint && !hasScatteredThisTurn)
72 {hasScatteredThisTurn = true;}
73 // already a first hit scattering point but need to know if it scattered at all on this turn
74 // hasScatteredThisTurn is externally updated (reset) each turn in a circular machine
75
77 {
78 if (lastPoint) // copy it if we've already done the work of preparing the point
80 else
82 }
83}
84
85void BDSTrajectoryPrimary::MergeTrajectory(G4VTrajectory* secondTrajectory)
86{
87 BDSTrajectory::MergeTrajectory(secondTrajectory);
88 if (secondTrajectory)
89 {
90 auto prim = dynamic_cast<BDSTrajectoryPrimary*>(secondTrajectory);
91 if (prim)
92 {
93 if (prim->LastPoint())
94 {
95 delete lastPoint;
96 lastPoint = new BDSTrajectoryPoint(*prim->LastPoint());
97 }
98 }
99 }
100}
101
102std::ostream& operator<< (std::ostream& out, BDSTrajectoryPrimary const& t)
103{
104 if (t.firstHit)
105 {out << "First hit: " << t.firstHit << G4endl;}
106 else
107 {out << "No first hit" << G4endl;}
108 if (t.lastPoint)
109 {out << "Last point: " << t.lastPoint << G4endl;}
110 else
111 {out << "No last point" << G4endl;}
112
113 const BDSTrajectory& b(t);
114 out << b;
115 return out;
116}
A Point in a trajectory with extra information.
G4bool IsScatteringPoint() const
Trajectory information for only the primary.
virtual void AppendStep(const G4Step *aStep)
BDSTrajectoryPoint * firstHit
Point owned by this class for the first scattering point.
static G4bool hasScatteredThisTurn
BDSTrajectoryPoint * lastPoint
Point owned by this class for the last-most point.
const G4bool storeTrajectoryPoints
Whether to use base class to store all points.
BDSTrajectoryPrimary()=delete
No default constructor required.
virtual void MergeTrajectory(G4VTrajectory *secondTrajectory)
Trajectory information from track including last scatter etc.
virtual void MergeTrajectory(G4VTrajectory *secondTrajectory)
Merge another trajectory into this one.
virtual void AppendStep(const G4Step *aStep)