ATLAS Offline Software
VerboseSelector.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 #include "VerboseSelector.h"
6 
8 #include "MCTruth/TrackHelper.h"
9 
10 #include "G4EventManager.hh"
11 #include "G4LogicalVolume.hh"
12 #include "G4PropagatorInField.hh"
13 #include "G4RunManagerKernel.hh"
14 #include "G4Step.hh"
15 #include "G4Event.hh"
16 #include "G4ThreeVector.hh"
17 #include "G4Track.hh"
18 #include "G4TransportationManager.hh"
19 #include "G4VProcess.hh"
20 
21 #include <iostream>
22 
23 #include "GaudiKernel/Bootstrap.h"
24 #include "GaudiKernel/ISvcLocator.h"
25 #include "GaudiKernel/IMessageSvc.h"
26 // to retrieve the event number
28 #include "StoreGate/ReadHandle.h"
29 
30 namespace G4UA
31 {
32 
34  AthMessaging(Gaudi::svcLocator()->service< IMessageSvc >( "MessageSvc" ),"VerboseSelector"),
35  m_evtStore("StoreGateSvc/StoreGateSvc", "VerboseSelector"),
36  m_detStore("StoreGateSvc/DetectorStore", "LooperKiller"),
37  m_config(config),m_evtCount(0)
38  {}
39 
41  {
42  SG::ReadHandle<xAOD::EventInfo> eic("EventInfo");
43  if (!eic.isValid()){
44  ATH_MSG_WARNING( "Failed to retrieve EventInfo" );
45  } else {
46  m_evtCount = eic->eventNumber();
47  }
48  }
49 
50  void VerboseSelector::UserSteppingAction(const G4Step* aStep)
51  {
53 
54  const G4ThreeVector& myPos = aStep->GetPostStepPoint()->GetPosition();
55  if ( ( myPos.x() < m_config.Xmax && myPos.x() > m_config.Xmin &&
56  myPos.y() < m_config.Ymax && myPos.y() > m_config.Ymin &&
57  myPos.z() < m_config.Zmax && myPos.z() > m_config.Zmin ) ||
58  m_config.verb==2){
59 
60  G4TransportationManager *tm = G4TransportationManager::GetTransportationManager();
61  tm->GetNavigatorForTracking()->SetVerboseLevel(m_config.verboseLevel);
62  tm->GetPropagatorInField()->SetVerboseLevel(m_config.verboseLevel);
63 
64  G4RunManagerKernel *rmk = G4RunManagerKernel::GetRunManagerKernel();
65  rmk->GetTrackingManager()->SetVerboseLevel(m_config.verboseLevel);
66  rmk->GetTrackingManager()->GetSteppingManager()->SetVerboseLevel(m_config.verboseLevel);
67  rmk->GetStackManager()->SetVerboseLevel(m_config.verboseLevel);
68 
69  G4Track *tr = aStep->GetTrack();
70  const G4ThreeVector& mom = tr->GetMomentumDirection();
71 
72  std::cout << "Moving " << tr->GetDefinition()->GetParticleName() << " at (" << myPos.x()
73  << ", " << myPos.y() << ", " << myPos.z() << ") to (" << mom.x() << ", " << mom.y()
74  << ", " << mom.z() << ") from ";
75  if (aStep->GetPreStepPoint()->GetPhysicalVolume()) std::cout << aStep->GetPreStepPoint()->GetPhysicalVolume()->GetName();
76  else std::cout << "noVolume";
77  std::cout << " to ";
78  if (aStep->GetPostStepPoint()->GetPhysicalVolume()) std::cout << aStep->GetPostStepPoint()->GetPhysicalVolume()->GetName();
79  else std::cout << "outOfWorld";
80  std::cout << " with KE=" << tr->GetKineticEnergy() << " pT=" << tr->GetMomentum().perp()
81  << " eta=" << tr->GetMomentum().eta() << " length " << aStep->GetStepLength() << " energy "
82  << aStep->GetTotalEnergyDeposit() << " with process ";
83  if (aStep->GetPostStepPoint()->GetProcessDefinedStep()) std::cout << aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName();
84  else std::cout << "Unknown";
85  std::cout << " from a ";
86  if (tr->GetCreatorProcess()) std::cout << tr->GetCreatorProcess()->GetProcessName();
87  else if (tr->GetParentID()==0) std::cout << "Generator";
88  else std::cout << "pid="<<tr->GetParentID();
89  if (tr->GetLogicalVolumeAtVertex()) std::cout << " in " << tr->GetLogicalVolumeAtVertex()->GetName() << ", ";
90  else std::cout << " nowhere, ";
91  std::cout << tr->GetTrackLength() << " mm ago" << std::endl;
92 
93  if (m_config.verb==1) m_config.verb=2;
94 
95  }
96  }
97  }
98 
99  void VerboseSelector::PreUserTrackingAction(const G4Track* aTrack)
100  {
102  {
103  int trackID = aTrack->GetTrackID();
104  TrackHelper trackHelper(aTrack);
105 
106  AtlasG4EventUserInfo* atlasG4EvtUserInfo = static_cast<AtlasG4EventUserInfo*>
107  (G4EventManager::GetEventManager()->GetConstCurrentEvent()->
108  GetUserInformation());
109 
110  int currentBarcode(0);
111 
112  if (trackHelper.IsPrimary() || trackHelper.IsRegisteredSecondary()) {
113  currentBarcode = HepMC::barcode(atlasG4EvtUserInfo->GetCurrentGenParticle()); // FIXME Barcode-based
114  }
115 
116  bool p1 = m_config.targetTrack<0 && m_config.targetBarcode<0 && m_config.targetPdgIDs.empty();
117  bool p2 = trackID==m_config.targetTrack;
118  bool p3 = currentBarcode==m_config.targetBarcode;
119  bool p4 = false;
120 
121  for (auto& pdgID : m_config.targetPdgIDs) {
122  if (std::abs(aTrack->GetParticleDefinition()->GetPDGEncoding()) == pdgID ) {
123  p4 = true;
124  break;
125  }
126  }
127 
128  if(p1 || p2 || p3 || p4) {
129  ATH_MSG_INFO(std::endl << "---------> Dumping now track #"
130  << trackID << " barcode " << currentBarcode
131  << " pdgID " << aTrack->GetParticleDefinition()->GetPDGEncoding()
132  << " in event " << m_evtCount);
133  G4EventManager::GetEventManager()->GetTrackingManager()->
134  SetVerboseLevel(m_config.verboseLevel);
135  }
136  }
137  }
138 
139  void VerboseSelector::PostUserTrackingAction(const G4Track* aTrack)
140  {
142  if(aTrack->GetTrackID()==m_config.targetTrack||m_config.targetTrack<0)
143  G4EventManager::GetEventManager()->GetTrackingManager()->SetVerboseLevel(0);
144  for (auto& pdgID : m_config.targetPdgIDs) {
145  if (std::abs(aTrack->GetParticleDefinition()->GetPDGEncoding()) == pdgID ) {
146  G4EventManager::GetEventManager()->GetTrackingManager()->SetVerboseLevel(0);
147  break;
148  }
149  }
150  }
151  }
152 
153 } // namespace G4UA
G4UA::VerboseSelector::Config::Ymin
double Ymin
Definition: VerboseSelector.h:37
G4UA::VerboseSelector::Config::Xmin
double Xmin
Definition: VerboseSelector.h:36
AtlasG4EventUserInfo
This class is attached to G4Event objects as UserInformation. It holds a pointer to the HepMC::GenEve...
Definition: AtlasG4EventUserInfo.h:21
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
xAOD::EventInfo_v1::eventNumber
uint64_t eventNumber() const
The current event's event number.
G4UA
for nSW
Definition: CalibrationDefaultProcessing.h:19
G4UA::VerboseSelector::UserSteppingAction
virtual void UserSteppingAction(const G4Step *) override
Definition: VerboseSelector.cxx:50
G4UA::VerboseSelector::Config::targetEvent
int targetEvent
Definition: VerboseSelector.h:31
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
G4UA::VerboseSelector::m_config
Config m_config
Definition: VerboseSelector.h:54
TrackHelper.h
G4UA::VerboseSelector::Config::Xmax
double Xmax
Definition: VerboseSelector.h:36
G4UA::VerboseSelector::PostUserTrackingAction
virtual void PostUserTrackingAction(const G4Track *) override
Definition: VerboseSelector.cxx:139
G4UA::VerboseSelector::Config::targetPdgIDs
std::vector< double > targetPdgIDs
Definition: VerboseSelector.h:39
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
ParticleJetTools::p3
Amg::Vector3D p3(const xAOD::TruthVertex *p)
Definition: ParticleJetLabelCommon.cxx:55
AtlasG4EventUserInfo::GetCurrentGenParticle
HepMC::GenParticlePtr GetCurrentGenParticle()
return a pointer to the GenParticle corresponding to the current G4Track (if there is one).
Definition: AtlasG4EventUserInfo.h:59
TrackHelper
Definition: TrackHelper.h:14
ParticleGun_EoverP_Config.mom
mom
Definition: ParticleGun_EoverP_Config.py:63
G4UA::VerboseSelector::BeginOfEventAction
virtual void BeginOfEventAction(const G4Event *) override
Definition: VerboseSelector.cxx:40
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
VerboseSelector.h
G4UA::VerboseSelector::Config::verboseLevel
int verboseLevel
Definition: VerboseSelector.h:34
G4UA::VerboseSelector::Config::Ymax
double Ymax
Definition: VerboseSelector.h:37
G4UA::VerboseSelector::Config::Zmin
double Zmin
Definition: VerboseSelector.h:38
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
G4UA::VerboseSelector::PreUserTrackingAction
virtual void PreUserTrackingAction(const G4Track *) override
Definition: VerboseSelector.cxx:99
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
G4UA::VerboseSelector::m_evtCount
uint64_t m_evtCount
Definition: VerboseSelector.h:55
G4UA::VerboseSelector::Config::Zmax
double Zmax
Definition: VerboseSelector.h:38
G4UA::VerboseSelector::Config::targetBarcode
int targetBarcode
Definition: VerboseSelector.h:33
G4UA::VerboseSelector::Config::targetTrack
int targetTrack
Definition: VerboseSelector.h:32
EventInfo.h
TrackHelper::IsRegisteredSecondary
bool IsRegisteredSecondary() const
Definition: TrackHelper.cxx:25
G4UA::VerboseSelector::Config::verb
int verb
Definition: VerboseSelector.h:35
AtlasG4EventUserInfo.h
G4UA::VerboseSelector::VerboseSelector
VerboseSelector(const Config &config)
Definition: VerboseSelector.cxx:33
TrackHelper::IsPrimary
bool IsPrimary() const
Definition: TrackHelper.cxx:15
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
G4UA::VerboseSelector::Config
Definition: VerboseSelector.h:29
Gaudi
=============================================================================
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:273
ReadHandle.h
Handle class for reading from StoreGate.