-
Notifications
You must be signed in to change notification settings - Fork 0
/
CouplingMergingTool.py
255 lines (230 loc) · 12.2 KB
/
CouplingMergingTool.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# Calculate HH model and combine samples.
# Author: Licheng ZHANG ([email protected])
#################################################
import numpy as np
import pandas as pd
import ROOT as R
import sys
import os
import argparse
import matplotlib.pyplot as plt
from util import *
from analysis_branch import *
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--multithread", dest="multi", default=0, type=int, help="enable multi-thread with n threads (default = 0)" )
parser.add_argument("-v", "--verbose", dest="verb", default=0, type=int, help="different verbose level (default = 0)" )
parser.add_argument("-n", "--nBasicSamples", dest="nBV", default=6, type=int, help="N basic samples (default = 6)" )
parser.add_argument("-c", "--CouplingType", dest="CType", default="c2v", type=str, help="Coupling type (default = c2v) cv/kl/kt ..." )
parser.add_argument("-hh", "--HHModel", dest="HHModel", default="VHH", type=str, help="Coupling type (default = VHH) GGF/VBF/VHH ..." )
parser.add_argument("-r", "--CouplingRange", dest="cRange", nargs='+', help="[lower limit] [higher limit] (default = -30 30)")
parser.add_argument("-d", "--ifDoCombine", dest="doComb", default=0, type=int, help="if do combination (default = 0)")
parser.add_argument("-w", "--NewCoupling", dest="newCoup", default=10, type=int, help="The new coupling we need to combine (default = 10)")
args = parser.parse_args()
if args.multi != 0:
R.EnableImplicitMT(args.multi)
print('using ' + str(args.multi) + ' threads in MT Mode!')
else:
print('Disable MT Mode!')
if args.HHModel == 'VHH':
if args.nBV == 6:
listOfCouplings0 = np.array([[1,1,1],[0.5,1,1],[1,1,2],[1,0,1],[1,1,0],[1,2,1]])
CrossSec0 = np.array([0.000363,0.0002278,0.000584,0.0001245,0.000212,0.000929])
elif args.nBV == 7:
listOfCouplings0 = np.array([[1,1,1],[0.5,1,1],[1,1,2],[1,0,1],[1,1,0],[1,2,1],[1.5,1,1]])
CrossSec0 = np.array([0.000363,0.0002278,0.000584,0.0001245,0.000212,0.000929,0.000790])
elif args.nBV == 8:
listOfCouplings0 = np.array([[1,1,1],[0.5,1,1],[1,1,2],[1,0,1],[1,1,0],[1,2,1],[1.5,1,1],[1,1,20]])
CrossSec0 = np.array([0.000363,0.0002278,0.000584,0.0001245,0.000212,0.000929,0.000790,0.0165721])
else:
print('There should be 6~8 samples to form the basic vectors, enforce to 6')
listOfCouplings0 = np.array([[1,1,1],[0.5,1,1],[1,1,2],[1,0,1],[1,1,0],[1,2,1]])
CrossSec0 = np.array([0.000363,0.0002278,0.000584,0.0001245,0.000212,0.000790])
elif args.HHModel == 'VBF':
listOfCouplings0 = np.array([[1,1,1],[1,1,0],[1,1,2],[1,0,1],[1,2,1],[0.5,1,1],[1.5,1,1]])
CrossSec0 = np.array([0.0017260,0.0046089,0.0014228,0.0270800,0.0142178,0.0108237,0.0660185])
elif args.HHModel == 'GGF':
listOfCouplings0 = np.array([[0,1],[1,1],[2.45,1],[5,1]])
CrossSec0 = np.array([0.069725,0.031047,0.013124,0.091172])
else:
print(args.HHModel+" model not exist or haven't been developped, please further check.")
exit(0)
lowLimit = int(args.cRange[0])
highLimit = int(args.cRange[1])
numEle = highLimit - lowLimit
longListOfXSec0 = []
if args.HHModel == 'VHH' or args.HHModel == 'VBF':
Coupling0 = convert_coupling_diagramweight_VBF_VHH(listOfCouplings0)
CouplingInv0=np.linalg.pinv(Coupling0)
matrix_ele0 = np.matmul(CouplingInv0, CrossSec0)
longListOfCoupling = np.zeros(shape=(numEle,3))
if args.CType == 'c2v':
for num in range(lowLimit,highLimit):
longListOfCoupling[num-lowLimit] = [1,num,1]
elif args.CType == 'kl':
for num in range(lowLimit,highLimit):
longListOfCoupling[num-lowLimit] = [1,1,num]
elif args.CType == 'cv':
for num in range(lowLimit,highLimit):
longListOfCoupling[num-lowLimit] = [num,1,1]
else:
print(args.CType + 'is not in SM coupling list or under developing, enforce to c2v')
for num in range(lowLimit,highLimit):
longListOfCoupling[num-lowLimit] = [1,num,1]
for element in longListOfCoupling:
elementCoupling = convert_coupling_diagramweight_VBF_VHH(element.reshape(1,3))
elementXsec = np.matmul(elementCoupling,matrix_ele0.reshape(6,1))
longListOfXSec0.append(elementXsec[0,0])
elif args.HHModel == 'GGF':
Coupling0 = convert_coupling_diagramweight_GGF(listOfCouplings0)
CouplingInv0=np.linalg.pinv(Coupling0)
matrix_ele0 = np.matmul(CouplingInv0, CrossSec0)
longListOfCoupling = np.zeros(shape=(numEle,2))
if args.CType == 'kl':
for num in range(lowLimit,highLimit):
longListOfCoupling[num-lowLimit] = [num,1]
elif args.CType == 'kt':
# for num in range(lowLimit,highLimit):
# longListOfCoupling[num-lowLimit] = [1,num]
print(args.CType + 'is under developing, enforce to kl')
for num in range(lowLimit,highLimit):
longListOfCoupling[num-lowLimit] = [num,1]
else:
print(args.CType + 'is not in SM coupling list or under developing, enforce to kl')
for num in range(lowLimit,highLimit):
longListOfCoupling[num-lowLimit] = [num,1]
for element in longListOfCoupling:
elementCoupling = convert_coupling_diagramweight_GGF(element.reshape(1,2))
elementXsec = np.matmul(elementCoupling,matrix_ele0.reshape(3,1))
longListOfXSec0.append(elementXsec[0,0])
else:
print(args.HHModel+" model not exist or haven't been developped, please further check.")
exit(0)
x = np.arange(lowLimit,highLimit)
plt.plot(x,longListOfXSec0,color='green',label='{0} bases'.format(args.nBV))
annotation = "Base [1,1,20]"
new_annotation = "{2} new point {0} = {1} \n xs = {3}".format(args.CType,args.newCoup,args.HHModel,str(longListOfXSec0[np.argwhere(x == int(args.newCoup))[0][0]]))
plt.xlabel(args.CType)
plt.ylabel("XSec")
plt.title("XSec variation with {0}".format(args.CType))
plt.grid()
plt.legend(loc='best',fontsize=12)
# plt.text(-20,0.035,'6 bases:[1,1,1],[0.5,1,1],[1,1,2],[1,0,1],[1,1,0],[1.5,1,1]\n7 bases: Add [0.5,1,1]\n8 bases: Add [1,1,20]\nAttention:The green and red line overlap',fontsize=12)
# plt.text(0,1,'other couplings are set to 1 (SM)')
# plt.scatter(x=20,y=0.0165721,s=40,c='y',marker='^')
# plt.annotate(annotation,(20,0.0165721))
plt.scatter(x=args.newCoup,y=longListOfXSec0[np.argwhere(x == int(args.newCoup))[0][0]],s=40,c='y',marker='^')
plt.annotate(new_annotation,(args.newCoup,longListOfXSec0[np.argwhere(x == int(args.newCoup))[0][0]]))
plt.savefig('./CouplingScan.jpg')
plt.show()
plt.close()
if args.HHModel == 'VHH' or args.HHModel == 'VBF':
if args.CType == 'c2v':
listOfCouplings = np.array([[1,args.newCoup,1]])
elif args.CType == 'kl':
listOfCouplings = np.array([[1,1,args.newCoup]])
elif args.CType == 'cv':
listOfCouplings = np.array([[args.newCoup,1,1]])
else:
print(args.CType + 'is not in SM coupling list or under developing, enforce to c2v')
listOfCouplings = np.array([[1,args.newCoup,1]])
NewCoupling = convert_coupling_diagramweight_VBF_VHH(listOfCouplings)
newXsec = np.matmul(NewCoupling,matrix_ele0.reshape(6,1))
composition = np.matmul(NewCoupling, CouplingInv0)
elif args.HHModel == 'GGF':
if args.CType == 'kl':
listOfCouplings = np.array([[args.newCoup,1]])
elif args.CType == 'kt':
print(args.CType + 'is under developing, enforce to kl') #Cautious
listOfCouplings = np.array([[args.newCoup,1]])
else:
print(args.CType + 'is not in SM coupling list or under developing, enforce to kl')
listOfCouplings = np.array([[args.newCoup,1]])
NewCoupling = convert_coupling_diagramweight_GGF(listOfCouplings)
newXsec = np.matmul(NewCoupling,matrix_ele0.reshape(3,1))
composition = np.matmul(NewCoupling, CouplingInv0)
else:
print(args.HHModel+" model not exist or haven't been developped, please further check.")
exit(0)
if args.verb == 0:
print('composition of basic files has been saved, now start merging! ... ')
else:
print("--------------------------- print Coupling0:")
print(listOfCouplings0)
print("--------------------------- print CrossSec0:")
print(CrossSec0)
print("---------------------------- print new couplings:")
print(listOfCouplings)
print("---------------------------- print new Xsec:")
print(newXsec)
print("---------------------------- print composition of original samples:")
print(composition)
if args.doComb == 0:
exit(0)
else:
print("Combination process starting...")
#TODO Add VBF & GGF channels
# args.path
# if VHH
if args.HHModel == 'VHH':
slimmed_sample_path = VHH_Sample_path
slimmed_signal_path = VHH_Signal_path
os.system('mkdir -p ' + slimmed_signal_path)
if args.nBV == 6:
file_list_ZHH = ['ZHHTo4B_CV_1_0_C2V_1_0_C3_1_0','ZHHTo4B_CV_0_5_C2V_1_0_C3_1_0','ZHHTo4B_CV_1_0_C2V_1_0_C3_2_0',\
'ZHHTo4B_CV_1_0_C2V_0_0_C3_1_0','ZHHTo4B_CV_1_0_C2V_1_0_C3_0_0','ZHHTo4B_CV_1_0_C2V_2_0_C3_1_0']
elif args.nBV == 7:
file_list_ZHH = ['ZHHTo4B_CV_1_0_C2V_1_0_C3_1_0','ZHHTo4B_CV_0_5_C2V_1_0_C3_1_0','ZHHTo4B_CV_1_0_C2V_1_0_C3_2_0',\
'ZHHTo4B_CV_1_0_C2V_0_0_C3_1_0','ZHHTo4B_CV_1_0_C2V_1_0_C3_0_0','ZHHTo4B_CV_1_0_C2V_2_0_C3_1_0',\
'ZHHTo4B_CV_1_5_C2V_1_0_C3_1_0']
elif args.nBV == 8:
file_list_ZHH = ['ZHHTo4B_CV_1_0_C2V_1_0_C3_1_0','ZHHTo4B_CV_0_5_C2V_1_0_C3_1_0','ZHHTo4B_CV_1_0_C2V_1_0_C3_2_0',\
'ZHHTo4B_CV_1_0_C2V_0_0_C3_1_0','ZHHTo4B_CV_1_0_C2V_1_0_C3_0_0','ZHHTo4B_CV_1_0_C2V_2_0_C3_1_0',\
'ZHHTo4B_CV_1_5_C2V_1_0_C3_1_0','ZHHTo4B_CV_1_0_C2V_1_0_C3_20_0']
else:
print('There should be 6~8 samples to form the basic vectors, combine failed')
exit(0)
for _sig_file in file_list_ZHH:
haddcmd = 'hadd -f {0}/{1}.root {2}/{1}_Zll_*.root'.format(slimmed_signal_path,_sig_file,slimmed_sample_path)
os.system(haddcmd)
rdf_dict[_sig_file] = R.RDataFrame('Events','{0}/{1}.root'.format(slimmed_signal_path,_sig_file))
#end
composition_string = []
for _comp in composition:
composition_string.append(str(_comp))
indexx = 0
for _sig_file in file_list_ZHH:
print('calculating new weight for '+_sig_file)
rdf_dict[_sig_file] = rdf_dict[_sig_file].Define('new_weight_for_signal','weight*{0}'.format(composition[0,indexx]))\
.Define('components','{0}'.format(composition[0,indexx]))
# .Define('HH_ptRatio','float x = 0.; if(VHH_H1_pT!=0) {x = VHH_H2_pT/VHH_H1_pT;} else {x=x;} return x;')
np_rdf_dict[_sig_file] = rdf_dict[_sig_file].AsNumpy()
pd_rdf_dict[_sig_file] = pd.DataFrame(np_rdf_dict[_sig_file])
pd_rdf_dict[_sig_file].drop(['weight'],axis=1,inplace=True)
pd_rdf_dict[_sig_file].rename(columns={'new_weight_for_signal' : 'weight'},inplace=True)
data = {key: pd_rdf_dict[_sig_file][key].values for key in list(pd_rdf_dict[_sig_file])}
rdf_dict[_sig_file] = R.RDF.MakeNumpyDataFrame(data)
rdf_dict[_sig_file].Snapshot('Events',VHH_Signal_path+'/'+_sig_file+'_new_sample.root')
indexx+=1
#end
if args.CType == 'c2v':
comb_filename = 'ZHHTo4B_CV_1_0_C2V_{0}_0_C3_1_0_new_sample_by_{1}'.format(str(args.newCoup),str(args.nBV))
elif args.CType == 'kl':
comb_filename = 'ZHHTo4B_CV_1_0_C2V_1_0_C3_{0}_0_new_sample_by_{1}'.format(str(args.newCoup),str(args.nBV))
elif args.CType == 'cv':
comb_filename = 'ZHHTo4B_CV_{0}_0_C2V_1_0_C3_1_0_new_sample_by_{1}'.format(str(args.newCoup),str(args.nBV))
else:
print(args.CType + 'is not in SM coupling list or under developing, enforce to c2v')
comb_filename = 'ZHHTo4B_CV_1_0_C2V_{0}_0_C3_1_0_new_sample_by_{1}'.format(str(args.newCoup),str(args.nBV))
haddcmd = 'hadd -f {0}/{1}.root {2}/*_new_sample.root'.format(slimmed_signal_path,comb_filename,slimmed_signal_path)
os.system(haddcmd)
elif args.HHModel == 'VBF':
print('VBF developing ^_^')
elif args.HHModel == 'GGF':
print('GGF developing ^_^')
else:
print(args.HHModel+" model not exist or haven't been developped, please further check.")
exit(0)
del rdf_dict
del np_rdf_dict
del pd_rdf_dict
del variables