ATLAS Offline Software
Loading...
Searching...
No Matches
G4UA::MCTruthSteppingAction Class Reference

User action which handles recording-envelope truth tracks. More...

#include <MCTruthSteppingAction.h>

Inheritance diagram for G4UA::MCTruthSteppingAction:
Collaboration diagram for G4UA::MCTruthSteppingAction:

Public Types

using VolumeCollectionMap_t = std::map<std::string, std::string>
 Map of volume name to output TrackRecordCollection name.

Public Member Functions

 MCTruthSteppingAction (const VolumeCollectionMap_t &collMap, int secondarySavingLevel, int subDetVolLevel, ISF::ITruthSvc &truthRecordSvc, ISF::IGeoIDSvc &geoIDSvc, IMessageSvc *msgSvc, MSG::Level level)
 Construct the action with specified volumes and output collections.
virtual void BeginOfEventAction (const G4Event *) override final
 Called at the start of each G4 event.
virtual void UserSteppingAction (const G4Step *) override final
 Process one particle step.
bool msgLvl (const MSG::Level lvl) const
 Test the output level.
MsgStream & msg () const
 The standard message stream.
MsgStream & msg (const MSG::Level lvl) const
 The standard message stream.
void setLevel (MSG::Level lvl)
 Change the current logging level.

Private Member Functions

void setupRecEnvelopes ()
 Setup the list of RecordingEnvelope helpers.
void propagatePrimaryInfoToSecondaries (const G4Step *) const
 Propagate parent primary truth attribution to ordinary secondaries.
void createTruthIncident (const G4Step *) const
 Create and register a truth incident for the current step.
void initMessaging () const
 Initialize our message level and MessageSvc.

Private Attributes

bool m_isInitialized
 Used to delay initialization until the event loop, after geo is ready.
int m_secondarySavingLevel
 The saving level for secondaries.
int m_subDetVolLevel
 The level in the G4 volume hierarchy at which we find the sub-detector.
ISF::ITruthSvcm_truthRecordSvc
 ISF Truth Service used to register truth incidents.
ISF::IGeoIDSvcm_geoIDSvc
 ISF GeoID Service used to identify the next detector region.
VolumeCollectionMap_t m_volumeCollectionMap
 Map of volume name to output collection name.
std::vector< RecordingEnvelopem_recordingEnvelopes
 List of RecordingEnvelope helpers to invoke.
std::string m_nm
 Message source name.
boost::thread_specific_ptr< MsgStream > m_msg_tls
 MsgStream instance (a std::cout like with print-out levels).
std::atomic< IMessageSvc * > m_imsg { nullptr }
 MessageSvc pointer.
std::atomic< MSG::Level > m_lvl { MSG::NIL }
 Current logging level.
std::atomic_flag m_initialized ATLAS_THREAD_SAFE = ATOMIC_FLAG_INIT
 Messaging initialized (initMessaging).

Detailed Description

User action which handles recording-envelope truth tracks.

This user action utilizes RecordingEnvelope objects to save truth tracks at entry/exit layers of certain configured detector layers.

Definition at line 38 of file MCTruthSteppingAction.h.

Member Typedef Documentation

◆ VolumeCollectionMap_t

using G4UA::MCTruthSteppingAction::VolumeCollectionMap_t = std::map<std::string, std::string>

Map of volume name to output TrackRecordCollection name.

Definition at line 46 of file MCTruthSteppingAction.h.

Constructor & Destructor Documentation

◆ MCTruthSteppingAction()

G4UA::MCTruthSteppingAction::MCTruthSteppingAction ( const VolumeCollectionMap_t & collMap,
int secondarySavingLevel,
int subDetVolLevel,
ISF::ITruthSvc & truthRecordSvc,
ISF::IGeoIDSvc & geoIDSvc,
IMessageSvc * msgSvc,
MSG::Level level )

Construct the action with specified volumes and output collections.

