BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSSDApertureImpacts.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
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 "BDSAuxiliaryNavigator.hh"
20#include "BDSDebug.hh"
21#include "BDSGlobalConstants.hh"
22#include "BDSPhysicalVolumeInfo.hh"
23#include "BDSPhysicalVolumeInfoRegistry.hh"
24#include "BDSSDApertureImpacts.hh"
25#include "BDSStep.hh"
26
27#include "globals.hh"
28#include "G4AffineTransform.hh"
29#include "G4Event.hh"
30#include "G4EventManager.hh"
31#include "G4LogicalVolume.hh"
32#include "G4ParticleDefinition.hh"
33#include "G4SDManager.hh"
34#include "G4Step.hh"
35#include "G4StepStatus.hh"
36#include "G4ThreeVector.hh"
37#include "G4Track.hh"
38#include "G4VPhysicalVolume.hh"
39#include "G4VTouchable.hh"
40
41BDSSDApertureImpacts::BDSSDApertureImpacts(const G4String& name):
42 G4VSensitiveDetector("aperture_impacts/"+name),
43 colName(name),
44 hits(nullptr),
45 HCIDe(-1),
46 auxNavigator(new BDSAuxiliaryNavigator())
47{
48 collectionName.insert(colName);
49}
50
51BDSSDApertureImpacts::~BDSSDApertureImpacts()
52{
53 delete auxNavigator;
54}
55
56void BDSSDApertureImpacts::Initialize(G4HCofThisEvent* HCE)
57{
59 if (HCIDe < 0)
60 {HCIDe = G4SDManager::GetSDMpointer()->GetCollectionID(hits);}
61 HCE->AddHitsCollection(HCIDe,hits);
62
63#ifdef BDSDEBUG
64 G4cout << __METHOD_NAME__ << "Hits Collection ID: " << HCIDe << G4endl;
65#endif
66}
67
69 G4TouchableHistory* /*th*/)
70{
71 // check if pre step point is on geometry boundary - ie first step into a volume
72 G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
73 G4StepStatus preStepStatus = preStepPoint->GetStepStatus();
74 if (preStepStatus != G4StepStatus::fGeomBoundary)
75 {return false;} // wasn't on a boundary - don't generate a hit
76
77 // check if momentum is away from accelerator axis - ie leaving vacuum and entering
78 // beam pip, not coming in from outside
79 // get local coordinates
80 G4ThreeVector globalPos = preStepPoint->GetPosition();
81 G4ThreeVector globalMom = preStepPoint->GetMomentum();
82 BDSStep localPosMom = auxNavigator->ConvertToLocal(globalPos,
83 globalMom,
84 0.1*CLHEP::mm);
85
86 G4VSolid* preStepSolid = preStepPoint->GetPhysicalVolume()->GetLogicalVolume()->GetSolid();
87 G4ThreeVector posLocal = localPosMom.PreStepPoint();
88 G4ThreeVector surfaceNormal = preStepSolid->SurfaceNormal(posLocal);
89
90 // check if we have a surface normal pointing into the beam pipe (ie inside
91 // of the beam pipe). do this by projecting the surface normal onto a vector
92 // from the central curvilinear axis to the local point (radial vector). If
93 // +ve, then it's pointing out, else flip it along its direction.
94 G4ThreeVector radius(posLocal.x(), posLocal.y(), 0);
95 if (radius.dot(surfaceNormal) < 0)
96 {surfaceNormal *= -1;}
97
98 // test if leaving or entering by projecting the momentum onto the outwards
99 // pointing surface normal
100 G4double dotProduct = surfaceNormal.dot(localPosMom.PreStepPoint());
101 G4bool leavingBeamPipe = dotProduct >= 0; // +ve means it's leaving beam pipe - ie in direction of unit normal out
102 if (!leavingBeamPipe)
103 {return false;}
104
105 G4ThreeVector momLocal = localPosMom.PostStepPoint();
106 G4ThreeVector momLocalUnit = momLocal.unit();
107 G4double x = posLocal.x();
108 G4double y = posLocal.y();
109 G4int turnsTaken = BDSGlobalConstants::Instance()->TurnsTaken();
110 G4int beamlineIndex = -1;
111
112 // get the (global) S coordinate of the hit in the beam line
113 G4double S = -1;
114
115 // volume is from curvilinear coordinate parallel geometry
117
118 // declare lambda for updating parameters if info found (avoid duplication of code)
119 auto UpdateParams = [&](BDSPhysicalVolumeInfo* info)
120 {
121 S = info->GetSPos() + posLocal.z();
122 beamlineIndex = info->GetBeamlineIndex();
123 };
124
125 if (theInfo)
126 {UpdateParams(theInfo);}
127 else
128 {// Try again but with the pre step point shifted marginally
129 G4ThreeVector unitDir = globalMom.unit();
130 G4ThreeVector newPos = globalPos + 1e-3*unitDir;
131 BDSStep stepLocal2 = auxNavigator->ConvertToLocal(newPos, unitDir);
133 if (theInfo)
134 {UpdateParams(theInfo);}
135 }
136
137 //create hits and put in hits collection of the event
138 G4Track* track = aStep->GetTrack();
139 G4int nElectrons = track->GetDynamicParticle()->GetTotalOccupancy();
140 BDSHitApertureImpact* hit = new BDSHitApertureImpact(preStepPoint->GetTotalEnergy(),
141 preStepPoint->GetKineticEnergy(),
142 S,
143 x,
144 y,
145 momLocalUnit.x(),
146 momLocalUnit.y(),
147 preStepPoint->GetWeight(),
148 preStepPoint->GetGlobalTime(),
149 track->GetDefinition()->GetPDGEncoding(),
150 track->GetTrackID(),
151 track->GetParentID(),
152 turnsTaken,
153 beamlineIndex,
154 nElectrons);
155
156 hits->insert(hit);
157
158 return true;
159}
Extra G4Navigator to get coordinate transforms.
BDSStep ConvertToLocal(G4Step const *const step, G4bool useCurvilinear=true) const
static BDSGlobalConstants * Instance()
Access method.
Snapshot of information for particle passing through a collimator.
BDSPhysicalVolumeInfo * GetInfo(G4VPhysicalVolume *logicalVolume, G4bool isTunnel=false)
static BDSPhysicalVolumeInfoRegistry * Instance()
Singleton accessor.
A class holding any information pertaining to a particular physical volume in a BDSIM geant4 model.
G4double GetSPos() const
Get the s position coordinate of the logical volume.
virtual G4bool ProcessHits(G4Step *aStep, G4TouchableHistory *th)
BDSAuxiliaryNavigator * auxNavigator
Navigator for checking points in read out geometry.
G4int HCIDe
Hits collection ID of the event.
BDSHitsCollectionApertureImpacts * hits
Hits.
G4String colName
Collection name.
A simple class to represent the positions of a step.
Definition: BDSStep.hh:33
G4ThreeVector PostStepPoint() const
Accessor.
Definition: BDSStep.hh:43
G4VPhysicalVolume * VolumeForTransform() const
Accessor.
Definition: BDSStep.hh:44
G4ThreeVector PreStepPoint() const
Accessor.
Definition: BDSStep.hh:42
const char * GetName(int)
Name of element.
Definition: python.cc:38