Belle II Software development
evtpdl.py
1#!/usr/bin/env python3
2
3
10
11# this is a test executable, not a module so we don't need doxygen warnings
12# @cond SUPPRESS_DOXYGEN
13
14"""
15Check reading and writing of evt.pdl files as well as errors when creating new particles.
16
17This script tests that
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
24"""
25
26
27from tempfile import NamedTemporaryFile
28import basf2
29import ROOT
30import os
31import sys
32import subprocess
33import pdg
34
35db = ROOT.TDatabasePDG.Instance()
36
37retcode = 0
38
39# Test particle data.
40if not pdg._get_instance().testParticleData():
41 retcode = 1
42
43# create particle which already exists, should print error
44db.AddParticle("e-", "electron", 0, False, 0, 0, "duplicate", 11, 0, 0, 0, 0, 0, 0)
45# create particle with whitespace in the name, should print error
46db.AddParticle("invalid name", "invalid name", 0, False, 0, 0, "custom", 2345, 0, 0, 0, 0, 0, 0)
47# and also create a particle with the python function: should give error due to
48# duplicate pdg code
49pdg.add_particle("test2", 1, 5.28, 0.1, 0, 0.5)
50pdg.add_particle("foo\tbar", 10001, 0, 0, 0, 0)
51
52# default evt.pdl filename
53default_evtpdl = basf2.find_file(os.path.join("data", "framework", "particledb", "evt.pdl"))
54# create a temporary one and compare
55with NamedTemporaryFile() as tempfile:
56 # write a evt.pdl from the EventGenDatabasePDG
57 db.WriteEvtGenTable(tempfile.name)
58 # and make sure that it's the same as the original one except for
59 # whitespace, empty lines and lines starting with a "*"
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")]
64
65 if len(old_lines) != len(new_lines):
66 basf2.B2ERROR("Different number of lines when writing pdl file")
67 retcode = 1
68 else:
69 # field types for comparison
70 types = [str] * 4 + [int, float, float, float, int, int, float, int]
71 # check all lines for equality but don't care about formatting
72 for old, new in zip(old_lines, new_lines):
73 # make sure the lines have the same number of fields
74 if len(old) != len(new):
75 basf2.B2ERROR("Number of field different between old and new:",
76 old=old, new=new)
77 retcode = 1
78 # and that they have enough fields for comparison
79 elif len(new) < len(types):
80 basf2.B2ERROR("Not enough fields", old=old, new=new)
81 retcode = 1
82 # and then make sure they're the same compariny the correct types
83 else:
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)
86 retcode = 1
87
88 with NamedTemporaryFile() as tempfile2:
89 # create a new particle
90 db.AddParticle("testparticle", "testparticle", 1, False, 2, 3, "custom", 10001, 4, 5, 6, 7, 8, 9)
91
92 # also test creating particle with the python functions
93 pdg.add_particle("test2", 10002, 5.28, 0.1, -1, 0.5)
94 # write the table again
95 db.WriteEvtGenTable(tempfile2.name)
96 # and make sure that it has the additional particle
97 subprocess.call(["diff", "--old-line-format=- %L", "--new-line-format=+ %L",
98 "--unchanged-line-format=", tempfile.name, tempfile2.name])
99
100db.ReadEvtGenTable("/no/such/file/(I hope)")
101# and also test the python function
102pdg.load("/no/such/file/(again)")
103
104print("now we should have zero entries: ", db.ParticleList().GetEntries())
105print("so reread default file...")
106db.ReadEvtGenTable()
107print("number of entries is now", db.ParticleList().GetEntries() and "positive" or "zero")
108sys.exit(retcode)
109
110# @endcond
_get_instance()
Definition pdg.py:38
add_particle(name, pdgCode, mass, width, charge, spin, max_width=None, lifetime=0, pythiaID=0, define_anti_particle=False)
Definition pdg.py:135
load(filename)
Definition pdg.py:122