Skip to content

Instantly share code, notes, and snippets.

@admorris
Created October 28, 2021 10:54
Show Gist options
  • Save admorris/e1bd683e9ab5cd2a26c781f375a1dac2 to your computer and use it in GitHub Desktop.
Save admorris/e1bd683e9ab5cd2a26c781f375a1dac2 to your computer and use it in GitHub Desktop.
Update conversions.csv in scikit-hep/particle with LHCb names
#!/usr/bin/env python
import requests
import csv
def download_table(url="https://gitlab.cern.ch/lhcb-conddb/DDDB/-/raw/master/param/ParticleTable.txt"):
r = requests.get(url)
r.raise_for_status()
lines = r.text.split("\n")
return [line.split() for line in filter(lambda x: x and x[0] == " ", lines)]
table = download_table()
lhcb_names = {int(pdg_id): name for name, _, pdg_id, *_ in table}
cell_format = "{:>18}"
new_key = cell_format.format("LHCBNAME")
with open("src/particle/data/conversions.csv", "r") as in_csv:
reader = csv.DictReader(filter(lambda row: row[0]!='#', in_csv), skipinitialspace=False)
pdg_id_key = list(filter(lambda x: "PDGID" in x, reader.fieldnames))[0]
with open("conversions.csv", "w") as out_csv:
out_csv.write("# (c) Scikit-HEP project - Particle package data file - conversions.csv - version 11 - 2021-10-28\n")
writer = csv.DictWriter(out_csv, fieldnames=reader.fieldnames + [new_key], lineterminator="\n")
writer.writeheader()
for row in reader:
try:
lhcb_name = lhcb_names[int(row[pdg_id_key])]
except KeyError as e:
lhcb_name = "unknown"
row[new_key] = cell_format.format(lhcb_name)
writer.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment