Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated PyJet to FastJet #158 #159

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graphicle/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _map_invert(mapping: ty.Dict[str, ty.Set[str]]) -> ty.Dict[str, str]:
"x": {"x", "px"},
"y": {"y", "py"},
"z": {"z", "pz"},
"e": {"e", "pe"},
"e": {"e", "pe", "E"},
}
)
_MOMENTUM_ORDER = tuple("xyze")
Expand Down
48 changes: 30 additions & 18 deletions graphicle/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
import operator as op
import typing as ty

import awkward as ak
import fastjet
import numpy as np
import numpy.lib.recfunctions as rfn
import pandas as pd
import pyjet
import scipy.optimize as opt
from pyjet import ClusterSequence, PseudoJet
from scipy.sparse.csgraph import breadth_first_tree

import graphicle as gcl
Expand Down Expand Up @@ -108,24 +107,37 @@ def fastjet_clusters(
``p_val`` set to ``-1`` gives **anti-kT**, ``0`` gives
**Cambridge-Aachen**, and ``1`` gives **kT** clusterings.
"""
_param_check(pmu, "pmu", gcl.MomentumArray)
pmu_pyjet = pmu.data[["e", "x", "y", "z"]]
pmu_pyjet.dtype.names = "E", "px", "py", "pz"
pmu_pyjet_idx = rfn.append_fields(
pmu_pyjet, "idx", np.arange(len(pmu_pyjet))
)
sequence: ClusterSequence = pyjet.cluster(
pmu_pyjet_idx, R=radius, p=p_val, ep=True
)
jets: ty.Iterable[PseudoJet] = sequence.inclusive_jets()
num_pcls = len(pmu)
pmu_renamed = pmu.data.copy()[["e", "x", "y", "z"]]
pmu_renamed.dtype.names = "E", "px", "py", "pz"
pmu_ak = ak.from_numpy(pmu_renamed)
extra_param = tuple()
if p_val == -1:
algo = fastjet.antikt_algorithm
elif p_val == 0:
algo = fastjet.cambridge_algorithm
elif p_val == 1:
algo = fastjet.kt_algorithm
else:
algo = fastjet.genkt_algorithm
extra_param = (p_val,)
jetdef = fastjet.JetDefinition(algo, radius, *extra_param)
sequence = fastjet.ClusterSequence(pmu_ak, jetdef)
jets = sequence.inclusive_jets()
jet_pmus = gcl.MomentumArray(jets.to_numpy())
pt_descend_idxs = np.argsort(jet_pmus.pt)[::-1].tolist()
jet_pmus = jet_pmus[pt_descend_idxs]
cuts = np.ones(len(pt_descend_idxs), dtype=np.bool_)
if pt_cut is not None:
jets = filter(lambda jet: jet.pt > pt_cut, jets)
cuts = jet_pmus.pt > pt_cut
if eta_cut is not None:
jets = filter(lambda jet: abs(jet.eta) < eta_cut, jets)
cuts = np.logical_and(cuts, np.abs(jet_pmus.eta) < eta_cut)
jet_idxs = sequence.constituent_index().to_list()
jet_idxs = op.itemgetter(*pt_descend_idxs)(jet_idxs)
jet_idxs = it.compress(jet_idxs, cuts)
if top_k is not None:
jets = it.islice(jets, top_k)
jet_idxs = map(lambda j: list(map(op.attrgetter("idx"), j)), jets)
mask_empty = gcl.MaskArray(np.zeros_like(pmu_pyjet, dtype="<?"))
jet_idxs = it.islice(jet_idxs, top_k)
mask_empty = gcl.MaskArray(np.zeros(num_pcls, dtype="<?"))
clusters: ty.List[gcl.MaskArray] = []
for jet_idx in jet_idxs:
mask = mask_empty.copy()
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ dependencies = [
"mcpid",
"typicle >=0.1.4",
"networkx",
"pyjet ==1.9.0",
"fastjet >=3.4.1.2",
"awkward",
"rich",
"deprecation",
"typing-extensions >=4.7.1",
Expand Down
Loading