forked from cms-btv-pog/ScaleFactorCombinationTools
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathScaleFactorSystematicsManipulator.py
More file actions
executable file
·165 lines (126 loc) · 8.55 KB
/
ScaleFactorSystematicsManipulator.py
File metadata and controls
executable file
·165 lines (126 loc) · 8.55 KB
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
#!/usr/bin/env python
import os
import sys
#!/usr/bin/env python
import ROOT
import math
import optparse
from array import *
import CondTools.BTau.dataLoader as dataLoader
from collections import defaultdict
if __name__ == '__main__':
# Input parameters
usage = 'usage: %prog [options]'
parser = optparse.OptionParser(usage)
parser.add_option('--inputfile' , dest='inputfile' , help='Name of the input csv file ' , default='')
parser.add_option('--outputfile' , dest='outputfile' , help='Name of the output csv file' , default='default')
parser.add_option('--csvpath ' , dest='csvpath' , help='Path where csv files are stored ' , default='./CSVFiles/')
parser.add_option('--yearcorroff' , dest='yearcorroff' , help='Turn off year correlations' , default=False, action='store_true')
parser.add_option('--splittype2' , dest='splittype2' , help='Split type2 uncertainties' , default=False, action='store_true')
parser.add_option('--custom' , dest='custom' , help='Custom list of wanted uncertainties' , default=None)
(opt, args) = parser.parse_args()
uncorrelatedList = [ 'statistic' ] # To be completed
type2List = [ "_pileup", "_jes", "_jer" ] # To be checked
splitList = opt.custom.split(',') if (opt.custom!=None) else [ ]
outputFlag = ''
if not opt.yearcorroff:
outputFlag += '_years'
if opt.splittype2:
outputFlag += '_type2'
if outputFlag=='':
outputFlag = '_basic'
for custom in splitList:
outputFlag += '_' + custom
if opt.splittype2:
splitList.extend(type2List)
outputfilename = opt.csvpath + '/' + opt.outputfile.replace('default', opt.inputfile.replace('.csv', '')) + outputFlag + '.csv'
loaders = dataLoader.get_data(opt.csvpath + '/' + opt.inputfile)
calib = ROOT.BTagCalibration(outputfilename.split('/')[-1].split('_')[0])
mergedSystematics = [ ]
mergedUncertainty = { }
if outputFlag!='_basic':
mergedSystematics = [ 'correlated', 'uncorrelated' ] if ('years' in outputFlag) else [ 'unsplit' ]
# Get the structure of the CSV file
for data in loaders:
for e in data.entries:
if e.params.sysType=='up':
for mergedSystematic in mergedSystematics:
paramList = [ e.params.operatingPoint, e.params.measurementType, mergedSystematic, e.params.jetFlavor, e.params.etaMin, e.params.etaMax, e.params.ptMin, e.params.ptMax, e.params.discrMin, e.params.discrMax ]
newMergedUncertainty = 0.
for param in reversed(paramList):
tmpMergedUncertainty = { }
tmpMergedUncertainty[param] = newMergedUncertainty
newMergedUncertainty = tmpMergedUncertainty
newMergedUncertaintyList = [ newMergedUncertainty ]
auxMergedUncertaintyList = [ mergedUncertainty ]
for par1 in range(len(paramList)):
if paramList[par1] in auxMergedUncertaintyList[par1]:
auxMergedUncertaintyList.append(auxMergedUncertaintyList[par1][paramList[par1]])
newMergedUncertaintyList.append(newMergedUncertaintyList[par1][paramList[par1]])
else:
auxMergedUncertaintyList[par1][paramList[par1]] = newMergedUncertaintyList[par1][paramList[par1]]
break
# Merge uncertainties
for data in loaders:
for e in data.entries:
# Systematics are symmetric
if 'up_' in e.params.sysType and e.params.sysType.split('_')[1] not in splitList:
mergedSystematic = 'unsplit'
if 'years' in outputFlag:
if e.params.sysType.split('_')[1] in uncorrelatedList:
mergedSystematic = 'uncorrelated'
else:
mergedSystematic = 'correlated'
systematicValue = e.formula
if '+' in systematicValue: # This is ok for b-tag SF combination
systematicValue = float(systematicValue.split('+')[-1])
mergedUncertainty[e.params.operatingPoint][e.params.measurementType][mergedSystematic][e.params.jetFlavor][e.params.etaMin][e.params.etaMax][e.params.ptMin][e.params.ptMax][e.params.discrMin][e.params.discrMax] += systematicValue*systematicValue
# Now we can write the output: first the cetral values, total uncertainties, and splitted systematics ...
for data in loaders:
for e in data.entries:
if e.params.sysType=='central' or e.params.sysType=='up' or e.params.sysType=='down' or e.params.sysType.split('_')[1] in splitList:
params = ROOT.BTagEntry.Parameters( e.params.operatingPoint,
e.params.measurementType,
e.params.sysType,
e.params.jetFlavor,
e.params.etaMin,
e.params.etaMax,
e.params.ptMin,
e.params.ptMax,
e.params.discrMin,
e.params.discrMax )
SFFun = ROOT.TF1 ('SFFun', e.formula, e.params.ptMin, e.params.ptMax)
entry = ROOT.BTagEntry(SFFun, params)
calib.addEntry(entry)
# ... then the merged systematics
for mergedSystematic in mergedSystematics:
for data in loaders:
for e in data.entries:
if e.params.sysType=='central':
auxMergedUncertainty = mergedUncertainty[e.params.operatingPoint][e.params.measurementType][mergedSystematic][e.params.jetFlavor]
for etamin in auxMergedUncertainty:
if etamin>=e.params.etaMin:
for etamax in auxMergedUncertainty[etamin]:
if etamax<=e.params.etaMax:
for ptmin in auxMergedUncertainty[etamin][etamax]:
if ptmin>=e.params.ptMin:
for ptmax in auxMergedUncertainty[etamin][etamax][ptmin]:
if ptmax<=e.params.ptMax:
for variation in [ 'up_', 'down_' ]:
params = ROOT.BTagEntry.Parameters( e.params.operatingPoint,
e.params.measurementType,
variation + mergedSystematic,
e.params.jetFlavor,
etamin,
etamax,
ptmin,
ptmax,
e.params.discrMin,
e.params.discrMax )
systematicValue = str(math.sqrt(auxMergedUncertainty[etamin][etamax][ptmin][ptmax][e.params.discrMin][e.params.discrMax]))
sign = '+' if (variation=='up_') else '-'
SFFun = ROOT.TF1 ('SFFun', e.formula + sign + systematicValue, ptmin, ptmax)
entry = ROOT.BTagEntry(SFFun, params)
calib.addEntry(entry)
with open(outputfilename, 'w') as f:
f.write(calib.makeCSV())