Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ configs.*/
*.pdf
*.png
*.root
*.feather

# C extensions
*.so
Expand Down
269 changes: 106 additions & 163 deletions FF_calculation/FF_QCD.py

Large diffs are not rendered by default.

333 changes: 137 additions & 196 deletions FF_calculation/FF_Wjets.py

Large diffs are not rendered by default.

145 changes: 38 additions & 107 deletions FF_calculation/FF_ttbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def calculation_ttbar_FFs(

log = logging.getLogger(logger)

# init histogram dict for FF measurement from MC
SR_hists = dict()
AR_hists = dict()

Expand All @@ -70,10 +69,7 @@ def calculation_ttbar_FFs(

rdf = ROOT.RDataFrame(config["tree"], sample_path)

# event filter for ttbar signal region
log.info(
f"Filtering events for the signal region. Target process: {process}"
)
log.info(f"Filtering events for the signal region. Target process: {process}")
region_conf = copy.deepcopy(process_conf["SR_cuts"])
rdf_SR = ff_func.apply_region_filters(
rdf=rdf,
Expand All @@ -84,10 +80,7 @@ def calculation_ttbar_FFs(
logger=logger,
)

# event filter for ttbar application region
log.info(
f"Filtering events for the application region. Target process: {process}"
)
log.info(f"Filtering events for the application region. Target process: {process}")
region_conf = copy.deepcopy(process_conf["AR_cuts"])
rdf_AR = ff_func.apply_region_filters(
rdf=rdf,
Expand All @@ -98,34 +91,21 @@ def calculation_ttbar_FFs(
logger=logger,
)

# get binning of the dependent variable
xbinning = array.array("d", splitting.var_bins)
nbinsx = len(splitting.var_bins) - 1

# making the histograms
h = RuntimeVariables.RDataFrameWrapper(rdf_SR).Histo1D(
(
process_conf["var_dependence"],
f"{sample}",
nbinsx,
xbinning,
),
SR_hists[sample] = RuntimeVariables.RDataFrameWrapper(rdf_SR).Histo1D(
(process_conf["var_dependence"], f"{sample}", nbinsx, xbinning),
process_conf["var_dependence"],
"weight",
)
SR_hists[sample] = h.GetValue()

h = RuntimeVariables.RDataFrameWrapper(rdf_AR).Histo1D(
(
process_conf["var_dependence"],
f"{sample}",
nbinsx,
xbinning,
),
).GetValue()

AR_hists[sample] = RuntimeVariables.RDataFrameWrapper(rdf_AR).Histo1D(
(process_conf["var_dependence"], f"{sample}", nbinsx, xbinning),
process_conf["var_dependence"],
"weight",
)
AR_hists[sample] = h.GetValue()
).GetValue()

# Start of the FF calculation
FF_hist = ff_func.calculate_ttbar_FF(
Expand All @@ -142,14 +122,15 @@ def calculation_ttbar_FFs(
logger=logger,
fit_option=splitting.fit_option,
limit_kwargs=splitting.limit_kwargs(hist=FF_hist),
stat_sigma=config["stat_sigma"] if "stat_sigma" in config else 1.0,
)
elif isinstance(splitting.fit_option, str):
nominal_draw_obj, results = ff_func.smooth_function(
hist=FF_hist.Clone(),
bin_edges=splitting.var_bins,
correction_option=splitting.fit_option,
bandwidth=splitting.bandwidth,
for_FF=True,
stat_sigma=config["stat_sigma"] if "stat_sigma" in config else 1.0,
)
unc_draw_obj = results["default"]
used_fit = splitting.fit_option
Expand All @@ -172,20 +153,10 @@ def calculation_ttbar_FFs(
save_data=True,
)

# doing some control plots
# doing control plots
for _hist, _region, _data, _samples in [
(
SRlike_hists,
"SR_like",
"data",
ff_func.controlplot_samples(config["use_embedding"]),
),
(
ARlike_hists,
"AR_like",
"data",
ff_func.controlplot_samples(config["use_embedding"]),
),
(SRlike_hists, "SR_like", "data", ff_func.controlplot_samples(sample_paths)),
(ARlike_hists, "AR_like", "data", ff_func.controlplot_samples(sample_paths)),
(SRlike_hists, "SR_like", "data_subtracted", ["ttbar_J"]),
(ARlike_hists, "AR_like", "data_subtracted", ["ttbar_J"]),
]:
Expand Down Expand Up @@ -215,11 +186,7 @@ def calculation_ttbar_FFs(
return results


@logging_helper.LogDecorator().grouped_logs(
extractor=lambda *args, **kwargs: (
kwargs["logger"] if "config" in kwargs else args[4]
)
)
@logging_helper.LogDecorator().grouped_logs(extractor=lambda *args, **kwargs: (kwargs["logger"] if "config" in kwargs else args[4]))
def calculation_FF_data_scaling_factor(
config: Dict[str, Union[str, Dict, List]],
process_conf: Dict[str, Union[str, Dict, List]],
Expand All @@ -244,10 +211,8 @@ def calculation_FF_data_scaling_factor(
"""
log = logging.getLogger(logger)

# init histogram dict for FF data correction
SRlike_hists = dict()
ARlike_hists = dict()
# init histogram dict for QCD SS/OS estimation
SRlike_hists_qcd = dict()
ARlike_hists_qcd = dict()

Expand All @@ -259,10 +224,7 @@ def calculation_FF_data_scaling_factor(

rdf = ROOT.RDataFrame(config["tree"], sample_path)

# event filter for ttbar signal-like region
log.info(
f"Filtering events for the signal-like region. Target process: {process}"
)
log.info(f"Filtering events for the signal-like region. Target process: {process}")
region_conf = copy.deepcopy(process_conf["SRlike_cuts"])
rdf_SRlike = ff_func.apply_region_filters(
rdf=rdf,
Expand All @@ -273,8 +235,7 @@ def calculation_FF_data_scaling_factor(
logger=logger,
)

# QCD estimation from same sign in signal-like region
if "tau_pair_sign" in region_conf:
if "tau_pair_sign" in region_conf: # QCD estimation from same sign in signal-like region
region_conf["tau_pair_sign"] = "(q_1*q_2) > 0" # same sign
else:
raise ValueError(
Expand All @@ -293,10 +254,7 @@ def calculation_FF_data_scaling_factor(
logger=logger,
)

# event filter for ttbar application-like region
log.info(
f"Filtering events for the application-like region. Target process: {process}"
)
log.info(f"Filtering events for the application-like region. Target process: {process}")
region_conf = copy.deepcopy(process_conf["ARlike_cuts"])
rdf_ARlike = ff_func.apply_region_filters(
rdf=rdf,
Expand All @@ -307,8 +265,7 @@ def calculation_FF_data_scaling_factor(
logger=logger,
)

# QCD estimation from same sign in application-like region
if "tau_pair_sign" in region_conf:
if "tau_pair_sign" in region_conf: # QCD estimation from same sign in application-like region
region_conf["tau_pair_sign"] = "(q_1*q_2) > 0" # same sign
else:
raise ValueError(
Expand All @@ -328,26 +285,22 @@ def calculation_FF_data_scaling_factor(
)

# make yield histograms for FF data correction
h = RuntimeVariables.RDataFrameWrapper(rdf_SRlike).Histo1D(
SRlike_hists[sample] = RuntimeVariables.RDataFrameWrapper(rdf_SRlike).Histo1D(
("#phi(#slash{E}_{T})", f"{sample}", 1, -3.5, 3.5), "metphi", "weight"
)
SRlike_hists[sample] = h.GetValue()
).GetValue()

h = RuntimeVariables.RDataFrameWrapper(rdf_ARlike).Histo1D(
ARlike_hists[sample] = RuntimeVariables.RDataFrameWrapper(rdf_ARlike).Histo1D(
("#phi(#slash{E}_{T})", f"{sample}", 1, -3.5, 3.5), "metphi", "weight"
)
ARlike_hists[sample] = h.GetValue()
).GetValue()

# make yield histograms for QCD estimation
h_qcd = RuntimeVariables.RDataFrameWrapper(rdf_SRlike_qcd).Histo1D(
SRlike_hists_qcd[sample] = RuntimeVariables.RDataFrameWrapper(rdf_SRlike_qcd).Histo1D(
("#phi(#slash{E}_{T})", f"{sample}", 1, -3.5, 3.5), "metphi", "weight"
)
SRlike_hists_qcd[sample] = h_qcd.GetValue()
).GetValue()

h_qcd = RuntimeVariables.RDataFrameWrapper(rdf_ARlike_qcd).Histo1D(
ARlike_hists_qcd[sample] = RuntimeVariables.RDataFrameWrapper(rdf_ARlike_qcd).Histo1D(
("#phi(#slash{E}_{T})", f"{sample}", 1, -3.5, 3.5), "metphi", "weight"
)
ARlike_hists_qcd[sample] = h_qcd.GetValue()
).GetValue()

# calculate QCD estimation
SRlike_hists["QCD"] = ff_func.QCD_SS_estimate(hists=SRlike_hists_qcd)
Expand Down Expand Up @@ -412,7 +365,6 @@ def non_closure_correction(

log = logging.getLogger(logger)

# init histogram dict for FF measurement
SR_hists = dict()
AR_hists = dict()

Expand All @@ -432,10 +384,7 @@ def non_closure_correction(

rdf = ROOT.RDataFrame(config["tree"], sample_path)

# event filter for ttbar signal region
log.info(
f"Filtering events for the signal region. Target process: {process}"
)
log.info(f"Filtering events for the signal region. Target process: {process}")
region_conf = copy.deepcopy(config["target_processes"][process]["SR_cuts"])
rdf_SR = ff_func.apply_region_filters(
rdf=rdf,
Expand All @@ -446,10 +395,7 @@ def non_closure_correction(
logger=logger,
)

# event filter for ttbar application region
log.info(
f"Filtering events for the application region. Target process: {process}"
)
log.info(f"Filtering events for the application region. Target process: {process}")
region_conf = copy.deepcopy(config["target_processes"][process]["AR_cuts"])
rdf_AR = ff_func.apply_region_filters(
rdf=rdf,
Expand All @@ -465,43 +411,27 @@ def non_closure_correction(
# additionally evaluate the previous corrections
corr_str = ""
for corr_evaluator in corr_evaluators:
rdf_AR = corr_evaluator.evaluate_correction(
rdf=rdf_AR,
)
rdf_AR = corr_evaluator.evaluate_correction(rdf=rdf_AR)
corr_str += f" * {corr_evaluator.corr_str}"

rdf_AR = rdf_AR.Define(
"weight_ff", f"weight * {process}_fake_factor{corr_str}"
)

# get binning of the dependent variable
xbinning = array.array("d", splitting.var_bins)
nbinsx = len(splitting.var_bins) - 1

# making the histograms
h = RuntimeVariables.RDataFrameWrapper(rdf_SR).Histo1D(
(
correction_conf["var_dependence"],
f"{sample}",
nbinsx,
xbinning,
),
SR_hists[sample] = RuntimeVariables.RDataFrameWrapper(rdf_SR).Histo1D(
(correction_conf["var_dependence"], f"{sample}", nbinsx, xbinning),
correction_conf["var_dependence"],
"weight",
)
SR_hists[sample] = h.GetValue()

h = RuntimeVariables.RDataFrameWrapper(rdf_AR).Histo1D(
(
correction_conf["var_dependence"],
f"{sample}",
nbinsx,
xbinning,
),
).GetValue()

AR_hists["ttbar_ff"] = RuntimeVariables.RDataFrameWrapper(rdf_AR).Histo1D(
(correction_conf["var_dependence"], f"{sample}", nbinsx, xbinning),
correction_conf["var_dependence"],
"weight_ff",
)
AR_hists["ttbar_ff"] = h.GetValue()
).GetValue()

correction_hist = ff_func.calculate_non_closure_correction_ttbar_fromMC(
SR=SR_hists, AR=AR_hists
Expand All @@ -512,6 +442,7 @@ def non_closure_correction(
bin_edges=splitting.var_bins,
correction_option=splitting.correction_option,
bandwidth=splitting.bandwidth,
stat_sigma=config["stat_sigma"] if "stat_sigma" in config else 1.0,
)

plotting.plot_correction(
Expand Down
Loading