forked from fcouderc/egm_tnp_analysis
-
Notifications
You must be signed in to change notification settings - Fork 42
/
tnpEGM_fitter.py
252 lines (206 loc) · 10.3 KB
/
tnpEGM_fitter.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
### python specific import
import argparse
import os
import sys
import pickle
import shutil
from multiprocessing import Pool
parser = argparse.ArgumentParser(description='tnp EGM fitter')
parser.add_argument('--checkBins' , action='store_true' , help = 'check bining definition')
parser.add_argument('--createBins' , action='store_true' , help = 'create bining definition')
parser.add_argument('--createHists', action='store_true' , help = 'create histograms')
parser.add_argument('--sample' , default='all' , help = 'create histograms (per sample, expert only)')
parser.add_argument('--altSig' , action='store_true' , help = 'alternate signal model fit')
parser.add_argument('--addGaus' , action='store_true' , help = 'add gaussian to alternate signal model failing probe')
parser.add_argument('--altBkg' , action='store_true' , help = 'alternate background model fit')
parser.add_argument('--altSigBkg' , action='store_true' , help = 'alternate signal and background model fit')
parser.add_argument('--doFit' , action='store_true' , help = 'fit sample (sample should be defined in settings.py)')
parser.add_argument('--mcSig' , action='store_true' , help = 'fit MC nom [to init fit parama]')
parser.add_argument('--doPlot' , action='store_true' , help = 'plotting')
parser.add_argument('--sumUp' , action='store_true' , help = 'sum up efficiencies')
parser.add_argument('--iBin' , dest = 'binNumber' , type = int, default=-1, help='bin number (to refit individual bin)')
parser.add_argument('--flag' , default = None , help ='WP to test')
parser.add_argument('settings' , default = None , help = 'setting file [mandatory]')
args = parser.parse_args()
print('===> settings %s <===' % args.settings)
importSetting = 'import %s as tnpConf' % args.settings.replace('/','.').split('.py')[0]
print(importSetting)
exec(importSetting)
### tnp library
import libPython.binUtils as tnpBiner
import libPython.rootUtils as tnpRoot
if args.flag is None:
print('[tnpEGM_fitter] flag is MANDATORY, this is the working point as defined in the settings.py')
sys.exit(0)
if not args.flag in tnpConf.flags.keys() :
print('[tnpEGM_fitter] flag %s not found in flags definitions' % args.flag)
print(' --> define in settings first')
print(' In settings I found flags: ')
print(tnpConf.flags.keys())
sys.exit(1)
outputDirectory = '%s/%s/' % (tnpConf.baseOutDir,args.flag)
print('===> Output directory: ')
print(outputDirectory)
####################################################################
##### Create (check) Bins
####################################################################
if args.checkBins:
tnpBins = tnpBiner.createBins(tnpConf.biningDef,tnpConf.cutBase)
tnpBiner.tuneCuts( tnpBins, tnpConf.additionalCuts )
for ib in range(len(tnpBins['bins'])):
print(tnpBins['bins'][ib]['name'])
print(' - cut: ',tnpBins['bins'][ib]['cut'])
sys.exit(0)
if args.createBins:
if os.path.exists( outputDirectory ):
shutil.rmtree( outputDirectory )
os.makedirs( outputDirectory )
tnpBins = tnpBiner.createBins(tnpConf.biningDef,tnpConf.cutBase)
tnpBiner.tuneCuts( tnpBins, tnpConf.additionalCuts )
pickle.dump( tnpBins, open( '%s/bining.pkl'%(outputDirectory),'wb') )
print('created dir: %s ' % outputDirectory)
print('bining created successfully... ')
print('Note than any additional call to createBins will overwrite directory %s' % outputDirectory)
sys.exit(0)
tnpBins = pickle.load( open( '%s/bining.pkl'%(outputDirectory),'rb') )
####################################################################
##### Create Histograms
####################################################################
for s in tnpConf.samplesDef.keys():
sample = tnpConf.samplesDef[s]
if sample is None: continue
setattr( sample, 'tree' ,'%s/fitter_tree' % tnpConf.tnpTreeDir )
setattr( sample, 'histFile' , '%s/%s_%s.root' % ( outputDirectory , sample.name, args.flag ) )
if args.createHists:
print(" ======== Creating Histograms ========")
import libPython.histUtils as tnpHist
def parallel_hists(sampleType):
sample = tnpConf.samplesDef[sampleType]
if sample is None : return
if sampleType == args.sample or args.sample == 'all' :
print('creating histogram for sample ')
sample.dump()
var = { 'name' : 'pair_mass', 'nbins' : 80, 'min' : 50, 'max': 130 }
if sample.mcTruth:
var = { 'name' : 'pair_mass', 'nbins' : 80, 'min' : 50, 'max': 130 }
tnpHist.makePassFailHistograms( sample, tnpConf.flags[args.flag], tnpBins, var )
#pool = Pool()
#pool.map(parallel_hists, tnpConf.samplesDef.keys())
for k in tnpConf.samplesDef.keys(): parallel_hists(k)
sys.exit(0)
####################################################################
##### Actual Fitter
####################################################################
sampleToFit = tnpConf.samplesDef['data']
if sampleToFit is None:
print('[tnpEGM_fitter, prelim checks]: sample (data or MC) not available... check your settings')
sys.exit(1)
sampleMC = tnpConf.samplesDef['mcNom']
if sampleMC is None:
print('[tnpEGM_fitter, prelim checks]: MC sample not available... check your settings')
sys.exit(1)
for s in tnpConf.samplesDef.keys():
sample = tnpConf.samplesDef[s]
if sample is None: continue
setattr( sample, 'mcRef' , sampleMC )
setattr( sample, 'nominalFit', '%s/%s_%s.nominalFit.root' % ( outputDirectory , sample.name, args.flag ) )
setattr( sample, 'altSigFit' , '%s/%s_%s.altSigFit.root' % ( outputDirectory , sample.name, args.flag ) )
setattr( sample, 'altBkgFit' , '%s/%s_%s.altBkgFit.root' % ( outputDirectory , sample.name, args.flag ) )
setattr( sample, 'altSigBkgFit' , '%s/%s_%s.altSigBkgFit.root' % ( outputDirectory , sample.name, args.flag ) )
### change the sample to fit is mc fit
if args.mcSig :
sampleToFit = tnpConf.samplesDef['mcNom']
if args.doFit:
print(" ======== Fitting ========")
sampleToFit.dump()
def parallel_fit(ib):
if (args.binNumber >= 0 and ib == args.binNumber) or args.binNumber < 0:
if args.altSig and not args.addGaus:
tnpRoot.histFitterAltSig( sampleToFit, tnpBins['bins'][ib], tnpConf.tnpParAltSigFit )
elif args.altSig and args.addGaus:
tnpRoot.histFitterAltSig( sampleToFit, tnpBins['bins'][ib], tnpConf.tnpParAltSigFit_addGaus, 1)
elif args.altBkg:
tnpRoot.histFitterAltBkg( sampleToFit, tnpBins['bins'][ib], tnpConf.tnpParAltBkgFit )
elif args.altSigBkg:
tnpRoot.histFitterAltSigBkg( sampleToFit, tnpBins['bins'][ib], tnpConf.tnpParAltSigBkgFit )
else:
tnpRoot.histFitterNominal( sampleToFit, tnpBins['bins'][ib], tnpConf.tnpParNomFit )
pool = Pool()
pool.map(parallel_fit, range(len(tnpBins['bins'])))
args.doPlot = True
####################################################################
##### dumping plots
####################################################################
if args.doPlot:
fileName = sampleToFit.nominalFit
fitType = 'nominalFit'
if args.altSig :
fileName = sampleToFit.altSigFit
fitType = 'altSigFit'
if args.altBkg :
fileName = sampleToFit.altBkgFit
fitType = 'altBkgFit'
if args.altSigBkg :
fileName = sampleToFit.altSigBkgFit
fitType = 'altSigBkgFit'
os.system('hadd -f %s %s' % (fileName, fileName.replace('.root', '-*.root')))
plottingDir = '%s/plots/%s/%s' % (outputDirectory,sampleToFit.name,fitType)
if not os.path.exists( plottingDir ):
os.makedirs( plottingDir )
shutil.copy('etc/inputs/index.php.listPlots','%s/index.php' % plottingDir)
for ib in range(len(tnpBins['bins'])):
if (args.binNumber >= 0 and ib == args.binNumber) or args.binNumber < 0:
tnpRoot.histPlotter( fileName, tnpBins['bins'][ib], plottingDir )
print(' ===> Plots saved in <=======')
# print 'localhost/%s/' % plottingDir
####################################################################
##### dumping egamma txt file
####################################################################
if args.sumUp:
sampleToFit.dump()
info = {
'data' : sampleToFit.histFile,
'dataNominal' : sampleToFit.nominalFit,
'dataAltSig' : sampleToFit.altSigFit ,
'dataAltBkg' : sampleToFit.altBkgFit ,
'dataAltSigBkg' : sampleToFit.altSigBkgFit ,
'mcNominal' : sampleToFit.mcRef.histFile,
'mcAlt' : None,
'tagSel' : None
}
if not tnpConf.samplesDef['mcAlt' ] is None:
info['mcAlt' ] = tnpConf.samplesDef['mcAlt' ].histFile
# if not tnpConf.samplesDef['tagSel'] is None:
# info['tagSel' ] = tnpConf.samplesDef['tagSel'].histFile
effis = None
effFileName ='%s/egammaEffi.txt' % outputDirectory
fOut = open( effFileName,'w')
for ib in range(len(tnpBins['bins'])):
effis = tnpRoot.getAllEffi( info, tnpBins['bins'][ib] )
### formatting assuming 2D bining -- to be fixed
v1Range = tnpBins['bins'][ib]['title'].split(';')[1].split('<')
v2Range = tnpBins['bins'][ib]['title'].split(';')[2].split('<')
if ib == 0 :
astr = '### var1 : %s' % v1Range[1]
print(astr)
fOut.write( astr + '\n' )
astr = '### var2 : %s' % v2Range[1]
print(astr)
fOut.write( astr + '\n' )
astr = '%+8.5f\t%+8.5f\t%+8.5f\t%+8.5f\t%5.5f\t%5.5f\t%5.5f\t%5.5f\t%5.5f\t%5.5f\t%5.5f\t%5.5f' % (
float(v1Range[0]), float(v1Range[2]),
float(v2Range[0]), float(v2Range[2]),
effis['dataNominal'][0],effis['dataNominal'][1],
effis['mcNominal' ][0],effis['mcNominal' ][1],
effis['dataAltBkg' ][0],
effis['dataAltSig' ][0],
effis['mcAlt' ][0],
# effis['tagSel'][0],
effis['dataAltSigBkg' ][0],
)
print(astr)
fOut.write( astr + '\n' )
fOut.close()
print('Effis saved in file : ', effFileName)
import libPython.EGammaID_scaleFactors as egm_sf
egm_sf.doEGM_SFs(effFileName,sampleToFit.lumi)