ATLAS Offline Software
Loading...
Searching...
No Matches
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
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
29
30namespace G4UA
31{
32
34 AthMessaging(Gaudi::svcLocator()->service< IMessageSvc >( "MessageSvc" ),"VerboseSelector"),
35 m_evtStore("StoreGateSvc/StoreGateSvc", "VerboseSelector"),
36 m_detStore("StoreGateSvc/DetectorStore", "LooperKiller"),
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 {
52 if(m_evtCount==(uint64_t)m_config.targetEvent||m_config.targetEvent<0){
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 {
101 if(m_evtCount==(uint64_t)m_config.targetEvent||m_config.targetEvent<0)
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 {
141 if(m_evtCount==(uint64_t)m_config.targetEvent||m_config.targetEvent<0){
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
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
Handle class for reading from StoreGate.
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
This class is attached to G4Event objects as UserInformation.
HepMC::GenParticlePtr GetCurrentGenParticle()
return a pointer to the GenParticle corresponding to the current G4Track (if there is one).
virtual void BeginOfEventAction(const G4Event *) override
VerboseSelector(const Config &config)
ServiceHandle< StoreGateSvc > m_detStore
Pointer to StoreGate (detector store by default)
ServiceHandle< StoreGateSvc > m_evtStore
Pointer to StoreGate (event store by default)
virtual void UserSteppingAction(const G4Step *) override
virtual void PreUserTrackingAction(const G4Track *) override
virtual void PostUserTrackingAction(const G4Track *) override
virtual bool isValid() override final
Can the handle be successfully dereferenced?
bool IsPrimary() const
bool IsRegisteredSecondary() const
=============================================================================
int barcode(const T *p)
Definition Barcode.h:16