def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        self.reader_uncal = TargetioFileReader(input_path=self.path_uncal,
                                               **kwargs)
        self.reader_cal = TargetioFileReader(input_path=self.path_cal,
                                             **kwargs)

        cleaner = CHECMWaveformCleanerAverage(**kwargs)
        extractor = AverageWfPeakIntegrator(**kwargs)
        self.dl0 = CameraDL0Reducer(**kwargs)
        self.dl1 = CameraDL1Calibrator(extractor=extractor,
                                       cleaner=cleaner,
                                       **kwargs)
        self.dead = Dead()

        self.fitter_uncal = CHECMSPEFitter(**kwargs)
        self.fitter_cal = CHECMSPEFitter(**kwargs)
        self.fitter_cal.range = [-3, 6]
        self.fitter_cal.initial = dict(norm=None,
                                       eped=0,
                                       eped_sigma=0.2,
                                       spe=1,
                                       spe_sigma=0.5,
                                       lambda_=0.2)

        first_event = self.reader_uncal.get_event(0)
        telid = list(first_event.r0.tels_with_data)[0]
        r1 = first_event.r1.tel[telid].pe_samples[0]
        self.n_pixels, self.n_samples = r1.shape
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        reader_factory = EventFileReaderFactory(**kwargs)
        reader_class = reader_factory.get_class()
        self.reader = reader_class(**kwargs)

        r1_factory = CameraR1CalibratorFactory(origin=self.reader.origin,
                                               **kwargs)
        r1_class = r1_factory.get_class()
        self.r1 = r1_class(**kwargs)

        self.dl0 = CameraDL0Reducer(**kwargs)

        self.cleaner = CHECMWaveformCleaner(**kwargs)
        self.extractor = CHECMExtractor(**kwargs)
        self.fitter = CHECMSPEFitter(**kwargs)
        self.dead = Dead()

        self.output_dir = join(self.reader.output_directory, "extract_adc2pe")
        if not exists(self.output_dir):
            self.log.info("Creating directory: {}".format(self.output_dir))
            makedirs(self.output_dir)

        n_events = self.reader.num_events
        first_event = self.reader.get_event(0)
        n_pixels, n_samples = first_event.r0.tel[0].adc_samples[0].shape

        self.charge = np.zeros((n_events, n_pixels))
Beispiel #3
0
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        reader_factory = EventFileReaderFactory(**kwargs)
        reader_class = reader_factory.get_class()
        self.reader = reader_class(**kwargs)

        r1_factory = CameraR1CalibratorFactory(origin=self.reader.origin,
                                               **kwargs)
        r1_class = r1_factory.get_class()
        self.r1 = r1_class(**kwargs)

        self.cleaner = CHECMWaveformCleanerLocal(**kwargs)

        extractor_factory = ChargeExtractorFactory(**kwargs)
        extractor_class = extractor_factory.get_class()
        self.extractor = extractor_class(**kwargs)

        self.dl0 = CameraDL0Reducer(**kwargs)

        self.dl1 = CameraDL1Calibrator(extractor=self.extractor,
                                       cleaner=self.cleaner,
                                       **kwargs)

        self.fitter = CHECMSPEFitter(**kwargs)
        self.dead = Dead()

        self.animator = Animator(**kwargs)
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        filepath = '/Volumes/gct-jason/data/170314/spe/Run00073_r1_adc.tio'
        self.reader = TargetioFileReader(input_path=filepath, **kwargs)

        cleaner = CHECMWaveformCleanerAverage(**kwargs)
        extractor = AverageWfPeakIntegrator(**kwargs)
        self.dl0 = CameraDL0Reducer(**kwargs)
        self.dl1 = CameraDL1Calibrator(extractor=extractor,
                                       cleaner=cleaner,
                                       **kwargs)
        self.fitter = CHECMSPEFitter(**kwargs)
        self.fitter.range = [-30, 160]
        self.dead = Dead()
        self.fw_calibrator = FWCalibrator(**kwargs)

        script = "filter_wheel"
        self.p_attenuation = Scatter(**kwargs, script=script, figure_name="attenuation")
        self.p_pe = Scatter(**kwargs, script=script, figure_name="pe")
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        reader_factory = EventFileReaderFactory(**kwargs)
        reader_class = reader_factory.get_class()
        self.reader = reader_class(**kwargs)

        r1_factory = CameraR1CalibratorFactory(origin=self.reader.origin,
                                               **kwargs)
        r1_class = r1_factory.get_class()
        self.r1 = r1_class(**kwargs)
        self.cleaner = CHECMWaveformCleanerLocal(**kwargs)
        self.extractor = AverageWfPeakIntegrator(**kwargs)
        self.dl0 = CameraDL0Reducer(**kwargs)
        self.dl1 = CameraDL1Calibrator(extractor=self.extractor,
                                       cleaner=self.cleaner,
                                       **kwargs)

        self.fitter = CHECMSPEFitter(**kwargs)
        # self.fitter.nbins = 60
        self.fitter.range = [-3, 6]
        self.fitter.initial = dict(norm=None,
                                   eped=0,
                                   eped_sigma=0.2,
                                   spe=1,
                                   spe_sigma=0.5,
                                   lambda_=0.2)

        self.dead = Dead()

        self.output_dir = join(self.reader.output_directory,
                               "extract_adc2pe_residuals")
        if not exists(self.output_dir):
            self.log.info("Creating directory: {}".format(self.output_dir))
            makedirs(self.output_dir)
