Skip to content

Commit

Permalink
dump commit; nan issue w/ trafo unresolved
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhundhausen committed Oct 21, 2024
1 parent 8814a05 commit 819f4fe
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 77 deletions.
13 changes: 13 additions & 0 deletions configs/V29/objects/met_ht_mht.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
jet:
eta_ranges:
inclusive: [0, 8]
ids:
ht:
label: "Gen HT"
trafo: "HT"
cuts:
event:
inclusive:
- "abs({eta}) < 2.4"
- "{pt} > 30"

phase1PuppiHT:
label: "Histogrammed Puppi HT"
ids:
Expand Down
4 changes: 3 additions & 1 deletion menu_tools/object_performance/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def reference_object(self) -> Object:
assert (
len(object_id_strs) == 1
), f"Multiple reference objects defined in {self.plot_name}!"
return ReferenceObject(self._cfg["reference_object"], self.version)
return ReferenceObject(
list(self._cfg["reference_object"].keys())[0], self.version
)

@property
def test_objects(self) -> dict[str, Any]:
Expand Down
7 changes: 4 additions & 3 deletions menu_tools/object_performance/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def _save_json(self, file_name):

xbins = self.turnon_collection.bins
xbins = 0.5 * (xbins[1:] + xbins[:-1])
print(len(xbins))

if self.cfg.iso_vs_eff_plot:
efficiency = self._get_iso_vs_eff_hist(gen_hist_trig[0])
Expand All @@ -117,9 +118,9 @@ def _save_json(self, file_name):
yerr = np.array(
[yerr[0][~np.isnan(efficiency)], yerr[1][~np.isnan(efficiency)]]
)
xerr = xerr[np.isfinite(efficiency)]
xbins = xbins[np.isfinite(efficiency)]
efficiency = efficiency[np.isfinite(efficiency)]
# xerr = xerr[np.isfinite(efficiency)]
# xbins = xbins[np.isfinite(efficiency)]
# efficiency = efficiency[np.isfinite(efficiency)]

xerr = xerr.tolist()
yerr = yerr.tolist()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,7 @@ ElectronsIsolation_Barrel:
iso_vs_efficiency: true
match_test_to_ref: true
reference_object:
part_e:inclusiveElectron: Pt

# reference_object: "GenElectron:"
# cuts:
# event:
# - '{dr_0.3} < 0.15'
# - abs({eta}) < 1.479
# object:
# - abs({eta}) < 1.479
# label: Gen Electrons
# object: part_e
# x_arg: Pt
part_e:default: "Pt"
sample: DYLL_M50
test_objects:
tkElectron:NoIsoLowPt: trkiso
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ HT_50perc:
step: 20
version: V29
reference_object:
cuts:
object:
- abs({eta}) < 2.4
- '{pt} > 30'
label: Gen HT
object: jet
x_arg: Pt
trafo: HT
jet:ht: "Pt"
sample: TT
scalings:
method: naive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ HT_90perc:
step: 20
version: V29
reference_object:
cuts:
object:
- abs({eta}) < 2.4
- '{pt} > 30'
label: Gen HT
object: jet
x_arg: Pt
trafo: HT
jet:ht: "Pt"
sample: TT
scalings:
method: naive
Expand Down
6 changes: 4 additions & 2 deletions menu_tools/object_performance/tests/test_turnons_v29.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@


testdata = [
"HT_50perc_350_V29",
# "HT_50perc_350_V29",
"HT_90perc_350_V29",
"ElectronsIsolation_Barrel_-999_V29",
# "ElectronsIsolation_Barrel_-999_V29",
]


Expand Down Expand Up @@ -49,7 +49,9 @@ def test_matching_plots_reproduced(test_name):
)
efficiencies_reference = np.array(val["efficiency"], dtype=np.float64)
print(efficiencies_reference)
print(efficiencies_test)
differences = efficiencies_test - efficiencies_reference
print(differences)
assert not np.any(abs(differences) > 1e-4)
else:
assert val == test_result[key]
32 changes: 15 additions & 17 deletions menu_tools/object_performance/turnon_collection.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from typing import Any, Optional
import re

import awkward as ak
import numpy as np
import vector

from menu_tools.object_performance.config import PerformancePlotConfig
from menu_tools.utils import utils
from menu_tools.utils.objects import Object, compute_selection_mask_for_event_cuts, compute_selection_mask_for_object_cuts
from menu_tools.utils.objects import (
Object,
compute_selection_mask_for_event_cuts,
compute_selection_mask_for_object_cuts,
)


vector.register_awkward()
Expand Down Expand Up @@ -51,7 +54,7 @@ def _load_array_from_parquet(self, obj: str):
)
array = ak.from_parquet(fname)
array_dict = {self._transform_key(key, obj): array[key] for key in array.fields}
if self.cfg_plot.reference_trafo and not obj.startswith("L1"):
if self.cfg_plot.reference_object.trafo and not obj.startswith("L1"):
array = ak.Array(array_dict)
else:
array = ak.zip(array_dict)
Expand Down Expand Up @@ -228,12 +231,6 @@ def _select_highest_pt_ref_object(self):
sel_pt = ak.argmax(self.ak_arrays["ref"]["pt"], axis=-1, keepdims=True)
self.ak_arrays["ref"] = self.ak_arrays["ref"][sel_pt]

