Skip to content

Commit 805f833

Browse files
Start adding get methods and tests for recently added EvtGen keywords (#348)
* Document possibility to parse Pythia definition PythiaAliasParam * Add a test for recently added support * Update test .dec file with new definitions such as Particle, BlattWeisskopf * Add method get_particle_property_definitions() to retrieve info defined via the 'Particle' keyword * Add a test for it * Simplify parsing of SetLineshapePW keyword
1 parent fbaf7f2 commit 805f833

File tree

4 files changed

+104
-21
lines changed

4 files changed

+104
-21
lines changed

src/decaylanguage/data/decfile.lark

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ inc_factor: ("IncludeBirthFactor" | "IncludeDecayFactor") label ("yes" | "no") /
1616

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

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

2121
cdecay : "CDecay" label
2222

2323
define : "Define" label value
2424

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

2727
model_alias : "ModelAlias" model_label model
2828

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

6262
%import common.WS_INLINE
63+
%import common.INT
6364
%import common.SIGNED_NUMBER
6465

6566
// Disregard comments, (multiple) newlines and whitespace in parser tree

src/decaylanguage/dec/dec.py

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,29 @@ def dict_charge_conjugates(self) -> dict[str, str]:
367367
self._check_parsing()
368368
return get_charge_conjugate_defs(self._parsed_dec_file)
369369

370+
def get_particle_property_definitions(self) -> dict[str, dict[str, float]]:
371+
"""
372+
Return a dictionary of all particle property definitions
373+
in the input parsed file, of the form "Particle <PARTICLE> <MASS> <WIDTH>",
374+
as {'PARTICLE1': {'mass': MASS1, 'width': WIDTH1},
375+
'PARTICLE2': {'mass': MASS2, 'width': WIDTH2}, ...}.
376+
377+
Note
378+
----
379+
Particles are often defined via aliases and post-processing may be needed
380+
to match the mass and width to the actual particle.
381+
"""
382+
self._check_parsing()
383+
return get_particle_property_definitions(self._parsed_dec_file)
384+
370385
def dict_pythia_definitions(self) -> dict[str, str | float]:
371386
"""
372-
Return a dictionary of all Pythia definitions in the input parsed file,
373-
of the form
374-
"PythiaBothParam <NAME>=<LABEL>"
387+
Return a dictionary of all Pythia definitions, of type
388+
<PYTHIA_DEF> = "PythiaBothParam" or "PythiaAliasParam",
389+
in the input parsed file, of the form
390+
"<PYTHIA_DEF> <NAME>=<LABEL>"
375391
or
376-
"PythiaBothParam <NAME>=<NUMBER>",
392+
"<PYTHIA_DEF> <NAME>=<NUMBER>",
377393
as {'NAME1': 'LABEL1', 'NAME2': VALUE2, ...}.
378394
"""
379395
self._check_parsing()
@@ -1500,30 +1516,52 @@ def get_charge_conjugate_defs(parsed_file: Tree) -> dict[str, str]:
15001516
) from err
15011517

15021518

