def plot_quick_camera(image, ax=None): ax = ax if ax is not None else plt.gca() cameraconfig = Config() pos = cameraconfig.pixel_pos * u.m foclen = cameraconfig.optical_foclen * u.m geom = CameraGeometry.guess(*pos, foclen) camera = CameraDisplay(geom, ax=ax, image=image, cmap='viridis') return camera
from targetpipe.calib.camera.filter_wheel import FWCalibrator from targetpipe.io.camera import Config Config('checm') from tqdm import tqdm, trange from traitlets import Dict, List import numpy as np import pandas as pd import matplotlib.lines as mlines from matplotlib.ticker import MultipleLocator, FormatStrFormatter, \ ScalarFormatter, FuncFormatter import seaborn as sns from os.path import realpath, join, dirname from ctapipe.calib.camera.dl0 import CameraDL0Reducer from ctapipe.calib.camera.dl1 import CameraDL1Calibrator from ctapipe.core import Tool from ctapipe.image.charge_extractors import AverageWfPeakIntegrator from ctapipe.image.waveform_cleaning import CHECMWaveformCleanerAverage from targetpipe.io.eventfilereader import TargetioFileReader from targetpipe.calib.camera.r1 import TargetioR1Calibrator from targetpipe.fitting.chec import CHECMSPEFitter from targetpipe.io.pixels import Dead from targetpipe.calib.camera.adc2pe import TargetioADC2PECalibrator from targetpipe.plots.official import ChecmPaperPlotter from IPython import embed class Scatter(ChecmPaperPlotter): name = 'Scatter'
def setup(self): self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]" data_config = self.config.copy() data_config['WaveformCleanerFactory'] = Config(cleaner='CHECMWaveformCleanerLocal') mc_config = self.config.copy() data_kwargs = dict(config=data_config, tool=self) mc_kwargs = dict(config=mc_config, tool=self) filepath = '/Volumes/gct-jason/data/170330/onsky-mrk501/Run05477_r1.tio' reader = TargetioFileReader(input_path=filepath, **data_kwargs) filepath = '/Users/Jason/Software/outputs/sim_telarray/meudon_cr/simtel_proton_nsb50_thrs30_1petal_rndm015_heide.gz' # filepath = '/Users/Jason/Software/outputs/sim_telarray/meudon_cr/simtel_proton_nsb50_thrs30.gz' reader_mc = HessioFileReader(input_path=filepath, **mc_kwargs) calibrator = CameraCalibrator(origin=reader.origin, **data_kwargs) calibrator_mc = CameraCalibrator(origin=reader_mc.origin, **mc_kwargs) first_event = reader.get_event(0) telid = list(first_event.r0.tels_with_data)[0] pos = first_event.inst.pixel_pos[telid] foclen = first_event.inst.optical_foclen[telid] geom = CameraGeometry.guess(*pos, foclen) first_event = reader_mc.get_event(0) telid = list(first_event.r0.tels_with_data)[0] pos_mc = first_event.inst.pixel_pos[telid] foclen = first_event.inst.optical_foclen[telid] geom_mc = CameraGeometry.guess(*pos_mc, foclen) d1 = dict(type='Data', reader=reader, calibrator=calibrator, pos=pos, geom=geom, t1=20, t2=10) d2 = dict(type='MC', reader=reader_mc, calibrator=calibrator_mc, pos=pos_mc, geom=geom_mc, t1=20, t2=10) self.reader_df = pd.DataFrame([d1, d2]) p_kwargs = data_kwargs p_kwargs['script'] = "checm_paper_hillas" p_kwargs['figure_name'] = "all_images" self.p_allimage = AllImagePlotter(**p_kwargs) p_kwargs['figure_name'] = "all_peak_time_images" self.p_alltimeimage = PeakTimePlotter(**p_kwargs) p_kwargs['figure_name'] = "all_mc_images" self.p_allmcimage = AllImagePlotter(**p_kwargs) p_kwargs['figure_name'] = "zero_width_images" self.p_zwimage = ZeroWidthImagePlotter(**p_kwargs) p_kwargs['figure_name'] = "zero_width_mc_images" self.p_zwmcimage = ZeroWidthImagePlotter(**p_kwargs) p_kwargs['figure_name'] = "muon_images" self.p_muonimage = MuonImagePlotter(**p_kwargs) p_kwargs['figure_name'] = "bright_images" self.p_brightimage = BrightImagePlotter(**p_kwargs) p_kwargs['figure_name'] = "count_image" self.p_countimage = CountPlotter(**p_kwargs) p_kwargs['figure_name'] = "whole_distribution" self.p_whole_dist = WholeDist(**p_kwargs, shape='wide') p_kwargs['figure_name'] = "width_vs_length" self.p_widthvslength = WidthVsLength(**p_kwargs, shape='wide') p_kwargs['figure_name'] = "size_vs_length" self.p_sizevslength = SizeVsLength(**p_kwargs, shape='wide') p_kwargs['figure_name'] = "width_div_length" self.p_widthdivlength = WidthDivLength(**p_kwargs, shape='wide') p_kwargs['figure_name'] = "length_div_size" self.p_lengthdivsize = LengthDivSize(**p_kwargs, shape='wide') p_kwargs['figure_name'] = "pair_plot" self.p_pair = PairPlotter(**p_kwargs, shape='wide') p_kwargs['figure_name'] = "pair_mc_plot" self.p_mc_pair = PairPlotter(**p_kwargs, shape='wide') p_kwargs['figure_name'] = "length" self.p_length = LengthPlotter(**p_kwargs, shape='wide') p_kwargs['figure_name'] = "width" self.p_width = WidthPlotter(**p_kwargs, shape='wide')
def __init__(self): cameraconfig = Config() self.dead_pixels = cameraconfig.dead_pixels self.n_pix = cameraconfig.n_pix
def get_geometry(): cameraconfig = Config() pixel_pos = cameraconfig.pixel_pos optical_foclen = cameraconfig.optical_foclen return CameraGeometry.guess(*pixel_pos * u.m, optical_foclen * u.m)
def toyio_event_source(url, max_events=None, allowed_tels=None, requested_event=None, use_event_id=False): """A generator that streams data from an EventIO/HESSIO MC data file (e.g. a standard CTA data file.) Parameters ---------- url : str path to file to open max_events : int, optional maximum number of events to read allowed_tels : list[int] select only a subset of telescope, if None, all are read. This can be used for example emulate the final CTA data format, where there would be 1 telescope per file (whereas in current monte-carlo, they are all interleaved into one file) requested_event : int Seek to a paricular waveforms index use_event_id : bool If True ,'requested_event' now seeks for a particular waveforms id instead of index """ waveforms_cells = np.load(url) waveforms = waveforms_cells[:, :, 1, :] cells = waveforms_cells[:, :, 0, :] n_events, n_pix, _, n_samples = waveforms_cells.shape chec_tel = 0 run_id = 0 cameraconfig = Config() pix_pos = cameraconfig.pixel_pos counter = 0 if allowed_tels is not None: allowed_tels = set(allowed_tels) data = DataContainer() data.meta['source'] = "toyio" # some hessio_event_source specific parameters data.meta['input'] = url data.meta['max_events'] = max_events data.meta['num_events'] = n_events for event_id in range(n_events): # Seek to requested waveforms if requested_event is not None: current = counter if use_event_id: current = event_id if not current == requested_event: counter += 1 continue data.r0.run_id = run_id data.r0.event_id = event_id data.r0.tels_with_data = {chec_tel} data.r1.run_id = run_id data.r1.event_id = event_id data.r1.tels_with_data = {chec_tel} data.dl0.run_id = run_id data.dl0.event_id = event_id data.dl0.tels_with_data = {chec_tel} # handle telescope filtering by taking the intersection of # tels_with_data and allowed_tels if allowed_tels is not None: selected = data.dl0.tels_with_data & allowed_tels if len(selected) == 0: continue # skip waveforms data.dl0.tels_with_data = selected data.trig.tels_with_trigger = [chec_tel] time_ns = counter data.trig.gps_time = Time(time_ns * u.ns, format='gps', scale='utc') data.count = counter # this should be done in a nicer way to not re-allocate the # data each time (right now it's just deleted and garbage # collected) data.r0.tel.clear() data.r1.tel.clear() data.dl0.tel.clear() data.dl1.tel.clear() data.inst.pixel_pos[chec_tel] = pix_pos data.inst.optical_foclen[chec_tel] = 2.283 * u.m data.inst.num_channels[chec_tel] = 1 data.inst.num_pixels[chec_tel] = n_pix # data.inst.num_samples[chec_tel] = n_samples # waveforms.mc.tel[tel_id] = MCCameraContainer() # load the data per telescope/chan data.r0.tel[chec_tel].adc_samples = \ waveforms[event_id, :, :][None, ...] # data.r0.tel[chec_tel].adc_sums = \ # targetio_extractor.waveforms.sum(axis=1)[None, ...] data.r1.tel[chec_tel].pe_samples = \ np.zeros(waveforms[0].shape)[None, ...] data.r0.tel[chec_tel].first_cell_ids = \ cells[event_id, :, 0] yield data counter += 1 if max_events is not None and counter >= max_events: break
class TargetioFileReader(EventFileReader): name = 'TargetioFileReader' origin = 'targetio' input_path = Unicode(None, allow_none=True, help='Path to the input file containing ' 'events.').tag(config=True) cameraconfig = Config() camera_id = CaselessStrEnum(list(cameraconfig.options.keys()) + [''], '', help='Camera configuration').tag(config=True) def __init__(self, config, tool, **kwargs): """ Class to handle targetio input files. Enables obtaining the "source" generator. Parameters ---------- config : traitlets.loader.Config Configuration specified by config file or cmdline arguments. Used to set traitlet values. Set to None if no configuration to pass. tool : ctapipe.core.Tool Tool executable that is calling this component. Passes the correct logger to the component. Set to None if no Tool to pass. kwargs """ if self.camera_id: self.cameraconfig.id = self.camera_id super().__init__(config=config, tool=tool, **kwargs) self.extractor = TargetioExtractor(self.input_path, self.max_events) @observe('input_path') def on_input_path_changed(self, change): new = change['new'] try: self.log.warning("Change: input_path={}".format(new)) self._num_events = None self._event_id_list = [] self._init_path(new) self.extractor = TargetioExtractor(new, self.max_events) except AttributeError: pass @staticmethod def check_file_compatibility(file_path): compatible = True # TODO: Change check to be a try of targetio_event_source? if not file_path.endswith('.fits') and not file_path.endswith('.tio'): compatible = False return compatible @property def num_events(self): if not self._num_events: # self.log.info("Obtaining number of events in file...") num = self.extractor.n_events if self.max_events and self.max_events < num: num = self.max_events # self.log.info("Number of events = {}".format(num)) self._num_events = num return self._num_events @property def event_id_list(self): if not self._event_id_list: # self.log.info("Building new list of event ids...") self._event_id_list = self.extractor.event_id_list[:self. max_events] # self.log.info("List of event ids built.") return self._event_id_list def read(self, allowed_tels=None, requested_event=None, use_event_id=False): """ Read the file using the appropriate method depending on the file origin Parameters ---------- allowed_tels : list[int] select only a subset of telescope, if None, all are read. This can be used for example emulate the final CTA data format, where there would be 1 telescope per file (whereas in current monte-carlo, they are all interleaved into one file) requested_event : int Seek to a paricular event index use_event_id : bool If True ,'requested_event' now seeks for a particular event id instead of index Returns ------- source : generator A generator that can be iterated over to obtain events """ # Obtain relevent source self.log.debug("Reading file...") if self.max_events: self.log.info("Max events being read = {}".format(self.max_events)) source = self.extractor.read_generator() self.log.debug("File reading complete") return source def get_event(self, requested_event, use_event_id=False): """ Loop through events until the requested event is found Parameters ---------- requested_event : int Seek to a paricular event index use_event_id : bool If True ,'requested_event' now seeks for a particular event id instead of index Returns ------- event : `ctapipe` event-container """ self.extractor.read_event(requested_event, use_event_id) event = self.extractor.data return deepcopy(event)
def __init__(self, url, max_events=None): """ Parameters ---------- url : string path to the TARGET fits file """ self._event_index = None self.url = url self.max_events = max_events self.event_id = 0 self.time_tack = None self.time_sec = None self.time_ns = None self.cameraconfig = Config() self.tio_reader = TIOReader(self.url, self.cameraconfig.n_cells, self.cameraconfig.skip_sample, self.cameraconfig.skip_end_sample, self.cameraconfig.skip_event, self.cameraconfig.skip_end_event) self.n_events = self.tio_reader.fNEvents first_event_id = self.tio_reader.fFirstEventID last_event_id = self.tio_reader.fLastEventID self.event_id_list = np.arange(first_event_id, last_event_id) self.run_id = self.tio_reader.fRunID self.n_pix = self.tio_reader.fNPixels self.n_modules = self.tio_reader.fNModules self.n_tmpix = self.n_pix // self.n_modules self.n_samples = self.tio_reader.fNSamples self.n_cells = self.tio_reader.fNCells # Setup camera geom if self.n_pix == self.n_tmpix: self.cameraconfig.switch_to_single_module() self.pixel_pos = self.cameraconfig.pixel_pos self.optical_foclen = self.cameraconfig.optical_foclen self.n_rows = self.cameraconfig.n_rows self.n_columns = self.cameraconfig.n_columns self.n_blocks = self.cameraconfig.n_blocks self.refshape = self.cameraconfig.refshape self.refstep = self.cameraconfig.refstep self.time_slice = self.cameraconfig.time_slice # Init arrays self.r0_samples = None self.r1_samples = np.zeros((self.n_pix, self.n_samples), dtype=np.float32)[None, ...] self.first_cell_ids = np.zeros(self.n_pix, dtype=np.uint16) # Setup if file is already r1 self.is_r1 = self.tio_reader.fR1 if self.is_r1: self.get_event = self.tio_reader.GetR1Event self.samples = self.r1_samples[0] else: self.r0_samples = np.zeros((self.n_pix, self.n_samples), dtype=np.uint16)[None, ...] self.get_event = self.tio_reader.GetR0Event self.samples = self.r0_samples[0] self.data = None self.init_container()
class TargetioExtractor: """ Extract waveforms from `target_io` and build them into a camera image Attributes ---------- tio_reader : target_io.TargetIOEventReader() n_events : int number of events in the fits file n_samples : int number of samples in the waveform r0_samples : ndarray two dimensional array to store the waveform for each pixel """ def __init__(self, url, max_events=None): """ Parameters ---------- url : string path to the TARGET fits file """ self._event_index = None self.url = url self.max_events = max_events self.event_id = 0 self.time_tack = None self.time_sec = None self.time_ns = None self.cameraconfig = Config() self.tio_reader = TIOReader(self.url, self.cameraconfig.n_cells, self.cameraconfig.skip_sample, self.cameraconfig.skip_end_sample, self.cameraconfig.skip_event, self.cameraconfig.skip_end_event) self.n_events = self.tio_reader.fNEvents first_event_id = self.tio_reader.fFirstEventID last_event_id = self.tio_reader.fLastEventID self.event_id_list = np.arange(first_event_id, last_event_id) self.run_id = self.tio_reader.fRunID self.n_pix = self.tio_reader.fNPixels self.n_modules = self.tio_reader.fNModules self.n_tmpix = self.n_pix // self.n_modules self.n_samples = self.tio_reader.fNSamples self.n_cells = self.tio_reader.fNCells # Setup camera geom if self.n_pix == self.n_tmpix: self.cameraconfig.switch_to_single_module() self.pixel_pos = self.cameraconfig.pixel_pos self.optical_foclen = self.cameraconfig.optical_foclen self.n_rows = self.cameraconfig.n_rows self.n_columns = self.cameraconfig.n_columns self.n_blocks = self.cameraconfig.n_blocks self.refshape = self.cameraconfig.refshape self.refstep = self.cameraconfig.refstep self.time_slice = self.cameraconfig.time_slice # Init arrays self.r0_samples = None self.r1_samples = np.zeros((self.n_pix, self.n_samples), dtype=np.float32)[None, ...] self.first_cell_ids = np.zeros(self.n_pix, dtype=np.uint16) # Setup if file is already r1 self.is_r1 = self.tio_reader.fR1 if self.is_r1: self.get_event = self.tio_reader.GetR1Event self.samples = self.r1_samples[0] else: self.r0_samples = np.zeros((self.n_pix, self.n_samples), dtype=np.uint16)[None, ...] self.get_event = self.tio_reader.GetR0Event self.samples = self.r0_samples[0] self.data = None self.init_container() @property def event_index(self): return self._event_index @event_index.setter def event_index(self, val): self._event_index = val self.get_event(self.event_index, self.samples, self.first_cell_ids) self.event_id = self.tio_reader.fCurrentEventID self.time_tack = self.tio_reader.fCurrentTimeTack self.time_sec = self.tio_reader.fCurrentTimeSec self.time_ns = self.tio_reader.fCurrentTimeNs self.update_container() def move_to_next_event(self): for self.event_index in range(self.n_events): yield self.run_id, self.event_id def init_container(self): url = self.url max_events = self.max_events chec_tel = 0 data = DataContainer() data.meta['origin'] = "targetio" # some targetio_event_source specific parameters data.meta['input'] = url data.meta['max_events'] = max_events data.meta['n_rows'] = self.n_rows data.meta['n_columns'] = self.n_columns data.meta['n_blocks'] = self.n_blocks data.meta['n_blockphases'] = N_BLOCKSAMPLES data.meta['n_cells'] = self.n_cells data.meta['n_modules'] = self.n_modules data.meta['tm'] = np.arange(self.n_pix, dtype=np.uint16) // self.n_tmpix data.meta['tmpix'] = np.arange(self.n_pix, dtype=np.uint16) % self.n_tmpix pix_pos = self.pixel_pos * u.m foclen = self.optical_foclen * u.m teldesc = TelescopeDescription.guess(*pix_pos, foclen) data.inst.subarray.tels[chec_tel] = teldesc self.data = data def update_container(self): data = self.data chec_tel = 0 event_id = self.event_id run_id = self.run_id data.r0.run_id = run_id data.r0.event_id = event_id data.r0.tels_with_data = {chec_tel} data.r1.run_id = run_id data.r1.event_id = event_id data.r1.tels_with_data = {chec_tel} data.dl0.run_id = run_id data.dl0.event_id = event_id data.dl0.tels_with_data = {chec_tel} data.trig.tels_with_trigger = [chec_tel] data.meta['tack'] = self.time_tack data.meta['sec'] = self.time_sec data.meta['ns'] = self.time_ns data.trig.gps_time = Time(self.time_sec * u.s, self.time_ns * u.ns, format='unix', scale='utc', precision=9) data.count = self.event_index # this should be done in a nicer way to not re-allocate the # data each time (right now it's just deleted and garbage # collected) data.r0.tel.clear() data.r1.tel.clear() data.dl0.tel.clear() data.dl1.tel.clear() data.mc.tel.clear() # load the data per telescope/chan data.r0.tel[chec_tel].adc_samples = self.r0_samples data.r1.tel[chec_tel].pe_samples = self.r1_samples data.r0.tel[chec_tel].first_cell_ids = self.first_cell_ids bp, r, c = get_bp_r_c(self.first_cell_ids) data.r0.tel[chec_tel].blockphase = bp data.r0.tel[chec_tel].row = r data.r0.tel[chec_tel].column = c data.r0.tel[chec_tel].num_samples = self.n_samples data.mc.tel[chec_tel].reference_pulse_shape = self.refshape data.mc.tel[chec_tel].meta['refstep'] = self.refstep data.mc.tel[chec_tel].time_slice = self.time_slice data.meta['n_blocks'] = self.n_blocks def read_generator(self): data = self.data n_events = self.n_events if self.max_events and self.max_events < self.n_events: n_events = self.max_events for self.event_index in range(n_events): yield data def read_event(self, requested_event, use_event_id=False): """ Obtain a particular event from the targetio file. Parameters ---------- requested_event : int use_event_id : bool If True ,'requested_event' now seeks for a particular events id instead of index """ index = requested_event if use_event_id: # Obtaining event id not implemented index = self.tio_reader.GetEventIndex(requested_event) n_events = self.n_events if self.max_events and self.max_events < self.n_events: n_events = self.max_events if (index >= n_events) | (index < 0): raise RuntimeError("Outside event range") self.event_index = index