Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further simplify the decay file parsing grammar and implement more get methods for EvtGen keywords + unit tests #354

Merged
merged 10 commits into from
Jul 24, 2023
17 changes: 8 additions & 9 deletions src/decaylanguage/data/decfile.lark
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
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_PYTHIA8_COMMANDS : "PythiaAliasParam" | "PythiaBothParam" | "PythiaGenericParam"

LABEL_PYTHIA_PARAMETERS : "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" // Lineshape flat | non-relativistic BW, spikes

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

Expand All @@ -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

Expand All @@ -35,7 +35,6 @@ alias : "Alias" LABEL LABEL
chargeconj : "ChargeConj" LABEL LABEL

changemasslimit : LABEL_CHANGE_MASS LABEL SIGNED_NUMBER // Set upper/lower mass cuts on a lineshape

LABEL_CHANGE_MASS : "ChangeMassMin" | "ChangeMassMax"

?commands : global_photos
Expand Down
99 changes: 67 additions & 32 deletions src/decaylanguage/dec/dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,16 @@ 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
<PYTHIA_DEF> = "PythiaBothParam" and/or "PythiaAliasParam",
set in the input parsed file with statements of the form
"<PYTHIA_DEF> <NAME>=<LABEL>"
or
"<PYTHIA_DEF> <NAME>=<NUMBER>",
as {"PythiaAliasParam": {'NAME1': 'LABEL1', 'NAME2': VALUE2, ...},
"PythiaBothParam": {'NAME3': 'LABEL3', 'NAME4': VALUE4, ...}}.
Return a dictionary of all Pythia 8 commands
"PythiaGenericParam" and/or "PythiaAliasParam" and/or "PythiaBothParam",
with keys corresponding to the 3 types specifying whether the command is for
generic decays, alias decays, or both.
The commands are set in the input parsed file with statements of the form
"Pythia<TYPE>Param <MODULE>:<PARAM>=<LABEL_OR_VALUE>.
The dictionary takes the form
{"PythiaAliasParam": {'<MODULE1>:<PARAM1>': 'LABEL1', '<MODULE1>:<PARAM2>': VALUE2, ...},
"PythiaBothParam": {'<MODULE2>:<PARAM3>': 'LABEL3', '<MODULE3>:<PARAM4>': VALUE4, ...},
"PythiaGenericParam": {'<MODULE4>:<PARAM5>': 'LABEL5', '<MODULE5>:<PARAM6>': VALUE6, ...}}.
"""
self._check_parsing()
return get_pythia_definitions(self._parsed_dec_file)
Expand All @@ -400,10 +402,11 @@ def dict_jetset_definitions(self) -> dict[str, dict[int, int | float | str]]:
"""
Return a dictionary of all JETSET definitions in the input parsed file,
of the form
"JetSetPar <PARAM>(<PNUMBER>)=<NUMBER>"
as {'PARAM1': {PNUMBER1: VALUE1, PNUMBER2: VALUE2, ...},
'PARAM2': {...},
...}.
"JetSetPar <MODULE>(<PARAMETER>)=<VALUE>"
as
{'MODULE1': {PARAMETER1: VALUE1, PARAMETER2: VALUE2, ...},
'MODULE2': {...},
...}.
"""
self._check_parsing()
return get_jetset_definitions(self._parsed_dec_file)
Expand All @@ -430,7 +433,9 @@ def dict_lineshape_settings(self) -> dict[str, dict[str, str | float]]:
PARTICLE2: {'lineshape': 'NAME2',
'BlattWeisskopf': VALUE2,
'ChangeMassMin': VALUE22,
'ChangeMassMax': VALUE23},
'ChangeMassMax': VALUE23,
'IncludeBirthFactor': TRUE_OR_FALSE,
'IncludeDecayFactor': TRUE_OR_FALSE},
...
}
where not all "sub-dictionaries" may contain all and/or the same keys.
Expand Down Expand Up @@ -1547,11 +1552,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),
tree.children[0].value: {
"mass": float(tree.children[1].value),
"width": float(tree.children[2].value),
}
for tree in parsed_file.find_data("particle_def")
}
Expand All @@ -1563,14 +1566,16 @@ def get_particle_property_definitions(parsed_file: Tree) -> dict[str, dict[str,

def get_pythia_definitions(parsed_file: Tree) -> dict[str, dict[str, str | float]]:
"""
Return a dictionary of all Pythia definitions, with keys corresponding to the types
<PYTHIA_DEF> = "PythiaBothParam" and/or "PythiaAliasParam",
set in the input parsed file with statements of the form
"<PYTHIA_DEF> <NAME>=<LABEL>"
or
"<PYTHIA_DEF> <NAME>=<NUMBER>",
as {"PythiaAliasParam": {'NAME1': 'LABEL1', 'NAME2': VALUE2, ...},
"PythiaBothParam": {'NAME3': 'LABEL3', 'NAME4': VALUE4, ...}}.
Return a dictionary of all Pythia 8 commands
"PythiaGenericParam" and/or "PythiaAliasParam" and/or "PythiaBothParam",
with keys corresponding to the 3 types specifying whether the command is for
generic decays, alias decays, or both.
The commands are set in the input parsed file with statements of the form
"Pythia<TYPE>Param <MODULE>:<PARAM>=<LABEL_OR_VALUE>.
The dictionary takes the form
{"PythiaAliasParam": {'<MODULE1>:<PARAM1>': 'LABEL1', '<MODULE1>:<PARAM2>': VALUE2, ...},
"PythiaBothParam": {'<MODULE2>:<PARAM3>': 'LABEL3', '<MODULE3>:<PARAM4>': VALUE4, ...},
"PythiaGenericParam": {'<MODULE4>:<PARAM5>': 'LABEL5', '<MODULE5>:<PARAM6>': VALUE6, ...}}.

Parameters
----------
Expand Down Expand Up @@ -1608,10 +1613,11 @@ def get_jetset_definitions(
"""
Return a dictionary of all JETSET definitions in the input parsed file,
of the form
"JetSetPar <PARAM>(<PNUMBER>)=<NUMBER>"
as {'PARAM1': {PNUMBER1: VALUE1, PNUMBER2: VALUE2, ...},
'PARAM2': {...},
...}.
"JetSetPar <MODULE>(<PARAMETER>)=<VALUE>"
as
{'MODULE1': {PARAMETER1: VALUE1, PARAMETER2: VALUE2, ...},
'MODULE2': {...},
...}.

Parameters
----------
Expand Down Expand Up @@ -1674,8 +1680,10 @@ def get_lineshape_settings(
PARTICLE2: {'lineshape': 'NAME2',
'BlattWeisskopf': VALUE21,
'ChangeMassMin': VALUE22,
'ChangeMassMax': VALUE23},
...
'ChangeMassMax': VALUE23,
'IncludeBirthFactor': TRUE_OR_FALSE,
'IncludeDecayFactor': TRUE_OR_FALSE},
...
}
where not all "sub-dictionaries" may contain all and/or the same keys.

Expand Down Expand Up @@ -1723,6 +1731,22 @@ 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:
Expand Down Expand Up @@ -1796,3 +1820,14 @@ 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
if arg == "no":
return False

raise ValueError(
f"String {arg!r} cannot be converted to boolean! Only 'yes/no' accepted."
)
15 changes: 12 additions & 3 deletions tests/data/defs-aliases-chargeconj.dec
Original file line number Diff line number Diff line change
Expand Up @@ -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
# Pythia 8 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
Expand All @@ -284,6 +292,7 @@ PythiaBothParam Next:numberShowEvent=0
PythiaAliasParam ParticleDecays:sophisticatedTau=3
PythiaAliasParam ParticleDecays:tauPolarization=-1.

# Old Pythia 6 parameter modifications
JetSetPar MSTU(1)=0
JetSetPar MSTU(2)=0
JetSetPar PARU(11)=0.001
Expand Down
9 changes: 9 additions & 0 deletions tests/dec/test_dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}


Expand Down Expand Up @@ -259,6 +260,14 @@ def test_dict_lineshape_settings():
"ChangeMassMin": 1.1,
"ChangeMassMax": 2.4,
},
"rho0": {
"lineshape": "LSNONRELBW",
"BlattWeisskopf": 3.0,
"ChangeMassMax": 0.9,
"ChangeMassMin": 0.7,
"IncludeBirthFactor": False,
"IncludeDecayFactor": True,
},
}


Expand Down