-
Notifications
You must be signed in to change notification settings - Fork 21
/
MakeCutsFilesForSyst.py
152 lines (136 loc) · 6.24 KB
/
MakeCutsFilesForSyst.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
'''
python script to create yaml files with set of cuts for cut-variation studies
'''
import os
import argparse
import copy
from itertools import product
import yaml
def get_variation_mult(edge, kind): # pylint: disable=too-many-return-statements
if kind == 'loose_1':
if edge == 'min':
return -1.
if edge == 'max':
return 1.
if kind == 'loose_2':
if edge == 'min':
return -2.
if edge == 'max':
return 2.
if kind == 'tight_1':
if edge == 'min':
return 1.
if edge == 'max':
return -1.
if kind == 'tight_2':
if edge == 'min':
return 2.
if edge == 'max':
return -2.
return 0.
def check_value(new_value, cut_lim, histo_lim, edge):
if edge == 'min':
return cut_lim <= new_value < histo_lim
if edge == 'max':
return histo_lim < new_value <= cut_lim
return False
def make_cuts():
var_name = ['cospkphi3', 'cp', 'cpxy', 'd0', 'deltamKK', 'dl', 'dlxy', 'ndlxy', 'sigvtx', 'topo']
var_key = ['CosPiKPhi3', 'CosP', 'CosPXY', 'd0', 'DeltaMassKK', 'DecL', 'DecLXY', 'NormDecLXY',
'SigmaVtx', 'd0d0Exp']
edge_to_vary = ['min', 'min', 'min', 'max', 'max', 'min', 'min', 'min', 'max', 'max']
step_variation = [0.5, 0.5, 0.5, 10., 1., 10., 10., 1., 5., 0.5]
histo_lims = [3.5, 100., 100., 0., 0., 105., 105., 10.5, 0., 0.]
variation_kind = ['loose_1', 'loose_2', 'tight_1', 'tight_2']
# [-1, -2, +1, +2]*step_variation added to central value
in_dir = 'configfiles/'
cut_file_central = 'cutset_3050_central_2018.yml'
cut_file_loose = 'cutset_3050_loose_2018.yml'
out_dir = 'configfiles/syst_cuts_Ds_3050/'
if not os.path.exists(out_dir):
os.makedirs(out_dir)
with open(in_dir + cut_file_central, 'r') as cut_file_yml:
cutset = yaml.load(cut_file_yml, yaml.FullLoader)
with open(in_dir + cut_file_loose, 'r') as cut_file_loose_yml:
cutset_loose = yaml.load(cut_file_loose_yml, yaml.FullLoader)
cutset_central = copy.deepcopy(cutset)
with open(out_dir + cut_file_central, 'w') as outfile:
yaml.dump(cutset_central, outfile, default_flow_style=False)
for name, key, edge, step, histo_lim in zip(var_name, var_key, edge_to_vary, step_variation, histo_lims):
loose_values = cutset_loose['cutvars'][key][edge]
for kind in variation_kind:
cutset_mod = copy.deepcopy(cutset)
mult_value = get_variation_mult(edge, kind)
modified_list = []
for value, cut_lim in zip(cutset_mod['cutvars'][key][edge], loose_values):
new_value = value + step * mult_value
if check_value(new_value, cut_lim, histo_lim, edge):
modified_list.append(new_value)
else:
modified_list.append(value)
cutset_mod['cutvars'][key][edge] = modified_list
cut_file_mod = cut_file_central.replace('central_2018', name + '_' + kind)
with open(out_dir + cut_file_mod, 'w') as outfile_mod:
yaml.dump(cutset_mod, outfile_mod, default_flow_style=False)
def make_cuts_ml():
var_key = ['ML_output_FD'] # ['ML_output_FD', 'ML_output_Bkg']
var_tag = ['outFD'] # ['outFD', 'outBkg'] # used in file names to reduce length
step_variation = [{"4": 0.02, "6": 0.02}]
# {"2": 0.0001, "3": 0.00005, "4": 0.00005, "5": 0.00005, "6": 0.0001, "8": 0.0002, "12": 0.002, "16": 0.002, "24": 0.002, "36": 0.001}]
# {"2": 0.00005, "3": 0.00005, "4": 0.00005, "5": 0.00005, "6": 0.0001, "8": 0.0002, "12": 0.002, "16": 0.002, "24": 0.002, "36": 0.001} 0-10%
# {"2": 0.0001, "3": 0.0001, "4": 0.0001, "5": 0.0001, "6": 0.0002, "8": 0.001, "12": 0.002, "16": 0.002, "24": 0.002, "36": 0.001} 30-50%
# [{"2": 0.01, "4": 0.01, "6": 0.01, "8": 0.01, "12": 0.01},
# {"2": 0.0005, "4": 0.0005, "6": 0.001, "8": 0.001, "12": 0.0005}]
num_step_pos = 13
num_step_neg = 10
edge_to_vary = ['min'] # ['min', 'max']
in_dir = 'configfiles/cutsets/LctopK0s/'
cut_file_central = 'cutset_LctopK0s_13TeV_presel_FDEn.yml'
out_dir = 'configfiles/cutsets/LctopK0s/'
out_file_tag = 'cutset_ML'
if not os.path.exists(out_dir):
os.makedirs(out_dir)
with open(in_dir + cut_file_central, 'r') as cut_file_yml:
cutset = yaml.load(cut_file_yml, yaml.FullLoader)
#same number of steps for all variables
neg_steps = [-i for i in range(1, num_step_neg + 1)]
neg_steps = neg_steps[::-1]
pos_steps = list(range(0, num_step_pos + 1))
steps = neg_steps + pos_steps
n_combinations = len(var_key)
for prod_steps in product(steps, repeat=n_combinations):
cutset_mod = copy.deepcopy(cutset)
file_tag = str()
for i, step in enumerate(prod_steps):
modified_list = []
cuts = cutset_mod['cutvars'][var_key[i]]
for min_val, max_val, pt_min in zip(cuts['min'], cuts['max'], cutset_mod['cutvars']['Pt']['min']):
if edge_to_vary[i] == 'min':
new_value = min_val + step * step_variation[i][f'{pt_min:.0f}']
if(new_value < 0. or new_value >= max_val):
new_value = min_val
else:
new_value = max_val + step * step_variation[i][f'{pt_min:.0f}']
if(new_value > 1. or new_value <= min_val):
new_value = max_val
modified_list.append(new_value)
cuts[edge_to_vary[i]] = modified_list
step_name = 'pos'
if step < 0.:
step_name = 'neg'
step += num_step_neg + 1
# more than 100 files unlikely
name = f'_{var_tag[i]}_{step_name}{str(int(step)).zfill(2)}'
file_tag += name
cut_file_mod = f'{out_file_tag}{file_tag}.yml'
with open(out_dir + cut_file_mod, 'w') as outfile_mod:
yaml.dump(cutset_mod, outfile_mod, default_flow_style=False)
def main():
parser = argparse.ArgumentParser(description='Arguments')
parser.add_argument("--ml", help="make cuts for ml", action="store_true")
args = parser.parse_args()
if args.ml:
make_cuts_ml()
else:
make_cuts()
main()