ATLAS Offline Software
PhysicsListSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "PhysicsListSvc.h"
6 
7 #include "G4VUserPhysicsList.hh"
8 #include "G4StateManager.hh"
9 #include "G4RunManager.hh"
10 #include "G4EmParameters.hh"
11 #include "G4UImanager.hh"
12 #include "G4PhysListFactory.hh"
13 #include "G4AntiNeutron.hh"
14 
15 #include "CLHEP/Units/PhysicalConstants.h"
16 
18 
19 #include <limits>
20 
21 PhysicsListSvc::PhysicsListSvc(const std::string& name, ISvcLocator* pSvcLocator)
22  : base_class(name,pSvcLocator)
23 {
24 }
25 
26 
28 {
29  ATH_MSG_DEBUG("PhysicsListSvc::initialize()");
30  if (m_phys_option.size())
31  {
32  ATH_MSG_INFO( "Initializing list of " << m_phys_option.size() << " physics options" );
33  CHECK( m_phys_option.retrieve() );
34  }
35 
36  if (m_phys_decay.size())
37  {
38  ATH_MSG_INFO( "Initializing list of " << m_phys_decay.size() << " Decays " );
39  CHECK( m_phys_decay.retrieve() );
40  }
41 
42  return StatusCode::SUCCESS;
43 }
44 
45 
47 {
48  ATH_MSG_DEBUG("PhysicsListSvc::CreatePhysicsList()");
49  if (m_physicsListName.value() != ""){
50  G4PhysListFactory factory;
51  AtlasPhysListFactory Atlasfactory;
52  if (factory.IsReferencePhysList(m_physicsListName.value()))
53  {
54  ATH_MSG_INFO("Creating Geant4 PhysicsList: " << m_physicsListName.value());
55  m_physicsList = factory.GetReferencePhysList(m_physicsListName.value());
56  }
57  else if (Atlasfactory.IsReferencePhysList(m_physicsListName.value()))
58  {
59  ATH_MSG_INFO("Creating ATLAS PhysicsList: " << m_physicsListName.value());
61  }
62  }
63 
64  if (!m_physicsList)
65  {
66  ATH_MSG_ERROR("Unable to initialize physics List: " << m_physicsList);
67  throw "PhysicsListInitializationError";
68  }
69  // Call these as functions. As this could be used as a base class, having
70  // these as separate functions lets someone who is inheriting from this use
71  // them...
72 
73  // sort m_phys_option list
74  std::vector<IPhysicsOptionTool*> sortedPhysicsOptions;
75  sortedPhysicsOptions.reserve(m_phys_option.size());
76  // Manually sorting ToolHandleArray
77  {
78  // BSM Physics
79  for (auto& physOptTool: m_phys_option) {
80  if (physOptTool->GetOptionType() == G4AtlasPhysicsOption::Type::BSMPhysics) {
81  sortedPhysicsOptions.push_back(&*physOptTool);
82  }
83  }
84 
85  // Add particles from the PDG Table not currently known to Geant4
86  for (auto& physOptTool: m_phys_option) {
87  if (physOptTool->GetOptionType() == G4AtlasPhysicsOption::Type::QS_ExtraParticles) {
88  sortedPhysicsOptions.push_back(&*physOptTool);
89  }
90  }
91 
92  // Add MSC and Ionisation processes for specific particles (possibly merge with the next one?)
93  for (auto& physOptTool: m_phys_option) {
94  if (physOptTool->GetOptionType() == G4AtlasPhysicsOption::Type::QS_ExtraProc) {
95  sortedPhysicsOptions.push_back(&*physOptTool);
96  }
97  }
98 
99  // G4StepLimitation, LUCID Op Process, TRT XTR process
100  for (auto& physOptTool: m_phys_option) {
101  if (physOptTool->GetOptionType() == G4AtlasPhysicsOption::Type::GlobalProcesses) {
102  sortedPhysicsOptions.push_back(&*physOptTool);
103  }
104  }
105 
106  // Unknown
107  for (auto& physOptTool: m_phys_option) {
108  if (physOptTool->GetOptionType() == G4AtlasPhysicsOption::Type::UnknownType) {
109  ATH_MSG_ERROR(physOptTool->name() << "set as UnknownType. This tool will not be used to modify the physics list of this job.");
110  }
111  }
112  }
113 
114  //Register physics options to the G4VModularPhysicsList
115  for (auto& physOptTool: sortedPhysicsOptions)
116  {
117  ATH_MSG_DEBUG("Registering " << physOptTool->name());
118  m_physicsList->RegisterPhysics(physOptTool->GetPhysicsOption());
119  }
120  //Register decays to the G4VModularPhysicsList
121  for (auto& physDecayTool: m_phys_decay)
122  {
123  ATH_MSG_DEBUG("Registering " << physDecayTool->name());
124  m_physicsList->RegisterPhysics(physDecayTool->GetPhysicsOption());
125  }
126 
127  //ConstructProcess();
128  ATH_MSG_DEBUG("end of PhysicsListSvc::CreatePhysicsList()");
129 }
130 
131 
132 G4VUserPhysicsList* PhysicsListSvc::GetPhysicsList()
133 {
134  if (!m_physicsList) {
135  this->CreatePhysicsList();
136  }
137  return m_physicsList;
138 }
139 
140 
142 {
143  if(!m_physicsList) {
144  this->CreatePhysicsList();
145  }
146  G4RunManager::GetRunManager()->SetUserInitialization(m_physicsList);
147 }
148 
149 
151 {
152  if (!m_physicsList)
153  {
154  ATH_MSG_WARNING("Physics list not initialized before calling ConstructProcess()");
155  return;
156  }
157 
158  if(m_generalCut.value() > 0. && std::abs(m_generalCut.value())>std::numeric_limits<double>::epsilon())
159  {
160  m_physicsList->SetDefaultCutValue(m_generalCut.value());
161  }
162 
163  std::vector<std::string> g4commands;
164  if (m_neutronTimeCut.value() > 0. && std::abs(m_neutronTimeCut.value())>std::numeric_limits<double>::epsilon())
165  {
166  std::ostringstream oss;
167  oss<<"/physics_engine/neutron/timeLimit "<<m_neutronTimeCut.value()<<" ns";
168  g4commands.push_back(oss.str());
169  }
170 
171  if (m_neutronEnergyCut.value() > 0. && std::abs(m_neutronEnergyCut.value())>std::numeric_limits<double>::epsilon())
172  {
173  std::ostringstream oss;
174  oss<<"/physics_engine/neutron/energyLimit "<<m_neutronEnergyCut.value()<<" MeV";
175  g4commands.push_back(oss.str());
176  }
177 
178  if(!g4commands.empty()) {
179  // Send UI commands
180  ATH_MSG_DEBUG("G4 Command: Trying in SetPhysicsOptions()");
181  G4UImanager* ui = G4UImanager::GetUIpointer();
182  for (const auto& g4command : g4commands) {
183  int returnCode = ui->ApplyCommand( g4command );
184  CommandLog(returnCode, g4command);
185  }
186  }
187 
188  G4EmParameters* emp = G4EmParameters::Instance();
189  if (m_emMaxEnergy.value()>=0) emp->SetMaxEnergy(m_emMaxEnergy.value());
190  if (m_emNumberOfBinsPerDecade.value()>=0) emp->SetNumberOfBinsPerDecade(m_emNumberOfBinsPerDecade.value());
191  if (m_emMinEnergy.value()>=0) emp->SetMinEnergy(m_emMinEnergy.value());
192  if (m_applyEMCuts.value())
193  {
194  emp->SetApplyCuts(true);
195  }
196 
198  G4AntiNeutron::Definition()->SetPDGStable(false);
199  }
200 
201  return;
202 }
203 
204 
205 void PhysicsListSvc::CommandLog(int returnCode, const std::string& commandString) const
206 {
207  switch(returnCode) {
208  case 0: { ATH_MSG_DEBUG("G4 Command: " << commandString << " - Command Succeeded"); } break;
209  case 100: { ATH_MSG_ERROR("G4 Command: " << commandString << " - Command Not Found!"); } break;
210  case 200: {
211  auto* stateManager = G4StateManager::GetStateManager();
212  ATH_MSG_DEBUG("G4 Command: " << commandString << " - Illegal Application State (" <<
213  stateManager->GetStateString(stateManager->GetCurrentState()) << ")!");
214  } break;
215  case 300: { ATH_MSG_ERROR("G4 Command: " << commandString << " - Parameter Out of Range!"); } break;
216  case 400: { ATH_MSG_ERROR("G4 Command: " << commandString << " - Parameter Unreadable!"); } break;
217  case 500: { ATH_MSG_ERROR("G4 Command: " << commandString << " - Parameter Out of Candidates!"); } break;
218  case 600: { ATH_MSG_ERROR("G4 Command: " << commandString << " - Alias Not Found!"); } break;
219  default: { ATH_MSG_ERROR("G4 Command: " << commandString << " - Unknown Status!"); } break;
220  }
221 
222 }
G4AtlasPhysicsOption::QS_ExtraProc
@ QS_ExtraProc
Definition: IPhysicsOptionTool.h:24
PhysicsListSvc::m_generalCut
Gaudi::Property< double > m_generalCut
!< Energy cut for neutrons (in the neutron killer process)
Definition: PhysicsListSvc.h:49
PhysicsListSvc::m_unstableAntiNeutrons
Gaudi::Property< bool > m_unstableAntiNeutrons
!< Switch for the G4 "apply cuts" EM physics flag
Definition: PhysicsListSvc.h:61
PhysicsListSvc::CreatePhysicsList
virtual void CreatePhysicsList() override
Definition: PhysicsListSvc.cxx:46
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
AtlasPhysListFactory::IsReferencePhysList
G4bool IsReferencePhysList(const G4String &)
PhysicsListSvc::PhysicsListSvc
PhysicsListSvc(const std::string &name, ISvcLocator *pSvcLocator)
Definition: PhysicsListSvc.cxx:21
AtlasPhysListFactory
Definition: AtlasPhysListFactory.h:12
PhysicsListSvc::m_physicsListName
Gaudi::Property< std::string > m_physicsListName
!< Handle on the physics list
Definition: PhysicsListSvc.h:46
AtlasPhysListFactory.h
PhysicsListSvc::m_emMaxEnergy
Gaudi::Property< double > m_emMaxEnergy
!< A general cut - this isn't normally used in our simulation
Definition: PhysicsListSvc.h:50
PhysicsListSvc::m_phys_option
ToolHandleArray< IPhysicsOptionTool > m_phys_option
Definition: PhysicsListSvc.h:43
PhysicsListSvc::m_emMinEnergy
Gaudi::Property< double > m_emMinEnergy
!< Maximum energy of the pre-calculated EM cross-section tables
Definition: PhysicsListSvc.h:51
PhysicsListSvc::m_phys_decay
ToolHandleArray< IPhysicsOptionTool > m_phys_decay
Definition: PhysicsListSvc.h:44
PhysicsListSvc::CommandLog
void CommandLog(int returnCode, const std::string &commandString) const
This command prints a message about a G4Command depending on its returnCode.
Definition: PhysicsListSvc.cxx:205
PhysicsListSvc::m_neutronTimeCut
Gaudi::Property< double > m_neutronTimeCut
!< Name for the physics list (property to be set in the tool)
Definition: PhysicsListSvc.h:47
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
PhysicsListSvc::initialize
virtual StatusCode initialize() override
Definition: PhysicsListSvc.cxx:27
PhysicsListSvc::m_neutronEnergyCut
Gaudi::Property< double > m_neutronEnergyCut
!< Time cut for neutrons (in the neutron killer process)
Definition: PhysicsListSvc.h:48
PhysicsListSvc::m_emNumberOfBinsPerDecade
Gaudi::Property< int > m_emNumberOfBinsPerDecade
!< Minimum energy of the pre-calculated EM cross-section tables
Definition: PhysicsListSvc.h:58
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
PhysicsListSvc::m_physicsList
G4VModularPhysicsList * m_physicsList
Definition: PhysicsListSvc.h:45
G4AtlasPhysicsOption::GlobalProcesses
@ GlobalProcesses
Definition: IPhysicsOptionTool.h:25
G4AtlasPhysicsOption::UnknownType
@ UnknownType
Definition: IPhysicsOptionTool.h:26
PhysicsListSvc.h
AtlasPhysListFactory::GetReferencePhysList
G4VModularPhysicsList * GetReferencePhysList(const G4String &)
PhysicsListSvc::m_applyEMCuts
Gaudi::Property< bool > m_applyEMCuts
!< Number of bins per Energy decade. Used for both DeDx and for the Lambda binning.
Definition: PhysicsListSvc.h:59
G4AtlasPhysicsOption::QS_ExtraParticles
@ QS_ExtraParticles
Definition: IPhysicsOptionTool.h:23
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
CI_EMPFlowData22test.returnCode
returnCode
Definition: CI_EMPFlowData22test.py:16
G4AtlasPhysicsOption::BSMPhysics
@ BSMPhysics
Definition: IPhysicsOptionTool.h:22
PhysicsListSvc::GetPhysicsList
virtual G4VUserPhysicsList * GetPhysicsList() override
Definition: PhysicsListSvc.cxx:132
PhysicsListSvc::SetPhysicsList
virtual void SetPhysicsList() override
Definition: PhysicsListSvc.cxx:141
PhysicsListSvc::SetPhysicsOptions
virtual void SetPhysicsOptions() override
Definition: PhysicsListSvc.cxx:150