1519+
def get_particle_property_definitions(parsed_file: Tree) -> dict[str, dict[str, float]]:
1520+
"""
1521+
Return a dictionary of all particle property definitions
1522+
in the input parsed file, of the form "Particle <PARTICLE> <MASS> <WIDTH>",
1523+
as {'PARTICLE1': {'mass': MASS1, 'width': WIDTH1},
1524+
'PARTICLE2': {'mass': MASS2, 'width': WIDTH2}, ...}.
1525+
1526+
Parameters
1527+
----------
1528+
parsed_file: Lark Tree instance
1529+
Input parsed file.
1530+
"""
1531+
try:
1532+
return {
1533+
tree.children[0]
1534+
.children[0]
1535+
.value: {
1536+
"mass": float(tree.children[1].children[0].value),
1537+
"width": float(tree.children[2].children[0].value),
1538+
}
1539+
for tree in parsed_file.find_data("particle_def")
1540+
}
1541+
except Exception as err:
1542+
raise RuntimeError(
1543+
"Input parsed file does not seem to have the expected structure."
1544+
) from err
1545+
1546+
15031547
def get_pythia_definitions(parsed_file: Tree) -> dict[str, str | float]:
15041548
"""
1505-
Return a dictionary of all Pythia definitions in the input parsed file,
1506-
of the form
1507-
"PythiaBothParam <NAME>=<LABEL>"
1549+
Return a dictionary of all Pythia definitions, of type
1550+
<PYTHIA_DEF> = "PythiaBothParam" or "PythiaAliasParam",
1551+
in the input parsed file, of the form
1552+
"<PYTHIA_DEF> <NAME>=<LABEL>"
15081553
or
1509-
"PythiaBothParam <NAME>=<NUMBER>",
1554+
"<PYTHIA_DEF> <NAME>=<NUMBER>",
15101555
as {'NAME1': 'LABEL1', 'NAME2': VALUE2, ...}.
15111556
15121557
Parameters
15131558
----------
15141559
parsed_file: Lark Tree instance
15151560
Input parsed file.
15161561
"""
1517-
1518-
def str_or_float(arg: str) -> str | float:
1519-
try:
1520-
return float(arg)
1521-
except Exception:
1522-
return arg
1523-
15241562
try:
15251563
return {
1526-
f"{tree.children[0].value}:{tree.children[1].value}": str_or_float(
1564+
f"{tree.children[0].value}:{tree.children[1].value}": _str_or_float(
15271565
tree.children[2].value
15281566
)
15291567
for tree in parsed_file.find_data("pythia_def")
@@ -1613,8 +1651,8 @@ def get_lineshape_definitions(
16131651
try:
16141652
d = []
16151653
for tree in parsed_file.find_data("setlspw"):
1616-
particles = [p.children[0].value for p in tree.children[:-1]]
1617-
val = int(tree.children[3].children[0].value)
1654+
particles = [p.value for p in tree.children[:-1]]
1655+
val = int(tree.children[3].value)
16181656
d.append((particles, val))
16191657
return d
16201658
except Exception as err:
@@ -1651,3 +1689,10 @@ def get_global_photos_flag(parsed_file: Tree) -> int:
16511689
end_item = tree[-1] # Use the last one if several are present !
16521690
val = end_item.children[0].data
16531691
return PhotosEnum.yes if val == "yes" else PhotosEnum.no
1692+
1693+
1694+
def _str_or_float(arg: str) -> str | float:
1695+
try:
1696+
return float(arg)
1697+
except Exception:
1698+
return arg

tests/data/defs-aliases-chargeconj.dec

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,28 @@ Alias Omega_cc+sig Omega_cc+
251251
Alias anti-Omega_cc-sig anti-Omega_cc-
252252
ChargeConj Omega_cc+sig anti-Omega_cc-sig
253253

254+
# Specifications typically relevant when dealing with lineshapes,
255+
# and typically using particle name aliases
256+
#
257+
Alias MyK*0 K*0
258+
Particle MyK*0 0.892 0.051 # To set a particle mass and width
259+
LSNONRELBW MyK*0 # To set the lineshape for a particle
260+
BlattWeisskopf MyK*0 0.0 # To set the Blatt-Weisskopf barrier factor for a lineshape
261+
ChangeMassMin MyK*0 0.5 # To set a lower mass cut on a lineshape
262+
ChangeMassMax MyK*0 3.5 # ... an upper mass ...
263+
#
264+
Alias MyPhi phi
265+
Particle MyPhi 1.02 0.004
266+
LSNONRELBW MyPhi
267+
BlattWeisskopf MyPhi 0.0
268+
ChangeMassMin MyPhi 1.0
269+
ChangeMassMax MyPhi 1.04
270+
#
271+
Alias MyKS0pipi K_10
272+
LSFLAT MyKS0pipi
273+
ChangeMassMin MyKS0pipi 1.1
274+
ChangeMassMax MyKS0pipi 2.4
275+
254276
#JetSet parameter modifications
255277
#(Very important that there are no blank spaces in the parameter string!)
256278
#Turn off B0-B0B mixing in Pythia
@@ -259,6 +281,9 @@ PythiaBothParam Init:showChangedSettings=off
259281
PythiaBothParam Init:showChangedParticleData=off
260282
PythiaBothParam Next:numberShowEvent=0
261283

284+
PythiaAliasParam ParticleDecays:sophisticatedTau=3
285+
PythiaAliasParam ParticleDecays:tauPolarization=-1.
286+
262287
JetSetPar MSTU(1)=0
263288
JetSetPar MSTU(2)=0
264289
JetSetPar PARU(11)=0.001

tests/dec/test_dec.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def test_aliases_parsing():
155155
p = DecFileParser(DIR / "../data/defs-aliases-chargeconj.dec")
156156
p.parse()
157157

158-
assert len(p.dict_aliases()) == 132
158+
assert len(p.dict_aliases()) == 135
159159

160160

161161
def test_model_aliases_parsing():
@@ -187,6 +187,16 @@ def test_charge_conjugates_parsing():
187187
assert len(p.dict_charge_conjugates()) == 77
188188

189189

190+
def test_particle_property_definitions():
191+
p = DecFileParser(DIR / "../data/defs-aliases-chargeconj.dec")
192+
p.parse()
193+
194+
assert p.get_particle_property_definitions() == {
195+
"MyK*0": {"mass": 0.892, "width": 0.051},
196+
"MyPhi": {"mass": 1.02, "width": 0.004},
197+
}
198+
199+
190200
def test_pythia_definitions_parsing():
191201
p = DecFileParser(DIR / "../data/defs-aliases-chargeconj.dec")
192202
p.parse()
@@ -196,6 +206,8 @@ def test_pythia_definitions_parsing():
196206
"Init:showChangedSettings": "off",
197207
"Init:showChangedParticleData": "off",
198208
"Next:numberShowEvent": 0.0,
209+
"ParticleDecays:sophisticatedTau": 3,
210+
"ParticleDecays:tauPolarization": -1.0,
199211
}
200212

201213

0 commit comments

Comments
 (0)