ATLAS Offline Software
Loading...
Searching...
No Matches
HyperspaceCatcher.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "HyperspaceCatcher.h"
6
7#include "G4LogicalVolume.hh"
8#include "G4LogicalVolumeStore.hh"
9#include "G4PropagatorInField.hh"
10#include "G4RunManagerKernel.hh"
11#include "G4Step.hh"
12#include "G4ThreeVector.hh"
13#include "G4Track.hh"
14#include "G4TrackVector.hh"
15#include "G4TransportationManager.hh"
16#include "G4VProcess.hh"
17#include "G4VSolid.hh"
18
19#include <cmath>
20
21#include "GaudiKernel/Bootstrap.h"
22#include "GaudiKernel/ISvcLocator.h"
23#include "GaudiKernel/IMessageSvc.h"
24
25namespace G4UA
26{
27
29 AthMessaging(Gaudi::svcLocator()->service< IMessageSvc >( "MessageSvc" ),"HyperspaceCatcher"),
31 m_world(0),
33 {}
34
36 {
37 // Highest level implemented is 2 at the moment
38 if (m_config.treatmentLevel>2) m_config.treatmentLevel=2;
39 ATH_MSG_INFO("Treatment level set to " << m_config.treatmentLevel );
40
41 ATH_MSG_INFO("Set to kill after " << m_config.killAfter );
42
43 G4LogicalVolumeStore * lvs = G4LogicalVolumeStore::GetInstance();
44 for (auto lv : *lvs){
45 if ( lv->GetName()=="Atlas::Atlas" ){
46 m_world=lv->GetSolid();
47 break;
48 }
49 }
50 if (0==m_world){
51 ATH_MSG_FATAL( "Could not find world volume!" );
52 throw std::runtime_error("Could not find world volume!");
53 }
54 }
55
56 void HyperspaceCatcher::UserSteppingAction(const G4Step* aStep)
57 {
58
59 bool hs = false;
60
61 // Various levels of testing. Simple testing at level 0 : just see if it's in the neighborhood
62 G4ThreeVector myPos = aStep->GetPostStepPoint()->GetPosition();
63 if (std::fabs( myPos.x() )>30000. ||
64 std::fabs( myPos.y() )>30000. ||
65 std::fabs( myPos.z() )>50000. ){
66 hs=true;
67 } // Check #1 failed
68 if (!hs && m_config.treatmentLevel<=0) return;
69
70 if ( m_world->Inside( myPos ) == kOutside ){
71 hs=true;
72 } // Check #2 failed
73 if (!hs && m_config.treatmentLevel<=1) return;
74
75 G4VSolid * pre = aStep->GetPreStepPoint()->GetPhysicalVolume() &&
76 aStep->GetPreStepPoint()->GetPhysicalVolume()->GetLogicalVolume() ?
77 aStep->GetPreStepPoint()->GetPhysicalVolume()->GetLogicalVolume()->GetSolid():0;
78 G4VSolid * post = aStep->GetPostStepPoint()->GetPhysicalVolume() &&
79 aStep->GetPostStepPoint()->GetPhysicalVolume()->GetLogicalVolume() ?
80 aStep->GetPostStepPoint()->GetPhysicalVolume()->GetLogicalVolume()->GetSolid():0;
81 if ( (post && post->Inside( myPos ) == kOutside ) ||
82 ( pre && pre->Inside( aStep->GetPreStepPoint()->GetPosition() ) == kOutside ) ) {
83 hs=true;
84 } // Check #3 failed
85 if (!hs && m_config.treatmentLevel<=2) return;
86
87 G4Track *tr = aStep->GetTrack();
88 const G4ThreeVector& mom = tr->GetMomentumDirection();
89
90 std::cout << "Caught a " << tr->GetDefinition()->GetParticleName() << " at (" << myPos.x()
91 << ", " << myPos.y() << ", " << myPos.z() << ") to (" << mom.x() << ", " << mom.y()
92 << ", " << mom.z() << ") from ";
93 if (aStep->GetPreStepPoint()->GetPhysicalVolume()) {
94 std::cout << aStep->GetPreStepPoint()->GetPhysicalVolume()->GetName();
95 }
96 else std::cout << "noVolume";
97 std::cout << " to ";
98 if (aStep->GetPostStepPoint()->GetPhysicalVolume()) {
99 std::cout << aStep->GetPostStepPoint()->GetPhysicalVolume()->GetName();
100 }
101 else std::cout << "outOfWorld";
102 std::cout << " with KE=" << tr->GetKineticEnergy() << " pT=" << tr->GetMomentum().perp()
103 << " eta=" << tr->GetMomentum().eta() << " length " << aStep->GetStepLength() << " energy "
104 << aStep->GetTotalEnergyDeposit() << " with process ";
105 if (aStep->GetPostStepPoint()->GetProcessDefinedStep()) {
106 std::cout << aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName();
107 }
108 else std::cout << "Unknown";
109 std::cout << " from a ";
110 if (tr->GetCreatorProcess()) std::cout << tr->GetCreatorProcess()->GetProcessName();
111 else if (tr->GetParentID()==0) std::cout << "Generator";
112 else std::cout << "pid="<<tr->GetParentID();
113 if (tr->GetLogicalVolumeAtVertex()) std::cout << " in " << tr->GetLogicalVolumeAtVertex()->GetName() << ", ";
114 else std::cout << " nowhere, ";
115 std::cout << tr->GetTrackLength() << " mm ago" << std::endl;
116 ATH_MSG_WARNING( "HYPERSPACE PARTICLE DETECTED!! KILLING!!" );
117
118 const G4TrackVector *tv = aStep->GetSecondary();
119 for (auto atv : *tv ){
120 if ( m_world->Inside( atv->GetPosition() ) == kOutside ){
121 atv->SetTrackStatus(fStopAndKill);
122 ++m_killCount;
123 ATH_MSG_WARNING("Had to kill a secondary as well");
124 }
125 }
126
127 aStep->GetTrack()->SetTrackStatus(fStopAndKill);
128 ++m_killCount;
129
130 if (m_config.killAfter>0 && m_killCount>m_config.killAfter){
131 ATH_MSG_FATAL( m_config.killAfter << " cases of hyperspace bug found. You have something seriously wrong going on here. Needs to be fixed. Bailing out." );
132 throw std::runtime_error("Hyperspace bug found");
133 }
134 }
135
136} // namespace G4UA
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
HyperspaceCatcher(const Config &config)
virtual void UserSteppingAction(const G4Step *) override
virtual void BeginOfRunAction(const G4Run *) override
=============================================================================