From 0a593ee19e680b2372e5c2e2ee1fdda1b8b701c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Silva-Rodr=C3=ADguez?= Date: Tue, 23 Apr 2024 15:48:18 +0200 Subject: [PATCH] Updated simset_sim Now, second sample simulation (for estimating n_photons) will only be performed if importance sampling is activated. This solves a problem when the user does not want to use importance sampling. Additionally, I entered a condition that will only trigger this calculation if the user has not stated photons manually. So, correct way of launching simulations with importance sampling and automatic calculations should be: s_photons = 1543214 (int num) photons = 0 This is well matched with photons = 0 and s_photons=0 triggers autocalculation (from simset) and no importance sampling --- src/simset/simset_sim.py | 53 ++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/simset/simset_sim.py b/src/simset/simset_sim.py index 5d6771a..fbbc1c0 100644 --- a/src/simset/simset_sim.py +++ b/src/simset/simset_sim.py @@ -150,32 +150,48 @@ def run_simset_simulation(self, sim_dir): ) my_log = join(sim_dir, "simset_s0_init.log") - print("Running first sampling simulation...") + print("Running first simulation without importance sampling...") command = "%s/bin/phg %s > %s" % (self.simset_dir, my_phg, my_log) tools.osrun(command, log_file) - my_phg = self.prepare_simset_files( - sim_dir, act_table_factor, act, sim_photons, sim_time, 1 - ) - # Copying phg file for posterior analysis - sim_phg = Path(sim_dir).joinpath("phg.rec") - shutil.copy( - sim_phg, - sim_phg.with_name("phg_second_sampling_sim.rec") - ) - my_log = join(sim_dir, "simset_s0.log") - - print("Running second sampling simulation...") - command = "%s/bin/phg %s > %s" % (self.simset_dir, my_phg, my_log) - tools.osrun(command, log_file) - w_quotient = read_ws_from_simset_log(my_log) - rec_weight = join(sim_dir, "rec.weight") det_hf = join(sim_dir, "det_hf.hist") phg_hf = join(sim_dir, "phg_hf.hist") if self.s_photons != 0 and self.params.get("add_randoms") != 1: - sim_photons = int(self.s_photons * w_quotient) + + # If the user did not state photons it will be calculated from sampling + if self.photons == 0: + + # Removes counts for preparing for the next simulation + os.remove(rec_weight) + if exists(det_hf): + os.remove(det_hf) + if exists(phg_hf): + os.remove(phg_hf) + + my_phg = self.prepare_simset_files( + sim_dir, act_table_factor, act, sim_photons, sim_time, 1 + ) + + # Copying phg file for posterior analysis + sim_phg = Path(sim_dir).joinpath("phg.rec") + shutil.copy( + sim_phg, + sim_phg.with_name("phg_second_sampling_sim.rec") + ) + my_log = join(sim_dir, "simset_s0.log") + + print("Running second sampling simulation to calculate photons...") + command = "%s/bin/phg %s > %s" % (self.simset_dir, my_phg, my_log) + tools.osrun(command, log_file) + w_quotient = read_ws_from_simset_log(my_log) + + sim_photons = int(self.s_photons * w_quotient) + + else: + # If the user stated photons, the provided value will be used + sim_photons = self.photons # Removes counts for preparing for the next simulation os.remove(rec_weight) @@ -190,7 +206,6 @@ def run_simset_simulation(self, sim_dir): my_log = join(sim_dir, "simset_s1.log") print("Running full simulation with importance sampling...") - command = "%s/bin/phg %s > %s" % (self.simset_dir, my_phg, my_log) tools.osrun(command, log_file)