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) 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 "BDSBeamline.hh"
19#include "BDSBeamlineElement.hh"
20#include "BDSTrajectoryPoint.hh"
21#include "BDSTrajectoryPrimary.hh"
22
23#include "globals.hh" // geant4 globals / types
24#include "G4Allocator.hh"
25#include "G4Step.hh"
26#include "G4Track.hh"
27#include "G4TrajectoryContainer.hh"
28
29#include <ostream>
30#include <set>
31
32G4Allocator<BDSTrajectoryPrimary> bdsTrajectoryPrimaryAllocator;
34
36 G4bool interactiveIn,
37 const BDS::TrajectoryOptions& storageOptionsIn,
38 G4bool storeTrajectoryPointsIn):
39 BDSTrajectory(aTrack,
40 interactiveIn,
41 storageOptionsIn),
42 firstHit(nullptr),
43 lastPoint(nullptr),
44 storeTrajectoryPoints(storeTrajectoryPointsIn)
45{;}
46
47BDSTrajectoryPrimary::~BDSTrajectoryPrimary()
48{
49 delete firstHit;
50 delete lastPoint;
51}
52
53void BDSTrajectoryPrimary::AppendStep(const G4Step* aStep)
54{
55 if (aStep->GetTrack()->GetTrackStatus() != G4TrackStatus::fAlive)
56 {
57 // particle is being killed, ie end of track. update last point
58 delete lastPoint;
59 lastPoint = new BDSTrajectoryPoint(aStep, true, true, true);
60 }
61
62 G4bool isScatteringPoint = BDSTrajectoryPoint::IsScatteringPoint(aStep);
63
64 // if we don't have a first hit already and it's a scattering point, record it
65 if (!firstHit && isScatteringPoint)
66 {
67 firstHit = new BDSTrajectoryPoint(aStep, true, true, true);
69 }
70 else if (isScatteringPoint && !hasScatteredThisTurn)
71 {hasScatteredThisTurn = true;}
72 // already a first hit scattering point but need to know if it scattered at all on this turn
73 // hasScatteredThisTurn is externally updated (reset) each turn in a circular machine
74
76 {
77 if (lastPoint) // copy it if we've already done the work of preparing the point
79 else
81 }
82}
83
84void BDSTrajectoryPrimary::MergeTrajectory(G4VTrajectory* secondTrajectory)
85{
86 BDSTrajectory::MergeTrajectory(secondTrajectory);
87 if (secondTrajectory)
88 {
89 auto prim = dynamic_cast<BDSTrajectoryPrimary*>(secondTrajectory);
90 if (prim)
91 {
92 if (prim->LastPoint())
93 {
94 delete lastPoint;
95 lastPoint = new BDSTrajectoryPoint(*prim->LastPoint());
96 }
97 }
98 }
99}
100
101std::ostream& operator<< (std::ostream& out, BDSTrajectoryPrimary const& t)
102{
103 if (t.firstHit)
104 {out << "First hit: " << t.firstHit << G4endl;}
105 else
106 {out << "No first hit" << G4endl;}
107 if (t.lastPoint)
108 {out << "Last point: " << t.lastPoint << G4endl;}
109 else
110 {out << "No last point" << G4endl;}
111
112 const BDSTrajectory& b(t);
113 out << b;
114 return out;
115}
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.
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)