Not able to modify anymore particle properties in Geant4 11 with .mac commands

Dear all,
in our application we were used to force the Ke3 branching ratio to 100%, with the following .mac commands:

/particle/select kaon+

/particle/property/decay/select 0
/particle/property/decay/br 0
/particle/property/decay/select 1
/particle/property/decay/br 0
/particle/property/decay/select 2
/particle/property/decay/br 0
/particle/property/decay/select 3
/particle/property/decay/br 1
/particle/property/decay/select 4
/particle/property/decay/br 0
/particle/property/decay/select 5
/particle/property/decay/br 0

/particle/property/decay/dump

This used to work with Geant4 10.6.3, but when running the same application compiled with Geant4 11.2.1 or Geant4 11.1.1, it is not working anymore and at runtime we get the following message thirteen times (it seems to be displayed once for each call to /particle/property/):

Particle is not selected yet !! Command ignored.

Has something changed in this respect in Geant4 11?

Many thanks.

Geant4 Version: 10.6.3 vs 11.2.1 or 11.1.1
Operating System: Red Hat Enterprise Linux 9.4
Compiler/Version: gcc 11.5.0
CMake Version: 3.29.2


Dear Fabio,

It look like that there is a bug since geant4-v11.0.0,
you can fix it by patching the G4ParticleMessenger::GetCurrentValue method:

--- geant4-v11.2.2/source/particles/management/src/G4ParticleMessenger.cc~	
+++ geant4-v11.2.2/source/particles/management/src/G4ParticleMessenger.cc	
@@ -201,6 +201,8 @@
     }
     selectCmd->SetCandidates((const char*)(candidates));
 
+    const G4ParticleDefinition* currentParticle = theParticleTable->GetSelectedParticle();
+    
     static const G4String noName("none");
     // current value
     if (currentParticle == nullptr) {

Best,
Fedor

Hi Fabio,

Yes, it seems there is a bug in Geant4 11.x where the selected particle isn’t properly registered, causing the /particle/property commands to fail.

Fedor’s patch should work for the issue. However, there is also a fix in the Geant4 development git. Below is the patch:

From 3e355fa48317d2df0c924e3663242c27327a1753 Mon Sep 17 00:00:00 2001
From: Dmitry Kalinkin <dmitry.kalinkin@gmail.com>
Date: Fri, 21 Jun 2024 15:28:31 -0400
Subject: [PATCH 1/4] G4ParticleMessenger: fix incorrect GetCurrentValue
 implementation for /particle/select

---
 source/particles/management/include/G4ParticleMessenger.hh | 1 -
 source/particles/management/src/G4ParticleMessenger.cc     | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)

diff --git a/source/particles/management/include/G4ParticleMessenger.hh b/source/particles/management/include/G4ParticleMessenger.hh
index 3ce426abf90..f5d6ecac43c 100644
--- a/source/particles/management/include/G4ParticleMessenger.hh
+++ b/source/particles/management/include/G4ParticleMessenger.hh
@@ -94,7 +94,6 @@ class G4ParticleMessenger : public G4UImessenger
     G4UIcmdWithAnInteger* verboseCmd = nullptr;
 
     G4ParticleTable* theParticleTable = nullptr;
-    G4ParticleDefinition* currentParticle = nullptr;
 
     G4ParticlePropertyMessenger* fParticlePropertyMessenger = nullptr;
 };
diff --git a/source/particles/management/src/G4ParticleMessenger.cc b/source/particles/management/src/G4ParticleMessenger.cc
index 93c3d4b6922..fde4a0900a9 100644
--- a/source/particles/management/src/G4ParticleMessenger.cc
+++ b/source/particles/management/src/G4ParticleMessenger.cc
@@ -201,6 +201,7 @@ G4String G4ParticleMessenger::GetCurrentValue(G4UIcommand* command)
     }
     selectCmd->SetCandidates((const char*)(candidates));
 
