Skip to content

Commit

Permalink
Proper use of factory function for detector class in simulation.py. M…
Browse files Browse the repository at this point in the history
…odify interface of simulation.py
  • Loading branch information
fschlueter committed Dec 20, 2023
1 parent 8792e89 commit 4c07621
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
31 changes: 17 additions & 14 deletions NuRadioMC/simulation/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class simulation:
def __init__(self, inputfilename,
outputfilename,
detectorfile,
det=None,
det_kwargs={},
outputfilenameNuRadioReco=None,
debug=False,
evt_time=datetime.datetime(2018, 1, 1),
Expand All @@ -124,6 +126,10 @@ def __init__(self, inputfilename,
specify hdf5 output filename.
detectorfile: string
path to the json file containing the detector description
det: detector object
Pass a detector class object
det_kwargs: dict
Pass arguments to the detector (only used when det == None)
station_id: int
the station id for which the simulation is performed. Must match a station
deself._fined in the detector description
Expand Down Expand Up @@ -157,8 +163,9 @@ def __init__(self, inputfilename,
allows to specify a custom ice model. This model is used if the config file specifies the ice model as "custom".
"""
logger.setLevel(log_level)
if 'write_mode' in kwargs.keys():
if 'write_mode' in kwargs:
logger.warning('Parameter write_mode is deprecated. Define the output format in the config file instead.')

self._log_level = log_level
self._log_level_ray_propagation = log_level_propagation
config_file_default = os.path.join(os.path.dirname(__file__), 'config_default.yaml')
Expand Down Expand Up @@ -212,19 +219,15 @@ def __init__(self, inputfilename,
self._mout_groups = collections.OrderedDict()
self._mout_attrs = collections.OrderedDict()

# read in detector positions
logger.status("Detectorfile {}".format(os.path.abspath(self._detectorfile)))
self._det = None
if default_detector_station is not None:
logger.warning(
'Deprecation warning: Passing the default detector station is deprecated. Default stations and default'
'channel should be specified in the detector description directly.'
)
logger.status(f"Default detector station provided (station {default_detector_station}) -> Using generic detector")
self._det = gdetector.GenericDetector(json_filename=self._detectorfile, default_station=default_detector_station,
default_channel=default_detector_channel, antenna_by_depth=False)
# Initialize detector
if det is None:
logger.status("Detectorfile {}".format(os.path.abspath(self._detectorfile)))
kwargs = dict(json_filename=self._detectorfile, default_station=default_detector_station,
default_channel=default_detector_channel, antenna_by_depth=False)
kwargs.update(det_kwargs)
self._det = detector.Detector(**kwargs)
else:
self._det = detector.Detector(json_filename=self._detectorfile, antenna_by_depth=False)
self._det = det

self._det.update(evt_time)

Expand Down Expand Up @@ -851,7 +854,7 @@ def run(self):
self._station = NuRadioReco.framework.station.Station(self._station_id)
self._station.set_sim_station(self._sim_station)
self._station.get_sim_station().set_station_time(self._evt_time)

# convert efields to voltages at digitizer
if hasattr(self, '_detector_simulation_part1'):
# we give the user the opportunity to define a custom detector simulation
Expand Down
20 changes: 17 additions & 3 deletions NuRadioReco/detector/detector.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import json
import os
import numpy as np
import logging

import NuRadioReco.detector.detector_base
import NuRadioReco.detector.generic_detector
import NuRadioReco.detector.RNO_G.rnog_detector
import json
import os



def find_path(name):
Expand Down Expand Up @@ -137,7 +141,17 @@ def Detector(*args, **kwargs):
if source == 'json':
f.close()

if has_reference_entry:
has_default = np.any([arg in kwargs and kwargs[arg] is not None for arg in ["default_station", "default_channel", "default_device"]])

if has_reference_entry or has_default:
if has_default:
logging.warning(
'Deprecation warning: Passing the default detector station is deprecated. Default stations and default'
'channel should be specified in the detector description directly.')

if "default_station" in kwargs:
logging.status(f'Default detector station provided (station {kwargs["default_station"]}) -> Using generic detector')

return NuRadioReco.detector.generic_detector.GenericDetector(
json_filename=filename, source=source, dictionary=dictionary,
assume_inf=assume_inf, antenna_by_depth=antenna_by_depth, **kwargs)
Expand Down

0 comments on commit 4c07621

Please sign in to comment.