Beispiel #6
0
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        reader_factory = EventFileReaderFactory(**kwargs)
        reader_class = reader_factory.get_class()
        self.reader = reader_class(**kwargs)

        r1_factory = CameraR1CalibratorFactory(origin=self.reader.origin,
                                               **kwargs)
        r1_class = r1_factory.get_class()
        self.r1 = r1_class(**kwargs)

        self.cleaner = CHECMWaveformCleanerLocal(**kwargs)

        extractor_factory = ChargeExtractorFactory(**kwargs)
        extractor_class = extractor_factory.get_class()
        self.extractor = extractor_class(**kwargs)

        self.dl0 = CameraDL0Reducer(**kwargs)

        self.dl1 = CameraDL1Calibrator(extractor=self.extractor,
                                       cleaner=self.cleaner,
                                       **kwargs)

        self.fitter = CHECMSPEFitter(**kwargs)
        self.dead = Dead()

        self.n_events = self.reader.num_events

        self.time = np.ma.zeros(self.n_events)
        self.size = np.ma.zeros(self.n_events)
        self.cen_x = np.ma.zeros(self.n_events)
        self.cen_y = np.ma.zeros(self.n_events)
        self.length = np.ma.zeros(self.n_events)
        self.width = np.ma.zeros(self.n_events)
        self.r = np.ma.zeros(self.n_events)
        self.phi = np.ma.zeros(self.n_events)
        self.psi = np.ma.zeros(self.n_events)
        self.miss = np.ma.zeros(self.n_events)
        self.skewness = np.ma.zeros(self.n_events)
        self.kurtosis = np.ma.zeros(self.n_events)
