Skip to content

Commit

Permalink
Cleanup and refactor configurations (#163)
Browse files Browse the repository at this point in the history
* Clean up help for configs

* Refactor configs

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Propagate to documentation

* Propagate to documentation

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove outdated comment

* Correct creaproc definition

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix whitespace

* Remove redundant double quotes

* Fix microphysics context to include simulation config file

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove drift velocity config from S2PhotonPropagation.rst

---------

Co-authored-by: mhliu0001 <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henning Schulze Eißing <[email protected]>
  • Loading branch information
4 people authored Mar 20, 2024
1 parent cc84886 commit 3c9a203
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 90 deletions.
12 changes: 10 additions & 2 deletions docs/source/plugins/detector_physics/ElectronDrift.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,18 @@ Config Options
-
- True
- Electron lifetime in liquid xenon [ns]
* - enable_field_dependencies
* - enable_drift_velocity_map
-
- True
- Field dependencies during electron drift
- Enable drift velocity map from field_dependencies_map_tmp
* - norm_drift_velocity
-
- True
- Enable normalization of drif velocity map with drift_velocity_liquid
* - enable_diffusion_longitudinal_map
-
- True
- Enable longitudinal diffusion map from field_dependencies_map_tmp
* - tpc_length
-
- True
Expand Down
8 changes: 2 additions & 6 deletions docs/source/plugins/detector_physics/S2PhotonPropagation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ S2PhotonPropagationBase plugin
- "gas"
- True
- phase of the s2 producing region
* - drift_velocity_liquid
-
- True
- Drift velocity of electrons in the liquid xenon [cm/ns]
* - tpc_length
-
- True
Expand All @@ -141,10 +137,10 @@ S2PhotonPropagationBase plugin
-
- True
- Width of the S2 area fraction top
* - enable_field_dependencies
* - enable_diffusion_transverse_map
-
- True
- Field dependencies during electron drift
- Enable transverse diffusion map from field_dependencies_map_tmp
* - s2_mean_area_fraction_top
-
- True
Expand Down
2 changes: 1 addition & 1 deletion docs/source/plugins/micro_physics/ChunkInput.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Provided Columns
- Geant4 process creating the particle
* - edproc
- <U25
- Geant4 process destroying the particle
- Geant4 process responsible for the energy deposit
* - evtid
- int32
- Geant4 event ID
Expand Down
10 changes: 7 additions & 3 deletions fuse/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
]


def microphysics_context(output_folder="./fuse_data"):
def microphysics_context(
output_folder="./fuse_data", simulation_config_file="fuse_config_nt_sr1_dev.json"
):
"""Function to create a fuse microphysics simulation context."""

