LDMX Software
Public Member Functions | Private Member Functions | Private Attributes | List of all members
simcore::TrackMap Class Reference

Defines a map of particle ancestry and particles to be saved. More...

#include <TrackMap.h>

Public Member Functions

void insert (const G4Track *track)
 Add a record in the map for the input track.
 
bool contains (const G4Track *track) const
 Check if the passed track has already been inserted into the track map.
 
bool isDescendant (int trackID, int ancestorID, int maximum_depth) const
 Check if the track with the given ID is a descendant of the track with the given ancestor ID up to a given depth.
 
int findIncident (int trackID) const
 Find a trajectory's nearest parent that is incident on the calorimeter region.
 
bool isSaved (int trackID) const
 Return true if the given track ID is saved i.e.
 
void save (const G4Track *track)
 Add a track to be stored into output map.
 
void traceAncestry ()
 Trace the ancestry for the particles that will be stored.
 
void clear ()
 Clear the internal maps.
 
std::map< int, ldmx::SimParticle > & getParticleMap ()
 Get the map of particles to be stored in output event.
 

Private Member Functions

bool isInCalorimeterRegion (const G4Track *track) const
 Was the input track generated inside the calorimeter region?
 

Private Attributes

std::unordered_map< int, std::pair< int, bool > > ancestry_
 ancestry map of particles in event (child -> parent)
 
std::unordered_map< int, std::vector< int > > descendents_
 descendents map of particles in event (parent -> children)
 
std::map< int, ldmx::SimParticleparticle_map_
 map of SimParticles that will be stored
 

Detailed Description

Defines a map of particle ancestry and particles to be saved.

This class keeps track of the ancestry (child -> parent) and descendents (parent -> children) of ALL particles generated in an event. This allows the particles that are chosen to be saved (via the TrackMap::save method) to have their parent and children faithfully recorded in the output file.

Definition at line 28 of file TrackMap.h.

Member Function Documentation

◆ clear()

void simcore::TrackMap::clear ( )

Clear the internal maps.

This should be called at the beginning of an event. The maps need to persist through the end of the event so that they are available to be written to the output file.

Definition at line 126 of file TrackMap.cxx.

126 {
127 ancestry_.clear();
128 descendents_.clear();
129 particle_map_.clear();
130}
std::unordered_map< int, std::pair< int, bool > > ancestry_
ancestry map of particles in event (child -> parent)
Definition TrackMap.h:132
std::unordered_map< int, std::vector< int > > descendents_
descendents map of particles in event (parent -> children)
Definition TrackMap.h:135
std::map< int, ldmx::SimParticle > particle_map_
map of SimParticles that will be stored
Definition TrackMap.h:138

References ancestry_, descendents_, and particle_map_.

Referenced by simcore::g4user::EventAction::BeginOfEventAction().

◆ contains()

bool simcore::TrackMap::contains ( const G4Track *  track) const
inline

Check if the passed track has already been inserted into the track map.

Definition at line 40 of file TrackMap.h.

40 {
41 return ancestry_.find(track->GetTrackID()) != ancestry_.end();
42 }

References ancestry_.

Referenced by simcore::g4user::TrackingAction::PreUserTrackingAction().

◆ findIncident()

int simcore::TrackMap::findIncident ( int  trackID) const

Find a trajectory's nearest parent that is incident on the calorimeter region.

We assume that the primary particles have a parent ID of 0.

If this track ID does not have such a trajectory, then the track ID of the primary in its parentage is returned.

Parameters
trackIDThe track ID to search its parentage for the incident

Definition at line 35 of file TrackMap.cxx.

35 {
36 int currTrackID = trackID;
37 bool foundIncident{false};
38 while (not foundIncident) {
39 auto& [parentID, inCalRegion] = ancestry_.at(currTrackID);
40 if (not inCalRegion or parentID == 0) {
41 // current track ID is nearest ancestor
42 // originating outside cal region
43 // or is a primary particle
44 foundIncident = true;
45 } else {
46 // still in cal region, keep going
47 currTrackID = parentID;
48 }
49 }
50 return currTrackID;
51}

References ancestry_.

Referenced by simcore::EcalSD::ProcessHits().