class FWInvestigator(Tool):
    name = "FWInvestigator"
    description = "Investigate the FW"

    aliases = Dict(dict())
    classes = List([])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.reader = None
        self.dl0 = None
        self.dl1 = None
        self.fitter = None
        self.dead = None
        self.fw_calibrator = None

        directory = join(realpath(dirname(__file__)), "../targetpipe/io")
        self.fw_txt_path = join(directory, "FW.txt")
        self.fw_storage_path = join(directory, "FW.h5")
        self.fw_storage_path_spe = join(directory, "FW_spe_LS62.h5")
        self.spe_fw = 1210

        self.p_attenuation = None
        self.p_pe = None

    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        filepath = '/Volumes/gct-jason/data/170314/spe/Run00073_r1_adc.tio'
        self.reader = TargetioFileReader(input_path=filepath, **kwargs)

        cleaner = CHECMWaveformCleanerAverage(**kwargs)
        extractor = AverageWfPeakIntegrator(**kwargs)
        self.dl0 = CameraDL0Reducer(**kwargs)
        self.dl1 = CameraDL1Calibrator(extractor=extractor,
                                       cleaner=cleaner,
                                       **kwargs)
        self.fitter = CHECMSPEFitter(**kwargs)
        self.fitter.range = [-30, 160]
        self.dead = Dead()
        self.fw_calibrator = FWCalibrator(**kwargs)

        script = "filter_wheel"
        self.p_attenuation = Scatter(**kwargs, script=script, figure_name="attenuation")
        self.p_pe = Scatter(**kwargs, script=script, figure_name="pe")
        # self.p_tmspe = TMSPEFitPlotter(**kwargs, script=script, figure_name="spe_fit_tm24")
        # self.p_tmspe_pe = TMSPEFitPlotter(**kwargs, script=script, figure_name="spe_fit_tm24_pe")
        # self.p_adc2pe = ADC2PEPlotter(**kwargs, script=script, figure_name="adc2pe", shape='wide')
        # self.p_adc2pe_1100tm = ADC2PE1100VTMPlotter(**kwargs, script=script, figure_name="adc2pe_1100V_tms", shape='wide')
        # self.p_adc2pe_1100tm_stats = ADC2PE1100VTMStatsPlotter(**kwargs, script=script, figure_name="adc2pe_1100V_tms_stats", shape='wide')

    def start(self):
        n_events = self.reader.num_events
        first_event = self.reader.get_event(0)
        telid = list(first_event.r0.tels_with_data)[0]
        n_pixels, n_samples = first_event.r1.tel[telid].pe_samples[0].shape

        dl1 = np.zeros((n_events, n_pixels))
        lambda_ = np.zeros(n_pixels)

        source = self.reader.read()
        desc = "Looping through file"
        for event in tqdm(source, total=n_events, desc=desc):
            index = event.count
            self.dl0.reduce(event)
            self.dl1.calibrate(event)
            dl1[index] = event.dl1.tel[telid].image

        desc = "Fitting pixels"
        for pix in trange(n_pixels, desc=desc):
            if not self.fitter.apply(dl1[:, pix]):
                self.log.warning("Pixel {} couldn't be fit".format(pix))
                continue
            lambda_[pix] = self.fitter.coeff['lambda_']

        lambda_ = self.dead.mask1d(lambda_)
        avg_lamda = np.mean(lambda_)

        columns = ['position', 'attenuation_mean', 'attenuation_rms']
        df = pd.read_table(self.fw_txt_path, sep=' ', names=columns, usecols=[0, 1, 2], skiprows=1)
        df = df.groupby('position').apply(np.mean)
        self.fw_calibrator.df = df

        self.fw_calibrator.save(self.fw_storage_path)
        self.fw_calibrator.set_calibration(self.spe_fw, avg_lamda)
        df = self.fw_calibrator.df

        x = df['position']
        y = df['attenuation_mean']
        y_err = df['attenuation_rms']
        self.p_attenuation.create(x, y, y_err, '', "Postion", "Attenuation", "Filter Wheel Attenuation")

        x = df['position']
        y = df['pe']
        y_err = df['pe_err']
        self.p_pe.create(x, y, y_err, '', "Postion", "Illumination (p.e.)", "Filter Wheel Calibrated")
        self.p_pe.ax.set_yscale('log')
        self.p_pe.ax.get_yaxis().set_major_formatter(FuncFormatter(lambda y, _: '{:g}'.format(y)))

    def finish(self):
        # Save figures
        self.p_attenuation.save()
        self.p_pe.save()
        self.fw_calibrator.save(self.fw_storage_path_spe)