Parameters
[in]collMapA map of recording envelope volume name to output TrackRecordCollection name.

Definition at line 28 of file MCTruthSteppingAction.cxx.

35 : AthMessaging(msgSvc, "MCTruthSteppingAction"),
36 m_isInitialized(false),
37 m_secondarySavingLevel(secondarySavingLevel),
38 m_subDetVolLevel(subDetVolLevel),
39 m_truthRecordSvc(truthRecordSvc),
40 m_geoIDSvc(geoIDSvc),
41 m_volumeCollectionMap(volCollMap)
42 {
43 msg().setLevel(level);
44 }
AthMessaging()
Default constructor:
MsgStream & msg() const
The standard message stream.
bool m_isInitialized
Used to delay initialization until the event loop, after geo is ready.
int m_subDetVolLevel
The level in the G4 volume hierarchy at which we find the sub-detector.
int m_secondarySavingLevel
The saving level for secondaries.
ISF::IGeoIDSvc & m_geoIDSvc
ISF GeoID Service used to identify the next detector region.
VolumeCollectionMap_t m_volumeCollectionMap
Map of volume name to output collection name.
ISF::ITruthSvc & m_truthRecordSvc
ISF Truth Service used to register truth incidents.

Member Function Documentation

◆ BeginOfEventAction()

void G4UA::MCTruthSteppingAction::BeginOfEventAction ( const G4Event * event)
finaloverridevirtual

Called at the start of each G4 event.

Used to ensure that the TrackRecordCollection WriteHandles are valid.

Definition at line 81 of file MCTruthSteppingAction.cxx.

82 {
83 // First time initialization
84 if(!m_isInitialized) {
86 if (m_recordingEnvelopes.size() == 0) {
87 ATH_MSG_WARNING("No recording envelopes found!");
88 }
89 m_isInitialized = true;
90 }
91 // Every event initialization
92 for (auto& recEnvelope : m_recordingEnvelopes) {
93 if(auto* eventInfo = static_cast<AtlasG4EventUserInfo*>( event->GetUserInformation())){
94 recEnvelope.BeginOfEvent(eventInfo->GetHitCollectionMap()->Find<TrackRecordCollection>(recEnvelope.GetTrackRecordCollectionName()));
95 }
96 }
97 }
#define ATH_MSG_WARNING(x,...)
AtlasHitsVector< TrackRecord > TrackRecordCollection
void setupRecEnvelopes()
Setup the list of RecordingEnvelope helpers.
std::vector< RecordingEnvelope > m_recordingEnvelopes
List of RecordingEnvelope helpers to invoke.

◆ createTruthIncident()

void G4UA::MCTruthSteppingAction::createTruthIncident ( const G4Step * aStep) const
private

Create and register a truth incident for the current step.

Definition at line 162 of file MCTruthSteppingAction.cxx.

163 {
164 const AtlasDetDescr::AtlasRegion geoID =
166 iGeant4::Geant4TruthIncident truth(aStep, geoID);
167 m_truthRecordSvc.registerTruthIncident(truth);
168 }
static AtlasDetDescr::AtlasRegion nextGeoId(const G4Step *aStep, int truthVolLevel, ISF::IGeoIDSvc *geoIDSvc)
AtlasRegion
A simple enum of ATLAS regions and sub-detectors.
Definition AtlasRegion.h:21

◆ initMessaging()

void AthMessaging::initMessaging ( ) const
privateinherited

Initialize our message level and MessageSvc.

This method should only be called once.

Definition at line 39 of file AthMessaging.cxx.

40{
42 // If user did not set an explicit level, set a default
43 if (m_lvl == MSG::NIL) {
44 m_lvl = m_imsg ?
45 static_cast<MSG::Level>( m_imsg.load()->outputLevel(m_nm) ) :
46 MSG::INFO;
47 }
48}
std::string m_nm
Message source name.
std::atomic< IMessageSvc * > m_imsg
MessageSvc pointer.
std::atomic< MSG::Level > m_lvl
Current logging level.
IMessageSvc * getMessageSvc(bool quiet=false)
int outputLevel(const IMessageSvc *ims, const std::string &source)

