ATLAS Offline Software
MCTruthSteppingAction.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include "MCTruth/TrackHelper.h"
7 
8 #include "G4Step.hh"
9 #include "G4StepPoint.hh"
10 #include "G4TouchableHistory.hh"
11 
12 
13 namespace G4UA
14 {
15 
16  //---------------------------------------------------------------------------
17  // Constructor
18  //---------------------------------------------------------------------------
21  IMessageSvc* msgSvc, MSG::Level level)
22  : AthMessaging(msgSvc, "MCTruthSteppingAction"),
23  m_isInitialized(false),
24  m_volumeCollectionMap(volCollMap)
25  {
26  msg().setLevel(level);
27  }
28 
29  //---------------------------------------------------------------------------
30  // Setup the recording envelopes
31  //---------------------------------------------------------------------------
33  {
34  ATH_MSG_DEBUG("Setting up " << m_volumeCollectionMap.size() <<
35  " recording envelopes:");
37  for(const auto& volCollPair : m_volumeCollectionMap) {
38  ATH_MSG_DEBUG(" " << volCollPair.first << ", " << volCollPair.second);
39 
40  // Construct the helper in place on the vector
41  m_recordingEnvelopes.emplace_back(volCollPair.first, volCollPair.second);
42  RecordingEnvelope& recEnvelope = m_recordingEnvelopes.back();
43 
44  // Make sure the RecEnvelope can initialize properly
45  if(!recEnvelope.Initialize()) {
46  //FIXME - should this be an error?
47  ATH_MSG_WARNING("Envelope volume " << recEnvelope.GetVolumeName() <<
48  " not found in geometry!");
49  ATH_MSG_WARNING("TrackRecordCollection " <<
50  recEnvelope.GetTrackRecordCollectionName() <<
51  " will NOT be recorded");
52  // Throw away uninitialized RecordingEnvelope
53  m_recordingEnvelopes.pop_back();
54  }
55  }
56  if (m_recordingEnvelopes.size() == 0) {
57  ATH_MSG_WARNING("No recording envelopes found!");
58  }
59  }
60 
61  //---------------------------------------------------------------------------
62  // Beginning of event
63  //---------------------------------------------------------------------------
65  {
66  // First time initialization
67  if(!m_isInitialized) {
69  if (m_recordingEnvelopes.size() == 0) {
70  ATH_MSG_WARNING("No recording envelopes found!");
71  }
72  m_isInitialized = true;
73  }
74  // Every event initialization
75  for (auto& recEnvelope : m_recordingEnvelopes) {
76  recEnvelope.BeginOfEvent();
77  }
78  }
79 
80  //---------------------------------------------------------------------------
81  // Process one tracking step
82  //---------------------------------------------------------------------------
84  {
85  if (m_recordingEnvelopes.size() == 0) return;
86  TrackHelper trackHelper(aStep->GetTrack());
87  if (trackHelper.IsSecondary()) return;
88 
89  G4StepPoint* preStep = aStep->GetPreStepPoint();
90  G4StepPoint* postStep = aStep->GetPostStepPoint();
91 
92  G4VPhysicalVolume* preVol = preStep->GetPhysicalVolume();
93  G4VPhysicalVolume* postVol = postStep->GetPhysicalVolume();
94 
95  if (preVol == postVol) return;
96 
97  const G4TouchableHistory* preTHist = static_cast<const G4TouchableHistory*>(preStep->GetTouchable());
98  const G4TouchableHistory* postTHist = static_cast<const G4TouchableHistory*>(postStep->GetTouchable());
99  const int preStepVolDepth = preTHist->GetHistoryDepth();
100  const int postStepVolDepth = postTHist->GetHistoryDepth();
101 
102  for (auto& recEnvelope : m_recordingEnvelopes)
103  {
104  const int envelopeLevel = recEnvelope.GetLevel();
105  if (envelopeLevel <= preStepVolDepth)
106  {
107  //NB preTHist->GetVolume(preStepVolDepth) would just give us the World volume.
108  const G4LogicalVolume* logicalVolume1 =
109  preTHist->GetVolume(preStepVolDepth-envelopeLevel)->GetLogicalVolume();
110  if (logicalVolume1 != recEnvelope.GetLogicalVolume()) continue;
111 
112  if (envelopeLevel <= postStepVolDepth &&
113  logicalVolume1 == postTHist->GetVolume(postStepVolDepth-envelopeLevel)
114  ->GetLogicalVolume())
115  {
116  continue;
117  }
118 
119  // We have a track crossing a recording envelope
120  // volume boundary, so make a TrackRecord
121  recEnvelope.AddTrackRecord(aStep);
122 
123  // Done with this volume.
124  break;
125  }
126  }
127  }
128 
129 } // namespace G4UA
RecordingEnvelope::GetVolumeName
std::string GetVolumeName() const
Returns the name of the recording envelope volume.
Definition: RecordingEnvelope.h:83
G4UA
for nSW
Definition: CalibrationDefaultProcessing.h:19
TrackHelper.h
G4UA::MCTruthSteppingAction::m_recordingEnvelopes
std::vector< RecordingEnvelope > m_recordingEnvelopes
List of RecordingEnvelope helpers to invoke.
Definition: MCTruthSteppingAction.h:71
G4UA::MCTruthSteppingAction::MCTruthSteppingAction
MCTruthSteppingAction(const VolumeCollectionMap_t &collMap, IMessageSvc *msgSvc, MSG::Level level)
Construct the action with specified volumes and output collections.
Definition: MCTruthSteppingAction.cxx:20
G4UA::MCTruthSteppingAction::VolumeCollectionMap_t
std::map< std::string, std::string > VolumeCollectionMap_t
Map of volume name to output TrackRecordCollection name.
Definition: MCTruthSteppingAction.h:42
G4UA::MCTruthSteppingAction::UserSteppingAction
virtual void UserSteppingAction(const G4Step *) override final
Process one particle step.
Definition: MCTruthSteppingAction.cxx:83
python.iconfTool.models.loaders.level
level
Definition: loaders.py:20
MCTruthSteppingAction.h
TrackHelper
Definition: TrackHelper.h:14
TrigConf::MSGTC::Level
Level
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:21
G4UA::MCTruthSteppingAction::m_volumeCollectionMap
VolumeCollectionMap_t m_volumeCollectionMap
Map of volume name to output collection name.
Definition: MCTruthSteppingAction.h:68
G4UA::MCTruthSteppingAction::setupRecEnvelopes
void setupRecEnvelopes()
Setup the list of RecordingEnvelope helpers.
Definition: MCTruthSteppingAction.cxx:32
StdJOSetup.msgSvc
msgSvc
Provide convenience handles for various services.
Definition: StdJOSetup.py:36
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
AthMessaging::msg
MsgStream & msg() const
The standard message stream.
Definition: AthMessaging.h:164
TrackHelper::IsSecondary
bool IsSecondary() const
Definition: TrackHelper.cxx:30
G4UA::MCTruthSteppingAction::BeginOfEventAction
virtual void BeginOfEventAction(const G4Event *) override final
Called at the start of each G4 event.
Definition: MCTruthSteppingAction.cxx:64
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
RecordingEnvelope::Initialize
bool Initialize()
Finds the pointer to the G4LogicalVolume called m_envelopeVolumeName and the number of levels beneath...
Definition: RecordingEnvelope.cxx:54
RecordingEnvelope::GetTrackRecordCollectionName
std::string GetTrackRecordCollectionName() const
Returns the name of the TrackRecordCollection to which tracks crossing this recording envelope should...
Definition: RecordingEnvelope.h:93
RecordingEnvelope
Responsible for finding the G4LogicalVolume pointer for each recording envelope and for creating and ...
Definition: RecordingEnvelope.h:25
G4UA::MCTruthSteppingAction::m_isInitialized
bool m_isInitialized
Used to delay initialization until the event loop, after geo is ready.
Definition: MCTruthSteppingAction.h:65