Belle II Software  release-05-01-25
evtpdl.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 """
5 Check reading and writing of evt.pdl files as well as errors when creating new particles.
6 
7 This script tests that
8  - the ROOT.TDatabasePDG instance is in fact an EvtGenDatabasePDG
9  - reading a pdl file raises a warning if it's not the original one
10  - creating a particle which already exists is forbidden
11  - creating particles with whitespace in the name is forbidden
12  - writing an evt.pdl results in the same output as the original pdl, except for
13  whitespace and additionally defined particles
14 """
15 
16 
17 from tempfile import NamedTemporaryFile
18 import basf2
19 import ROOT
20 import os
21 import sys
22 import subprocess
23 import pdg
24 
25 db = ROOT.TDatabasePDG.Instance()
26 # create particle which already exists, should print error
27 db.AddParticle("e-", "electron", 0, False, 0, 0, "duplicate", 11, 0, 0, 0, 0, 0, 0)
28 # create particle with whitespace in the name, should print error
29 db.AddParticle("invalid name", "invalid name", 0, False, 0, 0, "custom", 2345, 0, 0, 0, 0, 0, 0)
30 # and also create a particle with the python function: should give error due to
31 # dublicate pdg code
32 pdg.add_particle("test2", 1, 5.28, 0.1, 0, 0.5)
33 pdg.add_particle("foo\tbar", 10001, 0, 0, 0, 0)
34 
35 # default evt.pdl filename
36 default_evtpdl = basf2.find_file(os.path.join("data", "framework", "particledb", "evt.pdl"))
37 # create a temporary one and compare
38 retcode = 0
39 with NamedTemporaryFile() as tempfile:
40  # write a evt.pdl from the EventGenDatabasePDG
41  db.WriteEvtGenTable(tempfile.name)
42  # and make sure that it's the same as the original one except for
43  # whitespace, empty lines and lines starting with a "*"
44  with open(default_evtpdl) as default:
45  old_lines = [x.split() for x in default if x[0] != '*' and not x.startswith("end")]
46  with open(tempfile.name) as changed:
47  new_lines = [x.split() for x in changed if x[0] != '*' and not x.startswith("end")]
48 
49  if len(old_lines) != len(new_lines):
50  basf2.B2ERROR("Different number of lines when writing pdl file")
51  retcode = 1
52  else:
53  # field types for comparison
54  types = [str]*4 + [int, float, float, float, int, int, float, int]
55  # check all lines for equality but don't care about formatting
56  for old, new in zip(old_lines, new_lines):
57  # make sure the lines have the same number of fields
58  if len(old) != len(new):
59  basf2.B2ERROR("Number of field different between old and new:",
60  old=old, new=new)
61  retcode = 1
62  # and that they have enough fields for comparison
63  elif len(new) < len(types):
64  basf2.B2ERROR("Not enough fields", old=old, new=new)
65  retcode = 1
66  # and then make sure they're the same compariny the correct types
67  else:
68  if not all(t(o) == t(n) for t, o, n in zip(types, old, new)):
69  basf2.B2ERROR("Particles differ", old=old, new=new)
70  retcode = 1
71 
72  with NamedTemporaryFile() as tempfile2:
73  # create a new particle
74  db.AddParticle("testparticle", "testparticle", 1, False, 2, 3, "custom", 10001, 4, 5, 6, 7, 8, 9)
75 
76  # also test creating particle with the python functions
77  pdg.add_particle("test2", 10002, 5.28, 0.1, -1, 0.5)
78  # write the table again
79  db.WriteEvtGenTable(tempfile2.name)
80  # and make sure that it has the additional particle
81  subprocess.call(["diff", "--old-line-format=- %L", "--new-line-format=+ %L",
82  "--unchanged-line-format=", tempfile.name, tempfile2.name])
83 
84 db.ReadEvtGenTable("/no/such/file/(I hope)")
85 # and also test the python function
86 pdg.load("/no/such/file/(again)")
87 
88 print("now we should have zero entries: ", db.ParticleList().GetEntries())
89 print("so reread default file...")
90 db.ReadEvtGenTable()
91 print("number of entries is now", db.ParticleList().GetEntries() and "positive" or "zero")
92 sys.exit(retcode)
pdg.add_particle
def add_particle(name, pdgCode, mass, width, charge, spin, max_width=None, lifetime=0, pythiaID=0)
Definition: pdg.py:121
pdg.load
def load(filename)
Definition: pdg.py:109