# Run with Python 3 using the CERN CSVs below. # https://opendata.cern.ch/record/302/files/dielectron-Jpsi.csv # https://opendata.cern.ch/record/306/files/Zee.csv # python compare-energy-and-charge.py.txt dielectron-Jpsi.csv Zee.csv comparison.json from collections import Counter from pathlib import Path import bisect import csv import hashlib import json import math import statistics import sys edges = [*range(0, 75, 5), 100, 150, 250] datasets = [ ("J/psi selection", "https://opendata.cern.ch/record/302", sys.argv[1]), ("Z selection", "https://opendata.cern.ch/record/306", sys.argv[2]), ] def analyze(name, source, path): path = Path(path) positive = [0] * len(edges) negative = [0] * len(edges) events = Counter() pairs = Counter() mass_errors = [] with path.open(encoding="utf-8-sig", newline="") as stream: rows = csv.DictReader(stream) rows.fieldnames = [name.strip() for name in rows.fieldnames] required = {"Run", "Event", "M"} | { field + slot for slot in ("1", "2") for field in ("E", "px", "py", "pz", "Q") } assert required <= set(rows.fieldnames), rows.fieldnames for row in rows: events[row["Run"], row["Event"]] += 1 charges = [] for slot in ("1", "2"): e = float(row["E" + slot]) q = int(row["Q" + slot]) assert q in (-1, 1) and math.isfinite(e) and e >= 0, (q, e) bin_number = bisect.bisect_right(edges, e) - 1 (positive if q == 1 else negative)[bin_number] += 1 charges.append(q) pairs["opposite" if charges[0] != charges[1] else "same"] += 1 sums = [sum(float(row[field + slot]) for slot in ("1", "2")) for field in ("E","px","py","pz")] squared = sums[0]**2 - sum(value**2 for value in sums[1:]) assert squared > 0, sums mass_errors.append(abs(math.sqrt(squared) - float(row["M"]))) total_pos = sum(positive) total_neg = sum(negative) assert len(mass_errors) * 2 == total_pos + total_neg both = sum(a + b for a,b in zip(positive,negative) if a > 0 and b > 0) overlap = sum(min(a / total_pos, b / total_neg) for a,b in zip(positive,negative)) return { "selection": name, "source": source, "inputSha256": hashlib.sha256(path.read_bytes()).hexdigest(), "pairRows": len(mass_errors), "uniqueRecordedEventLabels": len(events), "positiveCandidates": total_pos, "negativeCandidates": total_neg, "pairChargeCounts": dict(pairs), "bins": [ {"minEnergyGeV": start, "positive": a, "negative": b} for start,a,b in zip(edges,positive,negative) ], "candidateCountInBinsWithBothSigns": both, "fractionInBinsWithBothSigns": both / (total_pos + total_neg), "signDistributionOverlap": overlap, "reconstructedPairMassCheck": { "maxAbsResidualGeV": max(mass_errors), "medianAbsResidualGeV": statistics.median(mass_errors), "countAbovePointOneGeV": sum(err > 0.1 for err in mass_errors), }, } report={"dataType":"Derived CMS educational event records","bothSigns":[analyze(*dataset) for dataset in datasets]} with open(sys.argv[3], "x", encoding="utf-8") as output: json.dump(report, output, indent=2) output.write("\n") print(json.dumps([{ key: value for key,value in row.items() if key != "bins" } for row in report["bothSigns"]]))