-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add muon analysis on calibrated data #50
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
magicctapipe/scripts/lst1_magic/muon_analysis_LST_or_MAGIC_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
#!/usr/bin/env python | ||
# coding: utf-8 | ||
|
||
""" | ||
Author: Gabriel Emery | ||
|
||
This script processes MAGIC calibrated data (*_Y_*.root) to perform the muon ring analysis. | ||
Current naming allows LST but first implementation will only handle MAGIC as script for LST are available in lstchain. | ||
|
||
Usage: | ||
$ python muon_analysis_LST_or_MAGIC_data.py | ||
--input-file ./data/calibrated/20201216_M1_05093711.001_Y_CrabNebula-W0.40+035.root | ||
--output-dir ./data/muons | ||
--config-file ./config.yaml | ||
""" | ||
import argparse | ||
import yaml | ||
import logging | ||
import numpy as np | ||
from pathlib import Path | ||
|
||
from astropy.table import Table | ||
from ctapipe.io import EventSource | ||
from lstchain.image.muon import create_muon_table | ||
from magicctapipe.image import MAGICClean | ||
from magicctapipe.image.muons import perform_muon_analysis | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.addHandler(logging.StreamHandler()) | ||
logger.setLevel(logging.INFO) | ||
|
||
pedestal_types = [ | ||
'fundamental', | ||
'from_extractor', | ||
'from_extractor_rndm', | ||
] | ||
|
||
|
||
def magic_muons_from_cal(input_file, output_dir, config, process_run): | ||
""" | ||
Process all event a single telescope MAGIC calibrated data run or subrun to perform the muon ring analysis. | ||
|
||
Parameters | ||
---------- | ||
input_file: | ||
output_dir: | ||
config: | ||
process_run: | ||
|
||
""" | ||
|
||
event_source = EventSource(input_url=input_file) | ||
subarray = event_source.subarray | ||
obs_id = event_source.obs_ids[0] | ||
tel_id = event_source.telescope | ||
|
||
# Create the table which will contain the selected muon ring parameters | ||
muon_parameters = create_muon_table() | ||
muon_parameters['telescope_name'] = [] | ||
|
||
logger.info(f'\nProcess the following data (process_run = {process_run}):') | ||
|
||
# Configure the MAGIC cleaning: | ||
config_cleaning = config['MAGIC']['magic_clean'] | ||
|
||
if config_cleaning['find_hotpixels'] == 'auto': | ||
config_cleaning.update({'find_hotpixels': True}) | ||
|
||
logger.info('\nConfiguration for the image cleaning:') | ||
logger.info(config_cleaning) | ||
|
||
ped_type = config_cleaning.pop('pedestal_type') | ||
i_ped_type = np.where(np.array(pedestal_types) == ped_type)[0][0] | ||
|
||
camera_geom = event_source.subarray.tel[tel_id].camera.geometry | ||
magic_clean = MAGICClean(camera_geom, config_cleaning) | ||
|
||
# Prepare for saving muons data to an output file: | ||
Path(output_dir).mkdir(exist_ok=True, parents=True) | ||
|
||
if process_run: | ||
output_file = f'{output_dir}/muons_M{tel_id}.Run{obs_id:08}.fits' | ||
else: | ||
subrun_id = event_source.metadata['subrun_number'] | ||
output_file = f'{output_dir}/muons_M{tel_id}.Run{obs_id:08}.{subrun_id:03}.fits' | ||
|
||
# Start processing events: | ||
logger.info('\nProcessing the events:') | ||
|
||
# load muon analysis config | ||
muon_config = {} | ||
if 'muon_ring' in config['MAGIC']: | ||
muon_config = config['MAGIC']['muon_ring'] | ||
# Select the telescope name to be filed in muon_parameters['telescope_name'] | ||
if tel_id == 1: | ||
tel_name = f'MAGIC-I' | ||
elif tel_id == 2: | ||
tel_name = f'MAGIC-II' | ||
else: | ||
tel_name = f'MAGIC_?' | ||
|
||
for event in event_source: | ||
|
||
# Apply the image cleaning: | ||
dead_pixels = event.mon.tel[tel_id].pixel_status.hardware_failing_pixels[0] | ||
try: | ||
badrms_pixels = event.mon.tel[tel_id].pixel_status.pedestal_failing_pixels[i_ped_type] | ||
except TypeError: | ||
badrms_pixels = np.zeros(dead_pixels.shape, dtype=bool) | ||
unsuitable_mask = np.logical_or(dead_pixels, badrms_pixels) | ||
|
||
signal_pixels, image, peak_time = magic_clean.clean_image( | ||
event.dl1.tel[tel_id].image, event.dl1.tel[tel_id].peak_time, unsuitable_mask, | ||
) | ||
perform_muon_analysis(muon_parameters, | ||
event=event, | ||
telescope_id=tel_id, | ||
telescope_name=tel_name, | ||
image=image, | ||
subarray=subarray, | ||
r1_dl1_calibrator_for_muon_rings=None, | ||
good_ring_config=muon_config, | ||
data_type='obs') | ||
|
||
table = Table(muon_parameters) | ||
table.write(output_file, format='fits', overwrite=True) | ||
logger.info(f'\nOutput muons file: {output_file}') | ||
|
||
|
||
def main(): | ||
|
||
parser = argparse.ArgumentParser() | ||
|
||
parser.add_argument( | ||
'--input-file', '-i', dest='input_file', type=str, required=True, | ||
help='Path to an input MAGIC calibrated data file (*_Y_*.root).', | ||
) | ||
|
||
parser.add_argument( | ||
'--output-dir', '-o', dest='output_dir', type=str, default='./data', | ||
help='Path to a directory where to save an output muon file.', | ||
) | ||
|
||
parser.add_argument( | ||
'--config-file', '-c', dest='config_file', type=str, default='./config.yaml', | ||
help='Path to a yaml configuration file.', | ||
) | ||
|
||
parser.add_argument( | ||
'--process-run', dest='process_run', action='store_true', | ||
help='Processes all the sub-run files of the same observation ID at once.', | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
with open(args.config_file, 'rb') as f: | ||
config = yaml.safe_load(f) | ||
|
||
# Process the input data: | ||
magic_muons_from_cal(args.input_file, args.output_dir, config, args.process_run) | ||
|
||
logger.info('\nDone.') | ||
|
||
if __name__ == '__main__': | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was the change of the thresholds intended? 12,8 looks quite large, did it come from your optimization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not optimised, but it followed a comment (by Abelardo?) that if we take only very bright pixels the biases from NSB should be reduced even if the charge extractor is not optimal.