2
Class to call G4BL and generate primaries for MAUS
4
# This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
6
# MAUS is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
11
# MAUS is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with MAUS. If not, see <http://www.gnu.org/licenses/>.
19
#pylint: disable=W0611
22
from cdb import Beamline
25
from subprocess import call
32
class CallG4bl: #pylint: disable=R0903
34
Calls G4BL and writes output to python dictionary
36
Creates primaries for MAUS depending on the parameters
37
set in the dictionary 'g4bl' in the json configuration
39
- magnet currents and proton absorber thickness can be set
40
along with the number of particles to generate
41
- to retrieve magnet currents and proton absorber
42
thickness from CDB set get_magnet_currents_pa_cdb to
43
True and run_number to the run of interest in json
44
configuration document
47
def __init__(#pylint: disable=C0103, R0903, R0912, R0913, R0914, R0915
48
self,newline='', file_path='', theta=0, deltaz=0,
49
path_g4bl='', output_path='', run_number=0, \
50
get_magnet_currents_pa_cdb='', random_seed=0):
52
- particles will be a dictionary filled by G4BL
53
- pdgid masses are input to calculate total energy,
60
self.pdgid_mass = {'211':139.5700, '-211':139.5700, '2212':938.27231, \
61
'-13':105.6584, '13':105.6584, '11':0.5109906, '-11':0.5109906, \
62
'22':0, '2112':939.5653, '14':0, '-14':0} #pylint: disable=C0103
65
off = open(file_path+'/MAY09-B1B2-positives.in',"r")
66
except IOError as err:
70
if str(get_magnet_currents_pa_cdb) in ['True']:
73
sf3 = open(file_path+'/key.txt',"w")
75
for k, v in beamline.get_beamline_for_run(run_number).iteritems(): #pylint: disable = C0301
76
for k, v in v.iteritems():
78
for k, v in v.iteritems():
79
newlines = str(k)+'\n'
80
sff = open(file_path+'/key.txt',"r")
83
sf2 = open(file_path+'/key.txt',"w")
87
if k == 'proton_absorber_thickness':
88
newlines = str(k)+'\n'
89
sff = open(file_path+'/key.txt',"r")
92
sf2 = open(file_path+'/key.txt',"w")
97
for row in fileinput.input([file_path+'/key.txt']):
100
sf4 = open(file_path+'/value.txt',"w")
102
for k, v in beamline.get_beamline_for_run(run_number).iteritems(): #pylint: disable = C0301
103
for k, v in v.iteritems():
105
for k, v in v.iteritems():
106
if k in ['q1', 'q3']:
107
for k, v in v.iteritems():
108
if k == 'set_current':
110
newlines = str(v)+'\n'
111
sf5 = open(file_path+'/value.txt',"r") #pylint: disable = C0301
114
sf6 = open(file_path+'/value.txt',"w") #pylint: disable = C0301
119
for k, v in v.iteritems():
120
if k == 'set_current':
122
newlines = str(v)+'\n'
123
sf5 = open(file_path+'/value.txt',"r") #pylint: disable = C0301
126
sf6 = open(file_path+'/value.txt',"w") #pylint: disable = C0301
130
if k in ['d2']: #pylint: disable = W0631
131
for k, v in v.iteritems(): #pylint: disable = W0631, C0301
132
if k == 'set_current':
133
coeff = [39.59, -55.998, 256.914, -v] #pylint: disable = C0301
134
roots = numpy.roots(coeff)
136
newlines = str(root)[1:-4]+'\n'
137
sf5 = open(file_path+'/value.txt',"r") #pylint: disable = C0301
140
sf6 = open(file_path+'/value.txt',"w") #pylint: disable = C0301
145
for k, v in v.iteritems():
146
if k == 'set_current':
148
newlines = str(v)+'\n'
149
sf5 = open(file_path+'/value.txt',"r") #pylint: disable = C0301
152
sf6 = open(file_path+'/value.txt',"w") #pylint: disable = C0301
157
for k, v in v.iteritems():
158
if k == 'set_current':
159
coeff = [39.59, -55.998, 256.914, -v] #pylint: disable = C0301
160
roots = numpy.roots(coeff)
162
newlines = str(root)[1:-4]+'\n'
163
sf5 = open(file_path+'/value.txt',"r") #pylint: disable = C0301
166
sf6 = open(file_path+'/value.txt',"w") #pylint: disable = C0301
170
if k == 'proton_absorber_thickness':
171
newlines = str(v)+'\n'
172
sf5 = open(file_path+'/value.txt',"r")
175
sf6 = open(file_path+'/value.txt',"w")
181
for row in fileinput.input([file_path+'/value.txt']):
182
line2.append(row[:-1])
185
sf7 = open(file_path+'/magnet_currents.txt',"w")
188
for c, d in zip(line, line2):
189
text2 = 'param {0}={1}'.format(c, d)+'\n'
190
sf8 = open(file_path+'/magnet_currents.txt',"r")
193
sf9 = open(file_path+'/magnet_currents.txt',"w")
198
sf8 = open(file_path+'/magnet_currents.txt',"r")
201
os.remove(file_path+'/key.txt')
202
os.remove(file_path+'/value.txt')
204
except cdb.CdbTemporaryError:
205
sys.excepthook(*sys.exc_info())
207
Failed to run - check network connection!
211
sf = open(file_path+'/MAY09-B1B2-positives-param.in', "w")
212
except IOError as err:
215
sf.write('param random_seed='+str(random_seed)+'\n')
224
sf = open(file_path+'/MAY09-B1B2-positives-param.in', "w")
225
except IOError as err:
228
sf.write('param random_seed='+str(random_seed))
233
g4bl_command_line_input = path_g4bl + '/g4bl ' + file_path + '/MAY09-B1B2-positives-param.in' #pylint: disable = C0301
235
call(g4bl_command_line_input, shell=True)
236
except Exception: #pylint: disable = W0703
237
print('Error: Cannot execute G4Beamline.')
240
of = open(output_path+'/G4BLoutput.txt',"r")
241
except IOError as err:
244
for row in fileinput.input([output_path+'/G4BLoutput.txt']):
253
theta = math.radians(theta)
254
for i in range(0, len(line)):
256
for word in line[i].split(' '):
258
px = float(element[3])
259
py = float(element[4])
260
pz = float(element[5])
262
rest_mass = self.pdgid_mass[str(element[7])]
263
part_energy = math.sqrt(px**2+py**2+pz**2+rest_mass**2)
268
self.particles[key] = dict(position = dict(\
269
x = math.cos(theta) * float(element[0]) + 737.63,
270
y = float(element[1]),
271
z = math.sin(theta) * float(element[0]) - deltaz))
273
self.particles[key].update(momentum = dict(\
274
x = math.cos(theta) * float(element[3]) - math.sin(theta) * float(element[5]), #pylint: disable = C0301
275
y = float(element[4]),
276
z = math.sin(theta) * float(element[3]) + math.cos(theta) * float(element[5]))) #pylint: disable = C0301
277
self.particles[key].update(time = float(element[6]), \
278
particle_id = int(element[7]))
279
self.particles[key].update(random_seed = random_seed, \
280
energy = part_energy)
281
except IOError as err:
282
print('Bad G4beamline file, not enough elements or empty file')
284
# with open('G4BLoutput.txt', 'w') as outfile:
285
# json.dump(self.particles, outfile)
287
# for i in range(0, len(line)):
288
# for j in range(i + 1, len(line)):
289
### if self.particles['entry' + str(i)]['random_seed']\
290
# == self.particles['entry' + str(j)]['random_seed']:
291
# self.particles['entry' + str(j)]['random_seed']\
292
# =self.particles['entry' + str(j)]['random_seed']\
295
if(os.path.exists(file_path + '/magnet_currents.txt') == True):
296
os.remove(file_path + '/magnet_currents.txt')