From bf9c02004121a612c6d6a758b91f5e3d50cecedc Mon Sep 17 00:00:00 2001 From: Andreas Morsch Date: Thu, 9 Oct 2025 15:51:39 +0200 Subject: [PATCH] plugin container for fluence user-scaling (needed for radiation calculations) --- .../scorer/include/G4PSCellFlux.hh | 9 +++--- source/digits_hits/scorer/src/G4PSCellFlux.cc | 17 ++++++++--- .../include/G4FluenceWeightCalculator.hh | 28 +++++++++++++++++++ source/digits_hits/utils/sources.cmake | 2 ++ .../utils/src/G4FluenceWeightCalculator.cc | 15 ++++++++++ .../utils/src/G4ScoreQuantityMessenger.cc | 4 +++ 6 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 source/digits_hits/utils/include/G4FluenceWeightCalculator.hh create mode 100644 source/digits_hits/utils/src/G4FluenceWeightCalculator.cc diff --git a/source/digits_hits/scorer/include/G4PSCellFlux.hh b/source/digits_hits/scorer/include/G4PSCellFlux.hh index 87b5820318..bf9e1c5197 100644 --- a/source/digits_hits/scorer/include/G4PSCellFlux.hh +++ b/source/digits_hits/scorer/include/G4PSCellFlux.hh @@ -60,14 +60,14 @@ class G4PSCellFlux : public G4VPrimitivePlotter G4PSCellFlux(G4String name, G4int depth = 0); G4PSCellFlux(G4String name, const G4String& unit, G4int depth = 0); ~G4PSCellFlux() override = default; - + // Use particle general weight inline void Weighted(G4bool flg = true) { weighted = flg; } - // Multiply track weight - + // Usee specific weight for scoring + inline void ScoreWeighted(G4bool flg = false) { scoreWeighted = flg; } + // void Initialize(G4HCofThisEvent*) override; void clear() override; void PrintAll() override; - virtual void SetUnit(const G4String& unit); protected: @@ -79,5 +79,6 @@ class G4PSCellFlux : public G4VPrimitivePlotter G4int HCID; G4THitsMap* EvtMap; G4bool weighted; + G4bool scoreWeighted; }; #endif diff --git a/source/digits_hits/scorer/src/G4PSCellFlux.cc b/source/digits_hits/scorer/src/G4PSCellFlux.cc index fc04f040a4..807fbf35fc 100644 --- a/source/digits_hits/scorer/src/G4PSCellFlux.cc +++ b/source/digits_hits/scorer/src/G4PSCellFlux.cc @@ -1,4 +1,3 @@ -// // ******************************************************************** // * License and Disclaimer * // * * @@ -35,6 +34,7 @@ #include "G4VPVParameterisation.hh" #include "G4UnitsTable.hh" #include "G4VScoreHistFiller.hh" +#include "G4FluenceWeightCalculator.hh" /////////////////////////////////////////////////////////////////////////////// // (Description) @@ -64,6 +64,7 @@ G4PSCellFlux::G4PSCellFlux(G4String name, const G4String& unit, G4int depth) , HCID(-1) , EvtMap(nullptr) , weighted(true) + , scoreWeighted(false) { DefineUnitAndCategory(); SetUnit(unit); @@ -78,13 +79,19 @@ G4bool G4PSCellFlux::ProcessHits(G4Step* aStep, G4TouchableHistory*) G4int idx = ((G4TouchableHistory*) (aStep->GetPreStepPoint()->GetTouchable())) ->GetReplicaNumber(indexDepth); G4double cubicVolume = ComputeVolume(aStep, idx); - - G4double CellFlux = stepLength / cubicVolume; + const G4Track* track = aStep->GetTrack(); + G4double pWeight = 1.; + if (scoreWeighted) { + G4double kineticEnergy = track->GetKineticEnergy(); + const auto* particle = track->GetParticleDefinition(); + pWeight = G4FluenceWeightCalculator::GetInstance()->GetWeight(particle, kineticEnergy); + } + G4double CellFlux = stepLength / cubicVolume * pWeight; if(weighted) CellFlux *= aStep->GetPreStepPoint()->GetWeight(); G4int index = GetIndex(aStep); EvtMap->add(index, CellFlux); - + if(!hitIDMap.empty() && hitIDMap.find(index) != hitIDMap.end()) { auto filler = G4VScoreHistFiller::Instance(); @@ -148,3 +155,5 @@ G4double G4PSCellFlux::ComputeVolume(G4Step* aStep, G4int idx) assert(solid); return solid->GetCubicVolume(); } + + diff --git a/source/digits_hits/utils/include/G4FluenceWeightCalculator.hh b/source/digits_hits/utils/include/G4FluenceWeightCalculator.hh new file mode 100644 index 0000000000..171122adbe --- /dev/null +++ b/source/digits_hits/utils/include/G4FluenceWeightCalculator.hh @@ -0,0 +1,28 @@ + +#ifndef G4FluenceWeightCalculator_h +#define G4FluenceWeightCalculator_h + +#include "G4Types.hh" +#include "G4ParticleDefinition.hh" + +class G4FluenceWeightCalculator { +public: + virtual ~G4FluenceWeightCalculator() = default; + + virtual G4double GetWeight(const G4ParticleDefinition*, G4double) const = 0; + + static void SetInstance(G4FluenceWeightCalculator* instance); + static const G4FluenceWeightCalculator* GetInstance(); + +private: + static G4FluenceWeightCalculator* fInstance; +}; + +class G4DefaultFluenceWeightCalculator : public G4FluenceWeightCalculator { +public: + virtual G4double GetWeight(const G4ParticleDefinition*, G4double) const override { + return 1.0; + } +}; + +#endif diff --git a/source/digits_hits/utils/sources.cmake b/source/digits_hits/utils/sources.cmake index 3c9f807f5a..a6d8ac5d1a 100644 --- a/source/digits_hits/utils/sources.cmake +++ b/source/digits_hits/utils/sources.cmake @@ -4,6 +4,7 @@ geant4_add_module(G4detutils PUBLIC_HEADERS G4DefaultLinearColorMap.hh + G4FluenceWeightCalculator.hh G4ScoreLogColorMap.hh G4VScoreNtupleWriter.hh G4TScoreNtupleWriter.hh @@ -22,6 +23,7 @@ geant4_add_module(G4detutils G4VScoringMesh.hh SOURCES G4DefaultLinearColorMap.cc + G4FluenceWeightCalculator.cc G4ScoreLogColorMap.cc G4VScoreNtupleWriter.cc G4ScoreQuantityMessenger.cc diff --git a/source/digits_hits/utils/src/G4FluenceWeightCalculator.cc b/source/digits_hits/utils/src/G4FluenceWeightCalculator.cc new file mode 100644 index 0000000000..6e07aab358 --- /dev/null +++ b/source/digits_hits/utils/src/G4FluenceWeightCalculator.cc @@ -0,0 +1,15 @@ +#include "G4FluenceWeightCalculator.hh" + +G4FluenceWeightCalculator* G4FluenceWeightCalculator::fInstance = nullptr; + +const G4FluenceWeightCalculator* G4FluenceWeightCalculator::GetInstance() { + if (!fInstance) { + static G4DefaultFluenceWeightCalculator defaultCalculator; + fInstance = &defaultCalculator; + } + return fInstance; +} + +void G4FluenceWeightCalculator::SetInstance(G4FluenceWeightCalculator* instance) { + fInstance = instance; +} diff --git a/source/digits_hits/utils/src/G4ScoreQuantityMessenger.cc b/source/digits_hits/utils/src/G4ScoreQuantityMessenger.cc index e6f67f4f7d..cfca1f3e6f 100644 --- a/source/digits_hits/utils/src/G4ScoreQuantityMessenger.cc +++ b/source/digits_hits/utils/src/G4ScoreQuantityMessenger.cc @@ -175,6 +175,9 @@ void G4ScoreQuantityMessenger::QuantityCommands() param = new G4UIparameter("unit", 's', true); param->SetDefaultValue("percm2"); qCellFluxCmd->SetParameter(param); + param = new G4UIparameter("scoreweighted", 'b', true); + param->SetDefaultValue("false"); + qCellFluxCmd->SetParameter(param); // qPassCellFluxCmd = new G4UIcommand("/score/quantity/passageCellFlux", this); qPassCellFluxCmd->SetGuidance("Passage cell flux scorer"); @@ -635,6 +638,7 @@ void G4ScoreQuantityMessenger::SetNewValue(G4UIcommand* command, ps = new G4PSCellFlux(token[0]); } ps->SetUnit(token[1]); + ps->ScoreWeighted(StoB(token[2])); mesh->SetPrimitiveScorer(ps); } }