◆ msg() [1/2]

MsgStream & AthMessaging::msg ( ) const
inlineinherited

The standard message stream.

Returns a reference to the default message stream May not be invoked before sysInitialize() has been invoked.

Definition at line 167 of file AthMessaging.h.

168{
169 MsgStream* ms = m_msg_tls.get();
170 if (!ms) {
171 if (!m_initialized.test_and_set()) initMessaging();
172 ms = new MsgStream(m_imsg,m_nm);
173 m_msg_tls.reset( ms );
174 }
175
176 ms->setLevel (m_lvl);
177 return *ms;
178}
boost::thread_specific_ptr< MsgStream > m_msg_tls
MsgStream instance (a std::cout like with print-out levels).
void initMessaging() const
Initialize our message level and MessageSvc.

◆ msg() [2/2]

MsgStream & AthMessaging::msg ( const MSG::Level lvl) const
inlineinherited

The standard message stream.

Returns a reference to the default message stream May not be invoked before sysInitialize() has been invoked.

Definition at line 182 of file AthMessaging.h.

183{ return msg() << lvl; }

◆ msgLvl()

bool AthMessaging::msgLvl ( const MSG::Level lvl) const
inlineinherited

Test the output level.

Parameters
lvlThe message level to test against
Returns
boolean Indicating if messages at given level will be printed
Return values
trueMessages at level "lvl" will be printed

Definition at line 151 of file AthMessaging.h.

152{
153 // If user did not set explicit message level we have to initialize
154 // the messaging and retrieve the default via the MessageSvc.
155 if (m_lvl==MSG::NIL && !m_initialized.test_and_set()) initMessaging();
156
157 if (m_lvl <= lvl) {
158 msg() << lvl;
159 return true;
160 } else {
161 return false;
162 }
163}

◆ propagatePrimaryInfoToSecondaries()

void G4UA::MCTruthSteppingAction::propagatePrimaryInfoToSecondaries ( const G4Step * aStep) const
private

Propagate parent primary truth attribution to ordinary secondaries.

Definition at line 170 of file MCTruthSteppingAction.cxx.

171 {
172 if (!aStep) {
173 return;
174 }
175
176 TrackHelper parentHelper(aStep->GetTrack());
177 TrackInformation* parentInfo = parentHelper.GetTrackInformation();
178 HepMC::GenParticlePtr primaryGenParticle = parentInfo ? parentInfo->GetPrimaryGenParticle() : nullptr;
179 if (!primaryGenParticle) {
180 return;
181 }
182
183 const std::vector<const G4Track*>* secondaries = aStep->GetSecondaryInCurrentStep();
184 if (!secondaries) {
185 return;
186 }
187
188 for (const G4Track* secondary : *secondaries) {
189 if (!secondary || secondary->GetUserInformation()) {
190 continue;
191 }
192
193 auto trackInfo = std::make_unique<TrackInformation>();
194 trackInfo->SetPrimaryGenParticle(primaryGenParticle);
195 trackInfo->SetClassification(TrackInformation::Secondary);
196 secondary->SetUserInformation(trackInfo.release());
197 }
198 }
HepMC::ConstGenParticlePtr GetPrimaryGenParticle() const
return a pointer to the GenParticle used to create the initial G4PrimaryParticle from which the curre...
HepMC3::GenParticlePtr GenParticlePtr
Definition GenParticle.h:19

◆ setLevel()

void AthMessaging::setLevel ( MSG::Level lvl)
inherited

Change the current logging level.

Use this rather than msg().setLevel() for proper operation with MT.

Definition at line 28 of file AthMessaging.cxx.

29{
30 m_lvl = lvl;
31}

◆ setupRecEnvelopes()

