-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathff_calculation.py
157 lines (134 loc) · 5.15 KB
/
ff_calculation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
Main script initializing the fake factor calculation
"""
import argparse
import yaml
import os
import logging
import concurrent.futures
from typing import Dict, Union, List, Tuple
import FF_calculation.FF_QCD as FF_QCD
import FF_calculation.FF_Wjets as FF_Wjets
import FF_calculation.FF_ttbar as FF_ttbar
from FF_calculation.fractions import fraction_calculation
import helper.correctionlib_json as corrlib
import helper.functions as func
parser = argparse.ArgumentParser()
parser.add_argument(
"--config-file",
default=None,
help="Path to the config file which contains information for the fake factor calculation step.",
)
def run_ff_calculation(
args: Tuple[str, Dict[str, Union[Dict, List, str]], List[str], str]
) -> Dict:
"""
This function can be used for multiprocessing. It runs the fake factor calculation step for a specified process.
Args:
args: Tuple with a process name, a configuration for this process, a list of all sample paths and a path for the output
Return:
Depending on the "process" either a dictionary with fake factor function expressions or a dictionary with process fraction values
"""
process, config, sample_paths, output_path = args
log = logging.getLogger(f"ff_calculation.{process}")
if process in ["QCD", "QCD_subleading"]:
log.info(f"Calculating fake factors for the {process} process.")
log.info("-" * 50)
result = FF_QCD.calculation_QCD_FFs(
config=config,
sample_paths=sample_paths,
output_path=output_path,
process=process,
logger=f"ff_calculation.{process}",
)
elif process == "Wjets":
log.info("Calculating fake factors for the Wjets process.")
log.info("-" * 50)
result = FF_Wjets.calculation_Wjets_FFs(
config=config,
sample_paths=sample_paths,
output_path=output_path,
logger=f"ff_calculation.{process}",
)
elif process == "ttbar":
log.info("Calculating fake factors for the ttbar process.")
log.info("-" * 50)
result = FF_ttbar.calculation_ttbar_FFs(
config=config,
sample_paths=sample_paths,
output_path=output_path,
logger=f"ff_calculation.{process}",
)
elif process == "fractions":
log.info("Calculating the process fractions for the FF application.")
log.info("-" * 50)
result = fraction_calculation(
config=config,
sample_paths=sample_paths,
output_path=output_path,
logger=f"ff_calculation.{process}",
)
else:
raise Exception(f"Target process: Such a process is not known: {process}")
return result
if __name__ == "__main__":
args = parser.parse_args()
# loading of the chosen config file
with open(args.config_file, "r") as file:
config = yaml.load(file, yaml.FullLoader)
save_path_ffs = os.path.join("workdir", config["workdir_name"], config["era"])
func.check_path(path=os.path.join(os.getcwd(), save_path_ffs))
save_path_plots = os.path.join(
"workdir",
config["workdir_name"],
config["era"],
"fake_factors",
config["channel"],
)
func.check_path(path=os.path.join(os.getcwd(), save_path_plots))
# start output logging
if "process_fractions" in config:
subcategories = list(config["target_processes"].keys()) + ["fractions"]
else:
subcategories = config["target_processes"].keys()
func.setup_logger(
log_file=save_path_plots + "/ff_calculation.log",
log_name="ff_calculation",
subcategories=subcategories,
)
# getting all the input files
sample_paths = func.get_samples(config=config)
if len(sample_paths) == 0:
raise Exception("No input files found!")
# check binning of defined categories in the config
func.check_categories(config=config)
# initializing the fake factor calculation
fake_factors = dict()
fractions = None
if "target_processes" in config:
args_list = [
(process, config, sample_paths, save_path_plots)
for process in config["target_processes"]
]
if "process_fractions" in config:
args_list.append(("fractions", config, sample_paths, save_path_plots))
with concurrent.futures.ProcessPoolExecutor(max_workers=8) as executor:
for args, result in zip(
args_list, executor.map(run_ff_calculation, args_list)
):
if args[0] in config["target_processes"]:
fake_factors[args[0]] = result
elif args[0] == "fractions":
fractions = result
else:
raise Exception("No target processes are defined!")
corrlib.generate_ff_corrlib_json(
config=config,
ff_functions=fake_factors,
fractions=fractions,
output_path=save_path_ffs,
for_corrections=False,
)
# dumping config to output directory for documentation
with open(save_path_plots + "/config.yaml", "w") as config_file:
yaml.dump(config, config_file, default_flow_style=False)