Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

106 see if kaon properties can be configured at runtime patch #110

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions include/SimCore/KaonPhysics.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <G4ParticleDefinition.hh>
#include <G4VDecayChannel.hh>
#include <G4VPhysicsConstructor.hh>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>

Expand Down Expand Up @@ -38,22 +40,25 @@ class KaonPhysics : public G4VPhysicsConstructor {
* K^+ -> \mu^+ + \nu_\mu
* K^+ -> \pi^+ + \pi^0
* K^+ -> \pi^+ + \pi^- + \pi^+
* K^+ -> \pi^+ + \pi^0 + \pi^0
* K^+ -> \pi^0 + e^+ + \nu_e
* K^+ -> \pi^0 + \mu^+ + \nu_\mu
* K^+ -> \pi^+ + \pi^0 + \pi^0
*
* And vice versa for K^-.
* The indices here correspond to the position of the branching ratio for
* that process in the corresponding parameter as well as the position in
* the decay table.
*
* @note: The order in the the decay table is sorted by the branching ratios
* of the default physics settings!
*/
enum ChargedKaonDecayChannel {
mu_nu = 0,
pi_pi0 = 1,
pi_pi_pi = 2,
pi_pi0_pi0 = 3,
pi0_e_nu = 4,
pi0_mu_nu = 5
pi0_e_nu = 3,
pi0_mu_nu = 4,
pi_pi0_pi0 = 5,
};
/**
*
Expand All @@ -62,26 +67,29 @@ class KaonPhysics : public G4VPhysicsConstructor {
*
* The processes are
*
* K^0_L -> \pi^0 + \pi^0 + \pi^0
* K^0_L -> \pi^0 + \pi^+ + \pi^-
* K^0_L -> \pi^- + e^+ + \nu_e
* K^0_L -> \pi^+ + e^- + \nu_e
* K^0_L -> \pi^0 + \pi^0 + \pi^0
* K^0_L -> \pi^- + \mu^+ + \nu_\mu
* K^0_L -> \pi^+ + \mu^- + \nu_\mu
* K^0_L -> \pi^0 + \pi^+ + \pi^-
*
* and
*
* K^0_S -> \pi^+ + \pi^-
* K^0_S -> \pi^0 + \pi^0
*
* @note: The order in the the decay table is sorted by the branching ratios
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be "must be" rather than "is", if I understood the bug correctly? If I'm right, I think the stronger language will be more helpful for the future developer reading the comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and same on L52

* of the default physics settings!
*
**/
enum KaonZeroLongDecayChannel {
pi0_pi0_pi0 = 0,
pi0_pip_pim = 1,
pip_e_nu = 2,
pim_e_nu = 3,
pim_mu_nu = 4,
pip_mu_nu = 5,
pim_e_nu = 0,
pip_e_nu = 1,
pi0_pi0_pi0 = 2,
pim_mu_nu = 3,
pip_mu_nu = 4,
pi0_pip_pim = 5,
};
enum KaonZeroShortDecayChannel {
pip_pim = 0,
Expand All @@ -102,6 +110,10 @@ class KaonPhysics : public G4VPhysicsConstructor {
std::vector<double> k0l_branching_ratios;
std::vector<double> k0s_branching_ratios;

// If > 0, dump details about what was changed
// If > 1, dump details about the initial branching ratios
int verbosity;

public:
KaonPhysics(const G4String& name,
const framework::config::Parameters& parameters);
Expand All @@ -127,6 +139,8 @@ class KaonPhysics : public G4VPhysicsConstructor {

void ConstructParticle() override;

void DumpDecayDetails(const G4ParticleDefinition* kaon) const;

/**
* Construct processes
*
Expand Down
33 changes: 21 additions & 12 deletions python/kaon_physics.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,37 @@ class KaonPhysics():
Multiplicative factor to scale the lifetime of kaons by. Default is to
scale by 1 (no scaling)

verbosity : int

If > 0: Dump details about the decay after the update
If > 1: Also dump details about the decay before the update to show the
difference

"""
def __init__(self):
self.kplus_branching_ratios = [
0.6355, # K^+ -> mu^+ + nu_mu
0.2066, # K^+ -> pi^+ + pi^0
0.0559, # K^+ -> pi^+ + pi^- + pi^+
0.01761, # K^+ -> pi^+ + pi^0 + pi^0
0.0507, # K^+ -> pi^0 + e^+ + nu_e
0.0335, # K^+ -> pi^0 + mu^+ + nu_mu
0.01761, # K^+ -> pi^+ + pi^0 + pi^0
]
self.kminus_branching_ratios = [
0.6355, # K^- -> mu^- + anti_nu_mu
0.2066, # K^- -> pi^- + pi^0
0.0559, # K^- -> pi^- + pi^+ + pi^-
0.01761, # K-+ -> pi^- + pi^0 + pi^0
0.0507, # K-+ -> pi^0 + e^- + anti_nu_e
0.0335, # K-+ -> pi^0 + mu^- + anti_nu_mu
0.01761, # K-+ -> pi^- + pi^0 + pi^0
]
self.k0l_branching_ratios = [
0.1952, # K^0_L -> pi^0 + pi^0 + pi^0
0.1254, # K^0_L -> pi^0 + pi^+ + pi^-
0.2027, # K^0_L -> pi^- + e^+ + nu_e
0.2027, # K^0_L -> pi^+ + e^- + anti_nu_e
0.1952, # K^0_L -> pi^0 + pi^0 + pi^0
0.1352, # K^0_L -> pi^- + mu^+ + nu_mu
0.1352, # K^0_L -> pi^+ + mu^- + anti_nu_mu
0.1254, # K^0_L -> pi^0 + pi^+ + pi^-
]
self.k0s_branching_ratios = [
0.6920, # K^0_S -> pi^+ + pi^-
Expand All @@ -78,6 +84,8 @@ def __init__(self):
self.k0l_lifetime_factor = 1.
self.k0s_lifetime_factor = 1.

self.verbosity=0


def upKaons():
"""Returns a configuration of the kaon physics corresponding
Expand All @@ -93,22 +101,23 @@ def upKaons():
kaon_physics = KaonPhysics()
kaon_physics.kplus_branching_ratios = [
0.8831, # K^+ -> mu^+ + nu_mu
0., # K^+ -> pi^+ + pi^0
0., # K^+ -> pi^+ + pi^- + pi^+
0., # K^+ -> pi^+ + pi^0 + pi^0
0., # K^+ -> pi^+ + pi^0
0., # K^+ -> pi^+ + pi^- + pi^+
0.0704, # K^+ -> pi^0 + e^+ + nu_e
0.0465, # K^+ -> pi^0 + mu^+ + nu_mu
0., # K^+ -> pi^+ + pi^0 + pi^0
]
kaon_physics.kminus_branching_ratios = [
0.8831, # K^- -> mu^- + anti_nu_mu
0., # K^- -> pi^- + pi^0
0., # K^- -> pi^- + pi^+ + pi^-
0., # K-+ -> pi^- + pi^0 + pi^0
0.0704, # K-+ -> pi^0 + e^- + anti_nu_e
0.0464, # K-+ -> pi^0 + mu^- + anti_nu_mu
0., # K^- -> pi^- + pi^0
0., # K^- -> pi^- + pi^+ + pi^-
0.0704, # K^- -> pi^0 + e^- + anti_nu_e
0.0464, # K^- -> pi^0 + mu^- + anti_nu_mu
0., # K^- -> pi^- + pi^0 + pi^0
]
kaon_physics.kplus_lifetime_factor = 1/50.
kaon_physics.kminus_lifetime_factor = 1/50.
kaon_physics.verbosity = 2
return kaon_physics

def __setattr__(self, key, value):
Expand Down
36 changes: 35 additions & 1 deletion src/SimCore/KaonPhysics.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ KaonPhysics::KaonPhysics(const G4String& name,
parameters.getParameter<double>("kminus_lifetime_factor");
k0l_lifetime_factor = parameters.getParameter<double>("k0l_lifetime_factor");
k0s_lifetime_factor = parameters.getParameter<double>("k0s_lifetime_factor");
verbosity = parameters.getParameter<int>("verbosity");
}
void KaonPhysics::setDecayProperties(
G4ParticleDefinition* kaon, const std::vector<double>& branching_ratios,
double lifetime_factor) const {
kaon->SetPDGLifeTime(kaon->GetPDGLifeTime() * lifetime_factor);
auto table{kaon->GetDecayTable()};
if (!table) {
EXCEPTION_RAISE("KaonPhysics", "Unable to get the decay table from " +
kaon->GetParticleName());
}
if (verbosity > 1) {
std::cout << "Decay details after setting branching ratios and lifetimes"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and other places, would it be possible to use the logger instead of standard printouts? I see a use case where we use these modifications routinely and would only want to see details at debug or info level.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, although I'm not familiar with how to do that with things in LDMX-sw that aren't processors

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically ldmx_log is a macro that expects the logger to be a variable named theLog_

I also wrote another macro enableLogging that you could put in the class declaration which defines theLog_ as a member variable making it accessible to all member functions.

https://ldmx-software.github.io/Logging.html#more-detail-logging-outside-processors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me neither :) hence the hesitation. To be clear I think a combination of the local verbosity and the log level is the best way to do it in that case, so this suggestion is just about where the message is directed.

Copy link
Contributor Author

@EinarElen EinarElen Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, was straight-forward to do although the enableLogging macro didn't play nicely by default with the namespace I was in so had to do

  mutable framework::logging::logger theLog_{
      framework::logging::makeLogger("KaonPhysics")};

directly. Not the end of the world I guess

You run into something like

/Users/einarelen/ldmx/ldmx-sw/Framework/include/Framework/Logger.h:137:11: error: 'logging' does not name a type; did you mean 'long'?
  137 |   mutable logging::logger theLog_{logging::makeLogger(name)};
      |           ^~~~~~~
/Users/einarelen/ldmx/ldmx-sw/Framework/include/Framework/Logger.h:137:11: note: in definition of macro 'enableLogging'
  137 |   mutable logging::logger theLog_{logging::makeLogger(name)};
      |           ^~~~~~~

<< std::endl;
DumpDecayDetails(kaon);
}
kaon->SetPDGLifeTime(kaon->GetPDGLifeTime() * lifetime_factor);
if (kaon == G4KaonZeroLong::Definition()) {
(*table)[KaonZeroLongDecayChannel::pi0_pi0_pi0]->SetBR(
branching_ratios[KaonZeroLongDecayChannel::pi0_pi0_pi0]);
Expand Down Expand Up @@ -60,6 +66,11 @@ void KaonPhysics::setDecayProperties(
(*table)[ChargedKaonDecayChannel::pi0_mu_nu]->SetBR(
branching_ratios[ChargedKaonDecayChannel::pi0_mu_nu]);
}
if (verbosity > 0) {
std::cout << "Decay details after setting branching ratios and lifetimes"
<< std::endl;
DumpDecayDetails(kaon);
}
}
void KaonPhysics::ConstructParticle() {
auto kaonPlus{G4KaonPlus::Definition()};
Expand All @@ -79,4 +90,27 @@ void KaonPhysics::ConstructParticle() {
setDecayProperties(kaonShort, k0s_branching_ratios, k0s_lifetime_factor);
}

void KaonPhysics::DumpDecayDetails(const G4ParticleDefinition* kaon) const {
std::cout << "Decay table details for " << kaon->GetParticleName()
<< std::endl
<< std::scientific << std::setprecision(15);
std::cout << "PDG Lifetime " << kaon->GetPDGLifeTime() << std::endl;
const auto table{kaon->GetDecayTable()};
const int entries{table->entries()};
for (auto i{0}; i < entries; ++i) {
const auto channel{(*table)[i]};
std::cout << "Channel " << i << " Kinematics type "
<< channel->GetKinematicsName() << " with BR " << channel->GetBR()
<< std::endl;
std::cout << kaon->GetParticleName() << " -> ";
const auto daughters{channel->GetNumberOfDaughters()};
for (auto j{0}; j < daughters - 1; ++j) {
std::cout << channel->GetDaughter(j)->GetParticleName() << " + ";
}
// Special formatting for last one :)
std::cout << channel->GetDaughter(daughters - 1)->GetParticleName()
<< std::endl;
}
}

} // namespace simcore