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
8
9#include "G4EventManager.hh"
10#include "G4LogicalVolume.hh"
11#include "G4PropagatorInField.hh"
12#include "G4RunManagerKernel.hh"
13#include "G4Step.hh"
14#include "G4Event.hh"
15#include "G4ThreeVector.hh"
16#include "G4Track.hh"
17#include "G4TransportationManager.hh"
18#include "G4VProcess.hh"
19
20#include <iostream>
21
22#include "GaudiKernel/Bootstrap.h"
23#include "GaudiKernel/ISvcLocator.h"
24#include "GaudiKernel/IMessageSvc.h"
25// to retrieve the event number
28
29namespace G4UA
30{
31
33 AthMessaging(Gaudi::svcLocator()->service< IMessageSvc >( "MessageSvc" ),"VerboseSelector"),
34 m_evtStore("StoreGateSvc/StoreGateSvc", "VerboseSelector"),
35 m_detStore("StoreGateSvc/DetectorStore", "LooperKiller"),
36 m_config(config),m_evtCount(0)
37 {}
38
40 {
41 SG::ReadHandle<xAOD::EventInfo> eic("EventInfo");
42 if (!eic.isValid()){
43 ATH_MSG_WARNING( "Failed to retrieve EventInfo" );
44 } else {
45 m_evtCount = eic->eventNumber();
46 }
47 }
48
49 void VerboseSelector::UserSteppingAction(const G4Step* aStep)
50 {
51 if(m_evtCount==(uint64_t)m_config.targetEvent||m_config.targetEvent<0){
52
53 const G4ThreeVector& myPos = aStep->GetPostStepPoint()->GetPosition();
54 if ( ( myPos.x() < m_config.Xmax && myPos.x() > m_config.Xmin &&
55 myPos.y() < m_config.Ymax && myPos.y() > m_config.Ymin &&
56 myPos.z() < m_config.Zmax && myPos.z() > m_config.Zmin ) ||
57 m_config.verb==2){
58
59 G4TransportationManager *tm = G4TransportationManager::GetTransportationManager();
60 tm->GetNavigatorForTracking()->SetVerboseLevel(m_config.verboseLevel);
61 tm->GetPropagatorInField()->SetVerboseLevel(m_config.verboseLevel);
62
63 G4RunManagerKernel *rmk = G4RunManagerKernel::GetRunManagerKernel();
64 rmk->GetTrackingManager()->SetVerboseLevel(m_config.verboseLevel);
65 rmk->GetTrackingManager()->GetSteppingManager()->SetVerboseLevel(m_config.verboseLevel);
66 rmk->GetStackManager()->SetVerboseLevel(m_config.verboseLevel);
67
68 G4Track *tr = aStep->GetTrack();
69 const G4ThreeVector& mom = tr->GetMomentumDirection();
70
71 std::cout << "Moving " << tr->GetDefinition()->GetParticleName() << " at (" << myPos.x()
72 << ", " << myPos.y() << ", " << myPos.z() << ") to (" << mom.x() << ", " << mom.y()
73 << ", " << mom.z() << ") from ";
74 if (aStep->GetPreStepPoint()->GetPhysicalVolume()) std::cout << aStep->GetPreStepPoint()->GetPhysicalVolume()->GetName();
75 else std::cout << "noVolume";
76 std::cout << " to ";
77 if (aStep->GetPostStepPoint()->GetPhysicalVolume()) std::cout << aStep->GetPostStepPoint()->GetPhysicalVolume()->GetName();
78 else std::cout << "outOfWorld";
79 std::cout << " with KE=" << tr->GetKineticEnergy() << " pT=" << tr->GetMomentum().perp()
80 << " eta=" << tr->GetMomentum().eta() << " length " << aStep->GetStepLength() << " energy "
81 << aStep->GetTotalEnergyDeposit() << " with process ";
82 if (aStep->GetPostStepPoint()->GetProcessDefinedStep()) std::cout << aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName();
83 else std::cout << "Unknown";
84 std::cout << " from a ";
85 if (tr->GetCreatorProcess()) std::cout << tr->GetCreatorProcess()->GetProcessName();
86 else if (tr->GetParentID()==0) std::cout << "Generator";
87 else std::cout << "pid="<<tr->GetParentID();
88 if (tr->GetLogicalVolumeAtVertex()) std::cout << " in " << tr->GetLogicalVolumeAtVertex()->GetName() << ", ";
89 else std::cout << " nowhere, ";
90 std::cout << tr->GetTrackLength() << " mm ago" << std::endl;
91
92 if (m_config.verb==1) m_config.verb=2;
93
94 }
95 }
96 }
97
98 void VerboseSelector::PreUserTrackingAction(const G4Track* aTrack)
99 {
100 if(m_evtCount==(uint64_t)m_config.targetEvent||m_config.targetEvent<0)
101 {
102 int trackID = aTrack->GetTrackID();
103 TrackHelper trackHelper(aTrack);
104
105 int currentBarcode(0);
106
107 if (trackHelper.IsPrimary() || trackHelper.IsRegisteredSecondary()) {
108 currentBarcode = trackHelper.GetBarcode(); // FIXME Barcode-based
109 }
110
111 bool p1 = m_config.targetTrack<0 && m_config.targetBarcode<0 && m_config.targetPdgIDs.empty();
112 bool p2 = trackID==m_config.targetTrack;
113 bool p3 = currentBarcode==m_config.targetBarcode;
114 bool p4 = false;
115
116 for (auto& pdgID : m_config.targetPdgIDs) {
117 if (std::abs(aTrack->GetParticleDefinition()->GetPDGEncoding()) == pdgID ) {
118 p4 = true;
119 break;
120 }
121 }
122
123 if(p1 || p2 || p3 || p4) {
124 ATH_MSG_INFO(std::endl << "---------> Dumping now track #"
125 << trackID << " barcode " << currentBarcode
126 << " pdgID " << aTrack->GetParticleDefinition()->GetPDGEncoding()
127 << " in event " << m_evtCount);
128 G4EventManager::GetEventManager()->GetTrackingManager()->
129 SetVerboseLevel(m_config.verboseLevel);
130 }
131 }
132 }
133
134 void VerboseSelector::PostUserTrackingAction(const G4Track* aTrack)
135 {
136 if(m_evtCount==(uint64_t)m_config.targetEvent||m_config.targetEvent<0){
137 if(aTrack->GetTrackID()==m_config.targetTrack||m_config.targetTrack<0)
138 G4EventManager::GetEventManager()->GetTrackingManager()->SetVerboseLevel(0);
139 for (auto& pdgID : m_config.targetPdgIDs) {
140 if (std::abs(aTrack->GetParticleDefinition()->GetPDGEncoding()) == pdgID ) {
141 G4EventManager::GetEventManager()->GetTrackingManager()->SetVerboseLevel(0);
142 break;
143 }
144 }
145 }
146 }
147
148} // 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.
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
int GetBarcode() const
Return the truth barcode/id/status used for detector output.
bool IsRegisteredSecondary() const
=============================================================================