st = strax.Context(
Expand All @@ -75,14 +77,16 @@ def microphysics_context(output_folder="./fuse_data"):
for plugin in microphysics_plugins:
st.register(plugin)

set_simulation_config_file(st, simulation_config_file)

return st


def full_chain_context(
output_folder="./fuse_data",
corrections_version=None,
simulation_config_file="fax_config_nt_design.json",
corrections_run_id="026000",
simulation_config_file="fuse_config_nt_sr1_dev.json",
corrections_run_id="046477",
run_id_specific_config={
"gain_model_mc": "gain_model",
"electron_lifetime_liquid": "elife",
Expand Down
59 changes: 43 additions & 16 deletions fuse/plugins/detector_physics/electron_drift.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,40 @@ class ElectronDrift(FuseBasePlugin):
help="Electron lifetime in liquid xenon [ns]",
)

enable_field_dependencies = straxen.URLConfig(
enable_survival_probability_map = straxen.URLConfig(
default="take://resource://"
"SIMULATION_CONFIG_FILE.json?&fmt=json"
"&take=enable_field_dependencies",
"&take=enable_survival_probability_map",
type=bool,
cache=True,
help="Field dependencies during electron drift",
help="Use survival probability map from field_dependencies_map_tmp",
)

enable_drift_velocity_map = straxen.URLConfig(
default="take://resource://"
"SIMULATION_CONFIG_FILE.json?&fmt=json"
"&take=enable_drift_velocity_map",
type=bool,
cache=True,
help="Use drift velocity map from field_dependencies_map_tmp",
)

norm_drift_velocity = straxen.URLConfig(
default="take://resource://"
"SIMULATION_CONFIG_FILE.json?&fmt=json"
"&take=norm_drift_velocity",
type=bool,
cache=True,
help="Normalize drift velocity map according to value defined by drift_velocity_liquid",
)

enable_diffusion_longitudinal_map = straxen.URLConfig(
default="take://resource://"
"SIMULATION_CONFIG_FILE.json?&fmt=json"
"&take=enable_diffusion_longitudinal_map",
type=bool,
cache=True,
help="Use diffusion longitudinal map from field_dependencies_map_tmp",
)

tpc_length = straxen.URLConfig(
Expand Down Expand Up @@ -143,22 +171,21 @@ class ElectronDrift(FuseBasePlugin):
def setup(self):
super().setup()

# Can i do this scaling in the url config?
# Can this be done via URL config?
if self.field_distortion_model == "inverse_fdc":
self.fdc_map_fuse.scale_coordinates([1.0, 1.0, -self.drift_velocity_liquid])

# Field dependencies
if any(self.enable_field_dependencies.values()):
if self.enable_drift_velocity_map:
self.drift_velocity_scaling = 1.0
# Calculating drift velocity scaling to match total drift time
# for R = 0 between cathode and gate
if "norm_drift_velocity" in self.enable_field_dependencies.keys():
if self.enable_field_dependencies["norm_drift_velocity"]:
norm_dvel = self.field_dependencies_map_tmp(
np.array([[0], [-self.tpc_length]]).T, map_name="drift_speed_map"
)[0]
norm_dvel *= 1e-4
self.drift_velocity_scaling = self.drift_velocity_liquid / norm_dvel
if self.norm_drift_velocity:
norm_dvel = self.field_dependencies_map_tmp(
np.array([[0], [-self.tpc_length]]).T, map_name="drift_speed_map"
)[0]
norm_dvel *= 1e-4
self.drift_velocity_scaling = self.drift_velocity_liquid / norm_dvel

def rz_map(z, xy, **kwargs):
r = np.sqrt(xy[:, 0] ** 2 + xy[:, 1] ** 2)
Expand All @@ -168,7 +195,7 @@ def rz_map(z, xy, **kwargs):

# Data-driven longitudinal diffusion map
# TODO: Change to the best way to accommodate simulation/data-driven map
if self.enable_field_dependencies["diffusion_longitudinal_map"]:
if self.enable_diffusion_longitudinal_map:

def _rz_map(z, xy, **kwargs):
r = np.sqrt(xy[:, 0] ** 2 + xy[:, 1] ** 2)
Expand Down Expand Up @@ -282,7 +309,7 @@ def field_distortion_comsol(self, x, y, z):
return z, positions

def in_charge_sensitive_volume(self, xy_int, z_int):
if self.enable_field_dependencies["survival_probability_map"]:
if self.enable_survival_probability_map:
p_surv = self.field_dependencies_map(z_int, xy_int, map_name="survival_probability_map")
p_surv = np.clip(p_surv, a_min=0, a_max=1)

Expand All @@ -301,7 +328,7 @@ def get_s2_drift_time_params(self, xy_int, z_int):
returns two arrays of floats (mean drift time, drift time spread)
"""
drift_velocity_liquid = self.get_avg_drift_velocity(z_int, xy_int)
if self.enable_field_dependencies["diffusion_longitudinal_map"]:
if self.enable_diffusion_longitudinal_map:
diffusion_constant_longitudinal = self.diffusion_longitudinal_map(
z_int, xy_int
) # cm²/ns
Expand All @@ -323,7 +350,7 @@ def get_avg_drift_velocity(self, z, xy):
array of floats corresponding to average drift velocities
from given point to the gate.
"""
if self.enable_field_dependencies["drift_speed_map"]:
if self.enable_drift_velocity_map:
drift_v_LXe = self.field_dependencies_map(z, xy, map_name="drift_speed_map") # mm/µs
drift_v_LXe *= 1e-4 # cm/ns
drift_v_LXe *= self.drift_velocity_scaling
Expand Down
2 changes: 1 addition & 1 deletion fuse/plugins/detector_physics/s1_photon_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class S1PhotonPropagation(S1PhotonPropagationBase):
"&fmt=json.gz"
"&method=RegularGridInterpolator",
cache=True,
help="Spline for the optical propagation",
help="Spline for the optical propagation of S1 signals",
)

def setup(self):
Expand Down
58 changes: 19 additions & 39 deletions fuse/plugins/detector_physics/s2_photon_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,6 @@ class S2PhotonPropagationBase(FuseBaseDownChunkingPlugin):
help="Phase of the S2 producing region",
)

drift_velocity_liquid = straxen.URLConfig(
default="take://resource://"
"SIMULATION_CONFIG_FILE.json?&fmt=json"
"&take=drift_velocity_liquid",
type=(int, float),
cache=True,
help="Drift velocity of electrons in the liquid xenon [cm/ns]",
)

tpc_length = straxen.URLConfig(
default="take://resource://SIMULATION_CONFIG_FILE.json?&fmt=json&take=tpc_length",
type=(int, float),
Expand Down Expand Up @@ -188,12 +179,24 @@ class S2PhotonPropagationBase(FuseBaseDownChunkingPlugin):
help="Width of the S2 area fraction top",
)

enable_field_dependencies = straxen.URLConfig(
enable_diffusion_transverse_map = straxen.URLConfig(
default="take://resource://"
"SIMULATION_CONFIG_FILE.json?&fmt=json"
"&take=enable_field_dependencies",
"&take=enable_diffusion_transverse_map",
type=bool,
cache=True,
help="Use transverse diffusion map from field_dependencies_map_tmp",
)

# stupid naming problem...
field_dependencies_map_tmp = straxen.URLConfig(
default="itp_map://resource://simulation_config://"
"SIMULATION_CONFIG_FILE.json?"
"&key=field_dependencies_map"
"&fmt=json.gz"
"&method=WeightedNearestNeighbors",
cache=True,
help="Field dependencies during electron drift",
help="Map for the electric field dependencies",
)

s2_mean_area_fraction_top = straxen.URLConfig(
Expand All @@ -218,17 +221,6 @@ class S2PhotonPropagationBase(FuseBaseDownChunkingPlugin):
help="S2 pattern map",
)

# stupid naming problem...
field_dependencies_map_tmp = straxen.URLConfig(
default="itp_map://resource://simulation_config://"
"SIMULATION_CONFIG_FILE.json?"
"&key=field_dependencies_map"
"&fmt=json.gz"
"&method=WeightedNearestNeighbors",
cache=True,
help="Map for the electric field dependencies",
)

singlet_fraction_gas = straxen.URLConfig(
default="take://resource://"
"SIMULATION_CONFIG_FILE.json?&fmt=json"
Expand Down Expand Up @@ -317,19 +309,8 @@ def setup(self):
self.photon_area_distribution
)

# Move this part into a nice URLConfig protocol?
# Field dependencies
if any(self.enable_field_dependencies.values()):
self.drift_velocity_scaling = 1.0
# calculating drift velocity scaling to match total drift time
# for R = 0 between cathode and gate
if "norm_drift_velocity" in self.enable_field_dependencies.keys():
if self.enable_field_dependencies["norm_drift_velocity"]:
norm_dvel = self.field_dependencies_map_tmp(
np.array([[0], [-self.tpc_length]]).T, map_name="drift_speed_map"
)[0]
norm_dvel *= 1e-4
self.drift_velocity_scaling = self.drift_velocity_liquid / norm_dvel
if self.enable_diffusion_transverse_map:

def rz_map(z, xy, **kwargs):
r = np.sqrt(xy[:, 0] ** 2 + xy[:, 1] ** 2)
Expand Down Expand Up @@ -573,7 +554,7 @@ def s2_pattern_map_diffuse(self, n_electron, z, xy, drift_time_mean):
"""
assert all(z < 0), "All S2 in liquid should have z < 0"

if self.enable_field_dependencies["diffusion_transverse_map"]:
if self.enable_diffusion_transverse_map:
diffusion_constant_radial = self.field_dependencies_map(
z, xy, map_name="diffusion_radial_map"
) # cm²/s
Expand All @@ -583,7 +564,6 @@ def s2_pattern_map_diffuse(self, n_electron, z, xy, drift_time_mean):
diffusion_constant_radial *= 1e-9 # cm²/ns
diffusion_constant_azimuthal *= 1e-9 # cm²/ns
else:
# diffusion_constant_transverse = diffusion_constant_transverse
diffusion_constant_radial = self.diffusion_constant_transverse
diffusion_constant_azimuthal = self.diffusion_constant_transverse

Expand Down Expand Up @@ -784,7 +764,7 @@ class S2PhotonPropagationSimple(S2PhotonPropagationBase):
default="take://resource://"
"SIMULATION_CONFIG_FILE.json?&fmt=json"
"&take=enable_gas_gap_warping",
type=(int, float),
type=bool,
cache=True,
help="enable_gas_gap_warping",
)
Expand Down Expand Up @@ -857,7 +837,7 @@ class S2PhotonPropagationSimple(S2PhotonPropagationBase):
"&fmt=json.gz"
"&method=RegularGridInterpolator",
cache=True,
help="s2_optical_propagation_spline",
help="Spline for the optical propagation of S2 signals",
)

def setup(self):
Expand Down
2 changes: 1 addition & 1 deletion fuse/plugins/detector_physics/secondary_scintillation.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class SecondaryScintillation(FuseBasePlugin):
"&key=se_gain_map"
"&fmt=json",
cache=True,
help="Map of the single electron gain ",
help="Map of the single electron gain",
)

s2_correction_map = straxen.URLConfig(
Expand Down
6 changes: 3 additions & 3 deletions fuse/plugins/micro_physics/electric_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class ElectricField(FuseBasePlugin):
]

# Config options
# Field map not yet in simulation config file!
efield_map = straxen.URLConfig(
default="itp_map://resource://"
"fieldmap_2D_B2d75n_C2d75n_G0d3p_A4d9p_T0d9n_PMTs1d3n_FSR0d65p_QPTFE_0d5n_0d4p.json.gz?"
default="itp_map://resource://simulation_config://"
"SIMULATION_CONFIG_FILE.json?"
"&key=efield_map"
"&fmt=json.gz"
"&method=RegularGridInterpolator",
cache=True,
Expand Down
4 changes: 2 additions & 2 deletions fuse/plugins/micro_physics/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ChunkInput(FuseBasePlugin):
(("Particle type of the parent particle", "parenttype"), "<U18"),
(("Trackid of the parent particle", "parentid"), np.int16),
(("Geant4 process creating the particle", "creaproc"), "<U25"),
(("Geant4 process destroying the particle", "edproc"), "<U25"),
(("Geant4 process responsible for the energy deposit", "edproc"), "<U25"),
(("Geant4 event ID", "evtid"), np.int32),
(("x position of the primary particle", "x_pri"), np.float32),
(("y position of the primary particle", "y_pri"), np.float32),
Expand Down Expand Up @@ -244,7 +244,7 @@ def __init__(
(("Particle type of the parent particle", "parenttype"), "<U18"),
(("Trackid of the parent particle", "parentid"), np.int16),
(("Geant4 process creating the particle", "creaproc"), "<U25"),
(("Geant4 process destroying the particle", "edproc"), "<U25"),
(("Geant4 process responsible for the energy deposit", "edproc"), "<U25"),
(("Geant4 event ID", "evtid"), np.int32),
(("x position of the primary particle", "x_pri"), np.float32),
(("y position of the primary particle", "y_pri"), np.float32),
Expand Down
3 changes: 2 additions & 1 deletion fuse/plugins/micro_physics/merge_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class MergeCluster(FuseBasePlugin):

# Config options
tag_cluster_by = straxen.URLConfig(
default="energy",
default="take://resource://SIMULATION_CONFIG_FILE.json?fmt=json&take=tag_cluster_by",
cache=True,
help="Decide if you tag the cluster "
"according to first interaction (time) or most energetic (energy) one.",
)
Expand Down
4 changes: 2 additions & 2 deletions fuse/plugins/pmt_and_daq/pmt_afterpulses.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class PMTAfterPulses(FuseBasePlugin):
"&take=pmt_circuit_load_resistor",
type=(int, float),
cache=True,
help="PMT circuit load resistor",
help="PMT circuit load resistor [kg m^2/(s^3 A)]",
)

digitizer_bits = straxen.URLConfig(
Expand All @@ -88,7 +88,7 @@ class PMTAfterPulses(FuseBasePlugin):
"&take=digitizer_voltage_range",
type=(int, float),
cache=True,
help="Voltage range of the digitizer boards",
help="Voltage range of the digitizer boards [V]",
)

gain_model_mc = straxen.URLConfig(
Expand Down
Loading

0 comments on commit 3c9a203

Please sign in to comment.