-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun_stage2.py
169 lines (154 loc) · 4.63 KB
/
run_stage2.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
158
159
160
161
162
163
164
165
166
167
168
169
import glob
import tqdm
import argparse
import dask
from dask.distributed import Client
import dask.dataframe as dd
from python.io import load_dataframe
from stage2.postprocessor import process_partitions
from config.mva_bins import mva_bins
from config.variables import variables_lookup
__all__ = ["dask"]
parser = argparse.ArgumentParser()
parser.add_argument(
"-y", "--years", nargs="+", help="Years to process", default=["2018"]
)
parser.add_argument(
"-sl",
"--slurm",
dest="slurm_port",
default=None,
action="store",
help="Slurm cluster port (if not specified, will create a local cluster)",
)
args = parser.parse_args()
# Dask client settings
use_local_cluster = args.slurm_port is None
node_ip = "128.211.149.133"
if use_local_cluster:
ncpus_local = 40
slurm_cluster_ip = ""
dashboard_address = f"{node_ip}:34875"
else:
slurm_cluster_ip = f"{node_ip}:{args.slurm_port}"
dashboard_address = f"{node_ip}:8787"
# global parameters
parameters = {
# < general settings >
"slurm_cluster_ip": slurm_cluster_ip,
"global_path": "/depot/cms/hmm/copperhead/",
"years": args.years,
"label": "test",
"channels": ["vbf"],
"regions": ["h-peak", "h-sidebands"],
"syst_variations": ["nominal"],
# "custom_npartitions": {
# "vbf_powheg_dipole": 1,
# },
#
# < settings for histograms >
"hist_vars": ["dimuon_mass"],
"variables_lookup": variables_lookup,
"save_hists": True,
#
# < settings for unbinned output>
"tosave_unbinned": {
"vbf": ["dimuon_mass", "event", "wgt_nominal", "mu1_pt", "score_pytorch_test"],
"ggh_0jets": ["dimuon_mass", "wgt_nominal"],
"ggh_1jet": ["dimuon_mass", "wgt_nominal"],
"ggh_2orMoreJets": ["dimuon_mass", "wgt_nominal"],
},
"save_unbinned": True,
#
# < MVA settings >
"models_path": "data/trained_models/",
"dnn_models": {
"vbf": ["pytorch_test"],
},
"bdt_models": {},
"mva_bins_original": mva_bins,
}
parameters["datasets"] = [
"data_A",
"data_B",
"data_C",
"data_D",
"data_E",
"data_F",
"data_G",
"data_H",
"dy_m105_160_amc",
"dy_m105_160_vbf_amc",
"ewk_lljj_mll105_160_py_dipole",
"ttjets_dl",
"ttjets_sl",
"ttw",
"ttz",
"st_tw_top",
"st_tw_antitop",
"ww_2l2nu",
"wz_2l2q",
"wz_1l1nu2q",
"wz_3lnu",
"zz",
"www",
"wwz",
"wzz",
"zzz",
"ggh_amcPS",
"vbf_powheg_dipole",
]
# using one small dataset for debugging
# parameters["datasets"] = ["vbf_powheg_dipole"]
if __name__ == "__main__":
# prepare Dask client
if use_local_cluster:
print(
f"Creating local cluster with {ncpus_local} workers."
f" Dashboard address: {dashboard_address}"
)
client = Client(
processes=True,
dashboard_address=dashboard_address,
n_workers=ncpus_local,
threads_per_worker=1,
memory_limit="4GB",
)
else:
print(
f"Connecting to Slurm cluster at {slurm_cluster_ip}."
f" Dashboard address: {dashboard_address}"
)
client = Client(parameters["slurm_cluster_ip"])
parameters["ncpus"] = len(client.scheduler_info()["workers"])
print(f"Connected to cluster! #CPUs = {parameters['ncpus']}")
# add MVA scores to the list of variables to create histograms from
dnn_models = list(parameters["dnn_models"].values())
bdt_models = list(parameters["bdt_models"].values())
for models in dnn_models + bdt_models:
for model in models:
parameters["hist_vars"] += ["score_" + model]
# prepare lists of paths to parquet files (stage1 output) for each year and dataset
all_paths = {}
for year in parameters["years"]:
all_paths[year] = {}
for dataset in parameters["datasets"]:
paths = glob.glob(
f"{parameters['global_path']}/"
f"{parameters['label']}/stage1_output/{year}/"
f"{dataset}/*.parquet"
)
all_paths[year][dataset] = paths
# run postprocessing
for year in parameters["years"]:
print(f"Processing {year}")
for dataset, path in tqdm.tqdm(all_paths[year].items()):
if len(path) == 0:
continue
# read stage1 outputs
df = load_dataframe(client, parameters, inputs=[path], dataset=dataset)
if not isinstance(df, dd.DataFrame):
continue
# run processing sequence (categorization, mva, histograms)
info = process_partitions(client, parameters, df)
# print(info)