BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSSDCollimator.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 "BDSAcceleratorModel.hh"
20#include "BDSAuxiliaryNavigator.hh"
21#include "BDSBeamline.hh"
22#include "BDSSDCollimator.hh"
23#include "BDSDebug.hh"
24#include "BDSHitEnergyDeposition.hh"
25#include "BDSPhysicalVolumeInfo.hh"
26#include "BDSPhysicalVolumeInfoRegistry.hh"
27#include "BDSStep.hh"
28#include "BDSTrajectoryPoint.hh"
29
30#include "globals.hh" // geant4 types / globals
31#include "G4SDManager.hh"
32#include "G4Step.hh"
33#include "G4StepPoint.hh"
34#include "G4ThreeVector.hh"
35#include "G4Track.hh"
36#include "G4VPhysicalVolume.hh"
37#include "G4VProcess.hh"
38
39#include <map>
40#include <vector>
41
43 BDSSensitiveDetector("collimator/" + name),
44 collimatorCollection(nullptr),
45 itsCollectionName(name),
46 itsHCID(-1),
47 auxNavigator(new BDSAuxiliaryNavigator())
48{
49 collectionName.insert(name);
50}
51
52BDSSDCollimator::~BDSSDCollimator()
53{
54 delete auxNavigator;
55}
56
57void BDSSDCollimator::Initialize(G4HCofThisEvent* HCE)
58{
59 // Create Collimator hits collection
61
62 // Record id for use in EventAction to save time - slow string lookup by collection name
63 if (itsHCID < 0)
64 {itsHCID = G4SDManager::GetSDMpointer()->GetCollectionID(collimatorCollection);}
65 HCE->AddHitsCollection(itsHCID, collimatorCollection);
66
67#ifdef BDSDEBUG
68 G4cout << __METHOD_NAME__ << "Hits Collection ID: " << itsHCID << G4endl;
69#endif
70}
71
72G4bool BDSSDCollimator::ProcessHits(G4Step* step, G4TouchableHistory* rOHist)
73{
74 std::vector<G4VHit*> hits;
75 return ProcessHitsOrdered(step, rOHist, hits);
76}
77
79 G4TouchableHistory* /*rOHist*/,
80 const std::vector<G4VHit*>& hits)
81{
82 G4VHit* lastHit = nullptr;
83 BDSHitEnergyDeposition* lastHitEDep = nullptr;
84 if (!hits.empty())
85 {
86 lastHit = hits.back();
87 lastHitEDep = dynamic_cast<BDSHitEnergyDeposition*>(lastHit);
88 }
89
90 G4bool scatteringPoint = BDSTrajectoryPoint::IsScatteringPoint(step);
91 if (!scatteringPoint)
92 {return false;} // don't store it - could just be step through thin collimator
93
94 // get pre step point in local coordinates.
95 BDSStep stepLocal = auxNavigator->ConvertToLocal(step);
96 G4StepPoint* preStepPoint = step->GetPreStepPoint();
97 G4ThreeVector preStepPosLocal = stepLocal.PreStepPoint();
98 G4ThreeVector momGlobal = preStepPoint->GetMomentumDirection();
99 // uses existing transform cached in aux navigator here
100 G4ThreeVector preStepMomLocal = auxNavigator->ConvertAxisToLocal(momGlobal);
101 G4double totalEnergy = preStepPoint->GetTotalEnergy();
102
103 // get which beam line it's in and the index
105
106 // map the index to a collimator index
107 BDSBeamline* clBeamline = nullptr; // curvilinar beam line
108 BDSBeamline* mwBeamline = nullptr; // mass world beam line
109 G4int beamlineIndex = -1;
110 G4int collimatorIndex = -1;
111
112 if (theInfo)
113 {
114 beamlineIndex = theInfo->GetBeamlineIndex() - 1; // cl beam lines always have 1 more start and finish element
115 clBeamline = theInfo->GetBeamline();
116 mwBeamline = BDSAcceleratorModel::Instance()->CorrespondingMassWorldBeamline(clBeamline);
117
118 auto beamlineMap = mapping.find(mwBeamline);
119 if (beamlineMap != mapping.end())
120 {
121 const auto& indexMap = beamlineMap->second;
122 collimatorIndex = indexMap.at(beamlineIndex);
123 }
124 else
125 {// create map for this beam line
126 std::map<G4int, G4int> indexMap;
127 std::vector<G4int> collimatorIndices = mwBeamline->GetIndicesOfCollimators();
128 for (G4int i = 0; i < (G4int)collimatorIndices.size(); i++)
129 {indexMap[collimatorIndices[i]] = i;}
130 mapping[mwBeamline] = indexMap;
131 collimatorIndex = indexMap.at(beamlineIndex); // cl beam line
132 }
133 }
134
135 BDSHitCollimator* hit = new BDSHitCollimator(mwBeamline,
136 collimatorIndex,
137 preStepPosLocal,
138 preStepMomLocal,
139 totalEnergy,
140 lastHitEDep);
141
142 collimatorCollection->insert(hit);
143 return true;
144}
145
146
148{
149 auto hitsVector = collimatorCollection->GetVector();
150 if (hitsVector->empty())
151 {return nullptr;}
152 else
153 {
154 BDSHitCollimator* lastHit = hitsVector->back();
155 return static_cast<G4VHit*>(lastHit);
156 }
157}
BDSBeamline * CorrespondingMassWorldBeamline(BDSBeamline *bl) const
Extra G4Navigator to get coordinate transforms.
G4ThreeVector ConvertAxisToLocal(const G4ThreeVector &globalAxis, const G4bool useCurvilinear=true) const
BDSStep ConvertToLocal(G4Step const *const step, G4bool useCurvilinear=true) const
A vector of BDSBeamlineElement instances - a beamline.
Definition: BDSBeamline.hh:61
std::vector< G4int > GetIndicesOfCollimators() const
Return indices in order of ecol, rcol, jcol and crystalcol elements.
Definition: BDSBeamline.cc:957
Snapshot of information for particle passing through a collimator.
Information recorded for a single piece of energy deposition.
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.
BDSBeamline * GetBeamline() const
Accessor.
G4int GetBeamlineIndex() const
Accessor.
BDSSDCollimator(G4String name)
Include unique name for each instance.
BDSHitsCollectionCollimator * collimatorCollection
The hits collection for this sensitive detector class that's owned by each instance.
virtual void Initialize(G4HCofThisEvent *HCE) override
virtual G4VHit * last() const override
Return the last collimator hit.
virtual G4bool ProcessHits(G4Step *aStep, G4TouchableHistory *rOHist) override
BDSAuxiliaryNavigator * auxNavigator
An auxiliary navigator object for coordinate transforms.
virtual G4bool ProcessHitsOrdered(G4Step *step, G4TouchableHistory *rOHist, const std::vector< G4VHit * > &hits) override
G4String itsCollectionName
The name of the hits collection that's created and registered.
std::map< BDSBeamline *, std::map< G4int, G4int > > mapping
Virtual class to define interface for ordered multi-sensitive detector.
A simple class to represent the positions of a step.
Definition: BDSStep.hh:33
G4VPhysicalVolume * VolumeForTransform() const
Accessor.
Definition: BDSStep.hh:44
G4ThreeVector PreStepPoint() const
Accessor.
Definition: BDSStep.hh:42
G4bool IsScatteringPoint() const