-
Notifications
You must be signed in to change notification settings - Fork 14
/
vdmFitter.py
247 lines (185 loc) · 7.13 KB
/
vdmFitter.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
import FitManager
import SG_Fit
import SGConst_Fit
import DG_Fit
import DGConst_Fit
import GSupGConst_Fit
import DG_2D_Fit
from vdmUtilities import showAvailableFits
import sys
import json
import pickle
import os
import ROOT as r
def doRunVdmFitter(Fill, FitName, InputGraphsFile, OutputDir, 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 InputGraphsFile:
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)
if not os.path.isfile(InputGraphsFile):
print "--?? Input data file ", InputGraphsFile ," does not exist"
print "??--"
print " "
sys.exit(1)
print " "
print "Now open input graphs file: ", InputGraphsFile
graphsAll = {}
infile = open(InputGraphsFile, 'rb')
graphsAll = pickle.load(infile)
infile.close()
print graphsAll
# 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)
# print graphsList1D
resultsAll = {}
# first loop over scan numbers
print ">>", graphsAll.keys()
for keyAll in sorted(graphsAll.keys()):
print "Looking at ", keyAll
graphs = {}
results = {}
graphs = graphsAll[keyAll]
if '_2D' in FitName:
keyX = "Scan_" + keyAll.split("_")[1]
keyY = "Scan_" + keyAll.split("_")[2]
print "keyX", keyX
print "keyY", keyY
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
print graphs.keys()
for key in sorted(graphs.keys(), key=int):
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)
for entry in result:
print ">>", result, type(result)
results[key] = result
functions = result[0]
canvas = fitter.doPlot(graph, functions, Fill)
for entry in fitter.table:
print entry
resultsAll[keyAll] = results
print resultsAll
sys.stdout = sysstdout
sys.stderr = sysstderr
fitlogfile.close()
return resultsAll, fitter.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'])
InputGraphsFile = AnalysisDir + '/' + Luminometer + '/' + str(ConfigInfo['InputGraphsFile'])
corrFull = ""
for entry in Corr:
corrFull = corrFull + '_' + str(entry)
if corrFull[:1] == '_':
corrFull = corrFull[1:]
if not corrFull:
corrFull = "noCorr"
OutputDir = './' + AnalysisDir + '/' + Luminometer + '/results/' + corrFull + '/'
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 ", OutputDir
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
PlotsPath = './plotstmp/'
if not os.path.isdir(PlotsPath):
os.mkdir(PlotsPath, 0755)
else:
filelist = os.listdir(PlotsPath)
for element in filelist:
os.remove(PlotsPath+element)
resultsAll = {}
table = []
resultsAll, table = doRunVdmFitter(Fill, FitName, InputGraphsFile, OutputDir, FitConfigInfo)
outResults ='./'+ OutputDir + '/'+FitName+'_FitResults.pkl'
outFile = open(outResults, 'wb')
pickle.dump(table, outFile)
outFile.close()
import csv
csvfile = open('./'+ OutputDir + '/'+FitName+'_FitResults.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerows(table)
csvfile.close()
# outResults ='./'+ OutputDir + '/'+FitName+'_FitFunctions.pkl'
# outFile = open(outResults, 'wb')
# pickle.dump(resultsAll, outFile)
# outFile.close()
outFileMinuit = './'+OutputDir + '/'+FitName+'_Minuit.log'
os.rename(MinuitLogFile, outFileMinuit)
outPdf = './'+OutputDir + '/'+FitName+'_FittedGraphs.pdf'
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")