Skip to content

Commit

Permalink
Merge branch 'dev' into plot_postfit_shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lara813 authored Jan 21, 2025
2 parents 13970d5 + 53caa18 commit 932cedb
Show file tree
Hide file tree
Showing 50 changed files with 1,896 additions and 1,087 deletions.
66 changes: 34 additions & 32 deletions hbw/analysis/create_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from __future__ import annotations

import os
import importlib

import law
import order as od
Expand All @@ -24,6 +23,8 @@
ml_inputs_producer,
)

from hbw.tasks.campaigns import BuildCampaignSummary


@timeit_multiple
def create_hbw_analysis(
Expand Down Expand Up @@ -74,6 +75,8 @@ def create_hbw_analysis(
analysis_inst.x.default_weight_producer = "default"
analysis_inst.x.ml_inputs_producer = ml_inputs_producer(analysis_inst)
analysis_inst.x.default_ml_model = default_ml_model
analysis_inst.x.default_variables = ["jet0_pt", "mll", "n_jet", "ptll", "lepton0_pt", "lepton1_pt"]
analysis_inst.x.default_categories = ["incl", "sr", "ttcr", "dycr"]

#
# define configs
Expand All @@ -82,7 +85,6 @@ def create_hbw_analysis(
from hbw.config.config_run2 import add_config

def add_lazy_config(
campaigns: dict[str, str],
config_name: str,
config_id: int,
**kwargs,
Expand All @@ -101,22 +103,22 @@ def create_factory(
):
@timeit_multiple
def analysis_factory(configs: od.UniqueObjectIndex):
hbw_campaign_inst = None

for mod, campaign in campaigns.items():
# import the campaign
mod = importlib.import_module(mod)
if not hbw_campaign_inst:
# copy the main campaign
hbw_campaign_inst = getattr(mod, campaign).copy()
else:
# add datasets to the main campaign
campaign_inst = getattr(mod, campaign).copy()
for dataset in list(campaign_inst.datasets):
dataset.x.campaign = campaign
if not hbw_campaign_inst.has_dataset(dataset.name):
hbw_campaign_inst.add_dataset(dataset)

cpn_task = BuildCampaignSummary(
config=config_name,
)
if cpn_task.complete():
logger.warning(
f"Using pickled campaign for config {config_name}; to re-initialize, run:\n"
f"law run {cpn_task.task_family} --config {config_name} --remove-output 0,a,y",
)
else:
raise ValueError(
f"Campaign used for {config_name} is not yet initialized; to initialize, run: \n"
f"law run {cpn_task.task_family} --config {config_name} --remove-output 0,a,y",
)
# cpn_task.run()

hbw_campaign_inst = cpn_task.output()["hbw_campaign_inst"].load(formatter="pickle")
return add_config(
analysis_inst,
hbw_campaign_inst,
Expand All @@ -139,41 +141,39 @@ def analysis_factory(configs: od.UniqueObjectIndex):

# 2017
add_lazy_config(
{
"cmsdb.campaigns.run2_2017_nano_v9": "campaign_run2_2017_nano_v9",
},
"c17",
1700,
)

# 2022 preEE
add_lazy_config(
{
"cmsdb.campaigns.run3_2022_preEE_nano_v12": "campaign_run3_2022_preEE_nano_v12",
"cmsdb.campaigns.run3_2022_preEE_nano_v13": "campaign_run3_2022_preEE_nano_v13",
},
"c22pre",
2200,
)

# 2022 postEE
add_lazy_config(
{
"cmsdb.campaigns.run3_2022_postEE_nano_v12": "campaign_run3_2022_postEE_nano_v12",
"cmsdb.campaigns.run3_2022_postEE_nano_v13": "campaign_run3_2022_postEE_nano_v13",
},
"c22post",
2210,
)

add_lazy_config(
"c22pre_das",
2201,
)
add_lazy_config(
"c22post_das",
2211,
)

#
# modify store_parts
#

software_tasks = ("cf.BundleBashSandbox", "cf.BundleCMSSWSandbox", "cf.BundleSoftware")
shareable_analysis_tasks = ("cf.CalibrateEvents", "cf.GetDatasetLFNs")
limited_config_shared_tasks = ("cf.CalibrateEvents", "cf.GetDatasetLFNs", "cf.SelectEvents", "cf.ReduceEvents")
skip_new_version_schema = ("cf.CalibrateEvents", "cf.GetDatasetLFNs")
skip_new_version_schema = ()
known_parts = (
# from cf
"analysis", "task_family", "config", "configs", "dataset", "shift", "version",
Expand Down Expand Up @@ -234,11 +234,13 @@ def reorganize_parts(task, store_parts):
"analysis",
"calibrator", "calibrators", "calib",
"selector", "sel",
"producer", "producers", "prod",
"config", "configs",
"producers", "prod",
"ml_data", "ml_model", "ml_models",
"weightprod", "inf_model",
"task_family",
"config", "dataset", "shift",
"calibrator", "producer",
"shift", "dataset",
]
parts_order_end = ["version"]

Expand Down
83 changes: 83 additions & 0 deletions hbw/analysis/processes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# coding: utf-8

"""
Creation and modification of processes in the HH -> bbWW analysis.
NOTE: it is crucial to modify processes before the campaign is created. Otherwise,
the changes will not be reflected in the campaign and there will be inconsistencies.
"""

# import order as od


from hbw.config.processes import create_parent_process
from hbw.config.styling import color_palette
from cmsdb.util import add_decay_process


def modify_cmsdb_processes():
from cmsdb.processes import (
qcd_mu, qcd_em, qcd_bctoe,
tt, ttv, st, w_lnu, vv, h,
dy, dy_m4to10, dy_m10to50, dy_m50toinf, dy_m50toinf_0j, dy_m50toinf_1j, dy_m50toinf_2j,
)

qcd_mu.label = "QCD Muon enriched"
qcd_ele = create_parent_process(
[qcd_em, qcd_bctoe],
name="qcd_ele",
id=31199,
label="QCD Electron enriched",
)

v_lep = create_parent_process(
[w_lnu, dy],
name="v_lep",
id=64575573, # random number
label="W and DY",
)

t_bkg = create_parent_process(
[st, tt, ttv],
name="t_bkg",
id=97842611, # random number
label="tt + st",
)

background = create_parent_process( # noqa: F841
[t_bkg, v_lep, vv, w_lnu, h, qcd_ele, qcd_mu],
name="background",
id=99999,
label="background",
color=color_palette["blue"],
)

decay_map = {
"lf": {
"name": "lf",
"id": 50,
"label": "(lf)",
"br": -1,
},
"hf": {
"name": "hf",
"id": 60,
"label": "(hf)",
"br": -1,
},
}

for dy_proc_inst in (
dy, dy_m4to10, dy_m10to50, dy_m50toinf, dy_m50toinf_0j, dy_m50toinf_1j, dy_m50toinf_2j,
):
add_production_mode_parent = dy_proc_inst.name != "dy"
for flavour in ("hf", "lf"):
# the 'add_decay_process' function helps us to create all parent-daughter relationships
add_decay_process(
dy_proc_inst,
decay_map[flavour],
add_production_mode_parent=add_production_mode_parent,
name_func=lambda parent_name, decay_name: f"{parent_name}_{decay_name}",
label_func=lambda parent_label, decay_label: f"{parent_label} {decay_label}",
xsecs=None,
aux={"flavour": flavour},
)
Loading

0 comments on commit 932cedb

Please sign in to comment.