15Check reading and writing of evt.pdl files as well as errors when creating new particles.
18 - the ROOT.TDatabasePDG instance is in fact an EvtGenDatabasePDG
19 - reading a pdl file raises a warning if it's not the original one
20 - creating a particle which already exists is forbidden
21 - creating particles with whitespace in the name is forbidden
22 - writing an evt.pdl results in the same output as the original pdl, except for
23 whitespace and additionally defined particles
27from tempfile
import NamedTemporaryFile
35db = ROOT.TDatabasePDG.Instance()
44db.AddParticle(
"e-",
"electron", 0,
False, 0, 0,
"duplicate", 11, 0, 0, 0, 0, 0, 0)
46db.AddParticle(
"invalid name",
"invalid name", 0,
False, 0, 0,
"custom", 2345, 0, 0, 0, 0, 0, 0)
53default_evtpdl = basf2.find_file(os.path.join(
"data",
"framework",
"particledb",
"evt.pdl"))
55with NamedTemporaryFile()
as tempfile:
57 db.WriteEvtGenTable(tempfile.name)
60 with open(default_evtpdl)
as default:
61 old_lines = [x.split()
for x
in default
if x[0] !=
'*' and not x.startswith(
"end")]
62 with open(tempfile.name)
as changed:
63 new_lines = [x.split()
for x
in changed
if x[0] !=
'*' and not x.startswith(
"end")]
65 if len(old_lines) != len(new_lines):
66 basf2.B2ERROR(
"Different number of lines when writing pdl file")
70 types = [str] * 4 + [int, float, float, float, int, int, float, int]
72 for old, new
in zip(old_lines, new_lines):
74 if len(old) != len(new):
75 basf2.B2ERROR(
"Number of field different between old and new:",
79 elif len(new) < len(types):
80 basf2.B2ERROR(
"Not enough fields", old=old, new=new)
84 if not all(t(o) == t(n)
for t, o, n
in zip(types, old, new)):
85 basf2.B2ERROR(
"Particles differ", old=old, new=new)
88 with NamedTemporaryFile()
as tempfile2:
90 db.AddParticle(
"testparticle",
"testparticle", 1,
False, 2, 3,
"custom", 10001, 4, 5, 6, 7, 8, 9)
95 db.WriteEvtGenTable(tempfile2.name)
97 subprocess.call([
"diff",
"--old-line-format=- %L",
"--new-line-format=+ %L",
98 "--unchanged-line-format=", tempfile.name, tempfile2.name])
100db.ReadEvtGenTable(
"/no/such/file/(I hope)")
104print(
"now we should have zero entries: ", db.ParticleList().GetEntries())
105print(
"so reread default file...")
107print(
"number of entries is now", db.ParticleList().GetEntries()
and "positive" or "zero")
add_particle(name, pdgCode, mass, width, charge, spin, max_width=None, lifetime=0, pythiaID=0, define_anti_particle=False)