From 75966535246f506b92e377cd517d3835edc025cf Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Mon, 24 Jul 2023 13:21:09 +0200 Subject: [PATCH 1/9] Enhance test dec file for definitions, etc. --- tests/data/defs-aliases-chargeconj.dec | 14 +++++++++++--- tests/dec/test_dec.py | 7 +++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/data/defs-aliases-chargeconj.dec b/tests/data/defs-aliases-chargeconj.dec index 61353b56..618a21bc 100644 --- a/tests/data/defs-aliases-chargeconj.dec +++ b/tests/data/defs-aliases-chargeconj.dec @@ -272,10 +272,18 @@ Alias MyKS0pipi K_10 LSFLAT MyKS0pipi ChangeMassMin MyKS0pipi 1.1 ChangeMassMax MyKS0pipi 2.4 +# +Particle rho0 0.8 0.2 +ChangeMassMin rho0 0.7 +ChangeMassMax rho0 0.9 +LSNONRELBW rho0 +BlattWeisskopf rho0 3.0 +IncludeBirthFactor rho0 no +IncludeDecayFactor rho0 yes -#JetSet parameter modifications -#(Very important that there are no blank spaces in the parameter string!) -#Turn off B0-B0B mixing in Pythia +# JetSet parameter modifications +# (Very important that there are no blank spaces in the parameter string!) +# Turn off B0-B0B mixing in Pythia PythiaBothParam ParticleDecays:mixB=off PythiaBothParam Init:showChangedSettings=off PythiaBothParam Init:showChangedParticleData=off diff --git a/tests/dec/test_dec.py b/tests/dec/test_dec.py index 21be1661..4bdccdb4 100644 --- a/tests/dec/test_dec.py +++ b/tests/dec/test_dec.py @@ -204,6 +204,7 @@ def test_particle_property_definitions(): assert p.get_particle_property_definitions() == { "MyK*0": {"mass": 0.892, "width": 0.051}, "MyPhi": {"mass": 1.02, "width": 0.004}, + "rho0": {"mass": 0.8, "width": 0.2}, } @@ -259,6 +260,12 @@ def test_dict_lineshape_settings(): "ChangeMassMin": 1.1, "ChangeMassMax": 2.4, }, + "rho0": { + "lineshape": "LSNONRELBW", + "BlattWeisskopf": 3.0, + "ChangeMassMax": 0.9, + "ChangeMassMin": 0.7, + }, } From e8c154af707a9e6b201c018668c5342874cfe179 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Mon, 24 Jul 2023 13:47:53 +0200 Subject: [PATCH 2/9] Simplify grammar for 'Particle' --- src/decaylanguage/data/decfile.lark | 2 +- src/decaylanguage/dec/dec.py | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/decaylanguage/data/decfile.lark b/src/decaylanguage/data/decfile.lark index 1bb7746c..4090531e 100644 --- a/src/decaylanguage/data/decfile.lark +++ b/src/decaylanguage/data/decfile.lark @@ -26,7 +26,7 @@ cdecay : "CDecay" LABEL define : "Define" LABEL SIGNED_NUMBER -particle_def: "Particle" label value value // Set the mass and width of a particle (in GeV) +particle_def: "Particle" LABEL SIGNED_NUMBER SIGNED_NUMBER // Set the mass and width of a particle (in GeV) model_alias : "ModelAlias" model_label model diff --git a/src/decaylanguage/dec/dec.py b/src/decaylanguage/dec/dec.py index c1997d5b..f24be66c 100644 --- a/src/decaylanguage/dec/dec.py +++ b/src/decaylanguage/dec/dec.py @@ -1548,10 +1548,9 @@ def get_particle_property_definitions(parsed_file: Tree) -> dict[str, dict[str, try: return { tree.children[0] - .children[0] .value: { - "mass": float(tree.children[1].children[0].value), - "width": float(tree.children[2].children[0].value), + "mass": float(tree.children[1].value), + "width": float(tree.children[2].value), } for tree in parsed_file.find_data("particle_def") } From 3f656366549edcca129bf0bf9ba6bc7bb6f1c63b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Jul 2023 11:55:05 +0000 Subject: [PATCH 3/9] style: pre-commit fixes --- src/decaylanguage/dec/dec.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/decaylanguage/dec/dec.py b/src/decaylanguage/dec/dec.py index f24be66c..dc5ab6e7 100644 --- a/src/decaylanguage/dec/dec.py +++ b/src/decaylanguage/dec/dec.py @@ -1547,8 +1547,7 @@ def get_particle_property_definitions(parsed_file: Tree) -> dict[str, dict[str, """ try: return { - tree.children[0] - .value: { + tree.children[0].value: { "mass": float(tree.children[1].value), "width": float(tree.children[2].value), } From 26a8ac6344f460da8126a043238b67194303e2af Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Mon, 24 Jul 2023 14:45:15 +0200 Subject: [PATCH 4/9] Improve grammar for include factors and add a get method --- src/decaylanguage/data/decfile.lark | 5 ++++- src/decaylanguage/dec/dec.py | 26 ++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/decaylanguage/data/decfile.lark b/src/decaylanguage/data/decfile.lark index 4090531e..58e2b95b 100644 --- a/src/decaylanguage/data/decfile.lark +++ b/src/decaylanguage/data/decfile.lark @@ -16,7 +16,10 @@ ls_def : LABEL_LINESHAPE LABEL // Choose a lineshape for a particle LABEL_LINESHAPE : "LSFLAT" | "LSNONRELBW" | "LSMANYDELTAFUNC" -inc_factor: ("IncludeBirthFactor" | "IncludeDecayFactor") label ("yes" | "no") // Presence of the birth/decay momentum factor and form-factor +inc_factor: LABEL_INCLUDE_FACTOR LABEL BOOLEAN_INCLUDE_FACTOR // Presence of the birth/decay momentum factor and form-factor + +LABEL_INCLUDE_FACTOR : "IncludeBirthFactor" | "IncludeDecayFactor" +BOOLEAN_INCLUDE_FACTOR : "yes" | "no" setlsbw : "BlattWeisskopf" LABEL SIGNED_NUMBER // Set Blatt-Weisskopf barrier factor for a lineshape diff --git a/src/decaylanguage/dec/dec.py b/src/decaylanguage/dec/dec.py index f24be66c..cef64c1a 100644 --- a/src/decaylanguage/dec/dec.py +++ b/src/decaylanguage/dec/dec.py @@ -1547,8 +1547,7 @@ def get_particle_property_definitions(parsed_file: Tree) -> dict[str, dict[str, """ try: return { - tree.children[0] - .value: { + tree.children[0].value: { "mass": float(tree.children[1].value), "width": float(tree.children[2].value), } @@ -1722,6 +1721,20 @@ def get_lineshape_settings( f"{tree.children[0].value}": float(tree.children[2].value) } + # Presence of the birth/decay momentum factor and form-factor + for tree in parsed_file.find_data("inc_factor"): + particle_or_alias = tree.children[1].value + if particle_or_alias in d: + if tree.children[0].value in d[particle_or_alias]: + raise RuntimeError( + f"The birth/decay momentum factor for particle/alias {particle_or_alias} seems to be redefined." + ) from None + d[particle_or_alias][f"{tree.children[0].value}"] = _str_to_bool(tree.children[2].value) + else: + d[particle_or_alias] = { + f"{tree.children[0].value}": _str_to_bool(tree.children[2].value) + } + return d except Exception as err: @@ -1795,3 +1808,12 @@ def _str_or_float(arg: str) -> str | float: return float(arg) except Exception: return arg + + +def _str_to_bool(arg: str) -> bool: + if arg == "yes": + return True + elif arg == "no": + return False + else: + raise(f"String {arg!r} cannot be converted to boolean! Only 'yes/no' accepted.") From 205c21471b699f47acd6be52f02f7c05e2ecaf52 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Mon, 24 Jul 2023 14:45:36 +0200 Subject: [PATCH 5/9] Add test for it --- tests/dec/test_dec.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/dec/test_dec.py b/tests/dec/test_dec.py index 4bdccdb4..da6a703b 100644 --- a/tests/dec/test_dec.py +++ b/tests/dec/test_dec.py @@ -265,6 +265,8 @@ def test_dict_lineshape_settings(): "BlattWeisskopf": 3.0, "ChangeMassMax": 0.9, "ChangeMassMin": 0.7, + "IncludeBirthFactor" : False, + "IncludeDecayFactor": True, }, } From 481aae871e6b0b178fa421c10f7be683434f4e06 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Mon, 24 Jul 2023 14:49:47 +0200 Subject: [PATCH 6/9] Please style implemented in pre-commit --- src/decaylanguage/dec/dec.py | 14 +++++++++----- tests/dec/test_dec.py | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/decaylanguage/dec/dec.py b/src/decaylanguage/dec/dec.py index cef64c1a..49e6721f 100644 --- a/src/decaylanguage/dec/dec.py +++ b/src/decaylanguage/dec/dec.py @@ -1729,10 +1729,12 @@ def get_lineshape_settings( raise RuntimeError( f"The birth/decay momentum factor for particle/alias {particle_or_alias} seems to be redefined." ) from None - d[particle_or_alias][f"{tree.children[0].value}"] = _str_to_bool(tree.children[2].value) + d[particle_or_alias][f"{tree.children[0].value}"] = _str_to_bool( + tree.children[2].value + ) else: d[particle_or_alias] = { - f"{tree.children[0].value}": _str_to_bool(tree.children[2].value) + f"{tree.children[0].value}": _str_to_bool(tree.children[2].value) } return d @@ -1813,7 +1815,9 @@ def _str_or_float(arg: str) -> str | float: def _str_to_bool(arg: str) -> bool: if arg == "yes": return True - elif arg == "no": + if arg == "no": return False - else: - raise(f"String {arg!r} cannot be converted to boolean! Only 'yes/no' accepted.") + + raise ValueError( + f"String {arg!r} cannot be converted to boolean! Only 'yes/no' accepted." + ) diff --git a/tests/dec/test_dec.py b/tests/dec/test_dec.py index da6a703b..63f86dca 100644 --- a/tests/dec/test_dec.py +++ b/tests/dec/test_dec.py @@ -265,7 +265,7 @@ def test_dict_lineshape_settings(): "BlattWeisskopf": 3.0, "ChangeMassMax": 0.9, "ChangeMassMin": 0.7, - "IncludeBirthFactor" : False, + "IncludeBirthFactor": False, "IncludeDecayFactor": True, }, } From 411cdcfe301062eb3374bc2a53086c6ec6d9c7d1 Mon Sep 17 00:00:00 2001 From: Eduardo Rodrigues Date: Mon, 24 Jul 2023 15:01:55 +0200 Subject: [PATCH 7/9] Improve bits of docstrings --- src/decaylanguage/data/decfile.lark | 8 ++++---- src/decaylanguage/dec/dec.py | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/decaylanguage/data/decfile.lark b/src/decaylanguage/data/decfile.lark index 58e2b95b..01765cbe 100644 --- a/src/decaylanguage/data/decfile.lark +++ b/src/decaylanguage/data/decfile.lark @@ -6,15 +6,15 @@ start : _NEWLINE* (line _NEWLINE+)* ("End" _NEWLINE+)? ?line : define | particle_def | pythia_def | jetset_def | ls_def | model_alias | alias | chargeconj | commands | decay | cdecay | copydecay | setlspw | setlsbw | changemasslimit | inc_factor -pythia_def : LABEL_PYTHIA_PARAMETERS LABEL ":" LABEL "=" (LABEL | SIGNED_NUMBER) +pythia_def : LABEL_PYTHIA8_COMMANDS LABEL ":" LABEL "=" (LABEL | SIGNED_NUMBER) // Pythia 8 commands -LABEL_PYTHIA_PARAMETERS : "PythiaBothParam" | "PythiaAliasParam" +LABEL_PYTHIA8_COMMANDS : "PythiaBothParam" | "PythiaAliasParam" -jetset_def : "JetSetPar" LABEL "=" SIGNED_NUMBER +jetset_def : "JetSetPar" LABEL "=" SIGNED_NUMBER // Old Pythia 6 commands ls_def : LABEL_LINESHAPE LABEL // Choose a lineshape for a particle -LABEL_LINESHAPE : "LSFLAT" | "LSNONRELBW" | "LSMANYDELTAFUNC" +LABEL_LINESHAPE : "LSFLAT" | "LSNONRELBW" | "LSMANYDELTAFUNC" // Lineshape flat | non-relativistic BW, spikes inc_factor: LABEL_INCLUDE_FACTOR LABEL BOOLEAN_INCLUDE_FACTOR // Presence of the birth/decay momentum factor and form-factor diff --git a/src/decaylanguage/dec/dec.py b/src/decaylanguage/dec/dec.py index 49e6721f..60de9acb 100644 --- a/src/decaylanguage/dec/dec.py +++ b/src/decaylanguage/dec/dec.py @@ -384,7 +384,7 @@ def get_particle_property_definitions(self) -> dict[str, dict[str, float]]: def dict_pythia_definitions(self) -> dict[str, dict[str, str | float]]: """ - Return a dictionary of all Pythia definitions, with keys corresponding to the types + Return a dictionary of all Pythia 8 commands, with keys corresponding to the types = "PythiaBothParam" and/or "PythiaAliasParam", set in the input parsed file with statements of the form " =