ATLAS Offline Software
LooperKiller.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 "LooperKiller.h"
6 #include <iostream>
7 #include "G4RunManagerKernel.hh"
8 #include "G4TransportationManager.hh"
9 #include "G4Navigator.hh"
10 #include "G4PropagatorInField.hh"
11 #include "G4TrackingManager.hh"
12 #include "G4SteppingManager.hh"
13 #include "G4StackManager.hh"
14 #include "G4EventManager.hh"
15 #include "G4Event.hh"
16 #include "MCTruth/TrackHelper.h"
19 #include "StoreGate/WriteHandle.h"
20 #include "StoreGate/StoreGateSvc.h"
21 
22 // For setting an error state in event info
23 #include "EventInfo/EventInfo.h"
24 
25 #include "GaudiKernel/Bootstrap.h"
26 #include "GaudiKernel/ISvcLocator.h"
27 #include "GaudiKernel/IMessageSvc.h"
28 
29 namespace G4UA
30 {
31 
32  //---------------------------------------------------------------------------
34  : AthMessaging(Gaudi::svcLocator()->service<IMessageSvc>("MessageSvc"),
35  "LooperKiller"),
36  m_evtStore("StoreGateSvc/StoreGateSvc", "LooperKiller"),
37  m_detStore("StoreGateSvc/DetectorStore", "LooperKiller"),
38  m_config(config), m_report(), m_count_steps(0)
39  {
40  }
41 
42  //---------------------------------------------------------------------------
43  void LooperKiller::UserSteppingAction(const G4Step* aStep)
44  {
45 
46  if (aStep->GetTrack()->GetCurrentStepNumber() < m_config.MaxSteps) {
47  if (m_count_steps==0) return;
48  // Track recovered...
49  ATH_MSG_WARNING("Track finished on its own. Congrats. Moving on with the event.");
50  m_count_steps = 0;
51  G4TransportationManager *tm = G4TransportationManager::GetTransportationManager();
52  tm->GetNavigatorForTracking()->SetVerboseLevel(0);
53  tm->GetPropagatorInField()->SetVerboseLevel(0);
54  G4RunManagerKernel *rmk = G4RunManagerKernel::GetRunManagerKernel();
55  rmk->GetTrackingManager()->SetVerboseLevel(0);
56  rmk->GetTrackingManager()->GetSteppingManager()->SetVerboseLevel(0);
57  rmk->GetStackManager()->SetVerboseLevel(0);
58  return;
59  } else if (aStep->GetTrack()->GetCurrentStepNumber() == m_config.MaxSteps) {
60  ATH_MSG_WARNING("LooperKiller triggered!! Hold on to your hats!!!!!!!!" );
61  }
62 
63  G4TransportationManager *tm = G4TransportationManager::GetTransportationManager();
64  tm->GetNavigatorForTracking()->SetVerboseLevel(m_config.VerboseLevel);
65  tm->GetPropagatorInField()->SetVerboseLevel(m_config.VerboseLevel);
66 
67  G4RunManagerKernel *rmk = G4RunManagerKernel::GetRunManagerKernel();
68  rmk->GetTrackingManager()->SetVerboseLevel(m_config.VerboseLevel);
69  rmk->GetTrackingManager()->GetSteppingManager()->SetVerboseLevel(m_config.VerboseLevel);
70  rmk->GetStackManager()->SetVerboseLevel(m_config.VerboseLevel);
71 
72  m_count_steps++;
73 
75  m_count_steps = 0;
77  aStep->GetTrack()->SetTrackStatus(fStopAndKill);
78  tm->GetNavigatorForTracking()->SetVerboseLevel(0);
79  tm->GetPropagatorInField()->SetVerboseLevel(0);
80  rmk->GetTrackingManager()->SetVerboseLevel(0);
81  rmk->GetTrackingManager()->GetSteppingManager()->SetVerboseLevel(0);
82  rmk->GetStackManager()->SetVerboseLevel(0);
83  int pdg_id{0};
84  TrackHelper trackHelper(aStep->GetTrack());
85  if ( m_config.BSM_Only && (trackHelper.IsPrimary() || trackHelper.IsRegisteredSecondary()) ) {
87  if (part) { pdg_id = part->pdg_id(); }
88  }
89  if ( !m_config.BSM_Only || MC::isBSM(pdg_id)) { // Sometimes we may ony want to bail out for BSM particles.
90  // Bail out...
91  if (m_config.AbortEvent){
92  rmk->GetEventManager()->AbortCurrentEvent();
93  rmk->GetEventManager()->GetNonconstCurrentEvent()->SetEventAborted();
94  }
95  if (m_config.SetError){
96 
97  // Set error state in eventInfo
98  SG::WriteHandle<EventInfo> eic("McEventInfo");
99  if (! eic.isValid()){
100  ATH_MSG_WARNING( "Failed to retrieve EventInfo" );
101  } else {
103  ATH_MSG_WARNING( "Set error state in event info!" );
104  }
105  } // End of set error
106  } // End of BSM-only check
107  const std::string name = aStep->GetTrack()->GetDefinition()->GetParticleName();
108  if ( ( m_config.BSM_Only && !MC::isBSM(pdg_id) ) || !m_config.AbortEvent ) {
109  ATH_MSG_INFO ("Quietly stopped tracking looping " << name
110  << " (trackID " << aStep->GetTrack()->GetTrackID()
111  << ", track pos: "<<aStep->GetTrack()->GetPosition()
112  << ", mom: "<<aStep->GetTrack()->GetMomentum()
113  << ", parentID " << aStep->GetTrack()->GetParentID() << ")");
114  }
115  else {
116  ATH_MSG_WARNING ("Stopped tracking looping " << name
117  << " (trackID " << aStep->GetTrack()->GetTrackID()
118  << ", track pos: "<<aStep->GetTrack()->GetPosition()
119  << ", mom: "<<aStep->GetTrack()->GetMomentum()
120  << ", parentID " << aStep->GetTrack()->GetParentID() << "). The event will abort now.");
121  }
122  } // End of handling end of error time
123  }
124 
125 } // namespace G4UA
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
EventInfo::Error
@ Error
Definition: EventInfo/EventInfo/EventInfo.h:67
G4UA::LooperKiller::Config
Definition: LooperKiller.h:28
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
G4UA
for nSW
Definition: CalibrationDefaultProcessing.h:19
isBSM
bool isBSM(const T &p)
APID: graviton and all Higgs extensions are BSM.
Definition: AtlasPID.h:591
G4UA::LooperKiller::m_count_steps
int m_count_steps
Definition: LooperKiller.h:63
TrackHelper.h
G4UA::LooperKiller::Config::AbortEvent
bool AbortEvent
Definition: LooperKiller.h:32
HepMC::GenParticlePtr
GenParticle * GenParticlePtr
Definition: GenParticle.h:37
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
TrackHelper
Definition: TrackHelper.h:14
EventInfo::setErrorState
bool setErrorState(EventFlagSubDet subDet, EventFlagErrorState errorState)
Set error state flag for a particular sub detector.
Definition: EventInfo.cxx:139
WriteHandle.h
Handle class for recording to StoreGate.
G4UA::LooperKiller::Report::killed_tracks
long killed_tracks
Definition: LooperKiller.h:39
G4UA::LooperKiller::UserSteppingAction
virtual void UserSteppingAction(const G4Step *) override
Definition: LooperKiller.cxx:43
EventInfo::Core
@ Core
Definition: EventInfo/EventInfo/EventInfo.h:61
G4UA::LooperKiller::Config::PrintSteps
int PrintSteps
Definition: LooperKiller.h:30
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
G4UA::LooperKiller::m_config
Config m_config
My configuration options.
Definition: LooperKiller.h:61
G4UA::LooperKiller::Config::BSM_Only
bool BSM_Only
Definition: LooperKiller.h:34
G4UA::LooperKiller::LooperKiller
LooperKiller(const Config &config)
Definition: LooperKiller.cxx:33
G4UA::LooperKiller::m_report
Report m_report
Definition: LooperKiller.h:62
TrackHelper::GetTrackInformation
TrackInformation * GetTrackInformation()
Definition: TrackHelper.h:24
G4UA::LooperKiller::Config::VerboseLevel
int VerboseLevel
Definition: LooperKiller.h:31
SG::WriteHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
TrackInformation.h
TrackHelper::IsRegisteredSecondary
bool IsRegisteredSecondary() const
Definition: TrackHelper.cxx:25
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
TrackHelper::IsPrimary
bool IsPrimary() const
Definition: TrackHelper.cxx:15
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Gaudi
=============================================================================
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:273
G4UA::LooperKiller::Config::SetError
bool SetError
Definition: LooperKiller.h:33
LooperKiller.h
StoreGateSvc.h
HepMCHelpers.h
G4UA::LooperKiller::Config::MaxSteps
int MaxSteps
Definition: LooperKiller.h:29
TrackInformation::GetCurrentGenParticle
virtual HepMC::ConstGenParticlePtr GetCurrentGenParticle() const override
return a pointer to the GenParticle corresponding to the current G4Track (if there is one).
Definition: TrackInformation.h:49