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, 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 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.
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 34 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 42 of file MCTruthSteppingAction.h.

Constructor & Destructor Documentation

◆ MCTruthSteppingAction()

G4UA::MCTruthSteppingAction::MCTruthSteppingAction ( const VolumeCollectionMap_t & collMap,
int secondarySavingLevel,
int subDetVolLevel,
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 25 of file MCTruthSteppingAction.cxx.

30 : AthMessaging(msgSvc, "MCTruthSteppingAction"),
31 m_isInitialized(false),
32 m_secondarySavingLevel(secondarySavingLevel),
33 m_subDetVolLevel(subDetVolLevel),
34 m_volumeCollectionMap(volCollMap)
35 {
36 msg().setLevel(level);
37 }
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.
VolumeCollectionMap_t m_volumeCollectionMap
Map of volume name to output collection name.

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 74 of file MCTruthSteppingAction.cxx.

75 {
76 // First time initialization
77 if(!m_isInitialized) {
79 if (m_recordingEnvelopes.size() == 0) {
80 ATH_MSG_WARNING("No recording envelopes found!");
81 }
82 m_isInitialized = true;
83 }
84 // Every event initialization
85 for (auto& recEnvelope : m_recordingEnvelopes) {
86 if(auto* eventInfo = static_cast<AtlasG4EventUserInfo*>( event->GetUserInformation())){
87 recEnvelope.BeginOfEvent(eventInfo->GetHitCollectionMap()->Find<TrackRecordCollection>(recEnvelope.GetTrackRecordCollectionName()));
88 }
89 }
90 }
#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.

◆ 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)

◆ 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 157 of file MCTruthSteppingAction.cxx.

158 {
159 if (!aStep) {
160 return;
161 }
162
163 TrackHelper parentHelper(aStep->GetTrack());
164 TrackInformation* parentInfo = parentHelper.GetTrackInformation();
165 HepMC::GenParticlePtr primaryGenParticle = parentInfo ? parentInfo->GetPrimaryGenParticle() : nullptr;
166 if (!primaryGenParticle) {
167 return;
168 }
169
170 const std::vector<const G4Track*>* secondaries = aStep->GetSecondaryInCurrentStep();
171 if (!secondaries) {
172 return;
173 }
174
175 for (const G4Track* secondary : *secondaries) {
176 if (!secondary || secondary->GetUserInformation()) {
177 continue;
178 }
179
180 auto trackInfo = std::make_unique<TrackInformation>();
181 trackInfo->SetPrimaryGenParticle(primaryGenParticle);
182 trackInfo->SetClassification(TrackInformation::Secondary);
183 secondary->SetUserInformation(trackInfo.release());
184 }
185 }
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 42 of file MCTruthSteppingAction.cxx.

43 {
44 ATH_MSG_DEBUG("Setting up " << m_volumeCollectionMap.size() <<
45 " recording envelopes:");
47 for(const auto& volCollPair : m_volumeCollectionMap) {
48 ATH_MSG_DEBUG(" " << volCollPair.first << ", " << volCollPair.second);
49
50 // Construct the helper in place on the vector
51 m_recordingEnvelopes.emplace_back(volCollPair.first, volCollPair.second);
52 RecordingEnvelope& recEnvelope = m_recordingEnvelopes.back();
53
54 // Make sure the RecEnvelope can initialize properly
55 if(!recEnvelope.Initialize()) {
56 //FIXME - should this be an error?
57 ATH_MSG_WARNING("Envelope volume " << recEnvelope.GetVolumeName() <<
58 " not found in geometry!");
59 ATH_MSG_WARNING("TrackRecordCollection " <<
60 recEnvelope.GetTrackRecordCollectionName() <<
61 " will NOT be recorded");
62 // Throw away uninitialized RecordingEnvelope
63 m_recordingEnvelopes.pop_back();
64 }
65 }
66 if (m_recordingEnvelopes.size() == 0) {
67 ATH_MSG_WARNING("No recording envelopes found!");
68 }
69 }
#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 95 of file MCTruthSteppingAction.cxx.

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

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_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 70 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 82 of file MCTruthSteppingAction.h.

◆ m_secondarySavingLevel

int G4UA::MCTruthSteppingAction::m_secondarySavingLevel
private

The saving level for secondaries.

Definition at line 73 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 76 of file MCTruthSteppingAction.h.

◆ m_volumeCollectionMap

VolumeCollectionMap_t G4UA::MCTruthSteppingAction::m_volumeCollectionMap
private

Map of volume name to output collection name.

Definition at line 79 of file MCTruthSteppingAction.h.


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