Beispiel #8
0
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        filepath = '/Volumes/gct-jason/data/170314/spe/Run00073_r1_adc.tio'
        self.reader = TargetioFileReader(input_path=filepath, **kwargs)

        filepath = '/Volumes/gct-jason/data/170314/spe/Run00073_r1.tio'
        self.reader_pe = TargetioFileReader(input_path=filepath, **kwargs)

        cleaner = CHECMWaveformCleanerAverage(**kwargs)
        extractor = AverageWfPeakIntegrator(**kwargs)
        self.dl0 = CameraDL0Reducer(**kwargs)
        self.dl1 = CameraDL1Calibrator(extractor=extractor,
                                       cleaner=cleaner,
                                       **kwargs)
        self.fitter = CHECMSPEFitter(**kwargs)
        self.fitter.range = [-30, 160]
        self.fitter_pe = CHECMSPEFitter(**kwargs)
        self.fitter_pe.range = [-1, 6]
        self.fitter_pe.initial = dict(norm=None,
                                      eped=0,
                                      eped_sigma=0.2,
                                      spe=1,
                                      spe_sigma=0.5,
                                      lambda_=0.2)
        self.dead = Dead()

        script = "checm_paper_adc2pe"
        self.p_pixelspe = PixelSPEFitPlotter(**kwargs,
                                             script=script,
                                             figure_name="spe_fit_pixel1559")
        self.p_tmspe = TMSPEFitPlotter(**kwargs,
                                       script=script,
                                       figure_name="spe_fit_tm24")
        self.p_tmspe_pe = TMSPEFitPlotter(**kwargs,
                                          script=script,
                                          figure_name="spe_fit_tm24_pe")
        self.p_adc2pe = ADC2PEPlotter(**kwargs,
                                      script=script,
                                      figure_name="adc2pe",
                                      shape='square')
        self.p_adc2pe_1100tm = ADC2PE1100VTMPlotter(
            **kwargs,
            script=script,
            figure_name="adc2pe_1100V_tms",
            shape='wide')
        self.p_adc2pe_1100tm_stats = ADC2PE1100VTMStatsPlotter(
            **kwargs,
            script=script,
            figure_name="adc2pe_1100V_tms_stats",
            shape='wide')
        self.p_eped = Hist(**kwargs,
                           script=script,
                           figure_name="f_eped",
                           shape='square')
        self.p_eped_sigma = Hist(**kwargs,
                                 script=script,
                                 figure_name="f_eped_sigma",
                                 shape='square')
        self.p_spe = Hist(**kwargs,
                          script=script,
                          figure_name="f_spe",
                          shape='square')
        self.p_spe_sigma = Hist(**kwargs,
                                script=script,
                                figure_name="f_spe_sigma",
                                shape='square')
        self.p_lambda = Hist(**kwargs,
                             script=script,
                             figure_name="f_lambda",
                             shape='square')
        self.p_enf = Hist(**kwargs,
                          script=script,
                          figure_name="f_enf",
                          shape='square')
