ATLAS Offline Software
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
10 #include "ISF_Event/EntryLayer.h"
11 
13 
14 // ISF Geant4 includes
16 
17 // Athena includes
20 
23 #include "MCTruth/TrackHelper.h"
25 
26 #include "StoreGate/StoreGateSvc.h"
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 
44 namespace G4UA {
45 
46 namespace iGeant4 {
47 
49 {
50  m_curBaseISP = nullptr;
52  return;
53 }
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  //
70  // call the ISFSteppingAction method of the implementation
71  //
73 
74  //
75  // propagate the current ISFParticle link to all secondaries
76  //
77  const std::vector<const G4Track*> *secondaryVector = aStep->GetSecondaryInCurrentStep();
78  for ( auto* aConstSecondaryTrack : *secondaryVector ) {
79  // get a non-const G4Track for current secondary (nasty!)
80  G4Track* aSecondaryTrack ATLAS_THREAD_SAFE = const_cast<G4Track*>( aConstSecondaryTrack ); // imposed by Geant4 interface
81 
82  auto *trackInfo = ::iGeant4::ISFG4Helper::getISFTrackInfo(*aSecondaryTrack);
83 
84  // G4Tracks aready returned to ISF will have a TrackInformation attached to them
85  bool particleReturnedToISF = trackInfo && trackInfo->GetReturnedToISF();
86  if (!particleReturnedToISF) {
87  HepMC::GenParticlePtr generationZeroGenParticle{};
89  *m_curBaseISP,
91  generationZeroGenParticle );
92  }
93  } // <- loop over secondaries from this step
94 
95  return;
96 }
97 
99 {
100  bool isPrimary = ! aTrack->GetParentID();
101  if (isPrimary) {
102  G4Track* nonConstTrack ATLAS_THREAD_SAFE = const_cast<G4Track*> (aTrack); // imposed by Geant4 interface
103  setupPrimary(*nonConstTrack);
104  } else {
105  setupSecondary(*aTrack);
106  }
107 
108  return;
109 }
110 
112 {
113  //
114  // Get PrimaryParticleInformation from G4PrimaryParticle (assigned by TransportTool::addPrimaryVertex)
115  //
116 
118  if ( trackInfo ) {
119  G4ExceptionDescription description;
120  description << G4String("PreUserTrackingAction: ")
121  << "Started simulation of primary particle which already has a TrackInformation/TrackBarcodeInfo object attached (trackID: "
122  << aTrack.GetTrackID() << ", track pos: "<<aTrack.GetPosition() << ", mom: "<<aTrack.GetMomentum()
123  << ", parentID " << aTrack.GetParentID() << ")";
124  G4Exception("iGeant4::TrackProcessorUserActionBase", "TrackInformationAlreadyExists", FatalException, description);
125  return; // The G4Exception call above should abort the job, but Coverity does not seem to pick this up.
126  }
127 
128  auto* primaryPartInfo = dynamic_cast <PrimaryParticleInformation*> (aTrack.GetDynamicParticle()->GetPrimaryParticle()->GetUserInformation());
129  if (!primaryPartInfo) {
130  G4ExceptionDescription description;
131  description << G4String("PreUserTrackingAction: ") + "NULL PrimaryParticleInformation pointer for current G4Step (trackID "
132  << aTrack.GetTrackID() << ", track pos: "<<aTrack.GetPosition() << ", mom: "<<aTrack.GetMomentum()
133  << ", parentID " << aTrack.GetParentID() << ")";
134  G4Exception("iGeant4::TrackProcessorUserActionBase", "NoPPInfo", FatalException, description);
135  return; // The G4Exception call above should abort the job, but Coverity does not seem to pick this up.
136  }
137 
138  // get base ISFParticle and link to TrackInformation
139  auto* baseISP = primaryPartInfo->GetISFParticle();
140  if (!baseISP) {
141  G4ExceptionDescription description;
142  description << G4String("PreUserTrackingAction: ") + "No ISFParticle associated with primary particle (trackID: "
143  << aTrack.GetTrackID() << ", track pos: "<<aTrack.GetPosition() << ", mom: "<<aTrack.GetMomentum()
144  << ", parentID " << aTrack.GetParentID() << ")";
145  G4Exception("iGeant4::TrackProcessorUserActionBase", "NoISFParticle", FatalException, description);
146  return; // The G4Exception call above should abort the job, but Coverity does not seem to pick this up.
147  }
148 
149  ISF::TruthBinding* truthBinding = baseISP->getTruthBinding();
150  if (!truthBinding) {
151  G4ExceptionDescription description;
152  description << G4String("PreUserTrackingAction: ") + "No ISF::TruthBinding associated with primary particle (trackID: "
153  << aTrack.GetTrackID() << ", track pos: "<<aTrack.GetPosition() << ", mom: "<<aTrack.GetMomentum()
154  << ", parentID " << aTrack.GetParentID() << ")";
155  G4Exception("iGeant4::TrackProcessorUserActionBase", "NoISFTruthBinding", FatalException, description);
156  return; // The G4Exception call above should abort the job, but Coverity does not seem to pick this up.
157  }
158 
159  int regenerationNr = primaryPartInfo->GetRegenerationNr();
160 
161  HepMC::GenParticlePtr primaryGenParticle = truthBinding->getGenerationZeroGenParticle();
162  HepMC::GenParticlePtr generationZeroGenParticle = truthBinding->getGenerationZeroGenParticle();
163  HepMC::GenParticlePtr currentGenParticle = truthBinding->getCurrentGenParticle();
164 
165  auto classification = classify(primaryGenParticle,
166  generationZeroGenParticle,
167  currentGenParticle,
168  regenerationNr);
169 
170  auto* newTrackInfo = ::iGeant4::ISFG4Helper::attachTrackInfoToNewG4Track(aTrack,
171  *baseISP,
172  classification,
173  generationZeroGenParticle );
174  newTrackInfo->SetRegenerationNr(regenerationNr);
175 
176  updateCachedParticleInfo(baseISP,
177  primaryGenParticle,
178  currentGenParticle);
179 
180  return;
181 }
182 
184 {
186 
187  HepMC::GenParticlePtr currentGenParticle = trackInfo->GetCurrentGenParticle();
188  HepMC::GenParticlePtr primaryGenParticle = trackInfo->GetPrimaryGenParticle();
189  ISF::ISFParticle* baseISFParticle = trackInfo->GetBaseISFParticle();
190 
191  updateCachedParticleInfo(baseISFParticle, primaryGenParticle, currentGenParticle);
192 
193  return;
194 }
195 
197  HepMC::ConstGenParticlePtr primaryGenParticle,
198  HepMC::GenParticlePtr currentGenParticle)
199 {
200  m_curBaseISP = baseISFParticle;
201  m_atlasG4EvtUserInfo->SetCurrentPrimaryGenParticle( primaryGenParticle );
202  m_atlasG4EvtUserInfo->SetCurrentGenParticle( currentGenParticle );
203  return;
204 }
205 
209  HepMC::ConstGenParticlePtr generationZeroGenParticle,
210  HepMC::ConstGenParticlePtr currentGenParticle,
211  int regenerationNumber) const
212 {
213  // if particle points to a non-zero truth particle it can not just be a 'simple' Secondary
214 
215  if (currentGenParticle) {
216  if (currentGenParticle == primaryGenParticle) {
218  }
219  else if (generationZeroGenParticle == primaryGenParticle && regenerationNumber>0) {
221  }
222  else {
224  }
225  }
226 
228 }
229 
230 
231 
233 {
234  m_curBaseISP = nullptr;
235  return;
236 }
237 
239 {
240  // For now, just return all particles
243  return result;
244 }
245 
246 } // namespace iGeant4
247 
248 } // namespace G4UA
ISF::ISFParticleContainer
std::list< ISF::ISFParticle * > ISFParticleContainer
generic ISFParticle container (not necessarily a std::list!)
Definition: ISFParticleContainer.h:23
AtlasG4EventUserInfo::SetCurrentPrimaryGenParticle
void SetCurrentPrimaryGenParticle(HepMC::ConstGenParticlePtr p)
set m_currentPrimaryGenParticle, the pointer to the HepMC::GenParticle used to create the current G4P...
Definition: AtlasG4EventUserInfo.h:53
get_generator_info.result
result
Definition: get_generator_info.py:21
iGeant4::ISFG4Helper::attachTrackInfoToNewG4Track
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...
Definition: ISFG4Helper.cxx:78
G4UA
for nSW
Definition: CalibrationDefaultProcessing.h:19
AtlasRegion.h
ISF::TruthBinding
Definition: TruthBinding.h:18
TrackHelper.h
ISF::TruthBinding::getCurrentGenParticle
HepMC::GenParticlePtr getCurrentGenParticle()
pointer to the particle in the simulation truth
G4UA::iGeant4::TrackProcessorUserActionBase::classify
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.
Definition: TrackProcessorUserActionBase.cxx:208
IParticleBroker.h
HepMC::GenParticlePtr
GenParticle * GenParticlePtr
Definition: GenParticle.h:37
ISF::ISFParticle
Definition: ISFParticle.h:42
G4UA::iGeant4::TrackProcessorUserActionBase::EndOfEventAction
virtual void EndOfEventAction(const G4Event *) override final
Definition: TrackProcessorUserActionBase.cxx:55
G4UA::iGeant4::TrackProcessorUserActionBase::ISFSteppingAction
virtual void ISFSteppingAction(const G4Step *, ISF::ISFParticle *)=0
This method is called by TrackProcessorUserActionBase after the G4Track->ISFParticle association has ...
G4UA::iGeant4::TrackProcessorUserActionBase::BeginOfEventAction
virtual void BeginOfEventAction(const G4Event *) override final
Definition: TrackProcessorUserActionBase.cxx:48
ISFParticle.h
G4UA::iGeant4::TrackProcessorUserActionBase::m_storedSecondaries
ISF::ISFParticleContainer m_storedSecondaries
Definition: TrackProcessorUserActionBase.h:50
EntryLayer.h
ISF::TruthBinding::getGenerationZeroGenParticle
HepMC::GenParticlePtr getGenerationZeroGenParticle()
pointer to the simulation truth particle before any regeneration happened (eg.
TrackProcessorUserActionBase.h
iGeant4::ISFG4Helper::getAtlasG4EventUserInfo
static AtlasG4EventUserInfo * getAtlasG4EventUserInfo()
return pointer to current AtlasG4EventUserInfo
Definition: ISFG4Helper.cxx:114
VTrackInformation::Primary
@ Primary
Definition: VTrackInformation.h:32
G4UA::iGeant4::TrackProcessorUserActionBase::ReturnSecondaries
ISF::ISFParticleContainer ReturnSecondaries(ISF::ISFParticle const *parent)
Definition: TrackProcessorUserActionBase.cxx:238
G4UA::iGeant4::TrackProcessorUserActionBase::setupSecondary
void setupSecondary(const G4Track &)
Setup the given G4Track as the current secondary particle which we'll process.
Definition: TrackProcessorUserActionBase.cxx:183
WriteCalibToCool.swap
swap
Definition: WriteCalibToCool.py:94
trackInfo
Definition: TrigInDetUtils.h:13
G4UA::iGeant4::TrackProcessorUserActionBase::m_atlasG4EvtUserInfo
AtlasG4EventUserInfo * m_atlasG4EvtUserInfo
event-global G4 UserInformation
Definition: TrackProcessorUserActionBase.h:48
HepMC::ConstGenParticlePtr
const GenParticle * ConstGenParticlePtr
Definition: GenParticle.h:38
TrackInformation.h
G4UA::iGeant4::TrackProcessorUserActionBase::updateCachedParticleInfo
void updateCachedParticleInfo(ISF::ISFParticle *baseISFParticle, HepMC::ConstGenParticlePtr primaryGenParticle, HepMC::GenParticlePtr currentGenParticle)
Set the following information as the currently traced particle.
Definition: TrackProcessorUserActionBase.cxx:196
G4UA::iGeant4::TrackProcessorUserActionBase::setupPrimary
void setupPrimary(G4Track &)
Setup the given G4Track as the current primary particle which we'll process.
Definition: TrackProcessorUserActionBase.cxx:111
PrimaryParticleInformation
This class is attached to G4PrimaryParticle objects as UserInformation. The member variable m_thePart...
Definition: PrimaryParticleInformation.h:39
G4UA::iGeant4::TrackProcessorUserActionBase::PostUserTrackingAction
virtual void PostUserTrackingAction(const G4Track *) override final
Definition: TrackProcessorUserActionBase.cxx:232
AtlasG4EventUserInfo.h
VTrackInformation::TrackClassification
TrackClassification
Definition: VTrackInformation.h:32
PrimaryParticleInformation.h
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
G4UA::iGeant4::TrackProcessorUserActionBase::PreUserTrackingAction
virtual void PreUserTrackingAction(const G4Track *) override
Definition: TrackProcessorUserActionBase.cxx:98
G4UA::iGeant4::TrackProcessorUserActionBase::m_curBaseISP
ISF::ISFParticle * m_curBaseISP
The most recent ISFParticle ancestor that triggers the currently processed G4Track.
Definition: TrackProcessorUserActionBase.h:75
G4UA::iGeant4::TrackProcessorUserActionBase::UserSteppingAction
virtual void UserSteppingAction(const G4Step *) override final
Definition: TrackProcessorUserActionBase.cxx:62
ISFG4Helper.h
VTrackInformation::Secondary
@ Secondary
Definition: VTrackInformation.h:32
iGeant4::ISFG4Helper::getISFTrackInfo
static VTrackInformation * getISFTrackInfo(const G4Track &aTrack)
return a valid UserInformation object of the G4Track for use within the ISF
Definition: ISFG4Helper.cxx:69
checker_macros.h
Define macros for attributes used to control the static checker.
AtlasG4EventUserInfo::SetCurrentGenParticle
void SetCurrentGenParticle(HepMC::GenParticlePtr p)
set m_currentGenParticle, the pointer to the GenParticle corresponding to the current G4Track.
Definition: AtlasG4EventUserInfo.h:67
StoreGateSvc.h
VTrackInformation::RegisteredSecondary
@ RegisteredSecondary
Definition: VTrackInformation.h:32
VTrackInformation::RegeneratedPrimary
@ RegeneratedPrimary
Definition: VTrackInformation.h:32
egammaParameters::isPrimary
@ isPrimary
true if matched track has a hit in first or second pixel layer
Definition: egammaParamDefs.h:225
description
std::string description
glabal timer - how long have I taken so far?
Definition: hcg.cxx:88
iGeant4
Definition: Geant4TruthIncident.h:35