Skip to content

Commit

Permalink
Script to compute NSB-tuning parameter for MC at the waveforms level
Browse files Browse the repository at this point in the history
  • Loading branch information
moralejo committed Jan 30, 2024
1 parent cd5f92f commit 7700c80
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions lstchain/scripts/lstchain_tune_nsb_waveform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env python3

"""
Usage:
$> python lstchain_tune_nsb_waveform.py
--config config_file.json (must be the one used in the DL1 production)
--input-mc simtel_file.simtel.gz simulation simtel file
--input-data dl1_data.h5 real data DL1 file
Calculates the parameters needed to tune the NSB in the waveforms (in the
R0 to DL1 stage) to the level of NSB in a given data file
"""

import argparse
import json
import logging
import sys
import numpy as np
from pathlib import Path

from lstchain.image.modifier import calculate_required_additional_nsb
from lstchain.io.config import dump_config, read_configuration_file
from traitlets.config import Config

log = logging.getLogger(__name__)

parser = argparse.ArgumentParser(description="Tune NSB in waveforms")

# Required arguments
parser.add_argument(
'--config', type=Path,
help='Path to the configuration file for the production (must be the '
'one used for calibration and DL1 creation)',
required=True,
)

parser.add_argument(
'--input-mc', type=Path,
help='Path to a simtel file of the production (must include the true '
'p.e. images)',
required=True,
)

parser.add_argument(
'--input-data', type=Path,
help='Path to a data DL1 file of the production (must include DL1a)',
required=True,
)

parser.add_argument(
'--output-file', '-o',
type=Path,
help='Path to a output file where to dump the update config',
)

parser.add_argument(
'--overwrite',
action='store_true',
help='Use to overwrite output-file',
)


def main():
args = parser.parse_args()

if not args.config.is_file():
log.error('Config file does not exist or is not a file')
sys.exit(1)
if not args.input_mc.is_file():
log.error('MC simtel file does not exist or is not a file')
sys.exit(1)
if not args.input_data.is_file():
log.error('DL1 data file does not exist or is not a file')
sys.exit(1)

Check warning on line 76 in lstchain/scripts/lstchain_tune_nsb_waveform.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_tune_nsb_waveform.py#L68-L76

Added lines #L68 - L76 were not covered by tests

log.setLevel(logging.INFO)
handler = logging.StreamHandler()
logging.getLogger().addHandler(handler)

Check warning on line 80 in lstchain/scripts/lstchain_tune_nsb_waveform.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_tune_nsb_waveform.py#L78-L80

Added lines #L78 - L80 were not covered by tests

config = read_configuration_file(args.config)

Check warning on line 82 in lstchain/scripts/lstchain_tune_nsb_waveform.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_tune_nsb_waveform.py#L82

Added line #L82 was not covered by tests

nsb_correction_ratio, data_ped_variance, mc_ped_variance = \

Check warning on line 84 in lstchain/scripts/lstchain_tune_nsb_waveform.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_tune_nsb_waveform.py#L84

Added line #L84 was not covered by tests
calculate_required_additional_nsb(args.input_mc, args.input_data,
config=Config(config))

dict_nsb = {

Check warning on line 88 in lstchain/scripts/lstchain_tune_nsb_waveform.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_tune_nsb_waveform.py#L88

Added line #L88 was not covered by tests
"nsb_tuning": True,
"nsb_tuning_ratio": np.round(nsb_correction_ratio, decimals=2),
"spe_location": "lstchain/data/SinglePhE_ResponseInPhE_expo2Gaus.dat"
}

log.info(f'\ndata_ped_stdev: {data_ped_variance**0.5:.3f} p.e.')
log.info(f'mc_ped_stdev: {mc_ped_variance**0.5:.3f} p.e.\n')

Check warning on line 95 in lstchain/scripts/lstchain_tune_nsb_waveform.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_tune_nsb_waveform.py#L94-L95

Added lines #L94 - L95 were not covered by tests

log.info(json.dumps(dict_nsb, indent=2))
log.info('\n')

Check warning on line 98 in lstchain/scripts/lstchain_tune_nsb_waveform.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_tune_nsb_waveform.py#L97-L98

Added lines #L97 - L98 were not covered by tests

if args.output_file:
cfg = read_configuration_file(args.config)
cfg['waveform_nsb_tuning'].update(dict_nsb)
dump_config(cfg, args.output_file, overwrite=args.overwrite)

Check warning on line 103 in lstchain/scripts/lstchain_tune_nsb_waveform.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_tune_nsb_waveform.py#L100-L103

Added lines #L100 - L103 were not covered by tests


if __name__ == '__main__':
main()

Check warning on line 107 in lstchain/scripts/lstchain_tune_nsb_waveform.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_tune_nsb_waveform.py#L107

Added line #L107 was not covered by tests

0 comments on commit 7700c80

Please sign in to comment.