def _apply_list_of_reference_cuts(self, cut_dict: Optional[dict[str, list[str]]]) -> None:
for cut in cut_list:
cut = re.sub(r"{([^&|]*)}", r"self.ak_arrays['ref']['\1']", cut)
sel = eval(cut)
self.ak_arrays["ref"] = self.ak_arrays["ref"][sel]

def _apply_reference_cuts(self) -> None:
"""Applies configured cuts on reference objects.
Expand All @@ -246,20 +243,19 @@ def _apply_reference_cuts(self) -> None:

# Apply object level cuts, i.e. removing objects from event, while
# retaining the events.
sel = compute_selection_mask_for_object_cuts(
self.cfg_plot.reference_object, self.ak_arrays["ref"]
)
self.ak_arrays["ref"] = self.ak_arrays["ref"][sel]
if self.cfg_plot.reference_object.cuts:
sel = compute_selection_mask_for_object_cuts(
self.cfg_plot.reference_object, self.ak_arrays["ref"]
)
self.ak_arrays["ref"] = self.ak_arrays["ref"][sel]

# If spceified apply transformation
# If specified, apply transformation
if self.cfg_plot.reference_object.trafo:
# In this case each event is reduced to a single value already
return None

# Select highest pt object from each event and apply event level cuts.
self._select_highest_pt_ref_object()
# ref_event_cuts = self.cfg_plot.reference_object.event_cuts
# self._apply_list_of_reference_cuts(ref_event_cuts)
sel = compute_selection_mask_for_event_cuts(
self.cfg_plot.reference_object, self.ak_arrays["ref"]
)
Expand All @@ -273,7 +269,9 @@ def _apply_test_obj_cuts(self):
for test_obj, _ in self.test_objects:
if not test_obj.cuts:
continue
sel = compute_selection_mask_for_object_cuts(test_obj, self.ak_arrays[str(test_obj)])
sel = compute_selection_mask_for_object_cuts(
test_obj, self.ak_arrays[str(test_obj)]
)
self.ak_arrays[str(test_obj)] = self.ak_arrays[str(test_obj)][sel]

def _skim_to_hists(self) -> None:
Expand Down
48 changes: 22 additions & 26 deletions menu_tools/utils/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,25 +184,27 @@ def __init__(self, object_key: str, version: str) -> None:

@property
def trafo(self) -> Optional[str]:
"""Returns the trafo key of the (reference) object if it is defined (HT, MHT, etc).
"""Returns the trafo key of the (reference) object
if it is defined (HT, MHT, etc).
This is intended only for the *reference* object.
"""
try:
return self._nano_obj["trafo"]
except KeyError:
return self._nano_obj["transformation"]
return self._object_params["trafo"]
except KeyError:
print("No transformation defined in reference object")
return None

@property
def cuts(self) -> Optional[dict[str, list[str]]]:
"""OBJECT level cuts! I.e. individual objects that don't fulfill the
criteria are removed from the events, but the events themselves are
retained.
"""
def _get_cuts(self, event_or_object: str) -> Optional[dict[str, list[str]]]:
assert event_or_object in [
"event",
"object",
], "Cuts are either event or object level"
_cuts = {}
if "cuts" in self._object_params.keys():
_cuts = self._object_params["cuts"]["object"]
if event_or_object in self._object_params["cuts"].keys():
_cuts = self._object_params["cuts"][event_or_object]
else:
return None
if self.eta_range != "inclusive":
# if a region other than inclusive is specified an eta cut
eta_min = self.eta_ranges[self.eta_range][0]
Expand All @@ -216,27 +218,21 @@ def cuts(self) -> Optional[dict[str, list[str]]]:
_cuts["inclusive"] = [global_eta_cut]
return _cuts

@property
def cuts(self) -> Optional[dict[str, list[str]]]:
"""OBJECT level cuts! I.e. individual objects that don't fulfill the
criteria are removed from the events, but the events themselves are
retained.
"""
return self._get_cuts("object")

@property
def event_cuts(self) -> Optional[dict[str, list[str]]]:
"""EVENT level cuts! I.e. individual objects that don't fulfill the
criteria are removed from the events, but the events themselves are
retained.
"""
_cuts = {}
if "cuts" in self._object_params.keys():
_cuts = self._object_params["cuts"]["event"]
if self.eta_range != "inclusive":
# if a region other than inclusive is specified an eta cut
eta_min = self.eta_ranges[self.eta_range][0]
eta_max = self.eta_ranges[self.eta_range][1]
global_eta_cut = (
f"((abs({{eta}}) > {eta_min}) & (abs({{eta}}) < {eta_max}))"
)
try:
_cuts["inclusive"].append(global_eta_cut)
except KeyError:
_cuts["inclusive"] = [global_eta_cut]
return _cuts
return self._get_cuts("event")


def compute_selection_mask_for_cuts(
Expand Down

0 comments on commit 819f4fe

Please sign in to comment.