Skip to content

Commit

Permalink
Merge branch 'refs/heads/dev_sim2root' into dev_io_root
Browse files Browse the repository at this point in the history
  • Loading branch information
lwpiotr committed Jul 4, 2024
2 parents 707bc0d + 5431868 commit 9e5e617
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 45 deletions.
3 changes: 2 additions & 1 deletion grand/basis/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def find_max_with_parabola_interp_3pt(x_trace, y_trace, idx_max):
return x_max, y_max


def find_max_with_parabola_interp(x_trace, y_trace, idx_max, factor_hill=0.8):
def find_max_with_parabola_interp(x_trace, y_trace, idx_max, factor_hill=0.96):
"""Parabolic interpolation of the maximum with more than 3 points
trace : all values >= 0
Expand All @@ -76,6 +76,7 @@ def find_max_with_parabola_interp(x_trace, y_trace, idx_max, factor_hill=0.8):
:param factor_hill:
:type factor_hill:
"""
# y threshold mean around max (so 3 points) * factor_hill
y_lim = (y_trace[idx_max - 1 : idx_max + 2].sum() / 3) * factor_hill
logger.debug(f"y_lim={y_lim}")
# 1
Expand Down
6 changes: 3 additions & 3 deletions sim2root/CoREASRawRoot/CoreasToRawROOT.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,10 @@ def CoreasToRawRoot(file, simID=None):

# define time params:
t_length = len(timestamp)
t_0 = timestamp[0] + t_length/2
t_0 = timestamp[0] + t_length/2 * TimeBinSize
t_pre = 800#ns
t_pre = t_length/2
t_post = t_length/2
t_pre = t_length/2 * TimeBinSize
t_post = t_length/2 * TimeBinSize