void G4UA::MCTruthSteppingAction::setupRecEnvelopes ( )
private

Setup the list of RecordingEnvelope helpers.

Definition at line 49 of file MCTruthSteppingAction.cxx.

50 {
51 ATH_MSG_DEBUG("Setting up " << m_volumeCollectionMap.size() <<
52 " recording envelopes:");
54 for(const auto& volCollPair : m_volumeCollectionMap) {
55 ATH_MSG_DEBUG(" " << volCollPair.first << ", " << volCollPair.second);
56
57 // Construct the helper in place on the vector
58 m_recordingEnvelopes.emplace_back(volCollPair.first, volCollPair.second);
59 RecordingEnvelope& recEnvelope = m_recordingEnvelopes.back();
60
61 // Make sure the RecEnvelope can initialize properly
62 if(!recEnvelope.Initialize()) {
63 //FIXME - should this be an error?
64 ATH_MSG_WARNING("Envelope volume " << recEnvelope.GetVolumeName() <<
65 " not found in geometry!");
66 ATH_MSG_WARNING("TrackRecordCollection " <<
67 recEnvelope.GetTrackRecordCollectionName() <<
68 " will NOT be recorded");
69 // Throw away uninitialized RecordingEnvelope
70 m_recordingEnvelopes.pop_back();
71 }
72 }
73 if (m_recordingEnvelopes.size() == 0) {
74 ATH_MSG_WARNING("No recording envelopes found!");
75 }
76 }
#define ATH_MSG_DEBUG(x,...)
const std::string & GetTrackRecordCollectionName() const
Returns the name of the TrackRecordCollection to which tracks crossing this recording envelope should...
const std::string & GetVolumeName() const
Returns the name of the recording envelope volume.
bool Initialize()
Finds the pointer to the G4LogicalVolume called m_envelopeVolumeName and the number of levels beneath...

◆ UserSteppingAction()

void G4UA::MCTruthSteppingAction::UserSteppingAction ( const G4Step * aStep)
finaloverridevirtual

Process one particle step.

If the step crosses a recording envelope volume boundary, passes the step to the corresponding RecordingEnvelope to add a TrackRecord.

Definition at line 102 of file MCTruthSteppingAction.cxx.

103 {
104 TrackHelper trackHelper(aStep->GetTrack());
105 const std::vector<const G4Track*>* secondaries = aStep->GetSecondaryInCurrentStep();
106
107 // info must be propagated to secondaries before MC truth incident can be created
109
110 // A saved primary is reclassified, but its old trajectory stayed active.
111 const bool processTruth =
112 trackHelper.IsPrimary() || trackHelper.IsRegeneratedPrimary() ||
113 (trackHelper.IsRegisteredSecondary() && m_secondarySavingLevel > 1);
114
115 if (secondaries && !secondaries->empty() && processTruth) {
116 createTruthIncident(aStep);
117 }
118
119 if (m_recordingEnvelopes.size() == 0) return;
120 if (trackHelper.IsSecondary()) return;
121
122 G4StepPoint* preStep = aStep->GetPreStepPoint();
123 G4StepPoint* postStep = aStep->GetPostStepPoint();
124
125 G4VPhysicalVolume* preVol = preStep->GetPhysicalVolume();
126 G4VPhysicalVolume* postVol = postStep->GetPhysicalVolume();
127
128 if (preVol == postVol) return;
129
130 const G4TouchableHistory* preTHist = static_cast<const G4TouchableHistory*>(preStep->GetTouchable());
131 const G4TouchableHistory* postTHist = static_cast<const G4TouchableHistory*>(postStep->GetTouchable());
132 const int preStepVolDepth = preTHist->GetHistoryDepth();
133 const int postStepVolDepth = postTHist->GetHistoryDepth();
134
135 for (auto& recEnvelope : m_recordingEnvelopes)
136 {
137 const int envelopeLevel = recEnvelope.GetLevel();
138 if (envelopeLevel <= preStepVolDepth)
139 {
140 //NB preTHist->GetVolume(preStepVolDepth) would just give us the World volume.
141 const G4LogicalVolume* logicalVolume1 =
142 preTHist->GetVolume(preStepVolDepth-envelopeLevel)->GetLogicalVolume();
143 if (logicalVolume1 != recEnvelope.GetLogicalVolume()) continue;
144
145 if (envelopeLevel <= postStepVolDepth &&
146 logicalVolume1 == postTHist->GetVolume(postStepVolDepth-envelopeLevel)
147 ->GetLogicalVolume())
148 {
149 continue;
150 }
151
152 // We have a track crossing a recording envelope
153 // volume boundary, so make a TrackRecord
154 recEnvelope.AddTrackRecord(aStep);
155
156 // Done with this volume.
157 break;
158 }
159 }
160 }
void propagatePrimaryInfoToSecondaries(const G4Step *) const
Propagate parent primary truth attribution to ordinary secondaries.
void createTruthIncident(const G4Step *) const
Create and register a truth incident for the current step.