◆ getParticleMap()

std::map< int, ldmx::SimParticle > & simcore::TrackMap::getParticleMap ( )
inline

Get the map of particles to be stored in output event.

Definition at line 101 of file TrackMap.h.

101{ return particle_map_; }

References particle_map_.

Referenced by simcore::UserAction::getCurrentParticleMap().

◆ insert()

void simcore::TrackMap::insert ( const G4Track *  track)

Add a record in the map for the input track.

Parameters
trackG4Track to insert

Definition at line 29 of file TrackMap.cxx.

29 {
30 ancestry_[track->GetTrackID()] =
31 std::make_pair(track->GetParentID(), isInCalorimeterRegion(track));
32 descendents_[track->GetParentID()].push_back(track->GetTrackID());
33}
bool isInCalorimeterRegion(const G4Track *track) const
Was the input track generated inside the calorimeter region?
Definition TrackMap.cxx:132

References ancestry_, descendents_, and isInCalorimeterRegion().

Referenced by simcore::g4user::TrackingAction::PreUserTrackingAction().

◆ isDescendant()

bool simcore::TrackMap::isDescendant ( int  trackID,
int  ancestorID,
int  maximum_depth 
) const

Check if the track with the given ID is a descendant of the track with the given ancestor ID up to a given depth.

Definition at line 9 of file TrackMap.cxx.

10 {
11 int current_depth{0};
12 int current_track{trackID};
13 // Walk the tree until we either no longer have a parent or we reach the
14 // desired depth
15 while (current_depth < maximum_depth &&
16 (ancestry_.find(current_track) != ancestry_.end())) {
17 // See if we have encountered the parent of the current track
18 //
19 // operator[] is not const, so we need to use at()
20 current_track = ancestry_.at(current_track).first;
21 if (current_track == ancestorID) {
22 // If one of the parents is the track of interest, we are done!
23 return true;
24 }
25 current_depth++;
26 }
27 return false;
28}

References ancestry_.

◆ isInCalorimeterRegion()

bool simcore::TrackMap::isInCalorimeterRegion ( const G4Track *  track) const
private

Was the input track generated inside the calorimeter region?

We rely on the fact that the calorimeter region is named 'CalorimeterRegion' and no other region names contain the string 'Calorimeter'

Definition at line 132 of file TrackMap.cxx.

132 {
133 auto region{track->GetLogicalVolumeAtVertex()->GetRegion()->GetName()};
134 return region.contains("Calorimeter");
135}

Referenced by insert().

◆ isSaved()

bool simcore::TrackMap::isSaved ( int  trackID) const
inline

Return true if the given track ID is saved i.e.

will be stored in output file

Parameters
trackIDThe track ID.
Returns
True if the track ID has been inserted in output particle map

Definition at line 68 of file TrackMap.h.

68 {
69 return particle_map_.find(trackID) != particle_map_.end();
70 }

References particle_map_.

◆ save()

void simcore::TrackMap::save ( const G4Track *  track)

Add a track to be stored into output map.

Note
We assume that the track is at the end of processing so that its current kinematics can be labeled as the "end-point" kinematics.
Parameters
trackG4Track to store into output

Definition at line 53 of file TrackMap.cxx.

