Skip to content

Commit

Permalink
Rename and comment function
Browse files Browse the repository at this point in the history
  • Loading branch information
gondiaz committed Dec 10, 2021
1 parent 6a7b18f commit 5388c53
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
4 changes: 2 additions & 2 deletions bin/event_mixer
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import argparse

from invisible_cities.core.configure import read_config_file

from invisible_cities.evm.mixer import Event_Mixer, get_nevents_from_db
from invisible_cities.evm.mixer import Event_Mixer, get_mixer_nevents


# parse the config filename
Expand All @@ -28,7 +28,7 @@ if __name__ == "__main__":
nevents_per_file = conf.get("nevents_per_file")

# get event df with (g4volume, isotope, nevts)
nevent_df = get_nevents_from_db(detector_db, isotopes, exposure)
nevent_df = get_mixer_nevents(exposure, detector_db, isotopes)

if "0nubb" in isotopes:
nevts = 1 # TO-DO: compute from T1/2
Expand Down
48 changes: 31 additions & 17 deletions invisible_cities/evm/mixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,40 +199,54 @@ def _concat_data(self):
return


def get_nevents_from_db(detector_db : str, isotopes : list, exposure : float):

def get_mixer_nevents(exposure : float, detector_db : str = "next100", isotopes : list = "all"):
'''
This function computes the number of events of each component (isotope, volume) pairs
based on the activity-assumptions provided in the database.
Parameters:
----------
:exposure: exposure time
:detector_db: detector database
:isotopes: (default "all") list with the isotopes to simulate,
ignores signal-like events "0nubb" and "2nubb"
'''

# get activities and efficiencies from database
act, eff = RadioactivityData(detector_db)

# warn about missing isotope
act_in = np.isin(isotopes, act.Isotope.unique())
eff_in = np.isin(isotopes, eff.Isotope.unique())
missing = ~(act_in | eff_in)
# if a list of isotopes is provided, warn missing and select them
if not (isotopes == "all"):
# warn about missing isotopes in the database
act_in = np.isin(isotopes, act.Isotope.unique())
eff_in = np.isin(isotopes, eff.Isotope.unique())
missing = ~(act_in | eff_in)

if missing.any():
isos = [iso for b, iso in zip(missing, isotopes) if b and (iso not in ["0nubb", "2nubb"])]
if len(isos)>0:
msg = f"Missing database isotopes: {isos}"
warnings.warn(msg)
if missing.any():
isos = [iso for b, iso in zip(missing, isotopes) if b and (iso not in ["0nubb", "2nubb"])]
if len(isos)>0:
msg = f"Missing database isotopes: {isos}"
warnings.warn(msg)

# select requested isotopes
act = act[act.Isotope.isin(isotopes)]
eff = eff[eff.Isotope.isin(isotopes)]
# select requested isotopes
act = act[act.Isotope.isin(isotopes)]
eff = eff[eff.Isotope.isin(isotopes)]

# warn about missing components
act_uniq = act.value_counts(subset=["G4Volume", "Isotope"]).index
eff_uniq = eff.value_counts(subset=["G4Volume", "Isotope"]).index

act_in = act_uniq.isin(eff_uniq)
if not act_in.all():
msg = f"Fowolling components missing at Efficiency table: {str(act_uniq[act_in].to_list())}"
msg = f"Components missing at Efficiency table: {str(act_uniq[act_in].to_list())}"
warnings.warn(msg)

eff_in = eff_uniq.isin(act_uniq)
if not eff_in.all():
msg = f"Fowolling components missing at Activity table: {str(eff_uniq[eff_in].to_list())}"
msg = f"Components missing at Activity table: {str(eff_uniq[eff_in].to_list())}"
warnings.warn(msg)

# create nevents df
# create nevents df and return it
df = pd.merge(act, eff, on=["G4Volume", "Isotope"])
df.loc[:, "nevts"] = df.TotalActivity * df.MCEfficiency * exposure
df = df.drop(columns=["TotalActivity", "MCEfficiency"])
Expand Down
6 changes: 3 additions & 3 deletions invisible_cities/evm/mixer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from .mixer import get_file_number
from .mixer import Event_Mixer
from .mixer import get_nevents_from_db
from .mixer import get_mixer_nevents


def test_Event_Mixer_writes_all_tables(ICDATADIR, output_tmpdir):
Expand Down Expand Up @@ -76,13 +76,13 @@ def test_Event_Mixer_nevents(ICDATADIR, output_tmpdir):
pd.testing.assert_frame_equal(df, n)


def test_get_nevents_from_db():
def test_get_mixer_nevents():

detector_db = "next100"
isotopes = ["Bi214", "Co60"]
exposure = 1 # dummy

got = get_nevents_from_db(detector_db, isotopes, exposure)
got = get_mixer_nevents(exposure, detector_db, isotopes)
got = got.set_index(["G4Volume", "Isotope"]).nevts

act, eff = RadioactivityData(detector_db)
Expand Down

0 comments on commit 5388c53

Please sign in to comment.