forked from CMS-LUMI-POG/VdMFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvdmFitterII.py
More file actions
306 lines (242 loc) · 10.3 KB
/
vdmFitterII.py
File metadata and controls
306 lines (242 loc) · 10.3 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
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
import FitManager
import SG_Fit
import SGConst_Fit
import DG_Fit
import DGConst_Fit
import SimCapSigma_PCCAndVtx_Fit
import GSupGConst_Fit
import DG_2D_Fit
from vdmUtilities import showAvailableFits
from vdmUtilities import orderedIntKeysFirst
import sys
import json
import pickle
import os
import ROOT as r
def doRunVdmFitter(Fill, FitName, InputGraphsFiles, OutputDir, PlotsTempPath, FitConfigInfo):
showAvailableFits()
availableFits = FitManager.get_plugins(FitManager.FitProvider)
key = FitName + '_Fit'
if key not in availableFits:
print "Fit " + FitName + " requested via json file does not exist, nothing to fit with, exit."
sys.exit(1)
fitter = availableFits[FitName+'_Fit']()
FitLogFile = OutputDir + FitName +'.log'
fitlogfile = open(FitLogFile,'w')
sysstdout = sys.stdout
sys.stdout = fitlogfile
sysstderr = sys.stderr
sys.stderr = fitlogfile
# temporarily: move graphs files over from forTmpStorage directory
#list = os.listdir('./forTmpStorage/')
#import shutil
#for element in list:
# shutil.copy('./forTmpStorage/'+element, InputDataDir)
# for 2D fits
if '_2D' in FitName:
# test that when 2D fit function is requested, the graph file used is also a 2D one
if not '2D' in InputGraphsFiles:
print "--?? You selected a 2D fitting function, but chose as input a graphs file without the \"2D\" in its name"
print "??--"
print " "
sys.exit(1)
for graphFile in InputGraphsFiles:
if not os.path.isfile(graphFile):
print "--?? Input data file ", graphFile ," does not exist"
print "??--"
print " "
sys.exit(1)
if (not any('VTX' in graph for graph in InputGraphsFiles) and len(InputGraphsFiles)>1):
print "--?? Input data file ", graphFile ," does not correspond to Vertex file"
print "??--"
print " "
sys.exit(1)
graphsAll_dict = {}
graphsAll_list = []
resultsAll = {}
for graphFile in InputGraphsFiles:
print " "
print "Now open input graphs file: ", graphFile
infile = open(graphFile, 'rb')
graphsAll_dict = pickle.load(infile)
infile.close()
graphsAll_list.append(graphsAll_dict)
# if input file is 2D graphs file, also open the corresponding 1D graph file
if '_2D' in FitName:
fileName1D = InputGraphsFile.replace("graphs2D", "graphs")
infile1D = open(fileName1D, 'rb')
graphs1D = pickle.load(infile1D)
# first loop over scan numbers
orderedKeyList=[]
if (len(InputGraphsFiles)==1):
for keyAll in sorted(graphsAll_dict.keys()):
graphs = {}
results = {}
graphs = graphsAll_dict[keyAll]
if '_2D' in FitName:
keyX = "Scan_" + keyAll.split("_")[1]
keyY = "Scan_" + keyAll.split("_")[2]
graphsX = graphs1D[keyX]
graphsY = graphs1D[keyY]
# order keys in natural order, i.e. from smallest BCID to largest
# determine which of the bcid among those with collisions are indeed represented with a TGraphErrors() in the input graphs file
# need to do this because PCC uses only subset of 5 bcids of all possible bcids with collisions
for key in orderedIntKeysFirst(graphs.keys()):
print "------>>>>"
print "Now fitting BCID ", key
graph = graphs[key]
if '_2D' in FitName:
result = fitter.doFit2D(graph, graphsX[key], graphsY[key], FitConfigInfo)
for entry in result:
print ">>", result, type(result)
results[key] = result
functions = result[0]
canvas = fitter.doPlot2D(graphsX[key], graphsY[key], functions, Fill)
else:
result = fitter.doFit(graph, FitConfigInfo)
results[key] = result
functions = result[0]
canvas = fitter.doPlot(graph, functions, Fill, PlotsTempPath[0])
resultsAll[keyAll] = results
table = [fitter.table]
sys.stdout = sysstdout
sys.stderr = sysstderr
fitlogfile.close()
return resultsAll, table
else: #Sim Fit
for (keyAll1,keyAll2) in sorted(zip(graphsAll_list[0].keys(), graphsAll_list[1].keys())):
graphs = {}
graphs1 = {}
graphs2 = {}
results = {}
graphs1 = graphsAll_list[0][keyAll1]
graphs2 = graphsAll_list[1][keyAll1]
ds = [graphs1, graphs2]
for k in graphs1.iterkeys():
graphs[k] = list(graphs[k] for graphs in ds)
# order keys in natural order, i.e. from smallest BCID to largest
# determine which of the bcid among those with collisions are indeed represented with a TGraphErrors() in the input graphs file
# need to do this because PCC uses only subset of 5 bcids of all possible bcids with collisions
for key in orderedIntKeysFirst(graphs.keys()):
print "------>>>>"
print "Now fitting BCID ", key
result = fitter.doFit(graphs[key], FitConfigInfo)
results[key] = result
functions = result[0]
canvas = fitter.doPlot(graphs[key][0], functions[:4], Fill, PlotsTempPath[0])
canvas = fitter.doPlot(graphs[key][1], functions[4:], Fill, PlotsTempPath[1])
sys.stdout = sysstdout
sys.stderr = sysstderr
fitlogfile.close()
table = [fitter.table_Luminometer1, fitter.table_Luminometer2]
return resultsAll, table
if __name__ == '__main__':
ConfigFile = sys.argv[1]
Config=open(ConfigFile)
ConfigInfo = json.load(Config)
Config.close()
Fill = str(ConfigInfo['Fill'])
Luminometer = str(ConfigInfo['Luminometer'])
Corr = ConfigInfo['Corr']
AnalysisDir = str(ConfigInfo['AnalysisDir'])
FitName = str(ConfigInfo['FitName'])
FitConfigFile = str(ConfigInfo['FitConfigFile'])
PlotsTempPath = [["./plotstmp/"]]
corrFull = ""
for entry in Corr:
corrFull = corrFull + '_' + str(entry)
if corrFull[:1] == '_':
corrFull = corrFull[1:]
if not corrFull:
corrFull = "noCorr"
InputGraphsFiles = []
OutputDirs = []
if 'InputGraphsFile' in vdmFitterConfig:
InputGraphsFile = AnalysisDir + '/' + Luminometer + '/' + vdmFitterConfig['InputGraphsFile']
InputGraphsFiles.append(InputGraphsFile)
else:
defaultGraphsFile = 'graphs' + '/' + 'graphs_' + Fill + '_' + corrFull + '.pkl'
InputGraphsFile = AnalysisDir + '/' + Luminometer + '/' + defaultGraphsFile
InputGraphsFiles.append(InputGraphsFile)
OutputDir = './' + AnalysisDir + '/' + Luminometer + '/results/' + corrFull + '/'
OutputDirs.append(OutputDir)
if 'Sim' in FitConfigFile:
PlotsTempPath = ConfigInfo['PlotsTempPath']
if 'InputSimGraphsFile' in vdmFitterConfig:
InputSimGraphsFile = AnalysisDir + '/' + 'VTX' + '/' + vdmFitterConfig['InputSimGraphsFile']
InputGraphsFiles.append(InputSimGraphsFile)
else:
defaultSimGraphsFile = 'graphs' + '/' + 'graphs_' + Fill + '_' + corrFull + '.pkl'
InputSimGraphsFile = AnalysisDir + '/' + 'VTX' + '/' + defaultSimGraphsFile
InputGraphsFiles.append(InputSimGraphsFile)
OutputDir = './' + AnalysisDir + '/' + 'VTX' + '/results/' + corrFull + '/'
OutputDirs.append(OutputDir)
for OutputDir in OutputDirs:
if not os.path.isdir(OutputDir):
print "Requested output directory ", OutputDir , " does not exist."
print "Please check if input for chosen corrections is available."
sys.exit(1)
print " "
print "ATTENTION: Output will be written into ", OutputDirs[0]
print "Please check there for log files."
print " "
FitConfig=open(FitConfigFile)
FitConfigInfo = json.load(FitConfig)
FitConfig.close()
# needs to be the same name as assumed in the fit function python files, where it is ./minuitlogtmp/Minuit.log
MinuitLogPath = './minuitlogtmp/'
MinuitLogFile = MinuitLogPath + 'Minuit.log'
if not os.path.isdir(MinuitLogPath):
os.mkdir(MinuitLogPath, 0755)
# need to do this before each fitting loop
if os.path.isfile(MinuitLogFile):
os.remove(MinuitLogFile)
# needs to be the same name as assumed in vdmUtilities, where it is ./plotstmp
for path in PlotsTempPath:
if not os.path.isdir(path[0]):
os.makedirs(path, 0755)
else:
filelist = os.listdir(path[0])
for element in filelist:
if ('ps' or 'root') in element:
os.remove(path[0]+element)
resultsAll = {}
table = []
resultsAll, table = doRunVdmFitter(Fill, FitName, InputGraphsFiles, OutputDirs[0], PlotsTempPath, FitConfigInfo)
for key in resultsAll:
print "keyResutsAll", key
for (i,OutputDir) in enumerate(OutputDirs):
outResults ='./'+ OutputDir + '/'+FitName+'_FitResults.pkl'
outFile = open(outResults, 'wb')
pickle.dump(table[i], outFile)
outFile.close()
csvfile = open('./'+ OutputDir + '/'+FitName+'_FitResults.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerows(table[i])
csvfile.close()
outResults ='./'+ OutputDir + '/'+FitName+'_Functions.pkl'
outFile = open(outResults, 'wb')
pickle.dump(resultsAll, outFile)
outFile.close()
outFileMinuit = './'+OutputDirs[0] + '/'+FitName+'_Minuit.log'
os.rename(MinuitLogFile, outFileMinuit)
output_FittedGraphs = dict(zip(OutputDirs,PlotsTempPath))
for OutputDir in output_FittedGraphs:
outPdf = './'+OutputDir + '/'+FitName+'_FittedGraphs.pdf'
PlotsPath = output_FittedGraphs[OutputDir][0]
filelist = os.listdir(PlotsPath)
merge =-999.
for element in filelist:
if element.find(".ps") > 0:
merge = +1.
if merge > 0:
os.system("gs -dNOPAUSE -sDEVICE=pdfwrite -dBATCH -sOutputFile="+outPdf+" " + PlotsPath+"/*.ps")
outRoot = './'+OutputDir + '/'+FitName+'_FittedGraphs.root'
if os.path.isfile(outRoot):
os.remove(outRoot)
merge =-999.
for element in filelist:
if element.find(".root") > 0:
merge = +1.
if merge > 0:
os.system("hadd " + outRoot + " " + PlotsPath + "*.root")