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