From 2a92ad5bf80ead0f61502a37d784920b4fd2a2e2 Mon Sep 17 00:00:00 2001 From: alvarotd Date: Wed, 16 Aug 2023 16:50:24 +0200 Subject: [PATCH 1/7] Implementation of Optical physics tool, taken from Dual readout, https://github.com/HEP-FCC/dual-readout/blob/master/DRsim/DRsimG4Full/src/components/SimG4OpticalPhysicsList.cpp --- .../src/SimG4OpticalPhysicsList.cpp | 44 +++++++++++++++++++ SimG4Components/src/SimG4OpticalPhysicsList.h | 25 +++++++++++ 2 files changed, 69 insertions(+) create mode 100644 SimG4Components/src/SimG4OpticalPhysicsList.cpp create mode 100644 SimG4Components/src/SimG4OpticalPhysicsList.h diff --git a/SimG4Components/src/SimG4OpticalPhysicsList.cpp b/SimG4Components/src/SimG4OpticalPhysicsList.cpp new file mode 100644 index 0000000..8c02515 --- /dev/null +++ b/SimG4Components/src/SimG4OpticalPhysicsList.cpp @@ -0,0 +1,44 @@ +#include "SimG4OpticalPhysicsList.h" + +// Geant4 +#include "G4VModularPhysicsList.hh" +#include "G4OpticalPhysics.hh" +#include "G4OpticalParameters.hh" + +DECLARE_COMPONENT(SimG4OpticalPhysicsList) + +SimG4OpticalPhysicsList::SimG4OpticalPhysicsList(const std::string& aType, const std::string& aName, const IInterface* aParent) +: AlgTool(aType, aName, aParent) { + declareInterface(this); + declareProperty("fullphysics", m_physicsListTool, "Handle for the full physics list tool"); +} + +SimG4OpticalPhysicsList::~SimG4OpticalPhysicsList() {} + +StatusCode SimG4OpticalPhysicsList::initialize() { + if (AlgTool::initialize().isFailure()) + return StatusCode::FAILURE; + + m_physicsListTool.retrieve().ignore(); + + return StatusCode::SUCCESS; +} + +StatusCode SimG4OpticalPhysicsList::finalize() { return AlgTool::finalize(); } + +G4VModularPhysicsList* SimG4OpticalPhysicsList::physicsList() { + // ownership passed to SimG4Svc which will register it in G4RunManager. To be deleted in ~G4RunManager() + G4VModularPhysicsList* physicsList = m_physicsListTool->physicsList(); + + G4OpticalPhysics* opticalPhysics = new G4OpticalPhysics(); // deleted in ~G4VModularPhysicsList() + physicsList->RegisterPhysics(opticalPhysics); + + auto* opticalParams = G4OpticalParameters::Instance(); + opticalParams->SetBoundaryInvokeSD(true); + opticalParams->SetProcessActivation("Cerenkov",true); + opticalParams->SetProcessActivation("Scintillation",true); + opticalParams->SetCerenkovTrackSecondariesFirst(true); + opticalParams->SetScintTrackSecondariesFirst(true); + + return physicsList; +} diff --git a/SimG4Components/src/SimG4OpticalPhysicsList.h b/SimG4Components/src/SimG4OpticalPhysicsList.h new file mode 100644 index 0000000..386abd3 --- /dev/null +++ b/SimG4Components/src/SimG4OpticalPhysicsList.h @@ -0,0 +1,25 @@ +#ifndef SimG4OpticalPhysicsList_h +#define SimG4OpticalPhysicsList_h 1 + +// Gaudi +#include "GaudiKernel/AlgTool.h" +#include "GaudiKernel/ToolHandle.h" + +// FCCSW +#include "k4Interface/ISimG4PhysicsList.h" + +class SimG4OpticalPhysicsList : public AlgTool, virtual public ISimG4PhysicsList { +public: + explicit SimG4OpticalPhysicsList(const std::string& aType, const std::string& aName, const IInterface* aParent); + virtual ~SimG4OpticalPhysicsList(); + + virtual StatusCode initialize(); + virtual StatusCode finalize(); + virtual G4VModularPhysicsList* physicsList(); + +private: + /// Handle for the full physics list tool + ToolHandle m_physicsListTool{"SimG4FtfpBert", this, true}; +}; + +#endif From 04a90d395d3ca4b75274112a4c5b348342c7745d Mon Sep 17 00:00:00 2001 From: alvarotd Date: Fri, 18 Aug 2023 08:44:54 +0200 Subject: [PATCH 2/7] Added setters of the 3 optical processes --- SimG4Components/src/SimG4OpticalPhysicsList.cpp | 5 +++++ SimG4Components/src/SimG4OpticalPhysicsList.h | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/SimG4Components/src/SimG4OpticalPhysicsList.cpp b/SimG4Components/src/SimG4OpticalPhysicsList.cpp index 8c02515..19a7c0b 100644 --- a/SimG4Components/src/SimG4OpticalPhysicsList.cpp +++ b/SimG4Components/src/SimG4OpticalPhysicsList.cpp @@ -42,3 +42,8 @@ G4VModularPhysicsList* SimG4OpticalPhysicsList::physicsList() { return physicsList; } + +void SimG4OpticalPhysicsList::SetCerenkov(bool b) { G4OpticalParameters::Instance()->SetProcessActivation("Cerenkov",b); } +void SimG4OpticalPhysicsList::SetScintillation(bool b) { G4OpticalParameters::Instance()->SetProcessActivation("Scintillation",b); } +void SimG4OpticalPhysicsList::SetTransitionRadiation(bool b){ G4OpticalParameters::Instance()->SetProcessActivation("TransitionRadiation",b); } + diff --git a/SimG4Components/src/SimG4OpticalPhysicsList.h b/SimG4Components/src/SimG4OpticalPhysicsList.h index 386abd3..b884582 100644 --- a/SimG4Components/src/SimG4OpticalPhysicsList.h +++ b/SimG4Components/src/SimG4OpticalPhysicsList.h @@ -16,7 +16,9 @@ class SimG4OpticalPhysicsList : public AlgTool, virtual public ISimG4PhysicsList virtual StatusCode initialize(); virtual StatusCode finalize(); virtual G4VModularPhysicsList* physicsList(); - + void SetCerenkov(bool b); + void SetScintillation(bool b); + void SetTransitionRadiation(bool b); private: /// Handle for the full physics list tool ToolHandle m_physicsListTool{"SimG4FtfpBert", this, true}; From 73bf514324d722009898876a83811833caf12dce Mon Sep 17 00:00:00 2001 From: alvarotd Date: Mon, 21 Aug 2023 10:29:48 +0200 Subject: [PATCH 3/7] Optical physics lists tool. Optical processes defined at initialization time based on constructor arguments --- SimG4Components/src/SimG4OpticalPhysicsList.cpp | 9 +++------ SimG4Components/src/SimG4OpticalPhysicsList.h | 6 +++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/SimG4Components/src/SimG4OpticalPhysicsList.cpp b/SimG4Components/src/SimG4OpticalPhysicsList.cpp index 19a7c0b..5e6e79c 100644 --- a/SimG4Components/src/SimG4OpticalPhysicsList.cpp +++ b/SimG4Components/src/SimG4OpticalPhysicsList.cpp @@ -35,15 +35,12 @@ G4VModularPhysicsList* SimG4OpticalPhysicsList::physicsList() { auto* opticalParams = G4OpticalParameters::Instance(); opticalParams->SetBoundaryInvokeSD(true); - opticalParams->SetProcessActivation("Cerenkov",true); - opticalParams->SetProcessActivation("Scintillation",true); + opticalParams->SetProcessActivation("Cerenkov",SetCerenkov); + opticalParams->SetProcessActivation("Scintillation",SetScintillation); + opticalParams->SetProcessActivation("TransitionRadiation",SetTransitionRadiation); opticalParams->SetCerenkovTrackSecondariesFirst(true); opticalParams->SetScintTrackSecondariesFirst(true); return physicsList; } -void SimG4OpticalPhysicsList::SetCerenkov(bool b) { G4OpticalParameters::Instance()->SetProcessActivation("Cerenkov",b); } -void SimG4OpticalPhysicsList::SetScintillation(bool b) { G4OpticalParameters::Instance()->SetProcessActivation("Scintillation",b); } -void SimG4OpticalPhysicsList::SetTransitionRadiation(bool b){ G4OpticalParameters::Instance()->SetProcessActivation("TransitionRadiation",b); } - diff --git a/SimG4Components/src/SimG4OpticalPhysicsList.h b/SimG4Components/src/SimG4OpticalPhysicsList.h index b884582..4acaba4 100644 --- a/SimG4Components/src/SimG4OpticalPhysicsList.h +++ b/SimG4Components/src/SimG4OpticalPhysicsList.h @@ -16,9 +16,9 @@ class SimG4OpticalPhysicsList : public AlgTool, virtual public ISimG4PhysicsList virtual StatusCode initialize(); virtual StatusCode finalize(); virtual G4VModularPhysicsList* physicsList(); - void SetCerenkov(bool b); - void SetScintillation(bool b); - void SetTransitionRadiation(bool b); + Gaudi::Property SetCerenkov{this, "SetCerenkov", true, "Bool variable that enables Cerenkov process. Default true."}; + Gaudi::Property SetScintillation{this, "SetScintillation", true, "Bool variable that enables Scintillation process.. Default true."}; + Gaudi::Property SetTransitionRadiation{this, "SetTransitionRadiation", false, "Bool variable that enables transition_radiation process. . Default false."}; private: /// Handle for the full physics list tool ToolHandle m_physicsListTool{"SimG4FtfpBert", this, true}; From 6e5c84702fd39f38bede75c27a4e9088e2477f01 Mon Sep 17 00:00:00 2001 From: Alvaro Tolosa Delgado Date: Fri, 22 Sep 2023 16:18:42 +0200 Subject: [PATCH 4/7] set max verbosity level of optical physics --- SimG4Components/src/SimG4OpticalPhysicsList.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/SimG4Components/src/SimG4OpticalPhysicsList.cpp b/SimG4Components/src/SimG4OpticalPhysicsList.cpp index 5e6e79c..015e862 100644 --- a/SimG4Components/src/SimG4OpticalPhysicsList.cpp +++ b/SimG4Components/src/SimG4OpticalPhysicsList.cpp @@ -31,6 +31,7 @@ G4VModularPhysicsList* SimG4OpticalPhysicsList::physicsList() { G4VModularPhysicsList* physicsList = m_physicsListTool->physicsList(); G4OpticalPhysics* opticalPhysics = new G4OpticalPhysics(); // deleted in ~G4VModularPhysicsList() + opticalPhysics->SetVerboseLevel (2); physicsList->RegisterPhysics(opticalPhysics); auto* opticalParams = G4OpticalParameters::Instance(); From 25769609107dbdecbe23d9c894b5befd0a029db3 Mon Sep 17 00:00:00 2001 From: Alvaro Tolosa Delgado Date: Fri, 22 Sep 2023 16:27:07 +0200 Subject: [PATCH 5/7] Test of optical physics added --- SimG4Components/CMakeLists.txt | 6 + .../tests/options/optical_physics_test.py | 109 ++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 SimG4Components/tests/options/optical_physics_test.py diff --git a/SimG4Components/CMakeLists.txt b/SimG4Components/CMakeLists.txt index 72f0cae..80443fd 100644 --- a/SimG4Components/CMakeLists.txt +++ b/SimG4Components/CMakeLists.txt @@ -31,6 +31,12 @@ add_test(NAME MagFieldFromDD4hep WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMAND bash -c "source k4simgeant4env.sh; k4run ${CMAKE_CURRENT_LIST_DIR}/tests/options/magFieldTool.py" ) +add_test(NAME OpticalPhysicsTest + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + COMMAND bash -c "source k4simgeant4env.sh; k4run ${CMAKE_CURRENT_LIST_DIR}/tests/options/optical_physics_test.py" +) +SET_TESTS_PROPERTIES( OpticalPhysicsTest PROPERTIES FAIL_REGULAR_EXPRESSION " Exception;EXCEPTION;ERROR;Error" ) +SET_TESTS_PROPERTIES( OpticalPhysicsTest PROPERTIES PASS_REGULAR_EXPRESSION " Cerenkov process active: 1; Scintillation process active: 1; Rayleigh process active: 1; Absorption process active: 1") #gaudi_add_test(GeantFullSimGdml # WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} # FRAMEWORK tests/options/geant_fullsim_gdml.py) diff --git a/SimG4Components/tests/options/optical_physics_test.py b/SimG4Components/tests/options/optical_physics_test.py new file mode 100644 index 0000000..91b944b --- /dev/null +++ b/SimG4Components/tests/options/optical_physics_test.py @@ -0,0 +1,109 @@ +# This script launch a simulation of 1 event and a given detector +# The physics invoked is the FTFpBert baseline + Optical physics + +import os +import copy + +from GaudiKernel.SystemOfUnits import MeV, GeV, tesla + +# Input for simulations (momentum is expected in GeV!) +# Parameters for the particle gun simulations, dummy if use_pythia is set to True +# theta from 80 to 100 degrees corresponds to -0.17 < eta < 0.17 +momentum = 1 # in GeV +#thetaMin = 90.25 # degrees +#thetaMax = 90.25 # degrees +thetaMin = 50 # degrees +thetaMax = 130 # degrees +pdgCode = 11 # 11 electron, 13 muon, 22 photon, 111 pi0, 211 pi+ + +from Gaudi.Configuration import * + +from Configurables import FCCDataSvc +podioevent = FCCDataSvc("EventDataSvc") + +################## Particle gun setup +_pi = 3.14159 + +from Configurables import GenAlg +genAlg = GenAlg() + +from Configurables import MomentumRangeParticleGun +pgun = MomentumRangeParticleGun("ParticleGun_Electron") +pgun.PdgCodes = [pdgCode] +pgun.MomentumMin = momentum * GeV +pgun.MomentumMax = momentum * GeV +pgun.PhiMin = 0 +#pgun.PhiMax = 0 +pgun.PhiMax = 2 * _pi +pgun.ThetaMin = thetaMin * _pi / 180. +pgun.ThetaMax = thetaMax * _pi / 180. +genAlg.SignalProvider = pgun +genAlg.hepmc.Path = "hepmc" + +from Configurables import HepMCToEDMConverter +hepmc_converter = HepMCToEDMConverter() +hepmc_converter.hepmc.Path="hepmc" +genParticlesOutputName = "genParticles" +hepmc_converter.GenParticles.Path = genParticlesOutputName +hepmc_converter.hepmcStatusList = [] + +################## Simulation setup +# Detector geometry +from Configurables import GeoSvc +geoservice = GeoSvc("GeoSvc") +# if FCC_DETECTORS is empty, this should use relative path to working directory +path_to_detector = os.environ.get("FCCDETECTORS", "") +print(path_to_detector) +detectors_to_use=[ + 'Detector/DetFCCeeIDEA-LAr/compact/FCCee_DectMaster.xml', + ] +# prefix all xmls with path_to_detector +geoservice.detectors = [os.path.join(path_to_detector, _det) for _det in detectors_to_use] +geoservice.OutputLevel = INFO + + +# Geant4 service +# Configures the Geant simulation: geometry, physics list and user actions + +from Configurables import SimG4FullSimActions, SimG4Alg, SimG4PrimariesFromEdmTool, SimG4SaveParticleHistory +actions = SimG4FullSimActions() +# Uncomment if history from Geant4 decays is needed (e.g. to get the photons from pi0) and set actions=actions in SimG4Svc + uncomment saveHistTool in SimG4Alg +#actions.enableHistory=True +#actions.energyCut = 0.2 * GeV +#saveHistTool = SimG4SaveParticleHistory("saveHistory") + +from Configurables import SimG4OpticalPhysicsList +physicslisttool = SimG4OpticalPhysicsList("Physics", fullphysics="SimG4FtfpBert") + +from Configurables import SimG4Svc +geantservice = SimG4Svc("SimG4Svc", detector='SimG4DD4hepDetector', physicslist=physicslisttool, actions=actions) + +# Fixed seed to have reproducible results, change it for each job if you split one production into several jobs +# Mind that if you leave Gaudi handle random seed and some job start within the same second (very likely) you will have duplicates +geantservice.randomNumbersFromGaudi = False +geantservice.seedValue = 4242 + +# Range cut +geantservice.g4PreInitCommands += ["/tracking/verbose 0"] +# next, create the G4 algorithm, giving the list of names of tools ("XX/YY") +from Configurables import SimG4PrimariesFromEdmTool +particle_converter = SimG4PrimariesFromEdmTool("EdmConverter") +particle_converter.GenParticles.Path = genParticlesOutputName + +from Configurables import SimG4Alg +geantsim = SimG4Alg("SimG4Alg", + eventProvider=particle_converter, + OutputLevel=INFO) + +from Configurables import ApplicationMgr +ApplicationMgr( + TopAlg = [ + genAlg, + hepmc_converter, + geantsim, + ], + EvtSel = 'NONE', + EvtMax = 1, + ExtSvc = [geoservice, geantservice], + StopOnSignal = True, + ) From ecf519e049b3ec58dfae1c745c4591e9f4a2a665 Mon Sep 17 00:00:00 2001 From: Alvaro Tolosa Delgado Date: Fri, 22 Sep 2023 16:30:25 +0200 Subject: [PATCH 6/7] Typos fixed --- SimG4Components/src/SimG4OpticalPhysicsList.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SimG4Components/src/SimG4OpticalPhysicsList.h b/SimG4Components/src/SimG4OpticalPhysicsList.h index 4acaba4..facf96f 100644 --- a/SimG4Components/src/SimG4OpticalPhysicsList.h +++ b/SimG4Components/src/SimG4OpticalPhysicsList.h @@ -17,8 +17,8 @@ class SimG4OpticalPhysicsList : public AlgTool, virtual public ISimG4PhysicsList virtual StatusCode finalize(); virtual G4VModularPhysicsList* physicsList(); Gaudi::Property SetCerenkov{this, "SetCerenkov", true, "Bool variable that enables Cerenkov process. Default true."}; - Gaudi::Property SetScintillation{this, "SetScintillation", true, "Bool variable that enables Scintillation process.. Default true."}; - Gaudi::Property SetTransitionRadiation{this, "SetTransitionRadiation", false, "Bool variable that enables transition_radiation process. . Default false."}; + Gaudi::Property SetScintillation{this, "SetScintillation", true, "Bool variable that enables Scintillation process. Default true."}; + Gaudi::Property SetTransitionRadiation{this, "SetTransitionRadiation", false, "Bool variable that enables transition_radiation process. Default false."}; private: /// Handle for the full physics list tool ToolHandle m_physicsListTool{"SimG4FtfpBert", this, true}; From fd7883772b3dfaf906570558e81104a3df3fbdbd Mon Sep 17 00:00:00 2001 From: Alvaro Tolosa Delgado Date: Mon, 25 Sep 2023 09:58:33 +0200 Subject: [PATCH 7/7] Doxygen comments added, as stated in https://github.com/HEP-FCC/k4SimGeant4/pull/44#discussion_r1335498595 --- SimG4Components/src/SimG4OpticalPhysicsList.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/SimG4Components/src/SimG4OpticalPhysicsList.h b/SimG4Components/src/SimG4OpticalPhysicsList.h index facf96f..24f1f23 100644 --- a/SimG4Components/src/SimG4OpticalPhysicsList.h +++ b/SimG4Components/src/SimG4OpticalPhysicsList.h @@ -8,6 +8,18 @@ // FCCSW #include "k4Interface/ISimG4PhysicsList.h" +/** @class SimG4OpticalPhysicsList SimG4Components/src/SimG4OpticalPhysicsList.h SimG4OpticalPhysicsList.h + * + * FTFP_BERT physics list + Optical photons physics lists tool. + * + * When instantiating the tool three booleans can be passed as arguments: + * --SetCerenkov, default true, to enable Cerenkov process + * --SetScintillation, default true, to enable Scintillation process + * --SetTransitionRadiation, default true, to enable Transition Radiation process + * + * @author Alvaro Tolosa-Delgado + */ + class SimG4OpticalPhysicsList : public AlgTool, virtual public ISimG4PhysicsList { public: explicit SimG4OpticalPhysicsList(const std::string& aType, const std::string& aName, const IInterface* aParent);