53 {
54 // create sim particle in map, keep reference to the newly created particle
55 ldmx::SimParticle& particle{particle_map_[track->GetTrackID()]};
56
57 // TODO: default gen status?
58 particle.setGenStatus(0);
59
60 // Update the gen status from the primary particle.
61 if (track->GetDynamicParticle()->GetPrimaryParticle() != nullptr) {
62 G4VUserPrimaryParticleInformation* primaryInfo =
63 track->GetDynamicParticle()->GetPrimaryParticle()->GetUserInformation();
64 if (primaryInfo != nullptr) {
65 particle.setGenStatus(
66 ((UserPrimaryParticleInformation*)primaryInfo)->getHepEvtStatus());
67 }
68 }
69
70 auto particle_def{track->GetDefinition()};
71
72 particle.setPdgID(particle_def->GetPDGEncoding());
73 particle.setCharge(particle_def->GetPDGCharge());
74 particle.setMass(track->GetDynamicParticle()->GetMass());
75 particle.setEnergy(track->GetVertexKineticEnergy() +
76 track->GetDynamicParticle()->GetMass());
77
78 auto track_info{UserTrackInformation::get(track)};
79 particle.setVertexVolume(track_info->getVertexVolume());
80
81 auto vert{track->GetVertexPosition()};
82 particle.setVertex(vert.x(), vert.y(), vert.z());
83 particle.setTime(track_info->getVertexTime());
84
85 auto init_momentum{track_info->getInitialMomentum()};
86 particle.setMomentum(init_momentum.x(), init_momentum.y(), init_momentum.z());
87
88 const G4VProcess* process{track->GetCreatorProcess()};
89 if (process) {
90 const G4String& name{process->GetProcessName()};
91 particle.setProcessType(ldmx::SimParticle::findProcessType(name));
92 } else {
93 if (track->GetParentID() == 0) {
94 particle.setProcessType(ldmx::SimParticle::ProcessType::Primary);
95 } else {
96 particle.setProcessType(ldmx::SimParticle::ProcessType::unknown);
97 }
98 }
99
100 // track's current kinematics is its end point kinematics
101 // because we are assuming this track is being stopped/killed
102
103 auto momentum{track->GetMomentum()};
104 particle.setEndPointMomentum(momentum.x(), momentum.y(), momentum.z());
105
106 auto end_pt{track->GetPosition()};
107 particle.setEndPoint(end_pt.x(), end_pt.y(), end_pt.z());
108}
Class representing a simulated particle.
Definition SimParticle.h:23
void setGenStatus(const int &genStatus)
Set the generator status of this particle.
static ProcessType findProcessType(std::string processName)
Get the process type enum from a G4VProcess name.
static UserTrackInformation * get(const G4Track *track)
get
const G4ThreeVector & getInitialMomentum() const
Get the initial momentum 3-vector of the track [MeV].

References ldmx::SimParticle::findProcessType(), simcore::UserTrackInformation::get(), simcore::UserTrackInformation::getInitialMomentum(), particle_map_, and ldmx::SimParticle::setGenStatus().

Referenced by simcore::g4user::TrackingAction::PostUserTrackingAction().

◆ traceAncestry()

void simcore::TrackMap::traceAncestry ( )

Trace the ancestry for the particles that will be stored.

This should be done at the end of the event before writing the particle map to the event bus and involves looping through the particles that will be saved.

Use [] instead of at() for descendents_ so that if it wasn't previously created, we will just silently create an empty vector and move on.

Definition at line 110 of file TrackMap.cxx.

110 {
111 for (auto& [id, particle] : particle_map_) {
112 particle.addParent(ancestry_.at(id).first);
113
120 for (auto& child : descendents_[id]) {
121 particle.addDaughter(child);
122 }
123 }
124}

References ancestry_, descendents_, and particle_map_.

Member Data Documentation

◆ ancestry_

std::unordered_map<int, std::pair<int, bool> > simcore::TrackMap::ancestry_
private

ancestry map of particles in event (child -> parent)

Primary particles are given a "parent" ID of 0 to reflect that they don't have a parent. This is the default in Geant4 and we assume that holds here.

This is helpful for the findIncident method which looks up through a track's history to find the first ancestor which originated outside of the calorimeter region.

The key value is a pair where the first entry is the parent track ID and the second entry is whether the child track is in the calorimeter region.

See also
isInCalorimeterRegion for how we check if a track originated in the calorimeter region.

Definition at line 132 of file TrackMap.h.

Referenced by clear(), contains(), findIncident(), insert(), isDescendant(), and traceAncestry().

◆ descendents_

std::unordered_map<int, std::vector<int> > simcore::TrackMap::descendents_
private

descendents map of particles in event (parent -> children)

Definition at line 135 of file TrackMap.h.

Referenced by clear(), insert(), and traceAncestry().

◆ particle_map_

std::map<int, ldmx::SimParticle> simcore::TrackMap::particle_map_
private

map of SimParticles that will be stored

Definition at line 138 of file TrackMap.h.

Referenced by clear(), getParticleMap(), isSaved(), save(), and traceAncestry().


The documentation for this class was generated from the following files: