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

Start adding get methods and tests for recently added EvtGen keywords #348

Merged
merged 6 commits into from
Jul 20, 2023
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
5 changes: 3 additions & 2 deletions src/decaylanguage/data/decfile.lark
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ inc_factor: ("IncludeBirthFactor" | "IncludeDecayFactor") label ("yes" | "no") /

setlsbw : "BlattWeisskopf" label value // Set Blatt-Weisskopf barrier factor for a lineshape

setlspw : "SetLineshapePW" label label label value
setlspw : "SetLineshapePW" LABEL LABEL LABEL INT // Redefine Partial Wave for label -> label label

cdecay : "CDecay" label

define : "Define" label value

particle_def: "Particle" label value value // Set the mass and width of a paricle (in GeV)
particle_def: "Particle" label value value // Set the mass and width of a particle (in GeV)

model_alias : "ModelAlias" model_label model

Expand Down Expand Up @@ -60,6 +60,7 @@ model_options : (value | LABEL | _NEWLINE | _COMMA)+
// To use a fast parser, we need to avoid conflicts

%import common.WS_INLINE
%import common.INT
%import common.SIGNED_NUMBER

// Disregard comments, (multiple) newlines and whitespace in parser tree
Expand Down
81 changes: 63 additions & 18 deletions src/decaylanguage/dec/dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,29 @@ def dict_charge_conjugates(self) -> dict[str, str]:
self._check_parsing()
return get_charge_conjugate_defs(self._parsed_dec_file)

def get_particle_property_definitions(self) -> dict[str, dict[str, float]]:
"""
Return a dictionary of all particle property definitions
in the input parsed file, of the form "Particle <PARTICLE> <MASS> <WIDTH>",
as {'PARTICLE1': {'mass': MASS1, 'width': WIDTH1},
'PARTICLE2': {'mass': MASS2, 'width': WIDTH2}, ...}.

Note
----
Particles are often defined via aliases and post-processing may be needed
to match the mass and width to the actual particle.
"""
self._check_parsing()
return get_particle_property_definitions(self._parsed_dec_file)

def dict_pythia_definitions(self) -> dict[str, str | float]:
"""
Return a dictionary of all Pythia definitions in the input parsed file,
of the form
"PythiaBothParam <NAME>=<LABEL>"
Return a dictionary of all Pythia definitions, of type
<PYTHIA_DEF> = "PythiaBothParam" or "PythiaAliasParam",
in the input parsed file, of the form
"<PYTHIA_DEF> <NAME>=<LABEL>"
or
"PythiaBothParam <NAME>=<NUMBER>",
"<PYTHIA_DEF> <NAME>=<NUMBER>",
as {'NAME1': 'LABEL1', 'NAME2': VALUE2, ...}.
"""
self._check_parsing()
Expand Down Expand Up @@ -1500,30 +1516,52 @@ def get_charge_conjugate_defs(parsed_file: Tree) -> dict[str, str]:
) from err


def get_particle_property_definitions(parsed_file: Tree) -> dict[str, dict[str, float]]:
"""
Return a dictionary of all particle property definitions
in the input parsed file, of the form "Particle <PARTICLE> <MASS> <WIDTH>",
as {'PARTICLE1': {'mass': MASS1, 'width': WIDTH1},
'PARTICLE2': {'mass': MASS2, 'width': WIDTH2}, ...}.

Parameters
----------
parsed_file: Lark Tree instance
Input parsed file.
"""
try:
return {
tree.children[0]
.children[0]
.value: {
"mass": float(tree.children[1].children[0].value),
"width": float(tree.children[2].children[0].value),
}
for tree in parsed_file.find_data("particle_def")
}
except Exception as err:
raise RuntimeError(
"Input parsed file does not seem to have the expected structure."
) from err


def get_pythia_definitions(parsed_file: Tree) -> dict[str, str | float]:
"""
Return a dictionary of all Pythia definitions in the input parsed file,
of the form
"PythiaBothParam <NAME>=<LABEL>"
Return a dictionary of all Pythia definitions, of type
<PYTHIA_DEF> = "PythiaBothParam" or "PythiaAliasParam",
in the input parsed file, of the form
"<PYTHIA_DEF> <NAME>=<LABEL>"
or
"PythiaBothParam <NAME>=<NUMBER>",
"<PYTHIA_DEF> <NAME>=<NUMBER>",
as {'NAME1': 'LABEL1', 'NAME2': VALUE2, ...}.

Parameters
----------
parsed_file: Lark Tree instance
Input parsed file.
"""

