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