-
Notifications
You must be signed in to change notification settings - Fork 0
/
harvesting_SignalRegionYields_Spring23.py
278 lines (232 loc) · 11.6 KB
/
harvesting_SignalRegionYields_Spring23.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
import ROOT as r
from ROOT import gROOT, TCanvas, TFile, TGraphErrors, SetOwnership
import math, sys, optparse, array, copy, os
import gc, inspect, __main__
import numpy as np
import time
import shutil
import include.Sample as Sample
import include.Launcher as Launcher
import include.helper as helper
import include.Canvas as Canvas
import include.CutManager as CutManager
from include.Utils import XSECS
def printSignals(signalNames):
'''
Function to print the legend of the tables
'''
print("_"*80)
print("Signal Legend")
print("_"*80)
for i in range(len(signalNames)):
print("["+str(i)+"] {:<12}".format(signalNames[i]))
def printTable(title, regions, BKG, signal):
'''
Function to print the tables with the yields in LaTeX format
'''
nsignals = len(signal[0])
print(" \\begin{center}")
print(" {\\footnotesize")
print(" \\begin{tabular}{ |c|c|"+"c|"*nsignals+" }")
print(" \\hline")
title_line = " \\multicolumn{%d}{|c|}{"%(nsignals+2)+title+"} \\\\"
print(title_line)
print(" \\hline")
header = " {:<6} & {:<7} "+"& {:<7} "*nsignals+" \\\\"
print(header.format('Region', 'BKG', *["["+str(i)+"]" for i in range(len(signal[0]))]))
print(" \\hline")
format_line = " {:<6} & {:<7.1f} "+"& {:<7.1f} "*len(signal[0])+" \\\\"
for n in range(len(regions)):
print(format_line.format(regions[n], BKG[n], *signal[n]))
print(" \\hline")
print(" \\end{tabular}")
print(" }")
print(" \\end{center}")
def buildPlot(title, treeBKG, treeSI, inputdir, regions, luminosity, LLlabel = 'MM'):
'''
Function to make the plots
Output (neccesary arrays to print the tables with 'printTable' and 'printSignals')
'''
# Arrays to store yields for table
BKG_yields = []
signal_yields = []
titles = []
nbins = len(regions)
hbkg = r.TH1F("hbkg","hbkg;;N events", nbins, 0, nbins)
hbkg.Sumw2()
## Fill BKG hist
for n,reg in enumerate(regions):
hname = 'h'+LLlabel+'_BCR'+reg
h_ = treeBKG.getLoopTH1F(inputdir, hname)
hbkg.GetXaxis().SetBinLabel(n+1,reg)
hbkg.SetBinContent(n+1,h_.GetBinContent(1))
hbkg.SetBinError(n+1,h_.GetBinError(1))
BKG_yields.append(h_.GetBinContent(1))
### Set background histos style
hbkg.SetFillColorAlpha(r.kCyan-6, 0.8)
hbkg.SetLineColor(r.kCyan-2)
hbkg.GetXaxis().SetTitleSize(0.045)
hbkg.GetYaxis().SetTitleSize(0.045)
# Get signal titles
hname = 'h'+LLlabel+'_SR'+regions[0]
print(hname)
h_stack = treeSI.getLoopStack(inputdir, hname)
s_histos = []
for i,h_ in enumerate(h_stack):
s_histos.append(r.TH1F(h_.GetTitle(),h_.GetTitle(),nbins,0,nbins)) # declare histograms
signal_yields.append([]) # declare array for yields
## Fill S_histos
for n,reg in enumerate(regions):
hname = 'h'+LLlabel+'_SR'+reg
h_stack = treeSI.getLoopStack(inputdir, hname)
for i,h_ in enumerate(h_stack):
s_histos[i].GetXaxis().SetBinLabel(n+1,reg)
print(h_.GetTitle(), XSECS[h_.GetTitle()])
h_.Scale(XSECS[h_.GetTitle()])
content = h_.GetBinContent(1)
s_histos[i].SetBinContent(n+1,content)
s_histos[i].SetBinError(n+1,h_.GetBinError(1))
signal_yields[i].append(content)
titles.append(h_.GetTitle())
### Get maximum
maxValbkg = hbkg.GetMaximum()
maxValSI = max([s_histos[i].GetMaximum() for i in range(0, len(s_histos))])
maxVal = max([maxValSI, maxValbkg])
hbkg.SetMaximum(1e3*maxVal)
### -> Canvas object
plot = Canvas.Canvas(title, 'png,pdf', 0.15, 0.65, 0.45, 0.89, 1)
plot.addHisto(hbkg, 'HIST', 'Background (predicted)', 'f', '', 1, 0)
colors = [r.kRed, r.kOrange, r.kGreen+2, r.kBlue, r.kMagenta]
for i,_h in enumerate(s_histos):
_h.SetLineWidth(2)
masses = eval(_h.GetTitle()[3:])
if 'HSS' in _h.GetTitle():
legend = 'H#rightarrowSS (%d GeV, %d GeV, %d mm)'%(masses[0], masses[1], masses[2])
else:
legend = 'RPV (%d GeV, %d GeV, %d mm)'%(masses[0], masses[1], masses[2])
plot.addHisto(_h, 'HIST, SAME', legend, 'l', colors[i], 1, i+1) # Signal
### Channel banner:
if LLlabel == 'EE':
plot.addLatex(0.7, 0.86, 'e^{+}e^{-} channel', font = 42, size = 0.035)
if LLlabel == 'MM':
plot.addLatex(0.7, 0.86, '#mu^{+}#mu^{-} channel', font = 42, size = 0.035)
### Save it
outdir = 'Plots_yieldRegions'
plot.save(1, 1, True, luminosity, '', outputDir = outdir, xlog = False, maxYnumbers = 4, is2d = True, inProgress = True)
# Return info for table
return np.asarray(BKG_yields), np.transpose(np.asarray(signal_yields)), titles
################################# GLOBAL VARIABLES DEFINITION ####################################
runningfile = os.path.abspath(__file__)
WORKPATH = ''
for level in runningfile.split('/')[:-1]:
WORKPATH += level
WORKPATH += '/'
if __name__ == "__main__":
parser = optparse.OptionParser(usage='usage: %prog [opts] FilenameWithSamples', version='%prog 1.0')
parser.add_option('-m', '--inputMuon', action='store', type=str, dest='inputMuons', default='', help='Target directory')
parser.add_option('-e', '--inputElectron', action='store', type=str, dest='inputElectrons', default='', help='Target directory')
(opts, args) = parser.parse_args()
############# Set the TDR plot style
r.gROOT.LoadMacro(WORKPATH + 'include/tdrstyle.C')
r.gROOT.SetBatch(1)
r.setTDRStyle()
############# Dat file
filename = 'dat/Samples_cern_UltraLegacy_Spring23.dat'
############# EG data definition
DoubleEG2016 = []
DoubleEG2016.append('DoubleEG_Run2016G_noHIPM')
DoubleEG2016.append('DoubleEG_Run2016H_noHIPM')
DoubleEG2017 = []
DoubleEG2017.append('DoubleEG_Run2017B')
DoubleEG2017.append('DoubleEG_Run2017C')
DoubleEG2017.append('DoubleEG_Run2017D')
DoubleEG2017.append('DoubleEG_Run2017E')
DoubleEG2017.append('DoubleEG_Run2017F')
EGamma2018 = []
EGamma2018.append('EGamma_Run2018A')
EGamma2018.append('EGamma_Run2018B')
EGamma2018.append('EGamma_Run2018C')
EGamma2018.append('EGamma_Run2018D')
############# Muon data definition
DoubleMuon2016 = []
DoubleMuon2016.append('DoubleMuon_Run2016B_HIPM')
DoubleMuon2016.append('DoubleMuon_Run2016C_HIPM')
DoubleMuon2016.append('DoubleMuon_Run2016D_HIPM')
DoubleMuon2016.append('DoubleMuon_Run2016E_HIPM')
DoubleMuon2016.append('DoubleMuon_Run2016F_HIPM')
DoubleMuon2016.append('DoubleMuon_Run2016F_noHIPM')
DoubleMuon2016.append('DoubleMuon_Run2016G_noHIPM')
DoubleMuon2016.append('DoubleMuon_Run2016H_noHIPM')
DoubleMuon2018 = []
DoubleMuon2018.append('DoubleMuon_Run2018A')
DoubleMuon2018.append('DoubleMuon_Run2018B')
DoubleMuon2018.append('DoubleMuon_Run2018C')
DoubleMuon2018.append('DoubleMuon_Run2018D')
############# Signal definition
Signals = []
Signals.append('HSS_300_50_100')
Signals.append('HSS_500_50_100')
Signals.append('HSS_1000_250_100')
#Signals.append('HSS_600_50_100')
#Signals.append('HSS_1000_350_100')
Signals.append('RPV_350_148_100')
Signals.append('RPV_1500_494_100')
Signals_2016preVFP = [i + '_2016APV' for i in Signals]
Signals_2016postVFP = [i + '_2016' for i in Signals]
Signals2016 = Signals_2016preVFP + Signals_2016postVFP
Signals2017 = [i + '_2017' for i in Signals]
Signals2018 = [i + '_2018' for i in Signals]
############# Luminosity definition
lumiB = 5.79
lumiC = 2.57
lumiD = 4.25
lumiE = 4.01
lumiF = 2.53 # total 3.10
lumiF_noHIMP = 0.57
lumiG = 7.54
lumiH = 8.61
lumi = lumiB + lumiC + lumiD + lumiE + lumiF + lumiG + lumiH# luminosity
lumi_2016 = 35.9
lumi_2016_GH = 16.2
lumi_2017 = 41.5
lumi_2018_EE = 54.5
lumi_2018_MM = 59.8
##### Cross sections
#xsecs = np.array([0.107e3, 4.938e3, 2.588e3, 0.67, 10e3])
############ Define regions
regions_mu = ["IaA", "IaB", "IaC", "IaD", "IbA", "IbB", "IbC", "IbD", "II"]
regions_ee = ["IaA", "IaB", "IaC", "IbA", "IbB", "IbC", "II"]
############ Dielectron plots
if opts.inputElectrons:
treeSI_2016_GH = Sample.Tree( fileName = helper.selectSamples(WORKPATH + 'dat/CombSignal_2016UL_Fall22.dat', Signals_2016postVFP, 'SI'), name = 'SI', isdata = 0 )
treeSI_2017 = Sample.Tree( fileName = helper.selectSamples(WORKPATH + 'dat/CombSignal_2017UL_Fall22.dat', Signals2017, 'SI'), name = 'SI', isdata = 0 )
treeSI_2018 = Sample.Tree( fileName = helper.selectSamples(WORKPATH + 'dat/CombSignal_2018UL_Fall22.dat', Signals2018, 'SI'), name = 'SI', isdata = 0 )
treeDATA_EG2016 = Sample.Tree( fileName = helper.selectSamples(WORKPATH + filename, DoubleEG2016, 'DATA'), name = 'DATA', isdata = 1 )
treeDATA_EG2017 = Sample.Tree( fileName = helper.selectSamples(WORKPATH + filename, DoubleEG2017, 'DATA'), name = 'DATA', isdata = 1 )
treeDATA_EG2018 = Sample.Tree( fileName = helper.selectSamples(WORKPATH + filename, EGamma2018, 'DATA'), name = 'DATA', isdata = 1 )
BKG_EG_2016, Signal_EG_2016, titles = buildPlot("EG_2016", treeDATA_EG2016, treeSI_2016_GH, opts.inputElectrons, regions_ee, lumi_2016_GH, LLlabel='EE')
BKG_EG_2017, Signal_EG_2017, _ = buildPlot("EG_2017", treeDATA_EG2017, treeSI_2017, opts.inputElectrons, regions_ee, lumi_2017, LLlabel='EE')
BKG_EG_2018, Signal_EG_2018, _ = buildPlot("EG_2018", treeDATA_EG2018, treeSI_2018, opts.inputElectrons, regions_ee, lumi_2018_EE, LLlabel='EE')
original_stdout = sys.stdout # Save a reference to the original standard output
with open('Plots_yieldRegions/tables_EE.txt', 'w') as f:
sys.stdout = f
printSignals(titles)
printTable("Electron channel 2016", regions_ee, BKG_EG_2016, Signal_EG_2016)
printTable("Electron channel 2017", regions_ee, BKG_EG_2017, Signal_EG_2017)
printTable("Electron channel 2018", regions_ee, BKG_EG_2018, Signal_EG_2018)
sys.stdout = original_stdout
if opts.inputMuons:
treeSI_2016 = Sample.Tree( fileName = helper.selectSamples(WORKPATH + 'dat/CombSignal_2016UL_Fall22.dat', Signals2016, 'SI'), name = 'SI', isdata = 0 )
treeSI_2017 = Sample.Tree( fileName = helper.selectSamples(WORKPATH + 'dat/CombSignal_2017UL_Fall22.dat', Signals2017, 'SI'), name = 'SI', isdata = 0 )
treeSI_2018 = Sample.Tree( fileName = helper.selectSamples(WORKPATH + 'dat/CombSignal_2018UL_Fall22.dat', Signals2018, 'SI'), name = 'SI', isdata = 0 )
treeDATA_Mu2016 = Sample.Tree( fileName = helper.selectSamples(WORKPATH + filename, DoubleMuon2016, 'DATA'), name = 'DATA', isdata = 1 )
treeDATA_Mu2018 = Sample.Tree( fileName = helper.selectSamples(WORKPATH + filename, DoubleMuon2018, 'DATA'), name = 'DATA', isdata = 1 )
BKG_Mu_2016, Signal_Mu_2016, titles = buildPlot("Mu_2016", treeDATA_Mu2016, treeSI_2016, opts.inputMuons, regions_mu, lumi_2016, LLlabel='MM')
BKG_Mu_2018, Signal_Mu_2018, _ = buildPlot("Mu_2018", treeDATA_Mu2018, treeSI_2018, opts.inputMuons, regions_mu, lumi_2018_MM, LLlabel='MM')
original_stdout = sys.stdout # Save a reference to the original standard output
with open('Plots_yieldRegions/tables_MuMu.txt', 'w') as f:
sys.stdout = f
printSignals(titles)
printTable("Muon channel 2016", regions_mu, BKG_Mu_2016, Signal_Mu_2016)
printTable("Muon channel 2018", regions_mu, BKG_Mu_2018, Signal_Mu_2018)
sys.stdout = original_stdout