-
Notifications
You must be signed in to change notification settings - Fork 1
/
runWith1DVanilla_v2_VR.py
390 lines (341 loc) · 15.5 KB
/
runWith1DVanilla_v2_VR.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
from time import time
from TwoDAlphabet import plot
from TwoDAlphabet.twoDalphabet import MakeCard, TwoDAlphabet
from TwoDAlphabet.alphawrap import BinnedDistribution, ParametricFunction
from TwoDAlphabet.helpers import make_env_tarball, cd, execute_cmd
from TwoDAlphabet.ftest import FstatCalc
import os,sys
import numpy as np
from optparse import OptionParser
parser = OptionParser(usage="Usage: python %prog workingArea config.json")
workingArea = sys.argv[1]
configJSON = sys.argv[2]
# Helper function to get region names
def _get_other_region_names(pass_reg_name):
'''
If passing e.g. "fail", will return ("fail", "pass")
In HSCP analysis, we're just considering F/P regions
'''
return pass_reg_name, pass_reg_name.replace('fail','pass')
# Helper function to generate constraints for parametric Transfer Functions
# Change values as you see fit
def _generate_constraints(nparams):
out = {}
for i in range(nparams):
if i == 0:
out[i] = {"MIN":0,"MAX":30}
else:
out[i] = {"MIN":-50,"MAX":50}
return out
# Dict to store transfer function forms and constraints
_rpf_options = {
'0x0': {
'form': '0.1*(@0)',
'constraints': {
0: {"MIN": 0.0, "MAX": 50},
}
},
'1x0': {
'form': '0.1*(@0+@1*x)',
'constraints': {
0: {"MIN": 0.0, "MAX": 50},
1: {"MIN": -50, "MAX": 500}
}
},
'0x1': {
'form': '0.1*(@0+@1*y)',
'constraints': _generate_constraints(2)
},
'1x1': {
'form': '0.1*(@0+@1*x)*(1+@2*y)',
'constraints': _generate_constraints(3)
},
'2x0': {
'form': '0.1*(@0+@1*x+@2*x**2)*(@3)',
'constraints': _generate_constraints(4)
},
'2x1': {
'form': '0.1*(@0+@1*x+@2*x**2)*(1+@3*y)',
'constraints': _generate_constraints(4)
},
'2x2': {
'form': '0.1*(@0+@1*x+@2*x**2)*(1+@3*y+@4*y**2)',
'constraints': _generate_constraints(4)
},
'expo': {
'form': 'exp(-@0*x+@1)',
'constraints': {
0: {"MIN": -1.0, "MAX": 50},
1: {"MIN": -500, "MAX": 500}
}
}
}
# Helper function for selecting the signal from the ledger
def _select_signal(row, args):
signame = args[0]
poly_order = args[1]
if row.process_type == 'SIGNAL':
if signame in row.process:
return True
else:
return False
elif 'Background' in row.process:
if row.process == 'Background_'+poly_order:
return True
elif row.process == 'Background':
return True
else:
return False
else:
return True
# Make the workspace
def make_workspace():
# Create the workspace directory, using info from the specified JSON file
twoD = TwoDAlphabet(workingArea, configJSON, loadPrevious=False)
# 2DAlphabet wasn't intended for an analysis like this, so the default function
# for Looping over all regions and for a given region's data histogram, subtracting
# the list of background histograms, and returning a data-bkgList is called initQCDHists.
# This is b/c QCD multijet is the main background we usually estimate via 2DAlphabe
bkg_hists = twoD.InitQCDHists()
#print('bkg_hists = {}'.format(bkg_hists))
# Now, we loop over "pass" and "fail" regions and get the binnings
for f, p in [_get_other_region_names(r) for r in twoD.ledger.GetRegions() if 'fail' in r]:
#print(f, p)
# get the binning for the fail region
binning_f, _ = twoD.GetBinningFor(f)
# you can change the name as you see fit
fail_name = 'Background_'+f
# this is the actual binned distribution of the fail
bkg_f = BinnedDistribution(fail_name, bkg_hists[f], binning_f, constant=False)
# now we add it to the 2DAlphabet ledger
twoD.AddAlphaObj('Background',f, bkg_f)
# now construct all of the possible transfer functions, to be chosen and used later
for opt_name, opt in _rpf_options.items():
bkg_rpf = ParametricFunction(
fail_name.replace('fail','rpf')+'_'+opt_name, # this is our pass/fail ratio
binning_f, # we use the binning from fail
opt['form'], # was _rpf_options['0x0']['form'],
opt['constraints'] # was _rpf_options['0x0']['constraints']
)
# now define the bkg in pass as the bkg in fail multiplied by the transfer function (bkg_rpf)
bkg_p = bkg_f.Multiply(fail_name.replace('fail','pass')+'_'+opt_name, bkg_rpf)
# then add this to the 2DAlphabet ledger
twoD.AddAlphaObj('Background_'+opt_name,p,bkg_p,title='Background')
# and save it out
twoD.Save()
# function for perfomring the fit
def perform_fit(signal, tf, rMaxExt = 30, extra=''):
'''
signal [str] = 'Type-Mass'
tf [str] = 0x0, 0x1, 1x0, 1x1, 1x2, 2x2
extra (str) = any extra flags to pass to Combine when running the ML fit
'''
# this is the name of the directory created in the workspace function
working_area = workingArea
# we reuse the workspace from the last step.
# The runConfig.json is copied from the origin JSON config file,
# and we must specify that we want to load the previous workspace
twoD = TwoDAlphabet(working_area, '{}/runConfig.json'.format(working_area), loadPrevious=True)
# Now we create a ledger and make a new area for it with a Combine card
# this select() method uses lambda functions. Will explain later
print("tf: " + str(tf))
subset = twoD.ledger.select(_select_signal, '{}'.format(signal), tf)
twoD.MakeCard(subset, '{}-{}_area'.format(signal, tf))
# perform fit
print("perform fit")
twoD.MLfit('{}-{}_area'.format(signal, tf), rMin=0, rMax=rMaxExt, verbosity=1, extra=extra)
def plot_fit(signal, tf):
working_area = workingArea
print("DoingTwoDAlphabet")
twoD = TwoDAlphabet(working_area, '{}/runConfig.json'.format(working_area), loadPrevious=True)
print("Doing twoD.ledger.select")
subset = twoD.ledger.select(_select_signal, '{}'.format(signal), tf)
print("Doing twoD.StdPlots")
twoD.StdPlots('{}-{}_area'.format(signal, tf), subset, lumiText=r'2023Dv2 (12.1M Events)', pf_slice_str={"fail":"RNNScore < 0.1","pass":"0.1 < RNNScore < 0.2"})
twoD.StdPlots('{}-{}_area'.format(signal, tf), subset, True, lumiText=r'2023Dv2 (12.1M Events)', pf_slice_str={"fail":"RNNScore < 0.1","pass":"0.1 < RNNScore < 0.2"})
def GOF(signal,tf,condor=True, extra=''):
# replace the blindedFit option in the config file with COMMENT to effectively "unblind" the GoF
#findReplace = {"blindedFit": "COMMENT"}
working_area = workingArea
signame = signal
twoD = TwoDAlphabet(working_area, '{}/runConfig.json'.format(working_area), loadPrevious=True)
if not os.path.exists(twoD.tag+'/'+signame+'-{}_area/card.txt'.format(tf)):
print('{}/{}-area/card.txt does not exist, making card'.format(twoD.tag,signame))
subset = twoD.ledger.select(_select_signal, signame, tf)
twoD.MakeCard(subset, signame+'_area')
if condor == False:
twoD.GoodnessOfFit(
signame+'-{}_area'.format(tf), ntoys=500, freezeSignal=0,
condor=False
)
else:
twoD.GoodnessOfFit(
signame+'-{}_area'.format(tf), ntoys=500, freezeSignal=0,
condor=True, njobs=10
)
def plot_GOF(signal, tf, condor=True):
working_area = workingArea
plot.plot_gof('{}'.format(working_area), '{}-{}_area'.format(signal, tf), condor=condor)
def load_RPF(twoD):
'''
loads the rpf parameter values for use in toy generation
'''
params_to_set = twoD.GetParamsOnMatch('rpf.*', 'Signal', 'b')
return {k:v['val'] for k,v in params_to_set.items()}
def SignalInjection(signal, tf, r, condor=False):
working_area = workingArea
twoD = TwoDAlphabet(working_area, '{}/runConfig.json'.format(working_area), loadPrevious=True)
#params = load_RPF(twoD)
twoD.SignalInjection(
'{}-{}_area'.format(signal, tf),
injectAmount = r, # injected signal xsec (r=0 : bias test)
ntoys=500, # will take forever if not on condor
blindData = True, # make sure you're blinding if working with data
#setParams = params, # give the toys the same RPF params
verbosity = 0, # you can change this if you need
njobs=10,
condor = condor
)
def plot_SignalInjection(signal, tf, r, condor=False):
working_area = workingArea
plot.plot_signalInjection(working_area, '{}-{}_area'.format(signal, tf), injectedAmount=r, condor=condor)
def Impacts(signal, tf):
working_area = workingArea
twoD = TwoDAlphabet(working_area, '{}/runConfig.json'.format(working_area), loadPrevious=True)
#twoD.Impacts('{}-{}_area'.format(signal, tf), cardOrW='card.txt', extra='-t 1')
twoD.Impacts('{}-{}_area'.format(signal, tf), cardOrW='initialFitWorkspace.root --snapshotName initialFit', extra='-t 1')
def run_limits(signal, tf):
working_area = workingArea
twoD = TwoDAlphabet(working_area, '{}/runConfig.json'.format(working_area), loadPrevious=True)
twoD.Limit(
subtag='{}-{}_area'.format(signal, tf),
blindData=False, # BE SURE TO CHANGE THIS IF YOU NEED TO BLIND YOUR DATA
verbosity=1,
condor=False
)
def _gof_for_FTest(twoD, subtag, card_or_w='card.txt'):
run_dir = twoD.tag+'/'+subtag
with cd(run_dir):
gof_data_cmd = [
'combine -M GoodnessOfFit',
'-d '+card_or_w,
'--algo=saturated',
'-n _gof_data'
]
gof_data_cmd = ' '.join(gof_data_cmd)
execute_cmd(gof_data_cmd)
def test_FTest(poly1, poly2, signal=''):
'''
Perform an F-test using existing working areas
'''
working_area = workingArea
twoD = TwoDAlphabet(working_area, '{}/runConfig.json'.format(working_area), loadPrevious=True)
binning = twoD.binnings['default']
nBins = (len(binning.xbinList)-1)*(len(binning.ybinList)-1)
# Get number of RPF params and run GoF for poly1
params1 = twoD.ledger.select(_select_signal, '{}'.format(signal), poly1).alphaParams
rpfSet1 = params1[params1["name"].str.contains("rpf")]
print("rpfSet1: " + str(rpfSet1))
nRpfs1 = len(rpfSet1.index)
print(" >>>>>> Num RPF parameters for poly1: " + str(nRpfs1))
_gof_for_FTest(twoD, 'Signal_M500GeV-{}_area'.format(poly1), card_or_w='card.txt')
gofFile1 = working_area+'/Signal_M500GeV-{}_area/higgsCombine_gof_data.GoodnessOfFit.mH120.root'.format(poly1)
# Get number of RPF params and run GoF for poly2
params2 = twoD.ledger.select(_select_signal, '{}'.format(signal), poly2).alphaParams
rpfSet2 = params2[params2["name"].str.contains("rpf")]
nRpfs2 = len(rpfSet2.index)
print(" >>>>>> Num RPF parameters for poly2: " + str(nRpfs2))
_gof_for_FTest(twoD, 'Signal_M500GeV-{}_area'.format(poly2), card_or_w='card.txt')
gofFile2 = working_area+'/Signal_M500GeV-{}_area/higgsCombine_gof_data.GoodnessOfFit.mH120.root'.format(poly2)
base_fstat = FstatCalc(gofFile1,gofFile2,nRpfs1,nRpfs2,nBins)
print(base_fstat)
def plot_FTest(base_fstat,nRpfs1,nRpfs2,nBins):
from ROOT import TF1, TH1F, TLegend, TPaveText, TLatex, TArrow, TCanvas, kBlue, gStyle
gStyle.SetOptStat(0000)
if len(base_fstat) == 0: base_fstat = [0.0]
ftest_p1 = min(nRpfs1,nRpfs2)
ftest_p2 = max(nRpfs1,nRpfs2)
ftest_nbins = nBins
fdist = TF1("fDist", "[0]*TMath::FDist(x, [1], [2])", 0,max(10,1.3*base_fstat[0]))
fdist.SetParameter(0,1)
fdist.SetParameter(1,ftest_p2-ftest_p1)
fdist.SetParameter(2,ftest_nbins-ftest_p2)
pval = fdist.Integral(0.0,base_fstat[0])
print('P-value: ' + str(pval))
c = TCanvas('c','c',800,600)
c.SetLeftMargin(0.12)
c.SetBottomMargin(0.12)
c.SetRightMargin(0.1)
c.SetTopMargin(0.1)
ftestHist_nbins = 30
ftestHist = TH1F("Fhist","",ftestHist_nbins,0,max(10,1.3*base_fstat[0]))
ftestHist.GetXaxis().SetTitle("F = #frac{-2log(#lambda_{1}/#lambda_{2})/(p_{2}-p_{1})}{-2log#lambda_{2}/(n-p_{2})}")
ftestHist.GetXaxis().SetTitleSize(0.025)
ftestHist.GetXaxis().SetTitleOffset(2)
ftestHist.GetYaxis().SetTitleOffset(0.85)
ftestHist.Draw("pez")
ftestobs = TArrow(base_fstat[0],0.25,base_fstat[0],0)
ftestobs.SetLineColor(kBlue+1)
ftestobs.SetLineWidth(2)
fdist.Draw('same')
ftestobs.Draw()
tLeg = TLegend(0.6,0.73,0.89,0.89)
tLeg.SetLineWidth(0)
tLeg.SetFillStyle(0)
tLeg.SetTextFont(42)
tLeg.SetTextSize(0.03)
tLeg.AddEntry(ftestobs,"observed = %.3f"%base_fstat[0],"l")
tLeg.AddEntry(fdist,"F-dist, ndf = (%.0f, %.0f) "%(fdist.GetParameter(1),fdist.GetParameter(2)),"l")
tLeg.Draw("same")
model_info = TPaveText(0.2,0.6,0.4,0.8,"brNDC")
model_info.AddText('p1 = '+poly1)
model_info.AddText('p2 = '+poly2)
model_info.AddText("p-value = %.2f"%(1-pval))
model_info.Draw('same')
latex = TLatex()
latex.SetTextAlign(11)
latex.SetTextSize(0.06)
latex.SetTextFont(62)
latex.SetNDC()
latex.DrawLatex(0.12,0.91,"CMS")
latex.SetTextSize(0.05)
latex.SetTextFont(52)
latex.DrawLatex(0.23,0.91,"Preliminary")
latex.SetTextFont(42)
latex.SetTextFont(52)
latex.SetTextSize(0.045)
c.SaveAs(working_area+'/ftest_{0}_vs_{1}_notoys.png'.format(poly1,poly2))
plot_FTest(base_fstat,nRpfs1,nRpfs2,nBins)
if __name__ == "__main__":
make_workspace()
signal_areas = ["Signal_M500GeV"]
#signal_areas = ["Signal_B1_MD2000_MBH3000_n2"]
# signal_areas = ["Signal_B1_MD2000_MBH3000_n2","Signal_B1_MD2000_MBH4000_n2","Signal_B1_MD2000_MBH5000_n2","Signal_B1_MD2000_MBH6000_n2","Signal_B1_MD2000_MBH7000_n2","Signal_B1_MD2000_MBH8000_n2","Signal_B1_MD2000_MBH9000_n2","Signal_B1_MD2000_MBH10000_n2","Signal_B1_MD2000_MBH11000_n2"]
#signal_areas = ["Signal_B1_MD4000_MBH5000_n2","Signal_B1_MD4000_MBH6000_n2","Signal_B1_MD4000_MBH7000_n2","Signal_B1_MD4000_MBH8000_n2","Signal_B1_MD4000_MBH9000_n2","Signal_B1_MD4000_MBH10000_n2","Signal_B1_MD4000_MBH11000_n2"]
#tf_type = '0x0'
tf_types = ['1x0']
for signal, tf_type in zip(signal_areas,tf_types) :
# When there are 100 signals, let's make sure we only run on the ones we didnt do before
if os.path.exists(workingArea + "/" + signal + f"-{tf_type}_area/done") : continue
fitPassed = False
# If the fit failed iterate on rMax
rMax = 50
while not (fitPassed) :
print("\n\n\nperform_fit with rMax = " + str(rMax))
perform_fit(signal,tf_type,rMax,extra='--robustHesse 1')
# Do fitting until the fit passes
with open(workingArea + "/" + signal + f"-{tf_type}_area/FitDiagnostics.log", 'r') as file:
content = file.read()
if not "Fit failed" in content: fitPassed = True
rMax = rMax / 10.
plot_fit(signal,tf_type)
print("\n\n\nFit is succesful, running limits now for " + str(signal))
run_limits(signal,tf_type)
#GOF(signal,tf_type,condor=False)
#plot_GOF(signal,tf_type,condor=False)
#SignalInjection(signal, tf_type, r=0, condor=False)
#plot_SignalInjection(signal, tf_type, r=0, condor=False)
#Impacts(signal,tf_type)
os.system("cp " + workingArea + "/base.root " + workingArea + "/" + signal + f"-{tf_type}_area/.")
open(workingArea + "/" + signal + f"-{tf_type}_area/done", 'w').close()
#test_FTest('1x0','2x0',"Signal_M500GeV")