Skip to content

Commit

Permalink
merge branch 'master' into 'trigger_sf'
Browse files Browse the repository at this point in the history
  • Loading branch information
nprouvost committed Feb 3, 2025
2 parents d8ae788 + 3cf1940 commit 1c4b505
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 17 deletions.
30 changes: 29 additions & 1 deletion hbt/categorization/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,35 @@ def cat_incl(self: Categorizer, events: ak.Array, **kwargs) -> tuple[ak.Array, a
return events, ak.ones_like(events.event) == 1


@categorizer(uses={"Jet.pt"})
@categorizer(uses={"Jet.{pt,phi}"})
def cat_2j(self: Categorizer, events: ak.Array, **kwargs) -> tuple[ak.Array, ak.Array]:
# two or more jets
return events, ak.num(events.Jet.pt, axis=1) >= 2


@categorizer(uses={"{Electron,Muon,Tau}.{pt,eta,phi,mass}"})
def cat_dy(self: Categorizer, events: ak.Array, **kwargs) -> tuple[ak.Array, ak.Array]:
# e/mu driven DY region: mll > 40 and met < 30 (to supress tau decays into e/mu)
leps = ak.concatenate([events.Electron * 1, events.Muon * 1, events.Tau * 1], axis=1)[:, :2]
mask = (
(leps.sum(axis=1).mass > 40) &
(events[self.config_inst.x.met_name].pt < 30)
)
return events, mask


@cat_dy.init
def cat_dy_init(self: Categorizer) -> None:
self.uses.add(f"{self.config_inst.x.met_name}.{{pt,phi}}")


@categorizer(uses={"{Electron,Muon,Tau}.{pt,eta,phi,mass}"})
def cat_tt(self: Categorizer, events: ak.Array, **kwargs) -> tuple[ak.Array, ak.Array]:
# tt region: met > 30 (due to neutrino presence in leptonic w decays)
mask = events[self.config_inst.x.met_name].pt > 30
return events, mask


@cat_tt.init
def cat_tt_init(self: Categorizer) -> None:
self.uses.add(f"{self.config_inst.x.met_name}.{{pt,phi}}")
25 changes: 22 additions & 3 deletions hbt/config/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import order as od

from columnflow.config_util import add_category, create_category_combinations
from columnflow.types import Any


def add_categories(config: od.Config) -> None:
Expand Down Expand Up @@ -36,15 +37,17 @@ def add_categories(config: od.Config) -> None:
# kinematic categories
_add_category(config, name="incl", id=100, selection="cat_incl", label="inclusive")
_add_category(config, name="2j", id=110, selection="cat_2j", label="2 jets")
_add_category(config, name="dy", id=210, selection="cat_dy", label="DY enriched")
_add_category(config, name="tt", id=220, selection="cat_tt", label=r"$t\bar{t}$ enriched")

#
# build groups
#

def name_fn(categories):
def name_fn(categories: dict[str, od.Category]) -> str:
return "__".join(cat.name for cat in categories.values() if cat)

def kwargs_fn(categories, add_qcd_group=True):
def kwargs_fn(categories: dict[str, od.Category], add_qcd_group: bool = True) -> dict[str, Any]:
# build auxiliary information
aux = {}
if add_qcd_group:
Expand Down Expand Up @@ -81,6 +84,7 @@ def kwargs_fn(categories, add_qcd_group=True):
"sign": [config.get_category("os"), config.get_category("ss")],
"tau2": [config.get_category("iso"), config.get_category("noniso")],
}

create_category_combinations(
config=config,
categories=main_categories,
Expand All @@ -95,13 +99,28 @@ def kwargs_fn(categories, add_qcd_group=True):
config.get_category("ee"), config.get_category("mumu"), config.get_category("emu"),
],
# kinematic regions in the middle (to be extended)
"kin": [config.get_category("incl"), config.get_category("2j")],
"kin": [config.get_category("incl"), config.get_category("dy"), config.get_category("tt")],
# relative sign last
"sign": [config.get_category("os")],
}

def skip_fn_ctrl(categories: dict[str, od.Category]) -> bool:
if "channel" not in categories or "kin" not in categories:
return False
ch_cat = categories["channel"]
kin_cat = categories["kin"]
# skip dy in emu
if kin_cat.name == "dy" and ch_cat.name == "emu":
return True
# skip tt in ee/mumu
if kin_cat.name == "tt" and ch_cat.name in ("ee", "mumu"):
return True
return False

