Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
Move all simulation contexts to WFSim
Browse files Browse the repository at this point in the history
  • Loading branch information
dachengx committed Jan 15, 2024
1 parent 580ca10 commit 57124be
Show file tree
Hide file tree
Showing 3 changed files with 376 additions and 4 deletions.
88 changes: 86 additions & 2 deletions tests/test_contexts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os
import strax
import straxen
from unittest import TestCase, skipIf
from straxen.contexts import xenonnt
import wfsim
from unittest import skipIf


@skipIf(not straxen.utilix_is_configured(), 'utilix is not configured')
def test_nt_context(register=None, context=None):
Expand All @@ -15,11 +18,92 @@ def test_nt_context(register=None, context=None):
context
"""
if context is None:
context = straxen.contexts.xenonnt_simulation(cmt_run_id_sim='010000', cmt_version='global_ONLINE')
context = wfsim.contexts.xenonnt_simulation(cmt_run_id_sim='010000', cmt_version='global_ONLINE')
assert isinstance(context, strax.Context), f'{context} is not a context'

if register is not None:
assert issubclass(register, strax.Plugin), f'{register} is not a plugin'

# Search all plugins for the time field (each should have one)
context.search_field('time')


# Simulation contexts are only tested when special flags are set


@skipIf(
"ALLOW_WFSIM_TEST" not in os.environ,
"if you want test wfsim context do `export 'ALLOW_WFSIM_TEST'=1`",
)
class TestSimContextNT(TestCase):
@staticmethod
def context(*args, **kwargs):
kwargs.setdefault("cmt_version", "global_ONLINE")
return wfsim.contexts.xenonnt_simulation(*args, **kwargs)

@skipIf(not straxen.utilix_is_configured(), "No db access, cannot test!")
def test_nt_sim_context_main(self):
st = self.context(cmt_run_id_sim="008000")
st.search_field("time")

@skipIf(not straxen.utilix_is_configured(), "No db access, cannot test!")
def test_nt_sim_context_alt(self):
"""Some examples of how to run with a custom WFSim context."""
self.context(cmt_run_id_sim="008000", cmt_run_id_proc="008001")
self.context(cmt_run_id_sim="008000", cmt_option_overwrite_sim={"elife": 1e6})

self.context(cmt_run_id_sim="008000", overwrite_fax_file_sim={"elife": 1e6})

@skipIf(not straxen.utilix_is_configured(), "No db access, cannot test!")
def test_nt_diverging_context_options(self):
"""Test diverging options. Idea is that you can use different settings for processing and
generating data, should have been handled by RawRecordsFromWFsim but is now hacked into the
xenonnt_simulation context.
Just to show how convoluted this syntax for the
xenonnt_simulation context / CMT is...
"""
self.context(
cmt_run_id_sim="008000",
cmt_option_overwrite_sim={"elife": ("elife_constant", 1e6, True)},
cmt_option_overwrite_proc={"elife": ("elife_constant", 1e5, True)},
overwrite_from_fax_file_proc=True,
overwrite_from_fax_file_sim=True,
_config_overlap={"electron_lifetime_liquid": "elife"},
)

def test_nt_sim_context_bad_inits(self):
with self.assertRaises(RuntimeError):
self.context(
cmt_run_id_sim=None,
cmt_run_id_proc=None,
)


@skipIf(
"ALLOW_WFSIM_TEST" not in os.environ,
"if you want test wfsim context do `export 'ALLOW_WFSIM_TEST'=1`",
)
def test_sim_context():
straxen.contexts.xenon1t_simulation()


@skipIf(not straxen.utilix_is_configured(), "No db access, cannot test!")
@skipIf(
"ALLOW_WFSIM_TEST" not in os.environ,
"if you want test wfsim context do `export 'ALLOW_WFSIM_TEST'=1`",
)
def test_sim_offline_context():
wfsim.contexts.xenonnt_simulation_offline(
run_id="026000",
global_version="global_v11",
fax_config="fax_config_nt_sr0_v4.json",
)


@skipIf(not straxen.utilix_is_configured(), "No db access, cannot test!")
def test_offline():
st = xenonnt("latest")
st.provided_dtypes()

4 changes: 2 additions & 2 deletions tests/test_wfsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_sim_nt_advanced(
with tempfile.TemporaryDirectory() as tempdir:
log.debug(f'Working in {tempdir}')

st = straxen.contexts.xenonnt_simulation(cmt_run_id_sim='010000',
st = wfsim.contexts.xenonnt_simulation(cmt_run_id_sim='010000',
cmt_version='global_ONLINE',
fax_config='fax_config_nt_sr0_v0.json',
_config_overlap={},)
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_sim_mc_chain():
url_data = requests.get(test_g4).content
with open('test.root', mode='wb') as f:
f.write(url_data)
st = straxen.contexts.xenonnt_simulation(cmt_run_id_sim='010000',
st = wfsim.contexts.xenonnt_simulation(cmt_run_id_sim='010000',
cmt_version='global_ONLINE',
_config_overlap={},)
st.set_config(dict(gain_model_nv="legacy-to-pe://adc_nv",
Expand Down
Loading

0 comments on commit 57124be

Please sign in to comment.