BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSSDSamplerLink.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 "BDSHitSamplerLink.hh"
21#include "BDSLinkRegistry.hh"
22#include "BDSParticleCoordsFull.hh"
23#include "BDSPhysicsUtilities.hh"
24#include "BDSSDSamplerLink.hh"
25
26#include "G4DynamicParticle.hh"
27#include "G4ParticleDefinition.hh"
28#include "G4SDManager.hh"
29#include "G4Step.hh"
30#include "G4StepPoint.hh"
31#include "G4ThreeVector.hh"
32#include "G4TouchableHistory.hh"
33#include "G4Track.hh"
34#include "G4Types.hh"
35
36#include "CLHEP/Geometry/Point3D.h"
37#include "CLHEP/Geometry/Vector3D.h"
38
39#include <vector>
40
42 BDSSensitiveDetector("samplerlink/" + name),
43 samplerLinkCollection(nullptr),
44 itsCollectionName(name),
45 itsHCID(-1),
46 registry(nullptr),
47 minimumEK(0),
48 protonsAndIonsOnly(true)
49{
50 collectionName.insert(name);
51}
52
53BDSSDSamplerLink::~BDSSDSamplerLink()
54{;}
55
56void BDSSDSamplerLink::Initialize(G4HCofThisEvent* HCE)
57{
58 // Create SamplerLink hits collection
60
61 // Record id for use in EventAction to save time - slow string lookup by collection name
62 if (itsHCID < 0)
63 {itsHCID = G4SDManager::GetSDMpointer()->GetCollectionID(samplerLinkCollection);}
64 HCE->AddHitsCollection(itsHCID,samplerLinkCollection);
65}
66
67G4bool BDSSDSamplerLink::ProcessHits(G4Step* aStep, G4TouchableHistory* /*readOutTH*/)
68{
69 // Do not store hit if the particle pre step point is not on the boundary
70 G4StepPoint* postStepPoint = aStep->GetPostStepPoint();
71 if (postStepPoint->GetStepStatus() != fGeomBoundary)
72 {return false;} // this step was not stored
73
74 G4Track* track = aStep->GetTrack();
75 const G4DynamicParticle* dp = track->GetDynamicParticle();
76 G4double charge = dp->GetCharge(); // dynamic effective charge
77 auto pd = dp->GetParticleDefinition();
78
79 // check against various filters
80 if (charge == 0) // don't return neutral particles
81 {return false;}
82 if (!pd->GetPDGStable()) // don't return unstable particles
83 {return false;}
84 G4double ek = track->GetKineticEnergy();
85 if (ek < minimumEK)
86 {return false;}
87
88 G4int PDGtype = pd->GetPDGEncoding();
90 {
91 if (!BDS::IsIon(dp) && PDGtype != 2212)
92 {return false;}
93 }
94
95 G4int trackID = track->GetTrackID(); // unique ID of track
96 G4int parentID = track->GetParentID(); // unique ID of track's mother
97 G4double T = track->GetGlobalTime(); // time since beginning of event
98 G4double energy = track->GetTotalEnergy(); // total track energy
99
100 const G4ThreeVector& pos = track->GetPosition(); // current particle position (global)
101 const G4ThreeVector& mom = track->GetMomentumDirection(); // current particle direction (global) (unit)
102 G4double weight = track->GetWeight(); // weighting
103 G4int nElectrons = dp->GetTotalOccupancy();
104 G4double mass = dp->GetMass();
105 G4double momentum = dp->GetTotalMomentum();
106
107 G4int z = pd->GetAtomicNumber();
108 G4int a = pd->GetAtomicMass();
109
110 // The copy number from the physical volume is used as our unique sampler ID
111 G4int samplerID = track->GetVolume()->GetCopyNo();
112 //G4cout << "samplerID " << samplerID << G4endl;
113
114 // Initialize variables for the local position and direction
115 G4ThreeVector localPosition;
116 G4ThreeVector localDirection;
117
118 // Get coordinate transform and prepare local coordinates
119 G4Transform3D globalToLocal = G4Transform3D::Identity;
120 G4ThreeVector globalToLocalOffset = G4ThreeVector();
121 G4bool noRotation = true;
122 if (registry)
123 {
124 noRotation = registry->NoRotation(samplerID);
125 globalToLocal = registry->TransformInverse(samplerID);
126 globalToLocalOffset = globalToLocal.getTranslation();
127 }
128 if (noRotation)
129 {
130 localPosition = pos + globalToLocalOffset;
131 localDirection = mom;
132 }
133 else if (globalToLocal != G4Transform3D::Identity)
134 {
135 // The global to local transform is defined in the registry.
136 // Cast 3 vector to 'point' to transform position (required to be explicit for * operator)
137 localPosition = globalToLocal * (HepGeom::Point3D<G4double>)pos;
138 // Now, if the sampler is infinitely thin, the local z should be 0, but it's finite.
139 // Account for this by purposively setting local z to be 0.
140 localPosition.setZ(0.0);
141 // Cast 3 vector to 3 vector to transform vector (required to be explicit for * operator)
142 localDirection = globalToLocal * (HepGeom::Vector3D<G4double>)mom;
143 }
144
145 BDSParticleCoordsFull coords(localPosition.x(),
146 localPosition.y(),
147 localPosition.z(),
148 localDirection.x(),
149 localDirection.y(),
150 localDirection.z(),
151 T,
152 localPosition.z(), // s = z here
153 energy,
154 weight);
155
156 BDSHitSamplerLink* smpHit = new BDSHitSamplerLink(samplerID,
157 coords,
158 momentum,
159 mass,
160 z,
161 a,
162 charge,
163 PDGtype,
164 parentID,
165 trackID,
166 nElectrons);
167
168 samplerLinkCollection->insert(smpHit);
169 return true; // the hit was stored
170}
171
173{
174 BDSHitSamplerLink* lastHit = samplerLinkCollection->GetVector()->back();
175 return dynamic_cast<G4VHit*>(lastHit);
176}
A set of particle coordinates including energy and weight.
Virtual class to define interface for ordered multi-sensitive detector.
G4bool IsIon(const G4ParticleDefinition *particle)
Whether a particle is an ion. A proton is counted NOT as an ion.