Skip to content

Commit

Permalink
Added timestamp calculation to detsim city
Browse files Browse the repository at this point in the history
Timestamp calculation is based on rate parameter and event number.

In this commit there are all the changes:
  - Added get_rate_safe() function to detsim/sensor_utils.py. This is
    a function to get rate value safely before generate timestamp (event_number / rate).
  - Implementation of calculated timestamp into MC_hits_from_files() and get rate safely
  - Added rate variable (0.5 Hz) to detsim.conf
  - Implemented rate parameter in detsim() function and MC_hits_from_files() call inside detsim()
  - Added test for get_rate_safe function.
  - Updated output file of test_detsim_exact
  • Loading branch information
MCruces-fz committed Mar 16, 2021
1 parent b7d66a4 commit cecf8f6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
7 changes: 5 additions & 2 deletions invisible_cities/cities/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from .. core .random_sampling import NoiseSampler
from .. detsim import buffer_functions as bf
from .. detsim .sensor_utils import trigger_times
from .. detsim .sensor_utils import get_rate_safe
from .. detsim .sensor_utils import create_timestamp
from .. reco import calib_functions as cf
from .. reco import sensor_functions as sf
Expand Down Expand Up @@ -507,21 +508,23 @@ def hits_and_kdst_from_files(paths: List[str]) -> Iterator[Dict[str,Union[HitCol
timestamp = timestamp)


def MC_hits_from_files(files_in : List[str]) -> Generator:
def MC_hits_from_files(files_in : List[str], rate: float) -> Generator:
rate = get_rate_safe(rate)
for filename in files_in:
try:
hits_df = load_mchits_df(filename)
except tb.exceptions.NoSuchNodeError:
continue
for evt, hits in hits_df.groupby(level=0):
timestamp = create_timestamp(evt, rate)
yield dict(event_number = evt,
x = hits.x .values,
y = hits.y .values,
z = hits.z .values,
energy = hits.energy.values,
time = hits.time .values,
label = hits.label .values,
timestamp = 0)
timestamp = timestamp)


def sensor_data(path, wf_type):
Expand Down
4 changes: 2 additions & 2 deletions invisible_cities/cities/detsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def get_bin_edges(pmt_wfs, sipm_wfs):
@city
def detsim(*, files_in, file_out, event_range, print_mod, compression,
detector_db, run_number, s1_lighttable, s2_lighttable, sipm_psf,
buffer_params, physics_params):
buffer_params, physics_params, rate):

physics_params_ = physics_params.copy()

Expand Down Expand Up @@ -199,7 +199,7 @@ def detsim(*, files_in, file_out, event_range, print_mod, compression,
, int(buffer_params["length"] / buffer_params["sipm_width"]))

write_nohits_filter = fl.sink(event_filter_writer(h5out, "active_hits"), args=("event_number", "passed_active"))
result = fl.push(source= MC_hits_from_files(files_in),
result = fl.push(source= MC_hits_from_files(files_in, rate),
pipe = fl.pipe( fl.slice(*event_range, close_all=True)
, event_count_in.spy
, print_every(print_mod)
Expand Down
2 changes: 2 additions & 0 deletions invisible_cities/config/detsim.conf
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ compression = "ZLIB4"

# How frequently to print events
print_mod = 1

rate = 0.5 * hertz
Git LFS file not shown
22 changes: 22 additions & 0 deletions invisible_cities/detsim/sensor_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import tables as tb
import pandas as pd
import warnings

from typing import Callable
from typing import List
Expand All @@ -14,6 +15,27 @@
from .. io .mcinfo_io import get_sensor_binning


def get_rate_safe(rate: float) -> float:
"""
Get rate value safely, and if it is zero, raise a warning.
Parameters
----------
rate : float
Value of the rate.
Returns
-------
rate if rate != 0, else rate = 0.5
"""
if rate == 0:
warnings.warn("Zero rate is unphysical, using default "
"rate of 0.5 Hz instead")
return 0.5 / units.ms
else:
return rate


def create_timestamp(event_number: Union[int, float],
rate : float ) -> float:
"""
Expand Down
13 changes: 13 additions & 0 deletions invisible_cities/detsim/sensor_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from . sensor_utils import pmt_and_sipm_bin_width
from . sensor_utils import trigger_times
from . sensor_utils import create_timestamp
from . sensor_utils import get_rate_safe


def test_trigger_times():
Expand Down Expand Up @@ -115,3 +116,15 @@ def test_create_timestamp_greater_with_greater_eventnumber():
assert abs(timestamp_1) == timestamp_1
assert timestamp_1 < timestamp_2
assert abs(timestamp_2) == timestamp_2


@mark.filterwarnings("ignore:Zero rate")
def test_get_rate_safe_allways_possitive():
"""
Be sure that it woll always return the rate value such that
it is never unphysical.
"""

rate = 0
assert get_rate_safe(rate) > 0

0 comments on commit cecf8f6

Please sign in to comment.