Member Data Documentation

◆ ATLAS_THREAD_SAFE

std::atomic_flag m_initialized AthMessaging::ATLAS_THREAD_SAFE = ATOMIC_FLAG_INIT
mutableprivateinherited

Messaging initialized (initMessaging).

Definition at line 141 of file AthMessaging.h.

◆ m_geoIDSvc

ISF::IGeoIDSvc& G4UA::MCTruthSteppingAction::m_geoIDSvc
private

ISF GeoID Service used to identify the next detector region.

Definition at line 91 of file MCTruthSteppingAction.h.

◆ m_imsg

std::atomic<IMessageSvc*> AthMessaging::m_imsg { nullptr }
mutableprivateinherited

MessageSvc pointer.

Definition at line 135 of file AthMessaging.h.

135{ nullptr };

◆ m_isInitialized

bool G4UA::MCTruthSteppingAction::m_isInitialized
private

Used to delay initialization until the event loop, after geo is ready.

Definition at line 79 of file MCTruthSteppingAction.h.

◆ m_lvl

std::atomic<MSG::Level> AthMessaging::m_lvl { MSG::NIL }
mutableprivateinherited

Current logging level.

Definition at line 138 of file AthMessaging.h.

138{ MSG::NIL };

◆ m_msg_tls

boost::thread_specific_ptr<MsgStream> AthMessaging::m_msg_tls
mutableprivateinherited

MsgStream instance (a std::cout like with print-out levels).

Definition at line 132 of file AthMessaging.h.

◆ m_nm

std::string AthMessaging::m_nm
privateinherited

Message source name.

Definition at line 129 of file AthMessaging.h.

◆ m_recordingEnvelopes

std::vector<RecordingEnvelope> G4UA::MCTruthSteppingAction::m_recordingEnvelopes
private

List of RecordingEnvelope helpers to invoke.

Definition at line 97 of file MCTruthSteppingAction.h.

◆ m_secondarySavingLevel

int G4UA::MCTruthSteppingAction::m_secondarySavingLevel
private

The saving level for secondaries.

Definition at line 82 of file MCTruthSteppingAction.h.

◆ m_subDetVolLevel

int G4UA::MCTruthSteppingAction::m_subDetVolLevel
private

The level in the G4 volume hierarchy at which we find the sub-detector.

Definition at line 85 of file MCTruthSteppingAction.h.

◆ m_truthRecordSvc

ISF::ITruthSvc& G4UA::MCTruthSteppingAction::m_truthRecordSvc
private

ISF Truth Service used to register truth incidents.

Definition at line 88 of file MCTruthSteppingAction.h.

◆ m_volumeCollectionMap

VolumeCollectionMap_t G4UA::MCTruthSteppingAction::m_volumeCollectionMap
private

Map of volume name to output collection name.

Definition at line 94 of file MCTruthSteppingAction.h.


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