-
Notifications
You must be signed in to change notification settings - Fork 0
/
g4Macro.py
52 lines (47 loc) · 1.89 KB
/
g4Macro.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
# We want to read in the output of the EBIT sims generated G4 Macro and break it up
# into an array of classes
import os
class G4MacroRun:
# Class : G4MacroRun
# Store each macro process to later be executed in parallel, the header is just the START line
# The block is everything else
# status = is it done? has it started? did it fail? you get the idea..
# Macro file format below :
# -----
# # START: E:Sb,Z:51,A:129,Q:40:T:0.400002
# /pga/selectGunAction 1
# /gps/particle ion
# /gps/ion 51 129 40
# /gps/position 0 0 0
# /gps/ene/mono 0
# /histo/filename E_Sb_Z_51_A_129_Q_40_T_0.400002
# /run/beamOn 20000
# -----
def __init__(self, header, macro_block, g4id, status=None):
self.header = header
self.macro_block = macro_block
self.status = status
self.g4id = g4id
def read_in_geant4_macro_file(g4_macro_filename, work_dir):
list_of_macro_objects = []
macro_line_buffer = ""
macro_header = ""
macro_block = ""
g4id = 0
with open(g4_macro_filename) as g4_file:
for g4_file_line in g4_file:
if '# START' in g4_file_line:
macro_header = g4_file_line
elif '# END' in g4_file_line:
# WRITE OBJECT
unique_part_of_header = macro_header.rstrip()[9:]
full_path_to_root_file = work_dir + '/' + unique_part_of_header + '.root'
if not os.path.isfile(full_path_to_root_file):
list_of_macro_objects.append(G4MacroRun(unique_part_of_header, macro_block, g4id, 0))
else:
print("Root file : %s already exists, skipping" % (full_path_to_root_file))
g4id = g4id + 1
macro_block = "" # Setup for next block
else:
macro_block += g4_file_line
return list_of_macro_objects # fix me later