create_category_combinations(
config=config,
categories=control_categories,
name_fn=name_fn,
kwargs_fn=functools.partial(kwargs_fn, add_qcd_group=False),
skip_fn=skip_fn_ctrl,
)
18 changes: 8 additions & 10 deletions hbt/config/configs_hbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,9 @@ def if_not_era(*, values: list[str | None] | None = None, **kwargs) -> list[str]

# shift groups for conveniently looping over certain shifts
# (used during plotting)
cfg.x.shift_groups = {}
cfg.x.shift_groups = {

}

# selector step groups for conveniently looping over certain steps
# (used in cutflow tasks)
Expand Down Expand Up @@ -1400,9 +1402,7 @@ def add_external(name, value):
if shift_inst.has_tag(("jec", "jer"))
],
"lepton_sf": [
x.name for x in (
*get_shifts("e"), *get_shifts("mu"),
)
shift_inst.name for shift_inst in (*get_shifts("e"), *get_shifts("mu"))
],
"tec": [
shift_inst.name for shift_inst in cfg.shifts
Expand All @@ -1421,13 +1421,11 @@ def add_external(name, value):
if shift_inst.has_tag(("eer"))
],
"btag_sf": [
x.name for x in (
*get_shifts(*(f"btag_{unc}" for unc in cfg.x.btag_unc_names)),
)
shift_inst.name for shift_inst in get_shifts(*(f"btag_{unc}" for unc in cfg.x.btag_unc_names))
],
"pdf": [x.name for x in get_shifts("pdf")],
"murmuf": (x.name for x in get_shifts("murmuf")),
"pu": [x.name for x in get_shifts("minbias_xs")],
"pdf": [shift_inst.name for shift_inst in get_shifts("pdf")],
"murmuf": (shift_inst.name for shift_inst in get_shifts("murmuf")),
"pu": [shift_inst.name for shift_inst in get_shifts("minbias_xs")],
}

################################################################################################
Expand Down
30 changes: 30 additions & 0 deletions hbt/production/weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def normalized_pu_weight(self: Producer, events: ak.Array, **kwargs) -> ak.Array
if any(weight_name.endswith(postfix) for postfix in self.veto_postfix):
continue

# if there are postfixes to veto (i.e. we are not in the nominal case)
# skip the weight if it has a vetoed postfix
if any(weight_name.endswith(postfix) for postfix in self.veto_postfix):
continue

# create a weight vector starting with ones
norm_weight_per_pid = np.ones(len(events), dtype=np.float32)

Expand Down Expand Up @@ -140,6 +145,19 @@ def normalized_pdf_weight_init(self: Producer) -> None:
self.uses |= columns
self.produces |= {f"normalized_{column}" for column in columns}

@normalized_pdf_weight.init
def normalized_pdf_weight_init(self: Producer) -> None:

self.postfixes = [""]
if getattr(self, "global_shift_inst", None):
if self.global_shift_inst.is_nominal:
self.postfixes.extend(("_up", "_down"))
columns = {f"pdf_weight{postfix}" for postfix in self.postfixes}

self.uses |= columns
self.produces |= {f"normalized_{column}" for column in columns}


@normalized_pdf_weight.requires
def normalized_pdf_weight_requires(self: Producer, reqs: dict) -> None:
from columnflow.tasks.selection import MergeSelectionStats
Expand Down Expand Up @@ -201,6 +219,18 @@ def normalized_murmuf_weight_init(self: Producer) -> None:
self.produces |= {f"normalized_{column}" for column in columns}


@normalized_murmuf_weight.init
def normalized_murmuf_weight_init(self: Producer) -> None:
self.postfixes = [""]
if getattr(self, "global_shift_inst", None):
if self.global_shift_inst.is_nominal:
self.postfixes.extend(("_up", "_down"))
columns = {f"murmuf_weight{postfix}" for postfix in self.postfixes}

self.uses |= columns
self.produces |= {f"normalized_{column}" for column in columns}


@normalized_murmuf_weight.requires
def normalized_murmuf_weight_requires(self: Producer, reqs: dict) -> None:
from columnflow.tasks.selection import MergeSelectionStats
Expand Down
3 changes: 1 addition & 2 deletions hbt/selection/lepton.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,10 @@ def electron_selection(

@electron_selection.init
def electron_selection_init(self) -> None:
from columnflow.config_util import get_shifts_from_sources
if self.config_inst.campaign.x.run == 3 and self.config_inst.campaign.x.year == 2022:
self.shifts |= {
shift_inst.name for shift_inst in self.config_inst.shifts
if shift_inst.has_tag(("ees", "eer"))
if shift_inst.has_tag(("ees", "eer"))
}


Expand Down
2 changes: 1 addition & 1 deletion modules/columnflow

0 comments on commit 1c4b505

Please sign in to comment.