Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
BalduinLetzer committed Aug 6, 2024
1 parent 605903c commit 9cef7ab
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 30 deletions.
1 change: 1 addition & 0 deletions hbw/config/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def add_jet_categories(config: od.Config) -> None:
label="2b",
)


@call_once_on_config()
def add_categories_selection(config: od.Config) -> None:
"""
Expand Down
12 changes: 10 additions & 2 deletions hbw/trigger/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
ak = maybe_import("awkward")

# categorizer muon channel


@categorizer()
def catid_trigger_mu(
self: Categorizer,
Expand All @@ -28,6 +30,8 @@ def catid_trigger_mu(
raise NotImplementedError(f"Category didn't receive a SelectionResult")

# categorizer electron channel


@categorizer()
def catid_trigger_ele(
self: Categorizer,
Expand All @@ -41,6 +45,8 @@ def catid_trigger_ele(
raise NotImplementedError(f"Category didn't receive a SelectionResult")

# categorizer for orthogonal measurement (muon channel)


@categorizer()
def catid_trigger_orth_mu(
self: Categorizer,
Expand All @@ -49,11 +55,13 @@ 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)


@categorizer()
def catid_trigger_orth_ele(
self: Categorizer,
Expand All @@ -62,6 +70,6 @@ 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")
26 changes: 14 additions & 12 deletions hbw/trigger/plot_efficiencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@

'''
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 --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
'''


def plot_efficiencies(
hists: OrderedDict,
config_inst: od.Config,
Expand Down Expand Up @@ -71,7 +72,7 @@ def plot_efficiencies(

# loop over processes
for proc_inst, myhist in hists.items():

# get normalisation from first histogram (all events)
norm_hist = np.array(myhist[:, 0].values())

Expand All @@ -91,11 +92,12 @@ def plot_efficiencies(
# plot config for the individual triggers
if "bin_sel" in kwargs:
mask_bins = tuple(bin for bin in kwargs["bin_sel"] if bin)
else :
else:
mask_bins = myhist.axes[1]
for i in mask_bins:
if i == "allEvents": continue

if i == "allEvents":
continue

plot_config[f"hist_{proc_inst.label}_{i}"] = {
"method": "draw_efficiency",
"hist": myhist[:, i],
Expand All @@ -104,7 +106,7 @@ def plot_efficiencies(
"label": f"{proc_inst.label}: {config_inst.x.trigger_short[i]}",
},
}

# set legend title to process name
if "title" in default_style_config["legend_cfg"]:
default_style_config["legend_cfg"]["title"] += " & " + proc_inst.label
Expand All @@ -117,7 +119,7 @@ def plot_efficiencies(
kwargs["skip_ratio"] = True

style_config = law.util.merge_dicts(default_style_config, style_config, deep=True)

# set correct CMS label TODO: this should be implemented correctly in columnflow by default at one point
style_config["cms_label_cfg"]["exp"] = ""
if "data" in proc_inst.name:
Expand All @@ -127,4 +129,4 @@ def plot_efficiencies(
if "xlim" in kwargs:
style_config["ax_cfg"]["xlim"] = kwargs["xlim"]

return plot_all(plot_config, style_config, **kwargs)
return plot_all(plot_config, style_config, **kwargs)
33 changes: 21 additions & 12 deletions hbw/trigger/trigger_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

logger = law.logger.get_logger(__name__)


@call_once_on_config()
def add_trigger_columns(config: od.Config) -> None:
"""
Expand Down Expand Up @@ -46,6 +47,7 @@ def add_trigger_columns(config: od.Config) -> None:
x_title="Trigger names (orthogonal)",
)


@call_once_on_config()
def add_trigger_categories(config: od.Config) -> None:
"""
Expand Down Expand Up @@ -78,6 +80,7 @@ def add_trigger_categories(config: od.Config) -> None:
label="Electron\northogonal\nmeasurement",
)


@call_once_on_config()
def add_trigger_config(config: od.Config, **kwargs) -> None:
"""
Expand Down Expand Up @@ -107,20 +110,26 @@ def add_trigger_config(config: od.Config, **kwargs) -> None:
}
elif year == 2022:
config.x.trigger = {
"e": [
"Ele30_WPTight_Gsf", "Ele28_eta2p1_WPTight_Gsf_HT150", "Ele15_IsoVVVL_PFHT450", "QuadPFJet70_50_40_35_PFBTagParticleNet_2BTagSum0p65",
],
"mu": [
"IsoMu24", "Mu50", "Mu15_IsoVVVL_PFHT450", "QuadPFJet70_50_40_35_PFBTagParticleNet_2BTagSum0p65",
],
}
"e": [
"Ele30_WPTight_Gsf",
"Ele28_eta2p1_WPTight_Gsf_HT150",
"Ele15_IsoVVVL_PFHT450",
"QuadPFJet70_50_40_35_PFBTagParticleNet_2BTagSum0p65",
],
"mu": [
"IsoMu24",
"Mu50",
"Mu15_IsoVVVL_PFHT450",
"QuadPFJet70_50_40_35_PFBTagParticleNet_2BTagSum0p65",
],
}
config.x.ref_trigger = {
"e": "IsoMu24",
"mu": "Ele30_WPTight_Gsf",
}
"e": "IsoMu24",
"mu": "Ele30_WPTight_Gsf",
}
else:
raise NotImplementedError(f"Trigger for year {year} is not defined.")

# short labels for triggers with long names
config.x.trigger_short = {
"Ele30_WPTight_Gsf": "Ele30Tight",
Expand All @@ -133,4 +142,4 @@ def add_trigger_config(config: od.Config, **kwargs) -> None:
}

# add trigger columns
add_trigger_columns(config)
add_trigger_columns(config)
10 changes: 6 additions & 4 deletions hbw/trigger/trigger_prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def trigger_prod(self: Producer, events: ak.Array, **kwargs) -> ak.Array:
trig_bits_orth = ak.Array([["allEvents"]] * len(events))

ref_trig = self.config_inst.x.ref_trigger[channel]

for trigger in self.config_inst.x.trigger[channel]:

trig_passed = ak.where(events.HLT[trigger], [[trigger]], [[]])
Expand All @@ -39,17 +39,19 @@ def trigger_prod(self: Producer, events: ak.Array, **kwargs) -> ak.Array:

events = set_ak_column(events, f"trig_bits_{channel}", trig_bits)
events = set_ak_column(events, f"trig_bits_orth_{channel}", trig_bits_orth)

return events

@trigger_prod.init

@trigger_prod.init
def trigger_prod_init(self: Producer) -> None:

for channel in self.channel:
for trigger in self.config_inst.x.trigger[channel]:
self.uses.add(f"HLT.{trigger}")
self.uses.add(f"HLT.{self.config_inst.x.ref_trigger[channel]}")


# producers for single channels
mu_trigger_prod = trigger_prod.derive("mu_trigger_prod", cls_dict={"channel": ["mu"]})
ele_trigger_prod = trigger_prod.derive("ele_trigger_prod", cls_dict={"channel": ["e"]})
ele_trigger_prod = trigger_prod.derive("ele_trigger_prod", cls_dict={"channel": ["e"]})

0 comments on commit 9cef7ab

Please sign in to comment.