def str_or_float(arg: str) -> str | float:
try:
return float(arg)
except Exception:
return arg

try:
return {
f"{tree.children[0].value}:{tree.children[1].value}": str_or_float(
f"{tree.children[0].value}:{tree.children[1].value}": _str_or_float(
tree.children[2].value
)
for tree in parsed_file.find_data("pythia_def")
Expand Down Expand Up @@ -1613,8 +1651,8 @@ def get_lineshape_definitions(
try:
d = []
for tree in parsed_file.find_data("setlspw"):
particles = [p.children[0].value for p in tree.children[:-1]]
val = int(tree.children[3].children[0].value)
particles = [p.value for p in tree.children[:-1]]
val = int(tree.children[3].value)
d.append((particles, val))
return d
except Exception as err:
Expand Down Expand Up @@ -1651,3 +1689,10 @@ def get_global_photos_flag(parsed_file: Tree) -> int:
end_item = tree[-1] # Use the last one if several are present !
val = end_item.children[0].data
return PhotosEnum.yes if val == "yes" else PhotosEnum.no


def _str_or_float(arg: str) -> str | float:
try:
return float(arg)
except Exception:
return arg
25 changes: 25 additions & 0 deletions tests/data/defs-aliases-chargeconj.dec
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,28 @@ Alias Omega_cc+sig Omega_cc+
Alias anti-Omega_cc-sig anti-Omega_cc-
ChargeConj Omega_cc+sig anti-Omega_cc-sig

# Specifications typically relevant when dealing with lineshapes,
# and typically using particle name aliases
#
Alias MyK*0 K*0
Particle MyK*0 0.892 0.051 # To set a particle mass and width
LSNONRELBW MyK*0 # To set the lineshape for a particle
BlattWeisskopf MyK*0 0.0 # To set the Blatt-Weisskopf barrier factor for a lineshape
ChangeMassMin MyK*0 0.5 # To set a lower mass cut on a lineshape
ChangeMassMax MyK*0 3.5 # ... an upper mass ...
#
Alias MyPhi phi
Particle MyPhi 1.02 0.004
LSNONRELBW MyPhi
BlattWeisskopf MyPhi 0.0
ChangeMassMin MyPhi 1.0
ChangeMassMax MyPhi 1.04
#
Alias MyKS0pipi K_10
LSFLAT MyKS0pipi
ChangeMassMin MyKS0pipi 1.1
ChangeMassMax MyKS0pipi 2.4

#JetSet parameter modifications
#(Very important that there are no blank spaces in the parameter string!)
#Turn off B0-B0B mixing in Pythia
Expand All @@ -259,6 +281,9 @@ PythiaBothParam Init:showChangedSettings=off
PythiaBothParam Init:showChangedParticleData=off
PythiaBothParam Next:numberShowEvent=0

PythiaAliasParam ParticleDecays:sophisticatedTau=3
PythiaAliasParam ParticleDecays:tauPolarization=-1.

JetSetPar MSTU(1)=0
JetSetPar MSTU(2)=0
JetSetPar PARU(11)=0.001
Expand Down
14 changes: 13 additions & 1 deletion tests/dec/test_dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_aliases_parsing():
p = DecFileParser(DIR / "../data/defs-aliases-chargeconj.dec")
p.parse()

assert len(p.dict_aliases()) == 132
assert len(p.dict_aliases()) == 135


def test_model_aliases_parsing():
Expand Down Expand Up @@ -187,6 +187,16 @@ def test_charge_conjugates_parsing():
assert len(p.dict_charge_conjugates()) == 77


def test_particle_property_definitions():
p = DecFileParser(DIR / "../data/defs-aliases-chargeconj.dec")
p.parse()

assert p.get_particle_property_definitions() == {
"MyK*0": {"mass": 0.892, "width": 0.051},
"MyPhi": {"mass": 1.02, "width": 0.004},
}


def test_pythia_definitions_parsing():
p = DecFileParser(DIR / "../data/defs-aliases-chargeconj.dec")
p.parse()
Expand All @@ -196,6 +206,8 @@ def test_pythia_definitions_parsing():
"Init:showChangedSettings": "off",
"Init:showChangedParticleData": "off",
"Next:numberShowEvent": 0.0,
"ParticleDecays:sophisticatedTau": 3,
"ParticleDecays:tauPolarization": -1.0,
}


Expand Down