-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMuonSimulator.py
More file actions
130 lines (91 loc) · 4.11 KB
/
MuonSimulator.py
File metadata and controls
130 lines (91 loc) · 4.11 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
r'''Class that handles various inputs and systematic parameters
and simulates muons'''
import os, sys
import subprocess
from pipes import quote
class DECOMuonSimulator():
r'''Simulator class for muons that uses allpix
and GEANT'''
def __init__(self, pid, energy, theta, **kwargs):
self.pid = pid #Particle id, ie mu+, e-
self.energy = energy
self.theta = float(theta)
self.phi = float(kwargs.pop('phi', 0.))
self.depletion_thickness = float(kwargs.pop('depletion_thickness', 26.3))
#todo: NOTE: in unit of um !!!!!!!
# Set other possible systematics with kwargs, including
# electric fields, pixel size, etc.
self.path_to_root = os.getenv('DECO_ROOT_PATH')
self.path_to_geant = os.getenv('DECO_GEANT_PATH')
self.path_to_allpix = os.getenv('DECO_ALLPIX_PATH')
self.base_command = self.path_to_allpix + ' -c ./htc_wildfire/source_measurement.conf -o'
def write_source_file(self, n_events):
with open('./htc_wildfire/source_measurement_replace.conf', 'r') as f:
data = f.readlines()
data[3] = data[3].format(n_events)
with open('./htc_wildfire/source_measurement.conf', 'w') as wf:
wf.writelines(data)
wf.close()
def write_detector_file(self):
# USE THE PARAMETERS TO REWRITE DETECTOR FILE
with open('./htc_wildfire/detector_replace.conf', 'r') as f:
data = f.readlines()
data[-2] = data[-2].format(self.phi, self.theta, 0)
with open('./htc_wildfire/detector.conf', 'w') as wf:
wf.writelines(data)
wf.close()
with open('./htc_wildfire/htc_wildfire_shielded_replace.conf', 'r') as f:
data = f.readlines()
data[4] = data[4].format(self.depletion_thickness)
with open('./htc_wildfire/htc_wildfire_shielded.conf', 'w') as wf:
wf.writelines(data)
wf.close()
def set_output_file_name(self):
# write unique file name depending on parameters
if not os.path.exists("./output"):
os.system("mkdir ./output")
if not os.path.exists("./output/" + str(self.pid)):
os.system("mkdir ./output/" + str(self.pid))
self.outfile = str(self.pid) + "/" + "{}_theta_{:.1f}_phi_{:.1f}_thickiness_{:.1f}_highstats.txt".format(self.energy, self.theta, self.phi, self.depletion_thickness)
pass
def get_output_file_name(self):
try:
return self.outfile
except:
self.set_output_file_name()
return self.outfile
def run_simulation(self, n_events=100):
self.source_local_env()
# Run the allpix simulation
output_file = self.get_output_file_name()
self.write_detector_file()
self.write_source_file(n_events)
my_command = self.base_command[:]
my_command += ' DepositionGeant4.particle_type="{}"'.format(self.pid)
my_command += ' -o DepositionGeant4.source_energy="{}"'.format(self.energy)
my_command += ' -o TextWriter.file_name="' + output_file + '"'
"""check if simulated using file name"""
if self.check_if_simulated(output_file) is True:
print("has been simulated")
return
subprocess.call(my_command, shell=True)
return
def check_if_simulated(self, filename):
# Check to see if simulation has already been run for
# this set of parameters
if not os.path.exists("./output/" + filename):
return False
return True
def source_local_env(self):
geant = self.path_to_geant + "/bin/geant4.sh"
root = self.path_to_root + "/bin/thisroot.sh"
pass
# Run source scripts for ROOT and GEANT if the
# simulation doesn't work
energy = ['10keV', '31.6keV', '100keV', '316keV', '1MeV', '3.16MeV',
'10MeV', '31.6MeV', '100MeV', '316MeV', '1GeV', '3.16GeV', '10GeV']
angles = ['0', '15', '30', '45', '60', '75']
for e in energy:
for ang in angles:
a = DECOMuonSimulator('mu+', e, ang, phi='30', depletion_thickness='26.3')
a.run_simulation(100)