+    const G4ParticleDefinition* currentParticle = theParticleTable->GetSelectedParticle();
     static const G4String noName("none");
     // current value
     if (currentParticle == nullptr) {
-- 
GitLab


From 7c30dfeaa0acc6882b6c113eeba6d48101550f65 Mon Sep 17 00:00:00 2001
From: Ben Morgan <ben.morgan@warwick.ac.uk>
Date: Mon, 28 Oct 2024 14:16:44 +0000
Subject: [PATCH 2/4] Remove local static using ternary operator

---
 source/particles/management/src/G4ParticleMessenger.cc | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/source/particles/management/src/G4ParticleMessenger.cc b/source/particles/management/src/G4ParticleMessenger.cc
index fde4a0900a9..96e366048b0 100644
--- a/source/particles/management/src/G4ParticleMessenger.cc
+++ b/source/particles/management/src/G4ParticleMessenger.cc
@@ -202,14 +202,7 @@ G4String G4ParticleMessenger::GetCurrentValue(G4UIcommand* command)
     selectCmd->SetCandidates((const char*)(candidates));
 
     const G4ParticleDefinition* currentParticle = theParticleTable->GetSelectedParticle();
-    static const G4String noName("none");
-    // current value
-    if (currentParticle == nullptr) {
-      // no particle is selected. return null
-      return noName;
-    }
-
-    return currentParticle->GetParticleName();
+    return (currentParticle == nullptr) ? "none" : currentParticle->GetParticleName();
   }
   if (command == verboseCmd) {
     // Command   /particle/verbose
-- 
GitLab


From 7b569ebdeabc1a2c95f0c168d5b2f54f7bff0e0a Mon Sep 17 00:00:00 2001
From: Ben Morgan <ben.morgan@warwick.ac.uk>
Date: Mon, 28 Oct 2024 14:19:52 +0000
Subject: [PATCH 3/4] Update particles History file

---
 source/particles/History | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/source/particles/History b/source/particles/History
index e3aa57371f6..5f35ff2deed 100644
--- a/source/particles/History
+++ b/source/particles/History
@@ -6,6 +6,11 @@ It must **not** be used as a substitute for writing good git commit messages!
 
 -------------------------------------------------------------------------------
 
+## 2024-10-28 Ben Morgan (particles-V11-02-01)
+- Apply [GitHub PR 74](https://github.com/Geant4/geant4/pull/74), fixing bug in
+  `G4ParticleMessenger` that never updated the currently selected particle.
+  - Simplified logic in patch to remove local static.
+
 ## 2024-08-01 Alberto Ribon (particles-V11-02-00)
 - G4ChargedUnknownParticle : new class, similar to G4UnknownParticle for
   charged unknown particles.
-- 
GitLab


From eaf80e74c66cf3976e71bd2aa481c0a79d0256bb Mon Sep 17 00:00:00 2001
From: Ben Morgan <ben.morgan@warwick.ac.uk>
Date: Tue, 29 Oct 2024 11:34:41 +0000
Subject: [PATCH 4/4] Explicitly specify type to address Windows warnings

---
 source/particles/management/src/G4ParticleMessenger.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/source/particles/management/src/G4ParticleMessenger.cc b/source/particles/management/src/G4ParticleMessenger.cc
index 96e366048b0..fcdef39f3a3 100644
--- a/source/particles/management/src/G4ParticleMessenger.cc
+++ b/source/particles/management/src/G4ParticleMessenger.cc
@@ -202,7 +202,7 @@ G4String G4ParticleMessenger::GetCurrentValue(G4UIcommand* command)
     selectCmd->SetCandidates((const char*)(candidates));
 
     const G4ParticleDefinition* currentParticle = theParticleTable->GetSelectedParticle();
-    return (currentParticle == nullptr) ? "none" : currentParticle->GetParticleName();
+    return (currentParticle == nullptr) ? G4String("none") : currentParticle->GetParticleName();
   }
   if (command == verboseCmd) {
     // Command   /particle/verbose
-- 
GitLab

The good news is that this fix will be included in Geant4-11.3.0.

@bmorgan, are there any plans to backport this fix to a patch release for 11.2.x/11.1.x?
It would really help users who won’t be able to immediately upgrade to 11.3.0.
@bmorgan Sign in to CERN

Best,
Dmitri

@dkonst, yes, this will be an item to be back ported to the next patch release(s). There’s no firm date for these yet, but I’d expect early next year.

Thank you all for pointing out the bug.
Since I am using it on a computing center, I’ll wait for the availability of an official patch or for the release of version 11.3.0 and for the managers of the center to install it.

Best,
Fabio