# add to ROOT tree
# in Zhaires converter: AntennaN[ant_ID]
Expand Down
20 changes: 2 additions & 18 deletions sim2root/CoREASRawRoot/CorsikaInfoFuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,23 +273,7 @@ def read_long(pathLongFile):
TODO: fix hillas_parameter - something's not working yet
"""
with open(pathLongFile, mode="r") as file:
# create a temporary file to write the corrected contents
temp_file = io.StringIO()

for line in file:
# use a regex to search for a minus sign that is not part of an exponent
if search(r"(?<!e)(-)(?=\d)", line):
# if the minus sign is not part of an exponent, replace it with a space and a minus sign
line = line.replace("-", " -")
# write the corrected line to the temporary file
temp_file.write(line)

# set the file pointer to the beginning of the temporary file
temp_file.seek(0)

# read the contents of the temporary file into a list of strings
lines = temp_file.readlines()

lines=file.readlines()


n_steps = int(lines[0].rstrip().split()[3])
Expand All @@ -310,7 +294,7 @@ def read_long(pathLongFile):
hillas_parameters = []
for line in lines:
if bool(search("PARAMETERS", line)):
parts = re.split(r"\s+", line)
parts = line.split()
param1, param2, xmax, param4, param5, param6 = parts[2], parts[3], parts[4], parts[5], parts[6], parts[7]
hillas_parameters.append(xmax)
if bool(search("CHI", line)):
Expand Down
61 changes: 45 additions & 16 deletions sim2root/Common/IllustrateSimPipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,32 @@ def manage_args():
"directory",
help="Simulation output data directory in GRANDROOT format."
)
parser.add_argument(
"--savefig_dir",
type=str,
default=None,
help="Directory to save figures. Defaults to simulation output directory if not specified."
)
parser.add_argument(
"--verbose",
choices=["debug", "info", "warning", "error", "critical"],
default="info",
help="logger verbosity."
)
parser.add_argument(
"--savefig",
action="store_true",
default=False,
help="save figures to files insted of displaying them."
)
parser.add_argument(
"--savefig",
action="store_true",
default=False,
help="save figures to files insted of displaying them."
)
parser.add_argument(
"--sim",
default="None",
help="specify simulator: Coreas vs. Zhaires"
)
"--sim",
default="None",
help="specify simulator: Coreas vs. Zhaires"
)
# retrieve argument
return parser.parse_args()



def plot_core_positions(directory, t_0_shift=False):
d_input = groot.DataDirectory(directory)

Expand Down Expand Up @@ -115,7 +119,7 @@ def plot_core_positions(directory, t_0_shift=False):
ax1.set_ylabel("Y (westing)(m)")
plt.tight_layout()
if(args.savefig):
plt.savefig(f"{directory}/CorePositions_{myrun}.png")
plt.savefig(f"{plot_dir}/plots/CorePositions_{myrun}.png")
plt.close(fig)
else:
plt.show()
Expand Down Expand Up @@ -312,7 +316,7 @@ def plot_traces_all_levels(directory, t_0_shift=False):

plt.tight_layout()
if(args.savefig):
plt.savefig(f"{directory}/IllustrateSimPipe_{run_number}_{event_number}_{du_idx}_{savelabel}.png")
plt.savefig(f"{plot_dir}/plots/IllustrateSimPipe_{run_number}_{event_number}_{du_idx}_{savelabel}.png")
plt.close(fig)
else:
plt.show()
Expand Down Expand Up @@ -449,7 +453,7 @@ def plot_time_map(directory, simulator=None):

plt.tight_layout()
if(args.savefig):
plt.savefig(f"{directory}/TimeMap_{run_number}_{event_number}.png")
plt.savefig(f"{plot_dir}/plots/TimeMap_{run_number}_{event_number}.png")
plt.close(fig)
else:
plt.show()
Expand Down Expand Up @@ -513,7 +517,7 @@ def plot_raws(directory):

plt.tight_layout()
if(args.savefig):
plt.savefig(f"{directory}/rawtrace_{du}.png")
plt.savefig(f"{plot_dir}/plots/rawtrace_{du}.png")
plt.close()
else:
plt.show()
Expand All @@ -528,7 +532,32 @@ def plot_raws(directory):
logger.info(mlg.string_begin_script())
logger.info("Creating event plots in source directory "+args.directory)

# change plot directory if specified
if args.savefig:
if args.savefig_dir is None:
# Use simulation output directory by default
plot_dir = args.directory
else:
# Use specified savefig_dir
plot_dir = args.savefig_dir
else:
pass

directory = args.directory
savefig = args.savefig
savefig_dir = args.savefig_dir

if savefig:
if savefig_dir is None:
# Use simulation output directory by default
plot_dir = directory
else:
# Use specified savefig_dir
plot_dir = savefig_dir
else:
pass


plot_core_positions(directory)
plot_time_map(directory, simulator=args.sim)
plot_traces_all_levels(directory, t_0_shift=False)
Expand Down
14 changes: 7 additions & 7 deletions sim2root/Common/sim2root.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,13 +826,13 @@ def form_directory_name(clargs, date, time, run_number, site):
extra = clargs.extra.replace("_", "-")

# Go through serial numbers in directory names to find a one that does not exist
for sn in range(1000):
for sn in range(5000):
dir_name = Path(clargs.output_parent_directory, f"sim_{site}_{date}_{time}_RUN{run_number}_CD_{extra}_{sn:0>4}")
if not dir_name.exists():
break
# If directories with serial number up to 1000 already created
# If directories with serial number up to 5000 already created
else:
print("All directories with serial number up to 1000 already exist. Please clean up some directories!")
print("All directories with serial number up to 5000 already exist. Please clean up some directories!")
exit(0)

return dir_name
Expand All @@ -843,7 +843,7 @@ def rename_all_files(clargs, path, start_event_number, end_event_number, run_num
# Go through run output files
for fn_start in ["run", "runshowersim", "runefieldsim"]:
# Go through serial numbers in directory names to find a one that does not exist
for sn in range(1000):
for sn in range(5000):
fn_in = Path(path, f"{fn_start}.root")
# Proper name of the file
fn_out = Path(path, f"{fn_start}_{run_number}_L{clargs.analysis_level}_{sn:0>4}.root")
Expand All @@ -852,7 +852,7 @@ def rename_all_files(clargs, path, start_event_number, end_event_number, run_num
fn_in.rename(fn_out)
break
else:
print(f"Could not find a free filename for {fn_in} until serial number 1000. Please clean up some files!")
print(f"Could not find a free filename for {fn_in} until serial number 5000. Please clean up some files!")
exit(0)

# Rename the event files
Expand All @@ -863,7 +863,7 @@ def rename_event_files(clargs, path, start_event_number, end_event_number):
# Go through event output files
for fn_start in ["shower", "showersim", "efield"]:
# Go through serial numbers in directory names to find a one that does not exist
for sn in range(1000):
for sn in range(5000):
fn_in = Path(path, f"{fn_start}.root")
# Proper name of the file
fn_out = Path(path, f"{fn_start}_{start_event_number}-{end_event_number}_L{clargs.analysis_level}_{sn:0>4}.root")
Expand All @@ -872,7 +872,7 @@ def rename_event_files(clargs, path, start_event_number, end_event_number):
fn_in.rename(fn_out)
break
else:
print(f"Could not find a free filename for {fn_in} until serial number 1000. Please clean up some files!")
print(f"Could not find a free filename for {fn_in} until serial number 5s000. Please clean up some files!")
exit(0)

# Simple shifting of a single x,y,z trace
Expand Down

0 comments on commit 9e5e617

Please sign in to comment.