Beispiel #9
0
class ADC2PEPlots(Tool):
    name = "ADC2PEPlots"
    description = "Create plots related to adc2pe"

    aliases = Dict(dict(max_events='TargetioFileReader.max_events'))
    classes = List([
        TargetioFileReader,
        TargetioR1Calibrator,
    ])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.reader = None
        self.reader_pe = None
        self.dl0 = None
        self.dl1 = None
        self.fitter = None
        self.fitter_pe = None
        self.dead = None

        self.n_pixels = None
        self.n_samples = None

        self.spe_path = "/Volumes/gct-jason/data/170314/spe/Run00073_r0/extract_spe/spe.npy"
        self.gm_path = "/Volumes/gct-jason/data/170310/hv/gain_matching_coeff.npz"

        self.p_pixelspe = None
        self.p_tmspe = None
        self.p_tmspe_pe = None
        self.p_adc2pe = None
        self.p_adc2pe_1100tm = None
        self.p_adc2pe_1100tm_stats = None
        self.p_eped = None
        self.p_eped_sigma = None
        self.p_spe = None
        self.p_spe_sigma = None
        self.p_lambda = None
        self.p_enf = None

    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        filepath = '/Volumes/gct-jason/data/170314/spe/Run00073_r1_adc.tio'
        self.reader = TargetioFileReader(input_path=filepath, **kwargs)

        filepath = '/Volumes/gct-jason/data/170314/spe/Run00073_r1.tio'
        self.reader_pe = TargetioFileReader(input_path=filepath, **kwargs)

        cleaner = CHECMWaveformCleanerAverage(**kwargs)
        extractor = AverageWfPeakIntegrator(**kwargs)
        self.dl0 = CameraDL0Reducer(**kwargs)
        self.dl1 = CameraDL1Calibrator(extractor=extractor,
                                       cleaner=cleaner,
                                       **kwargs)
        self.fitter = CHECMSPEFitter(**kwargs)
        self.fitter.range = [-30, 160]
        self.fitter_pe = CHECMSPEFitter(**kwargs)
        self.fitter_pe.range = [-1, 6]
        self.fitter_pe.initial = dict(norm=None,
                                      eped=0,
                                      eped_sigma=0.2,
                                      spe=1,
                                      spe_sigma=0.5,
                                      lambda_=0.2)
        self.dead = Dead()

        script = "checm_paper_adc2pe"
        self.p_pixelspe = PixelSPEFitPlotter(**kwargs,
                                             script=script,
                                             figure_name="spe_fit_pixel1559")
        self.p_tmspe = TMSPEFitPlotter(**kwargs,
                                       script=script,
                                       figure_name="spe_fit_tm24")
        self.p_tmspe_pe = TMSPEFitPlotter(**kwargs,
                                          script=script,
                                          figure_name="spe_fit_tm24_pe")
        self.p_adc2pe = ADC2PEPlotter(**kwargs,
                                      script=script,
                                      figure_name="adc2pe",
                                      shape='square')
        self.p_adc2pe_1100tm = ADC2PE1100VTMPlotter(
            **kwargs,
            script=script,
            figure_name="adc2pe_1100V_tms",
            shape='wide')
        self.p_adc2pe_1100tm_stats = ADC2PE1100VTMStatsPlotter(
            **kwargs,
            script=script,
            figure_name="adc2pe_1100V_tms_stats",
            shape='wide')
        self.p_eped = Hist(**kwargs,
                           script=script,
                           figure_name="f_eped",
                           shape='square')
        self.p_eped_sigma = Hist(**kwargs,
                                 script=script,
                                 figure_name="f_eped_sigma",
                                 shape='square')
        self.p_spe = Hist(**kwargs,
                          script=script,
                          figure_name="f_spe",
                          shape='square')
        self.p_spe_sigma = Hist(**kwargs,
                                script=script,
                                figure_name="f_spe_sigma",
                                shape='square')
        self.p_lambda = Hist(**kwargs,
                             script=script,
                             figure_name="f_lambda",
                             shape='square')
        self.p_enf = Hist(**kwargs,
                          script=script,
                          figure_name="f_enf",
                          shape='square')

    def start(self):
        n_events = self.reader.num_events
        first_event = self.reader.get_event(0)
        telid = list(first_event.r0.tels_with_data)[0]
        n_pixels, n_samples = first_event.r1.tel[telid].pe_samples[0].shape

        ### SPE values from fit _______________________________________________
        # Prepare storage array

        dl1 = np.zeros((n_events, n_pixels))
        dl1_pe = np.zeros((n_events, n_pixels))
        hist_pix1559 = None
        edges_pix1559 = None
        between_pix1559 = None
        fit_pix1559 = None
        fitx_pix1559 = None
        hist_tm24 = np.zeros((64, self.fitter.nbins))
        edges_tm24 = np.zeros((64, self.fitter.nbins + 1))
        between_tm24 = np.zeros((64, self.fitter.nbins))
        hist_tm24_pe = np.zeros((64, self.fitter.nbins))
        edges_tm24_pe = np.zeros((64, self.fitter.nbins + 1))
        between_tm24_pe = np.zeros((64, self.fitter.nbins))

        f_eped = np.zeros(n_pixels)
        f_eped_sigma = np.zeros(n_pixels)
        f_spe = np.zeros(n_pixels)
        f_spe_sigma = np.zeros(n_pixels)
        f_lambda = np.zeros(n_pixels)

        source = self.reader.read()
        desc = "Looping through file"
        for event in tqdm(source, total=n_events, desc=desc):
            index = event.count
            self.dl0.reduce(event)
            self.dl1.calibrate(event)
            dl1[index] = event.dl1.tel[telid].image

        source = self.reader_pe.read()
        desc = "Looping through file (pe)"
        for event in tqdm(source, total=n_events, desc=desc):
            index = event.count
            self.dl0.reduce(event)
            self.dl1.calibrate(event)
            dl1_pe[index] = event.dl1.tel[telid].image

        desc = "Fitting pixels"
        for pix in trange(n_pixels, desc=desc):
            tm = pix // 64
            tmpix = pix % 64
            if not self.fitter.apply(dl1[:, pix]):
                self.log.warning("Pixel {} couldn't be fit".format(pix))
                continue
            if pix == 1559:
                hist_pix1559 = self.fitter.hist
                edges_pix1559 = self.fitter.edges
                between_pix1559 = self.fitter.between
                fit_pix1559 = self.fitter.fit
                fitx_pix1559 = self.fitter.fit_x
            if tm == 24:
                hist_tm24[tmpix] = self.fitter.hist
                edges_tm24[tmpix] = self.fitter.edges
                between_tm24[tmpix] = self.fitter.between
            f_eped[pix] = self.fitter.coeff['eped']
            f_eped_sigma[pix] = self.fitter.coeff['eped_sigma']
            f_spe[pix] = self.fitter.coeff['spe']
            f_spe_sigma[pix] = self.fitter.coeff['spe_sigma']
            f_lambda[pix] = self.fitter.coeff['lambda_']

        f_eped = checm_dac_to_volts(f_eped)
        f_eped_sigma = checm_dac_to_volts(f_eped_sigma)
        f_spe = checm_dac_to_volts(f_spe)
        f_spe_sigma = checm_dac_to_volts(f_spe_sigma)
        f_eped = self.dead.mask1d(f_eped).compressed()
        f_eped_sigma = self.dead.mask1d(f_eped_sigma).compressed()
        f_spe = self.dead.mask1d(f_spe).compressed()
        f_spe_sigma = self.dead.mask1d(f_spe_sigma).compressed()
        f_lambda = self.dead.mask1d(f_lambda).compressed()

        edges_pix1559 = checm_dac_to_volts(edges_pix1559)
        between_pix1559 = checm_dac_to_volts(between_pix1559)
        fitx_pix1559 = checm_dac_to_volts(fitx_pix1559)
        edges_tm24 = checm_dac_to_volts(edges_tm24)
        between_tm24 = checm_dac_to_volts(edges_tm24)

        desc = "Fitting pixels (pe)"
        for pix in trange(n_pixels, desc=desc):
            tm = pix // 64
            tmpix = pix % 64
            if tm != 24:
                continue
            if not self.fitter_pe.apply(dl1_pe[:, pix]):
                self.log.warning("Pixel {} couldn't be fit".format(pix))
                continue
            hist_tm24_pe[tmpix] = self.fitter_pe.hist
            edges_tm24_pe[tmpix] = self.fitter_pe.edges
            between_tm24_pe[tmpix] = self.fitter_pe.between

        ### SPE values for each hv setting ____________________________________
        kwargs = dict(config=self.config,
                      tool=self,
                      spe_path=self.spe_path,
                      gain_matching_path=self.gm_path)
        a2p = TargetioADC2PECalibrator(**kwargs)
        hv_dict = dict()
        hv_dict['800'] = [800] * 2048
        hv_dict['900'] = [900] * 2048
        hv_dict['1000'] = [1000] * 2048
        hv_dict['1100'] = [1100] * 2048
        hv_dict['800gm'] = [a2p.gm800[i // 64] for i in range(2048)]
        hv_dict['900gm'] = [a2p.gm900[i // 64] for i in range(2048)]
        hv_dict['1000gm'] = [a2p.gm1000[i // 64] for i in range(2048)]
        df_list = []
        for key, l in hv_dict.items():
            hv = int(key.replace("gm", ""))
            gm = 'gm' in key
            gm_t = 'Gain-matched' if 'gm' in key else 'Non-gain-matched'
            for pix in range(n_pixels):
                if pix in self.dead.dead_pixels:
                    continue
                adc2pe = a2p.get_adc2pe_at_hv(l[pix], pix)
                df_list.append(
                    dict(key=key,
                         hv=hv,
                         gm=gm,
                         gm_t=gm_t,
                         pixel=pix,
                         tm=pix // 64,
                         spe=1 / adc2pe))

        df = pd.DataFrame(df_list)
        df = df.sort_values(by='gm', ascending=True)
        df = df.assign(spe_mv=checm_dac_to_volts(df['spe']))

        # Create figures
        self.p_pixelspe.create(hist_pix1559, edges_pix1559, between_pix1559,
                               fit_pix1559, fitx_pix1559)
        self.p_tmspe.create(hist_tm24, edges_tm24, between_tm24, "V ns")
        self.p_tmspe_pe.create(hist_tm24_pe, edges_tm24_pe, between_tm24_pe,
                               "p.e.", 1)
        self.p_adc2pe.create(df)
        self.p_adc2pe_1100tm.create(df)
        self.p_adc2pe_1100tm_stats.create(df)
        self.p_eped.create(f_eped, "Pedestal (V ns)")
        self.p_eped_sigma.create(f_eped_sigma, "Pedestal Sigma (V ns)")
        self.p_spe.create(f_spe, "SPE (V ns)")
        self.p_spe_sigma.create(f_spe_sigma, "SPE Sigma (V ns)")
        self.p_lambda.create(f_lambda, "Illumination (Photoelectrons)")
        enf = np.sqrt(f_spe_sigma**2 - f_eped_sigma**2) / f_spe
        self.p_enf.create(enf, "Relative SPE Width")

    def finish(self):
        # Save figures
        self.p_pixelspe.save()
        self.p_tmspe.save()
        self.p_tmspe_pe.save()
        self.p_adc2pe.save()
        self.p_adc2pe_1100tm.save()
        self.p_adc2pe_1100tm_stats.save()
        self.p_eped.save()
        self.p_eped_sigma.save()
        self.p_spe.save()
        self.p_spe_sigma.save()
        self.p_lambda.save()
        self.p_enf.save()
class FitComparer(Tool):
    name = "ADC2PEPlots"
    description = "Create plots related to adc2pe"

    aliases = Dict(dict(max_events='TargetioFileReader.max_events'))
    classes = List([
        TargetioFileReader,
    ])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.reader_uncal = None
        self.reader_cal = None
        self.dl0 = None
        self.dl1 = None
        self.dead = None
        self.fitter_uncal = None
        self.fitter_cal = None

        self.n_pixels = None
        self.n_samples = None

        self.path_uncal = "/Volumes/gct-jason/data/170314/spe/Run00073_r1_adc.tio"
        self.path_cal = "/Volumes/gct-jason/data/170314/spe/Run00073_r1.tio"

        self.p_comparison = None
        self.p_tmspread = None
        self.p_dist = None

    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        self.reader_uncal = TargetioFileReader(input_path=self.path_uncal,
                                               **kwargs)
        self.reader_cal = TargetioFileReader(input_path=self.path_cal,
                                             **kwargs)

        cleaner = CHECMWaveformCleanerAverage(**kwargs)
        extractor = AverageWfPeakIntegrator(**kwargs)
        self.dl0 = CameraDL0Reducer(**kwargs)
        self.dl1 = CameraDL1Calibrator(extractor=extractor,
                                       cleaner=cleaner,
                                       **kwargs)
        self.dead = Dead()

        self.fitter_uncal = CHECMSPEFitter(**kwargs)
        self.fitter_cal = CHECMSPEFitter(**kwargs)
        self.fitter_cal.range = [-3, 6]
        self.fitter_cal.initial = dict(norm=None,
                                       eped=0,
                                       eped_sigma=0.2,
                                       spe=1,
                                       spe_sigma=0.5,
                                       lambda_=0.2)

        first_event = self.reader_uncal.get_event(0)
        telid = list(first_event.r0.tels_with_data)[0]
        r1 = first_event.r1.tel[telid].pe_samples[0]
        self.n_pixels, self.n_samples = r1.shape

    def start(self):
        n_events = self.reader_uncal.num_events

        dl1_uncal = np.zeros((n_events, self.n_pixels))
        dl1_cal = np.zeros((n_events, self.n_pixels))

        source_uncal = self.reader_uncal.read()
        desc = 'Looping through events: uncal'
        for event in tqdm(source_uncal, total=n_events, desc=desc):
            ev = event.count
            self.dl0.reduce(event)
            self.dl1.calibrate(event)
            dl1_uncal[ev] = event.dl1.tel[0].image[0]

        source_cal = self.reader_cal.read()
        desc = 'Looping through events: cal'
        for event in tqdm(source_cal, total=n_events, desc=desc):
            ev = event.count
            self.dl0.reduce(event)
            self.dl1.calibrate(event)
            dl1_cal[ev] = event.dl1.tel[0].image[0]

        # np.save("/Users/Jason/Downloads/dl1_uncal.npy", dl1_uncal)
        # np.save("/Users/Jason/Downloads/dl1_cal.npy", dl1_cal)
        #
        # dl1_uncal = np.load("/Users/Jason/Downloads/dl1_uncal.npy")
        # dl1_cal = np.load("/Users/Jason/Downloads/dl1_cal.npy")

        for pix in range(self.n_pixels):
            if not self.fitter_uncal.apply(dl1_uncal[:, pix]):
                self.log.warning("Pixel {} couldn't be fit".format(pix))
                continue
            if not self.fitter_cal.apply(dl1_cal[:, pix]):
                self.log.warning("Pixel {} couldn't be fit".format(pix))
                continue

            gain_uncal = self.fitter_uncal.coeff['spe']
            gain_cal = self.fitter_cal.coeff['spe']
            lambda_uncal = self.fitter_uncal.coeff['lambda_']
            lambda_cal = self.fitter_cal.coeff['lambda_']

            print("Pixel {}: Gain Uncal {:.3} Gain Cal {:.3} "
                  "Lambda Uncal {:.3} Lambda Cal {:.3}".format(
                      pix, gain_uncal, gain_cal, lambda_uncal, lambda_cal))

    def finish(self):
        pass
class ADC2PEResidualsExtractor(Tool):
    name = "ADC2PEResidualsExtractor"
    description = "Extract values used to show the residuals of the adc2pe " \
                  "calibration into a numpy array"

    aliases = Dict(
        dict(
            r='EventFileReaderFactory.reader',
            f='EventFileReaderFactory.input_path',
            max_events='EventFileReaderFactory.max_events',
            ped='CameraR1CalibratorFactory.pedestal_path',
            tf='CameraR1CalibratorFactory.tf_path',
            pe='CameraR1CalibratorFactory.pe_path',
        ))
    classes = List([
        EventFileReaderFactory,
        CameraR1CalibratorFactory,
        CHECMSPEFitter,
    ])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.reader = None
        self.r1 = None
        self.dl0 = None
        self.cleaner = None
        self.extractor = None
        self.dl1 = None

        self.fitter = None
        self.dead = None

        self.output_dir = None

        self.spe = None
        self.spe_sigma = None
        self.hist = None
        self.edges = None
        self.between = None

    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        reader_factory = EventFileReaderFactory(**kwargs)
        reader_class = reader_factory.get_class()
        self.reader = reader_class(**kwargs)

        r1_factory = CameraR1CalibratorFactory(origin=self.reader.origin,
                                               **kwargs)
        r1_class = r1_factory.get_class()
        self.r1 = r1_class(**kwargs)
        self.cleaner = CHECMWaveformCleanerLocal(**kwargs)
        self.extractor = AverageWfPeakIntegrator(**kwargs)
        self.dl0 = CameraDL0Reducer(**kwargs)
        self.dl1 = CameraDL1Calibrator(extractor=self.extractor,
                                       cleaner=self.cleaner,
                                       **kwargs)

        self.fitter = CHECMSPEFitter(**kwargs)
        # self.fitter.nbins = 60
        self.fitter.range = [-3, 6]
        self.fitter.initial = dict(norm=None,
                                   eped=0,
                                   eped_sigma=0.2,
                                   spe=1,
                                   spe_sigma=0.5,
                                   lambda_=0.2)

        self.dead = Dead()

        self.output_dir = join(self.reader.output_directory,
                               "extract_adc2pe_residuals")
        if not exists(self.output_dir):
            self.log.info("Creating directory: {}".format(self.output_dir))
            makedirs(self.output_dir)

    def start(self):
        n_events = self.reader.num_events
        first_event = self.reader.get_event(0)
        telid = list(first_event.r0.tels_with_data)[0]
        n_pixels, n_samples = first_event.r0.tel[telid].adc_samples[0].shape

        # Prepare storage array
        area = np.zeros((n_events, n_pixels))
        self.spe = np.ma.zeros(n_pixels)
        self.spe.mask = np.zeros(self.spe.shape, dtype=np.bool)
        self.spe_sigma = np.zeros(n_pixels)
        self.hist = np.zeros((n_pixels, self.fitter.nbins))
        self.edges = np.zeros((n_pixels, self.fitter.nbins + 1))
        self.between = np.zeros((n_pixels, self.fitter.nbins))

        source = self.reader.read()
        desc = "Looping through file"
        with tqdm(total=n_events, desc=desc) as pbar:
            for event in source:
                pbar.update(1)
                index = event.count

                self.r1.calibrate(event)
                self.dl0.reduce(event)
                self.dl1.calibrate(event)

                # Perform CHECM Charge Extraction
                peak_area = event.dl1.tel[telid].image

                area[index] = peak_area

        desc = "Fitting pixels"
        with tqdm(total=n_pixels, desc=desc) as pbar:
            for pix in range(n_pixels):
                pbar.update(1)
                if not self.fitter.apply(area[:, pix]):
                    self.log.warning("Pixel {} couldn't be fit".format(pix))
                    self.spe.mask[pix] = True
                    continue
                self.spe[pix] = self.fitter.coeff['spe']
                self.spe_sigma[pix] = self.fitter.coeff['spe_sigma']
                self.hist[pix] = self.fitter.hist
                self.edges[pix] = self.fitter.edges
                self.between[pix] = self.fitter.between

    def finish(self):
        output_path = join(self.output_dir, "adc2pe_residuals.npz")
        np.savez(output_path,
                 spe=np.ma.filled(self.spe, 0),
                 spe_sigma=self.spe_sigma,
                 hist=self.hist,
                 edges=self.edges,
                 between=self.between)
        self.log.info("Created numpy array: {}".format(output_path))