Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
BalduinLetzer committed Aug 8, 2024
1 parent 77ed322 commit 866617e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 40 deletions.
34 changes: 18 additions & 16 deletions hbw/trigger/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@

from __future__ import annotations

import law

from columnflow.util import maybe_import
from columnflow.categorization import Categorizer, categorizer
from columnflow.selection import SelectionResult

np = maybe_import("numpy")
ak = maybe_import("awkward")

# categorizer muon channel


# categorizer muon channel
@categorizer()
def catid_trigger_mu(
self: Categorizer,
Expand All @@ -27,11 +24,10 @@ def catid_trigger_mu(
if results:
return events, results.steps.SR_mu
else:
raise NotImplementedError(f"Category didn't receive a SelectionResult")

# categorizer electron channel
raise NotImplementedError("Category didn't receive a SelectionResult")


# categorizer electron channel
@categorizer()
def catid_trigger_ele(
self: Categorizer,
Expand All @@ -42,11 +38,10 @@ def catid_trigger_ele(
if results:
return events, results.steps.SR_ele
else:
raise NotImplementedError(f"Category didn't receive a SelectionResult")

# categorizer for orthogonal measurement (muon channel)
raise NotImplementedError("Category didn't receive a SelectionResult")


# categorizer for orthogonal measurement (muon channel)
@categorizer()
def catid_trigger_orth_mu(
self: Categorizer,
Expand All @@ -55,13 +50,16 @@ def catid_trigger_orth_mu(
**kwargs,
) -> tuple[ak.Array, ak.Array]:
if results:
return events, (results.steps.TrigEleMatch & results.steps.SR_mu & results.steps.ref_trigger_mu)
return events, (
results.steps.TrigEleMatch &
results.steps.SR_mu &
results.steps.ref_trigger_mu
)
else:
raise NotImplementedError(f"Category didn't receive a SelectionResult")

# categorizer for orthogonal measurement (electron channel)
raise NotImplementedError("Category didn't receive a SelectionResult")


# categorizer for orthogonal measurement (electron channel)
@categorizer()
def catid_trigger_orth_ele(
self: Categorizer,
Expand All @@ -70,6 +68,10 @@ def catid_trigger_orth_ele(
**kwargs,
) -> tuple[ak.Array, ak.Array]:
if results:
return events, (results.steps.TrigMuMatch & results.steps.SR_ele & results.steps.ref_trigger_e)
return events, (
results.steps.TrigMuMatch &
results.steps.SR_ele &
results.steps.ref_trigger_e
)
else:
raise NotImplementedError(f"Category didn't receive a SelectionResult")
raise NotImplementedError("Category didn't receive a SelectionResult")
13 changes: 6 additions & 7 deletions hbw/trigger/plot_efficiencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

from __future__ import annotations

from collections import defaultdict, OrderedDict
from collections import OrderedDict

import law

from columnflow.util import maybe_import
from columnflow.plotting.plot_all import plot_all
from columnflow.plotting.plot_util import (
prepare_plot_config,
prepare_style_config,
remove_residual_axis,
apply_variable_settings,
Expand All @@ -32,11 +31,11 @@

'''
law run cf.PlotVariables1D --version v1 --config l22post \
--processes tt --variables muon_pt-trig_bits \
--datasets tt_dl_powheg --categories trig_mu \
--selector trigger_studies --producers event_weights,trigger_prod \
--calibrators "" \
--plot-function hbw.trigger.plot_efficiencies.plot_efficiencies
--processes tt_dl --variables electron_pt-trig_bits --categories trig_mu \
--selector trigger_sel --producers event_weights,trigger_prod \
--plot-function hbw.trigger.plot_efficiencies.plot_efficiencies \
--variable-settings "electron_pt,rebin=5,x_min=0,x_max=100" \
--general-settings "bin_sel=Ele30_WPTight_Gsf;"
'''


Expand Down
2 changes: 1 addition & 1 deletion hbw/trigger/trigger_prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from columnflow.production import Producer, producer
from columnflow.util import maybe_import
from columnflow.columnar_util import set_ak_column, EMPTY_FLOAT
from columnflow.columnar_util import set_ak_column

np = maybe_import("numpy")
ak = maybe_import("awkward")
Expand Down
42 changes: 26 additions & 16 deletions hbw/trigger/trigger_sel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
np = maybe_import("numpy")
ak = maybe_import("awkward")


@selector(
uses=(four_vec({"Muon", "Electron"})) | {"TrigObj.*",},
uses=(four_vec({"Muon", "Electron"})) | {"TrigObj.*", },
exposed=True,
)
def trigger_sel(
Expand All @@ -29,7 +30,7 @@ def trigger_sel(
stats: defaultdict,
**kwargs,
) -> Tuple[ak.Array, SelectionResult]:

# get preselection and object definition
events, results = self[pre_selection](events, stats, **kwargs)

Expand All @@ -44,22 +45,30 @@ def trigger_sel(
muons = events.Muon[results.aux["mu_mask_tight"]]
electrons = events.Electron[results.aux["e_mask_tight"]]

muon_mask = ak.sum(muons.pt > 15, axis=1) >= 1 # muon SFs are only available for muons with pt > 15
electron_mask = ak.sum(electrons.pt > 15, axis=1) >= 1 # electron threshold could be lower, for now set to same value as muon pt
# muon SFs are only available for muons with pt > 15 GeV
# electron threshold could be lower, for now set to same value as muon pt
muon_mask = ak.sum(muons.pt > 15, axis=1) >= 1
electron_mask = ak.sum(electrons.pt > 15, axis=1) >= 1

results.steps["TrigMuMask"] = muon_mask
results.steps["TrigEleMask"] = electron_mask

# set lepton objects (these will be the objects used after cf.ReduceColumns)
results.objects["Muon"]["Muon"] = masked_sorted_indices((results.aux["mu_mask_tight"] & (events.Muon.pt > 15)), events.Muon.pt)
results.objects["Electron"]["Electron"] = masked_sorted_indices((results.aux["e_mask_tight"] & (events.Electron.pt > 15)), events.Electron.pt)
results.objects["Muon"]["Muon"] = masked_sorted_indices(
(results.aux["mu_mask_tight"] & (events.Muon.pt > 15)),
events.Muon.pt
)
results.objects["Electron"]["Electron"] = masked_sorted_indices(
(results.aux["e_mask_tight"] & (events.Electron.pt > 15)),
events.Electron.pt
)

# check for match between offline and HLT objects
hlt_muon_obj_mask = events.TrigObj.id == 13
hlt_electron_obj_mask = events.TrigObj.id == 11

hlt_muon_mask = hlt_muon_obj_mask & ((events.TrigObj.filterBits & (1<<3)) > 0)
hlt_electron_mask = hlt_electron_obj_mask & ((events.TrigObj.filterBits & (1<<1)) > 0)
hlt_muon_mask = hlt_muon_obj_mask & ((events.TrigObj.filterBits & (1 << 3)) > 0)
hlt_electron_mask = hlt_electron_obj_mask & ((events.TrigObj.filterBits & (1 << 1)) > 0)
hlt_muon = events.TrigObj[hlt_muon_mask]
hlt_electron = events.TrigObj[hlt_electron_mask]
mu_combo = ak.cartesian({"offl": muons, "trigobj": hlt_muon}, nested=True)
Expand Down Expand Up @@ -99,13 +108,13 @@ def trigger_sel(
results.steps["all_but_bjet"] = (
results.steps.cleanup &
results.steps.nJet3 &
(results.steps.TrigMuMask | results.steps.TrigEleMask )
(results.steps.TrigMuMask | results.steps.TrigEleMask)
)

results.steps["all"] = results.event = (
results.steps.SR_ele | results.steps.SR_mu
)

# some categories not used for the trigger studies (a.t.m.) need additional selection steps
# at some point it probably makes sense to completely use the object definition and categories of the main analysis
results.steps["SR"] = results.steps.SR_ele | results.steps.SR_mu
Expand All @@ -116,13 +125,14 @@ def trigger_sel(

return events, results


@trigger_sel.init
def trigger_sel_init(self: Selector) -> None:

# init gets called multiple times, so we need to check if the config and dataset instances are already set
if not getattr(self, "config_inst", None):
if not getattr(self, "config_inst", None):
return

# add categories used for trigger studies
add_trigger_categories(self.config_inst)

Expand All @@ -134,12 +144,12 @@ def trigger_sel_init(self: Selector) -> None:
self.uses.add(pre_selection)
self.uses.add(post_selection)
self.uses.add(lepton_definition)
self.uses.add(jet_selection)
self.uses.add(jet_selection)

self.produces = {
pre_selection,
post_selection,
lepton_definition,
pre_selection,
post_selection,
lepton_definition,
jet_selection,
}

Expand Down

0 comments on commit 866617e

Please sign in to comment.