-
Notifications
You must be signed in to change notification settings - Fork 0
/
makelhgrid.py
executable file
·315 lines (263 loc) · 9.89 KB
/
makelhgrid.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
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
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python
import sys
import os
import numpy
import glob
import argparse
import StringIO
def main():
"""
Create LHgrid file from HERAFitter lhapdf.block.txt
"""
parser = argparse.ArgumentParser(description='Create LHgrid File.')
parser.add_argument("-i", "--input_folder", help="Input Folder")
parser.add_argument("-o", "--output_filepath", help="Output Folder", default='./test.LHgrid')
parser.add_argument("-l", "--limit_members", help="Limit Members Folder", default=0, type=int)
parser.add_argument("--eig", help="Produce eigenvector pdf", action='store_true')
args = vars(parser.parse_args())
if args['eig']:
produce_eig_lhgrid(**args)
else:
produce_general_lhgrid(**args)
def produce_eig_lhgrid(**args):
member_folder = args['input_folder'] + '/MEMBER_00'
alphas = query(member_folder + '/minuit.out.txt','alphas')[-1].split()[2]
order = query(member_folder + '/steering.txt','Order')[0].split()[2].replace('\'','')
npar = query(member_folder + '/minuit.out.txt','NPAR')[-1].split()[6]
lhgrids = []
lhgrids.append(args['input_folder'] + '/MEMBER_00'+ '/lhapdf.block.txt')
lhgrids += sorted(glob.glob(args['input_folder'] + '/MEMBER_00'+ '/pdfs_*.lhgrid'))
print lhgrids
header= get_header(nmembers=len(lhgrids)-1,
order=order,
alphas_values=len(lhgrids)*(alphas,))
with open(args['output_filepath'], 'w') as f:
f.write(header)
for n, lhgrid in enumerate(lhgrids):
print lhgrid
griddef, pdfblock = get_lhapdf_block(lhgrid)
if n == 0:
griddef.seek(0)
f.write(griddef.read())
pdfblock.seek(0)
f.write(pdfblock.read())
f.write("'End:'\n")
def produce_general_lhgrid(**args):
member_folders = sorted(glob.glob(args['input_folder'] + '/*'))
if args['limit_members'] != 0:
member_folders = member_folders[0:args['limit_members']]
metadata = {'alphas' : [], 'order' : [], 'npar' : []}
valid_pdfs = []
#Get all the physics parameters
print "Begin checking PDF folders and extracting metadata"
for member_folder in member_folders:
try:
alphas = query(member_folder + '/minuit.out.txt','alphas')[-1].split()[2]
order = query(member_folder + '/steering.txt','Order')[0].split()[2].replace('\'','')
npar = query(member_folder + '/minuit.out.txt','NPAR')[-1].split()[6]
#Check if lhapdf.block.txt exists
with open(member_folder + '/lhapdf.block.txt') as file:
pass #Potential for unhandled exception
except:
print "Not a valid lhgrid folder", member_folder
continue
#PDF seems to be valid
valid_pdfs.append(member_folder)
metadata['alphas'].append(alphas)
metadata['order'].append(order)
metadata['npar'].append(npar)
print "Scanned all folders"
print "There are {0} valid pdfs".format(len(valid_pdfs))
header= get_header(nmembers=len(valid_pdfs)-1,
order=metadata['order'][0],
alphas_values=metadata['alphas'])
print "This is the used header file."
print "Check carefully."
print header
print "Begin writing the lhgrid blocks to file"
with open(args['output_filepath'], 'w') as f:
f.write(header)
for n, member_folder in enumerate(valid_pdfs):
print "Member {0}: {1}".format(n, member_folder)
print "Member {0}: AlphasMz: {0}, Order: {0}".format(n, metadata['alphas'][n], metadata['order'][n])
griddef, pdfblock = get_lhapdf_block(member_folder + '/lhapdf.block.txt')
if n == 0:
griddef.seek(0)
f.write(griddef.read())
pdfblock.seek(0)
f.write(pdfblock.read())
f.write("'End:'\n")
def query(file, pattern):
result = []
with open(file, 'r') as f:
for line in f:
if pattern in line:
result.append(line)
return result
def get_header(nmembers=0, order='nlo', alphas_values=None):
header="""'Version' '5.8'
'Description: '
'HERAPDF fit: varaint'
'member 0 - central'
' '
'Alphas:'
'Variable','{order}','EvolCode'
1,91.187,1.40,4.75,180.0
'MinMax:'
{nmembers},1
1.E-06,1.,1.0,200000000.
'QCDparams:'
{nmembers},1
0.338,0.243
'Parameterlist:'
'list',{nmembers},1
{alphas_values}
'Evolution:'
'{order}',1.9,1.0
'HERAGRID10'
{nmembers},0
"""
header = header.format(nmembers=nmembers,alphas_values='\n'.join(alphas_values),order=order.lower())
return header
def make_lhgrid(input_files, output_file, alphasmz, evolution):
q2valpdf = numpy.zeros(161)
xvalpdf = numpy.zeros(161)
alphas = numpy.zeros(161)
buffer = numpy.zeros((8,161,161))
if os.path.isfile(output_file):
print "Warning. File exists"
os.remove(output_file)
nmember = len(input_files)-1
header="""'Version' '5.8'
'Description: '
'HERAPDF fit: varaint'
'member 0 - central'
' '
'Alphas:'
'Variable','{evolution}','EvolCode'
1,91.187,1.40,4.75,180.0
'MinMax:'
{nmember},1
1.E-06,1.,1.0,200000000.
'QCDparams:'
{nmember},1
0.338,0.243
'Parameterlist:'
'list',{nmember},1
{alphasmz}
'Evolution:'
'{evolution}',1.9,1.0
'HERAGRID10'
{nmember},0
"""
header = header.format(nmember=nmember,alphasmz='\n'.join((nmember+1)*(alphasmz,)),evolution=evolution.lower())
print "Using the following header. Please check carefully!"
print header
with open(output_file, 'a') as f:
f.write(header)
with open(output_file,'a') as f:
if n == 0:
for iq2 in range(0,23):
line = []
for j in range(0,7):
line.append(q2valpdf[iq2*7+j])
f.write(''.join('{0:15.7E}'.format(x) for x in line) + '\n')
for jx in range(0,23):
line = []
for j in range(0,7):
line.append(xvalpdf[jx*7+j])
f.write(''.join('{0:15.7E}'.format(x) for x in line) + '\n')
for iq2 in range(0,23):
line = []
for j in range(0,7):
line.append(alphas[iq2*7+j])
f.write(''.join('{0:15.7E}'.format(x) for x in line) + '\n')
for k in range(0,8):
for i in range(0,161):
for j in range(0,23):
line = []
for ii in range(0,7):
line.append(buffer[k][j*7 + ii][i])
f.write(''.join('{0:15.7E}'.format(x) for x in line) + '\n')
with open(output_file,'a') as f:
f.write("'End:'\n")
def get_lhapdf_block(filepath):
q2valpdf = numpy.zeros(161)
xvalpdf = numpy.zeros(161)
alphas = numpy.zeros(161)
buffer = numpy.zeros((8,161,161))
with open(filepath) as f:
for iq2 in range(0,23):
line = f.readline().split()
for j in range(0,7):
q2valpdf[iq2*7 + j] = line[j]
for jx in range(0,23):
line = f.readline().split()
for j in range(0,7):
xvalpdf[(jx)*7+j] = line[j]
for iq2 in range(0,23):
line = f.readline().split()
for j in range(0,7):
alphas[(iq2)*7+j] = line[j]
for i in range(0,161):
for j in range(0,161):
line = f.readline().split()
for k in range(0,8):
buffer[k][j][i] = line[k]
griddef = StringIO.StringIO()
pdfblock = StringIO.StringIO()
for iq2 in range(0,23):
line = []
for j in range(0,7):
line.append(q2valpdf[iq2*7+j])
griddef.write(''.join('{0:15.7E}'.format(x) for x in line) + '\n')
for jx in range(0,23):
line = []
for j in range(0,7):
line.append(xvalpdf[jx*7+j])
griddef.write(''.join('{0:15.7E}'.format(x) for x in line) + '\n')
for iq2 in range(0,23):
line = []
for j in range(0,7):
line.append(alphas[iq2*7+j])
griddef.write(''.join('{0:15.7E}'.format(x) for x in line) + '\n')
for k in range(0,8):
for i in range(0,161):
for j in range(0,23):
line = []
for ii in range(0,7):
line.append(buffer[k][j*7 + ii][i])
pdfblock.write(''.join('{0:15.7E}'.format(x) for x in line) + '\n')
return griddef, pdfblock
def write_blocks(blocklist, filepath='test.LHgrid'):
with open(output_file, 'a') as f:
f.write(header)
with open(output_file,'a') as f:
if n == 0:
for iq2 in range(0,23):
line = []
for j in range(0,7):
line.append(q2valpdf[iq2*7+j])
f.write(''.join('{0:15.7E}'.format(x) for x in line) + '\n')
for jx in range(0,23):
line = []
for j in range(0,7):
line.append(xvalpdf[jx*7+j])
f.write(''.join('{0:15.7E}'.format(x) for x in line) + '\n')
for iq2 in range(0,23):
line = []
for j in range(0,7):
line.append(alphas[iq2*7+j])
f.write(''.join('{0:15.7E}'.format(x) for x in line) + '\n')
for k in range(0,8):
for i in range(0,161):
for j in range(0,23):
line = []
for ii in range(0,7):
line.append(buffer[k][j*7 + ii][i])
f.write(''.join('{0:15.7E}'.format(x) for x in line) + '\n')
with open(output_file,'a') as f:
f.write("'End:'\n")
pass
if __name__ == '__main__':
sys.exit(main())