"""Create a small ROOT fixture from a Delphes or ATLAS Open Data sample.""" from __future__ import annotations import argparse from pathlib import Path import uproot DELPHES_BRANCHES = ( "jet_pt", "jet_eta", "jet_phi", "jet_btag", "ph_pt", "ph_eta", "ph_phi", "ele_pt", "ele_eta", "ele_phi", "ele_charge", "mu_pt", "mu_eta", "mu_phi", "mu_charge", "MET_met", "MET_phi", "weight", "Number", ) #: Branches read by ``configs/gamgam.yaml``. ATLAS_OPENDATA_BRANCHES = ( "jet_pt", "jet_eta", "jet_phi", "jet_MV2c10", "photon_pt", "photon_eta", "photon_phi", "lep_pt", "lep_eta", "lep_phi", "lep_charge", "lep_type", "met_et", "met_phi", "mcWeight", "eventNumber", ) PRESETS = { "delphes": ("output", DELPHES_BRANCHES), "atlas_opendata": ("mini", ATLAS_OPENDATA_BRANCHES), } BRANCHES = DELPHES_BRANCHES def shrink_sample( source: Path, target: Path, entries: int, *, preset: str = "delphes" ) -> None: """Copy the active branches and first ``entries`` events to ``target``.""" if entries < 1: raise ValueError("entries must be positive") tree, branches = PRESETS[preset] target.parent.mkdir(parents=True, exist_ok=True) with uproot.open(source) as source_file: arrays = source_file[tree].arrays( branches, entry_start=0, entry_stop=entries, library="ak" ) with uproot.recreate(target) as target_file: target_file[tree] = arrays def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("source", type=Path) parser.add_argument("target", type=Path) parser.add_argument("--entries", type=int, default=64) parser.add_argument("--preset", choices=sorted(PRESETS), default="delphes") args = parser.parse_args() shrink_sample(args.source, args.target, args.entries, preset=args.preset) print(f"Wrote {args.entries} events to {args.target}") if __name__ == "__main__": main()