BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSHitThinThing.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 "BDSHitThinThing.hh"
20#include "BDSTrajectoryPoint.hh"
21#include "BDSTrajectoryPointHit.hh"
22#include "BDSTrajectoryPrimary.hh"
23
24#include "G4Allocator.hh"
25
26#include <algorithm>
27#include <map>
28#include <vector>
29
30G4Allocator<BDSHitThinThing> BDSAllocatorThinThing;
31
33 G4int trackIDIn,
34 G4int parentIDIn,
35 G4int turnsTakenIn,
36 BDSTrajectoryPoint* hitIn):
37 pdgID(pdgIDIn),
38 trackID(trackIDIn),
39 parentID(parentIDIn),
40 turnsTaken(turnsTakenIn),
41 hit(hitIn)
42{;}
43
45{
46 delete hit;
47}
48
49std::vector<const BDSTrajectoryPoint*> BDSHitThinThing::TrajectoryPointsFromHC(BDSHitsCollectionThinThing* hits)
50{
51 std::vector<const BDSTrajectoryPoint*> result;
52 if (!hits)
53 {return result;}
54 else
55 {
56 for (G4int i = 0; i < (G4int)hits->entries(); i++)
57 {result.push_back((*hits)[i]->hit);}
58 return result;
59 }
60}
61
63{
64 return new BDSTrajectoryPointHit(pdgID,trackID, parentID, hit);
65}
66
67std::vector<const BDSTrajectoryPointHit*>
68BDSHitThinThing::ResolvePossibleEarlierThinHits(const std::vector<const BDSTrajectoryPrimary*>& primaryTrajectoryHits,
69 const BDSHitsCollectionThinThing* thinThingHits)
70{
71 auto basicResult = [&]() // lambda to avoid code duplication
72 {
73 std::vector<const BDSTrajectoryPointHit*> result;
74 for (auto p : primaryTrajectoryHits)
75 {
76 auto fh = p->FirstHit();
77 if (fh) // may not have hit anything, ie nullptr
78 {result.push_back(new BDSTrajectoryPointHit(p, fh));}
79 }
80 return result;
81 };
82
83 // thinThingHits is optional
84 // the vector might be empty
85 if (!thinThingHits)
86 {return primaryTrajectoryHits.empty() ? std::vector<const BDSTrajectoryPointHit*>() : basicResult();}
87 else if (thinThingHits->entries() == 0)
88 {return basicResult();}
89 else
90 {
91 // note there could be more than one thin hit per primary particle (e.g. multiple wire scanners)
92 // build map of hits from the two sources per primary track ID
93 std::map<G4int, std::vector<const BDSTrajectoryPointHit*>> hitsPerPrimary;
94 for (G4int i = 0; i < (G4int)thinThingHits->entries(); i++)
95 {
96 auto hit = (*thinThingHits)[i];
97 hitsPerPrimary[hit->trackID].push_back(hit->GetNewTrajectoryPointHit());
98 }
99 for (auto p : primaryTrajectoryHits)
100 {hitsPerPrimary[p->GetTrackID()].push_back(new BDSTrajectoryPointHit(p, p->LastPoint()));}
101
102 // use a lambda function to compare contents of pointers and not pointers themselves
103 auto TrajPointComp = [](const BDSTrajectoryPointHit* a, const BDSTrajectoryPointHit* b)
104 {
105 if (!a || !b)
106 {return false;}
107 else
108 {return *a < *b;}
109 };
110
111 // reduce multiple hits to the earliest one in the beam line for the 'first' hit
112 std::vector<const BDSTrajectoryPointHit*> result;
113 for (auto& primaryPoints : hitsPerPrimary)
114 {
115 auto& points = primaryPoints.second;
116 std::sort(points.begin(), points.end(), TrajPointComp);
117 result.push_back(points[0]);
118 }
119 return result;
120 }
121}
BDSHitThinThing()=delete
No default constructor.
BDSTrajectoryPointHit * GetNewTrajectoryPointHit() const
Allocate and return a new hit object.
virtual ~BDSHitThinThing()
Note this should not be inline when we use a G4Allocator.
static std::vector< const BDSTrajectoryPoint * > TrajectoryPointsFromHC(BDSHitsCollectionThinThing *hits)
Utility function to get a vector of trajectory points from the hits collection.
A summary trajectory object of a loss point.
A Point in a trajectory with extra information.