ATLAS Offline Software
Loading...
Searching...
No Matches
TrackProcessorUserActionBase.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5// class header
7
8// ISF includes
11
13
14// ISF Geant4 includes
16
17// Athena includes
20
23#include "MCTruth/TrackHelper.h"
25
27
28// Geant4 includes
29#include "G4ParticleDefinition.hh"
30#include "G4DynamicParticle.hh"
31#include "G4TouchableHistory.hh"
32#include "G4Step.hh"
33#include "G4TransportationManager.hh"
34#include "G4LogicalVolumeStore.hh"
35
36#include "G4EventManager.hh"
37#include "G4Event.hh"
38#include "G4PrimaryParticle.hh"
39
40//#include "G4VPhysicalVolume.hh"
41
42#include <iostream>
43
44namespace G4UA {
45
46namespace iGeant4 {
47
54
56{
57 m_curBaseISP = nullptr;
58 m_atlasG4EvtUserInfo = nullptr;
59 return;
60}
61
63{
64 // get geoID from parent
65 //TODO ELLI AtlasDetDescr::AtlasRegion curGeoID = m_curBaseISP->nextGeoID();
66 //TODO ELLI ATH_MSG_DEBUG( "Currently simulating TrackID = " << aStep->GetTrack()->GetTrackID() <<
67 //TODO ELLI " inside geoID = " << curGeoID );
68
69 // AdePT currently does not return the G4Secondary vector per G4Step. As the vector is required to attach
70 // the G4VUserInfo in the PreUserTrackingAction below, the G4VUserInfo is not available, which
71 // would lead to nullptr access crashes
72#ifdef ATHSIMULATION_USE_ADEPT
73 return;
74#endif
75 //
76 // call the ISFSteppingAction method of the implementation
77 //
79
80 //
81 // propagate the current ISFParticle link to all secondaries
82 //
83 const std::vector<const G4Track*> *secondaryVector = aStep->GetSecondaryInCurrentStep();
84 if (secondaryVector && !secondaryVector->empty()) {
85 for ( auto* aConstSecondaryTrack : *secondaryVector ) {
86 // get a non-const G4Track for current secondary (nasty!)
87 G4Track* aSecondaryTrack ATLAS_THREAD_SAFE = const_cast<G4Track*>( aConstSecondaryTrack ); // imposed by Geant4 interface
88
89 auto *trackInfo = ::iGeant4::ISFG4Helper::getISFTrackInfo(*aSecondaryTrack);
90
91 // G4Tracks aready returned to ISF will have a TrackInformation attached to them
92 bool particleReturnedToISF = trackInfo && trackInfo->GetReturnedToISF();
93 if (!particleReturnedToISF) {
94 HepMC::GenParticlePtr generationZeroGenParticle{};
98 generationZeroGenParticle );
99 }
100 } // <- loop over secondaries from this step
101 }
102
103 return;
104}
105
107{
108 // AdePT currently does not return the G4Secondary vector per G4Step. As the vector is required to attach
109 // the G4VUserInfo below the G4VUserInfo is not available, which would lead to nullptr access crashes
110#ifdef ATHSIMULATION_USE_ADEPT
111 return;
112#endif
113
114 bool isPrimary = ! aTrack->GetParentID();
115 if (isPrimary) {
116 G4Track* nonConstTrack ATLAS_THREAD_SAFE = const_cast<G4Track*> (aTrack); // imposed by Geant4 interface
117 setupPrimary(*nonConstTrack);
118 } else {
119 setupSecondary(*aTrack);
120 }
121
122 return;
123}
124
126{
127 //
128 // Get PrimaryParticleInformation from G4PrimaryParticle (assigned by TransportTool::addPrimaryVertex)
129 //
130
132 if ( trackInfo ) {
133 G4ExceptionDescription description;
134 description << G4String("PreUserTrackingAction: ")
135 << "Started simulation of primary particle which already has a TrackInformation/TrackBarcodeInfo object attached (trackID: "
136 << aTrack.GetTrackID() << ", track pos: "<<aTrack.GetPosition() << ", mom: "<<aTrack.GetMomentum()
137 << ", parentID " << aTrack.GetParentID() << ")";
138 G4Exception("iGeant4::TrackProcessorUserActionBase", "TrackInformationAlreadyExists", FatalException, description);
139 return; // The G4Exception call above should abort the job, but Coverity does not seem to pick this up.
140 }
141
142 auto* primaryPartInfo = dynamic_cast <PrimaryParticleInformation*> (aTrack.GetDynamicParticle()->GetPrimaryParticle()->GetUserInformation());
143 if (!primaryPartInfo) {
144 G4ExceptionDescription description;
145 description << G4String("PreUserTrackingAction: ") + "NULL PrimaryParticleInformation pointer for current G4Step (trackID "
146 << aTrack.GetTrackID() << ", track pos: "<<aTrack.GetPosition() << ", mom: "<<aTrack.GetMomentum()
147 << ", parentID " << aTrack.GetParentID() << ")";
148 G4Exception("iGeant4::TrackProcessorUserActionBase", "NoPPInfo", FatalException, description);
149 return; // The G4Exception call above should abort the job, but Coverity does not seem to pick this up.
150 }
151
152 // get base ISFParticle and link to TrackInformation
153 auto* baseISP = primaryPartInfo->GetISFParticle();
154 if (!baseISP) {
155 G4ExceptionDescription description;
156 description << G4String("PreUserTrackingAction: ") + "No ISFParticle associated with primary particle (trackID: "
157 << aTrack.GetTrackID() << ", track pos: "<<aTrack.GetPosition() << ", mom: "<<aTrack.GetMomentum()
158 << ", parentID " << aTrack.GetParentID() << ")";
159 G4Exception("iGeant4::TrackProcessorUserActionBase", "NoISFParticle", FatalException, description);
160 return; // The G4Exception call above should abort the job, but Coverity does not seem to pick this up.
161 }
162
163 ISF::TruthBinding* truthBinding = baseISP->getTruthBinding();
164 if (!truthBinding) {
165 G4ExceptionDescription description;
166 description << G4String("PreUserTrackingAction: ") + "No ISF::TruthBinding associated with primary particle (trackID: "
167 << aTrack.GetTrackID() << ", track pos: "<<aTrack.GetPosition() << ", mom: "<<aTrack.GetMomentum()
168 << ", parentID " << aTrack.GetParentID() << ")";
169 G4Exception("iGeant4::TrackProcessorUserActionBase", "NoISFTruthBinding", FatalException, description);
170 return; // The G4Exception call above should abort the job, but Coverity does not seem to pick this up.
171 }
172
173 int regenerationNr = primaryPartInfo->GetRegenerationNr();
174
175 HepMC::GenParticlePtr primaryGenParticle = truthBinding->getGenerationZeroGenParticle();
176 HepMC::GenParticlePtr generationZeroGenParticle = truthBinding->getGenerationZeroGenParticle();
177 HepMC::GenParticlePtr currentGenParticle = truthBinding->getCurrentGenParticle();
178
179 auto classification = classify(primaryGenParticle,
180 generationZeroGenParticle,
181 currentGenParticle,
182 regenerationNr);
183
184 auto* newTrackInfo = ::iGeant4::ISFG4Helper::attachTrackInfoToNewG4Track(aTrack,
185 *baseISP,
186 classification,
187 generationZeroGenParticle );
188 newTrackInfo->SetRegenerationNr(regenerationNr);
189
191 primaryGenParticle,
192 currentGenParticle);
193
194 return;
195}
196
198{
200
201 HepMC::GenParticlePtr currentGenParticle = trackInfo->GetCurrentGenParticle();
202 HepMC::GenParticlePtr primaryGenParticle = trackInfo->GetPrimaryGenParticle();
203 ISF::ISFParticle* baseISFParticle = trackInfo->GetBaseISFParticle();
204
205 updateCachedParticleInfo(baseISFParticle, primaryGenParticle, currentGenParticle);
206
207 return;
208}
209
211 HepMC::ConstGenParticlePtr primaryGenParticle,
212 HepMC::GenParticlePtr currentGenParticle)
213{
214 m_curBaseISP = baseISFParticle;
215 m_atlasG4EvtUserInfo->SetCurrentPrimaryGenParticle( primaryGenParticle );
216 m_atlasG4EvtUserInfo->SetCurrentGenParticle( currentGenParticle );
217 return;
218}
219
223 HepMC::ConstGenParticlePtr generationZeroGenParticle,
224 HepMC::ConstGenParticlePtr currentGenParticle,
225 int regenerationNumber) const
226{
227 // if particle points to a non-zero truth particle it can not just be a 'simple' Secondary
228
229 if (currentGenParticle) {
230 if (currentGenParticle == primaryGenParticle) {
232 }
233 else if (generationZeroGenParticle == primaryGenParticle && regenerationNumber>0) {
235 }
236 else {
238 }
239 }
240
242}
243
244
245
247{
248 m_curBaseISP = nullptr;
249 return;
250}
251
253{
254 // For now, just return all particles
257 return result;
258}
259
260} // namespace iGeant4
261
262} // namespace G4UA
Define macros for attributes used to control the static checker.
#define ATLAS_THREAD_SAFE
VTrackInformation::TrackClassification classify(HepMC::ConstGenParticlePtr primaryGenParticle, HepMC::ConstGenParticlePtr generationZeroGenParticle, HepMC::ConstGenParticlePtr currentGenParticle, int regenerationNumber) const
Classify the particle represented by the given set of truth links.
virtual void PreUserTrackingAction(const G4Track *) override
virtual void EndOfEventAction(const G4Event *) override final
virtual void ISFSteppingAction(const G4Step *, ISF::ISFParticle *)=0
This method is called by TrackProcessorUserActionBase after the G4Track->ISFParticle association has ...
virtual void PostUserTrackingAction(const G4Track *) override final
virtual void UserSteppingAction(const G4Step *) override final
void setupPrimary(G4Track &)
Setup the given G4Track as the current primary particle which we'll process.
void setupSecondary(const G4Track &)
Setup the given G4Track as the current secondary particle which we'll process.
void updateCachedParticleInfo(ISF::ISFParticle *baseISFParticle, HepMC::ConstGenParticlePtr primaryGenParticle, HepMC::GenParticlePtr currentGenParticle)
Set the following information as the currently traced particle.
AtlasG4EventUserInfo * m_atlasG4EvtUserInfo
event-global G4 UserInformation
ISF::ISFParticleContainer ReturnSecondaries(ISF::ISFParticle const *parent)
virtual void BeginOfEventAction(const G4Event *) override final
ISF::ISFParticle * m_curBaseISP
The most recent ISFParticle ancestor that triggers the currently processed G4Track.
The generic ISF particle definition,.
Definition ISFParticle.h:42
HepMC::GenParticlePtr getCurrentGenParticle()
pointer to the particle in the simulation truth
HepMC::GenParticlePtr getGenerationZeroGenParticle()
pointer to the simulation truth particle before any regeneration happened (eg.
This class is attached to G4PrimaryParticle objects as UserInformation.
const ISF::ISFParticle * GetISFParticle() const
return a pointer to the ISFParticle used to create the G4PrimaryParticle
static VTrackInformation * getISFTrackInfo(const G4Track &aTrack)
return a valid UserInformation object of the G4Track for use within the ISF
static AtlasG4EventUserInfo * getAtlasG4EventUserInfo()
return pointer to current AtlasG4EventUserInfo
static TrackInformation * attachTrackInfoToNewG4Track(G4Track &aTrack, ISF::ISFParticle &baseIsp, VTrackInformation::TrackClassification classification, HepMC::GenParticlePtr generationZeroGenParticle=nullptr)
attach a new TrackInformation object to the given new (!) G4Track (the G4Track must not have a UserIn...
std::string description
glabal timer - how long have I taken so far?
Definition hcg.cxx:93
HepMC3::GenParticlePtr GenParticlePtr
Definition GenParticle.h:19
HepMC3::ConstGenParticlePtr ConstGenParticlePtr
Definition GenParticle.h:20
std::list< ISF::ISFParticle * > ISFParticleContainer
generic ISFParticle container (not necessarily a std::list!)
void swap(ElementLinkVector< DOBJ > &lhs, ElementLinkVector< DOBJ > &rhs)