Пример #1
0
def test_add_charges():
    chargeres = ChargeResolutionCalculator(None, None)
    true_charge = np.arange(100)
    measured_charge = np.arange(100)
    chargeres.add_charges(true_charge, measured_charge)
    variation_hist = chargeres.variation_hist

    assert(variation_hist[0][0] == 1)
Пример #2
0
def test_add_charges():
    chargeres = ChargeResolutionCalculator(None, None)
    true_charge = np.arange(100)
    measured_charge = np.arange(100)
    chargeres.add_charges(true_charge, measured_charge)
    variation_hist = chargeres.variation_hist

    assert (variation_hist[0][0] == 1)
Пример #3
0
    def setup(self):
        kwargs = dict(config=self.config, tool=self)
        self.dl0 = CameraDL0Reducer(**kwargs)

        self.dl1 = CameraDL1Calibrator(**kwargs)

        self.cal = CameraCalibrator(r1_product=self.calibrator)

        self.calculator = ChargeResolutionCalculator(**kwargs)
Пример #4
0
class ChargeResolutionViewer(Tool):
    name = "ChargeResolutionViewer"
    description = "Plot the charge resolution from " \
                  "ChargeResolutionCalculator objects restored via " \
                  "pickled dictionaries."

    input_files = List(Unicode, None,
                       help='Input pickle files that are produced from '
                            'ChargeResolutionCalculator.save().'
                            '').tag(config=True)

    aliases = Dict(dict(f='ChargeResolutionViewer.input_files',
                        B='ChargeResolutionCalculator.binning',
                        max_pe='ChargeResolutionPlotter.max_pe',
                        O='ChargeResolutionPlotter.output_path',
                        ))
    flags = Dict(dict(L=({'ChargeResolutionCalculator': {'log_bins': False}},
                         'Bin the x axis linearly instead of logarithmic.'),
                      linx=({'ChargeResolutionPlotter': {'linear_x': True}},
                            'Plot the x values on a linear axis, '
                            'instead of log.'),
                      liny=({'ChargeResolutionPlotter': {'linear_y': True}},
                            'Plot the x values on a linear axis, '
                            'instead of log.')
                      ))
    classes = List([ChargeResolutionCalculator,
                    ChargeResolutionPlotter
                    ])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.calculator = None
        self.plotter = None

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

        self.calculator = ChargeResolutionCalculator(**kwargs)
        self.plotter = ChargeResolutionPlotter(**kwargs)

    def start(self):
        self.plotter.plot_limit_curves()
        for fp in self.input_files:
            self.calculator.load(fp)
            x, res, res_error, scaled_res, scaled_res_error = \
                self.calculator.get_charge_resolution()

            name = basename(fp)
            self.plotter.plot_chargeres(name, x, res, res_error)
            self.plotter.plot_scaled_chargeres(x, scaled_res, scaled_res_error)

    def finish(self):
        self.plotter.save()
Пример #5
0
def test_get_binned_charge_resolution():
    chargeres = ChargeResolutionCalculator(None, None)
    true_charge = np.arange(100)
    measured_charge = np.arange(100)
    chargeres.add_charges(true_charge, measured_charge)
    true_charge, chargeres, chargeres_error, \
        scaled_chargeres, scaled_chargeres_error = \
        chargeres.get_charge_resolution()

    assert (true_charge[0] == 1)
    assert (chargeres[0] == 1)
    assert (round(chargeres_error[0], 7) == 0.7071068)
    assert (round(scaled_chargeres[0], 7) == 0.5550272)
    assert (round(scaled_chargeres_error[0], 7) == 0.3924635)
Пример #6
0
def test_get_binned_charge_resolution():
    chargeres = ChargeResolutionCalculator(None, None)
    true_charge = np.arange(100)
    measured_charge = np.arange(100)
    chargeres.add_charges(true_charge, measured_charge)
    true_charge, chargeres, chargeres_error, \
        scaled_chargeres, scaled_chargeres_error = \
        chargeres.get_charge_resolution()

    assert(true_charge[0] == 1)
    assert(chargeres[0] == 1)
    assert(round(chargeres_error[0], 7) == 0.7071068)
    assert(round(scaled_chargeres[0], 7) == 0.5550272)
    assert(round(scaled_chargeres_error[0], 7) == 0.3924635)
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        self.eventsource = SimTelEventSource(**kwargs)

        extractor = ChargeExtractorFactory.produce(**kwargs)

        self.r1 = HESSIOR1Calibrator(**kwargs)

        self.dl0 = CameraDL0Reducer(**kwargs)

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

        self.calculator = ChargeResolutionCalculator(**kwargs)
Пример #8
0
    def plot_limit_curves(self):
        x = np.logspace(log10(0.9), log10(self.max_pe * 1.1), 100)
        requirement = ChargeResolutionCalculator.requirement(x)
        goal = ChargeResolutionCalculator.goal(x)
        poisson = ChargeResolutionCalculator.poisson(x)

        r_p, = self.ax_l.plot(x, requirement, 'r', ls='--')
        g_p, = self.ax_l.plot(x, goal, 'g', ls='--')
        p_p, = self.ax_l.plot(x, poisson, c='0.75', ls='--')
        self.ax_r.plot(x, requirement / goal, 'r')
        self.ax_r.plot(x, np.ones_like(x), 'g')
        self.ax_r.plot(x, poisson / goal, c='0.75', ls='--')

        self.legend_handles.append(r_p)
        self.legend_labels.append("Requirement")
        self.legend_handles.append(g_p)
        self.legend_labels.append("Goal")
        self.legend_handles.append(p_p)
        self.legend_labels.append("Poisson")
Пример #9
0
    def plot_limit_curves(self):
        x = np.logspace(log10(0.9), log10(self.max_pe * 1.1), 100)
        requirement = ChargeResolutionCalculator.requirement(x)
        goal = ChargeResolutionCalculator.goal(x)
        poisson = ChargeResolutionCalculator.poisson(x)

        r_p, = self.ax_l.plot(x, requirement, 'r', ls='--')
        g_p, = self.ax_l.plot(x, goal, 'g', ls='--')
        p_p, = self.ax_l.plot(x, poisson, c='0.75', ls='--')
        self.ax_r.plot(x, requirement / goal, 'r')
        self.ax_r.plot(x, np.ones_like(x), 'g')
        self.ax_r.plot(x, poisson / goal, c='0.75', ls='--')

        self.legend_handles.append(r_p)
        self.legend_labels.append("Requirement")
        self.legend_handles.append(g_p)
        self.legend_labels.append("Goal")
        self.legend_handles.append(p_p)
        self.legend_labels.append("Poisson")
Пример #10
0
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        self.file_reader = HessioFileReader(**kwargs)

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

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

        self.dl0 = CameraDL0Reducer(**kwargs)

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

        self.calculator = ChargeResolutionCalculator(**kwargs)
Пример #11
0
class ChargeResolutionVariationViewer(Tool):
    name = "ChargeResolutionVariationViewer"
    description = "Plot the charge resolution from " \
                  "ChargeResolutionCalculator objects restored via " \
                  "pickled dictionaries."

    input_path = Unicode(None,
                         allow_none=True,
                         help='Path to the hdf5 file produced from'
                         'ChargeResolutionCalculator.save()'
                         '').tag(config=True)

    aliases = Dict(
        dict(
            f='ChargeResolutionVariationViewer.input_path',
            O='ChargeResolutionVariationPlotter.output_path',
        ))
    classes = List(
        [ChargeResolutionCalculator, ChargeResolutionVariationPlotter])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.calculator = None
        self.plotter = None

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

        self.calculator = ChargeResolutionCalculator(**kwargs)
        self.plotter = ChargeResolutionVariationPlotter(**kwargs)

    def start(self):
        self.calculator.load(self.input_path)

    def finish(self):
        hist = self.calculator.variation_hist
        xedges = self.calculator.variation_xedges
        yedges = self.calculator.variation_yedges
        self.plotter.plot_hist(hist, xedges, yedges)
class ChargeResolutionVariationViewer(Tool):
    name = "ChargeResolutionVariationViewer"
    description = "Plot the charge resolution from " \
                  "ChargeResolutionCalculator objects restored via " \
                  "pickled dictionaries."

    input_path = Unicode(None, allow_none=True,
                         help='Path to the hdf5 file produced from'
                              'ChargeResolutionCalculator.save()'
                              '').tag(config=True)

    aliases = Dict(dict(f='ChargeResolutionVariationViewer.input_path',
                        O='ChargeResolutionVariationPlotter.output_path',
                        ))
    classes = List([ChargeResolutionCalculator,
                    ChargeResolutionVariationPlotter
                    ])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.calculator = None
        self.plotter = None

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

        self.calculator = ChargeResolutionCalculator(**kwargs)
        self.plotter = ChargeResolutionVariationPlotter(**kwargs)

    def start(self):
        self.calculator.load(self.input_path)

    def finish(self):
        hist = self.calculator.variation_hist
        xedges = self.calculator.variation_xedges
        yedges = self.calculator.variation_yedges
        self.plotter.plot_hist(hist, xedges, yedges)
Пример #13
0
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        self.file_reader = HessioFileReader(**kwargs)

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

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

        self.dl0 = CameraDL0Reducer(**kwargs)

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

        self.calculator = ChargeResolutionCalculator(**kwargs)
class ChargeResolutionGenerator(Tool):
    name = "ChargeResolutionGenerator"
    description = "Generate the a pickle file of ChargeResolutionFile for " \
                  "a MC file."

    telescopes = List(Int,
                      None,
                      allow_none=True,
                      help='Telescopes to include from the event file. '
                      'Default = All telescopes').tag(config=True)
    output_name = Unicode('charge_resolution',
                          help='Name of the output charge resolution hdf5 '
                          'file').tag(config=True)

    aliases = Dict(
        dict(
            f='SimTelEventSource.input_url',
            max_events='SimTelEventSource.max_events',
            extractor='ChargeExtractorFactory.product',
            window_width='ChargeExtractorFactory.window_width',
            t0='ChargeExtractorFactory.t0',
            window_shift='ChargeExtractorFactory.window_shift',
            sig_amp_cut_HG='ChargeExtractorFactory.sig_amp_cut_HG',
            sig_amp_cut_LG='ChargeExtractorFactory.sig_amp_cut_LG',
            lwt='ChargeExtractorFactory.lwt',
            clip_amplitude='CameraDL1Calibrator.clip_amplitude',
            radius='CameraDL1Calibrator.radius',
            max_pe='ChargeResolutionCalculator.max_pe',
            T='ChargeResolutionGenerator.telescopes',
            O='ChargeResolutionGenerator.output_name',
        ))
    classes = List([
        SimTelEventSource, ChargeExtractorFactory, CameraDL1Calibrator,
        ChargeResolutionCalculator
    ])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.eventsource = None
        self.r1 = None
        self.dl0 = None
        self.dl1 = None
        self.calculator = None

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

        self.eventsource = SimTelEventSource(**kwargs)

        extractor = ChargeExtractorFactory.produce(**kwargs)

        self.r1 = HESSIOR1Calibrator(**kwargs)

        self.dl0 = CameraDL0Reducer(**kwargs)

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

        self.calculator = ChargeResolutionCalculator(**kwargs)

    def start(self):
        desc = "Filling Charge Resolution"
        for event in tqdm(self.eventsource, desc=desc):
            tels = list(event.dl0.tels_with_data)

            # Check events have true charge included
            if event.count == 0:
                try:
                    if np.all(event.mc.tel[tels[0]].photo_electron_image == 0):
                        raise KeyError
                except KeyError:
                    self.log.exception('Source does not contain '
                                       'true charge!')
                    raise

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

            if self.telescopes:
                tels = []
                for tel in self.telescopes:
                    if tel in event.dl0.tels_with_data:
                        tels.append(tel)

            for telid in tels:
                true_charge = event.mc.tel[telid].photo_electron_image
                measured_charge = event.dl1.tel[telid].image[0]
                self.calculator.add_charges(true_charge, measured_charge)

    def finish(self):
        input_url = self.eventsource.input_url
        input_directory = os.path.dirname(input_url)
        input_name = os.path.splitext(os.path.basename(input_url))[0]
        output_directory = os.path.join(input_directory, input_name)
        if not os.path.exists(output_directory):
            self.log.info("Creating directory: {}".format(output_directory))
            os.makedirs(output_directory)
        name = "{}.h5".format(self.output_name)
        ouput_path = os.path.join(output_directory, name)
        self.calculator.save(ouput_path)
Пример #15
0
def test_limit_curves():
    value = ChargeResolutionCalculator.limit_curves(1, 2, 3, 4, 5)
    assert(round(value, 7) == 6.78233)
Пример #16
0
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        self.calculator = ChargeResolutionCalculator(**kwargs)
        self.plotter = ChargeResolutionPlotter(**kwargs)
Пример #17
0
def test_goal():
    value = ChargeResolutionCalculator.goal(1)
    value2 = ChargeResolutionCalculator.goal(np.arange(1, 10))[-1]
    assert(round(value, 7) == 1.8017134)
    assert(round(value2, 7) == 0.4066657)
Пример #18
0
class ChargeResolutionGenerator(Tool):
    name = "ChargeResolutionGenerator"
    description = "Generate the a pickle file of ChargeResolutionFile for " \
                  "either MC or data files."

    telescopes = Int(1,help='Telescopes to include from the event file. '
                           'Default = 1').tag(config=True)
    output_name = Unicode('charge_resolution',
                          help='Name of the output charge resolution hdf5 '
                               'file').tag(config=True)
    input_path = Unicode(help='Path to directory containing data').tag(config=True)

    max_events = Int(1, help='Maximum number of events to use').tag(config=True)

    plot_cam = Bool(False, "enable plotting of individual camera").tag(config=True)

    use_true_pe = Bool(False, "Use true mc p.e.").tag(config=True)

    calibrator = Unicode('HESSIOR1Calibrator', help='which calibrator to use, default = HESSIOR1Calibrator').tag(config=True)

    aliases = Dict(dict(input_path='ChargeResolutionGenerator.input_path',
                        calibrator='ChargeResolutionGenerator.calibrator',
                        max_events='ChargeResolutionGenerator.max_events',
                        extractor='ChargeExtractorFactory.product',
                        window_width='ChargeExtractorFactory.window_width',
                        t0='ChargeExtractorFactory.t0',
                        window_shift='ChargeExtractorFactory.window_shift',
                        sig_amp_cut_HG='ChargeExtractorFactory.sig_amp_cut_HG',
                        sig_amp_cut_LG='ChargeExtractorFactory.sig_amp_cut_LG',
                        lwt='ChargeExtractorFactory.lwt',
                        clip_amplitude='CameraDL1Calibrator.clip_amplitude',
                        radius='CameraDL1Calibrator.radius',
                        max_pe='ChargeResolutionCalculator.max_pe',
                        T='ChargeResolutionGenerator.telescopes',
                        o='ChargeResolutionGenerator.output_name',
                        plot_cam='ChargeResolutionGenerator.plot_cam',
                        use_true_pe='ChargeResolutionGenerator.use_true_pe'
                        ))
    classes = List([EventSourceFactory,
                    HESSIOEventSource,
                    TargetIOEventSource,
                    ChargeExtractorFactory,
                    CameraDL1Calibrator,
                    ChargeResolutionCalculator,
                    CameraCalibrator
                    ])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.eventsource = None
        self.r1 = None
        self.dl0 = None
        self.dl1 = None
        self.calculator = None
        self.cal = None

    def setup(self):
        kwargs = dict(config=self.config, tool=self)
        self.dl0 = CameraDL0Reducer(**kwargs)

        self.dl1 = CameraDL1Calibrator(**kwargs)

        self.cal = CameraCalibrator(r1_product=self.calibrator)

        self.calculator = ChargeResolutionCalculator(**kwargs)


    def start(self):
        run_list = np.loadtxt('%s/runlist.txt' % self.input_path, unpack=True)
        plot_cam = False
        plot_delay = 0.5
        disp = None

        if debug:
            fig=plt.figure(1)
            ax=fig.add_subplot(111)
        for n, run in enumerate(run_list[0]):
            # TODO remove need for hardcoded file name
            if self.calibrator == "TargetIOR1Calibrator":
                file_name = "%s/Run%s_r1.tio" % (self.input_path, int(run))
                print(file_name)
            elif self.calibrator == "HESSIOR1Calibrator":
                file_name = "%s/sim_tel/run%s.simtel.gz" % (self.input_path, int(run))
                print(file_name)

            try:
                source = EventSourceFactory.produce(input_url =file_name, max_events=self.max_events)
                true_pe = []
                # lab_pe = []
                for event in tqdm(source):
                    self.cal.calibrate(event)
                    self.dl0.reduce(event)
                    self.dl1.calibrate(event)
                    input_pe = run_list[3][n]

                    if self.plot_cam == True:
                        if disp is None:
                            geom = event.inst.subarray.tel[self.telescopes].camera
                            disp = CameraDisplay(geom)
                            disp.add_colorbar()
                            plt.show(block=False)
                        im = event.dl1.tel[self.telescopes].image[0]
                        disp.image = im
                        plt.pause(plot_delay)

                    true_charge_mc = event.mc.tel[self.telescopes].photo_electron_image
                    measured_charge = event.dl1.tel[self.telescopes].image[0]
                    true_charge_lab = np.asarray([input_pe]*len(measured_charge))
                    true_pe.append(true_charge_mc)
                    if self.use_true_pe:
                        true_charge=true_charge_mc
                    else:
                        true_charge=true_charge_lab.astype(int)

                    self.calculator.add_charges(true_charge, measured_charge)

                    if debug:
                        plt.errorbar(input_pe, np.mean(true_pe), np.std(true_pe),color='k')
            except FileNotFoundError:
                stop=0
                print('file_not_found')
        if debug:
            plt.xscale('log')
            plt.yscale('log')
            plt.plot([0,1000],[0,1000], 'k:')
            plt.xlabel('Input p.e.')
            plt.ylabel('True mc p.e.')
            plt.show()
    def finish(self):
        out_file = '%s/charge_resolution_test.h5' % self.input_path
        self.calculator.save(self.output_name)
Пример #19
0
    def get_charge(self):
        fig = plt.figure(1)
        ax = fig.add_subplot(111)

        calculator = ChargeResolutionCalculator(config=None, tool=None)
        calculator2 = ChargeResolutionCalculator(config=None, tool=None)

        pevals = [
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20,
            21, 23, 24, 26, 28, 30, 32, 35, 37, 40, 43, 46, 49, 53, 57, 61, 65,
            70, 75, 81, 86, 93, 100, 107, 114, 123, 132, 141, 151, 162, 174,
            187, 200, 215, 231, 247, 265, 284, 305, 327, 351, 376, 403, 432,
            464, 497, 533, 572, 613, 657, 705, 756, 811, 869, 932, 1000
        ]

        for n, i in enumerate(pevals):
            self.filename = '/Users/armstrongt/Workspace/CTA/MCValidation/data/bypass2_enoise_pe_e%s.simtel.gz' % n
            ntrig = 0
            source = hessio_event_source(self.filename,
                                         allowed_tels=None,
                                         max_events=None)
            for event in source:
                self.calib.calibrate(event)
                true_charge = event.mc.tel[
                    1].photo_electron_image * self.pixel_mask
                measured_charge = event.dl1.tel[1].image[0] * self.pixel_mask
                true_charge2 = np.asarray(
                    [int(i)] * len(measured_charge)) * self.pixel_mask
                calculator.add_charges(true_charge, measured_charge)
                calculator2.add_charges(true_charge2, measured_charge)
                ntrig = ntrig + 1

        x, res, res_error, scaled_res, scaled_res_error = calculator.get_charge_resolution(
        )
        x2, res2, res_error2, scaled_res2, scaled_res_error2 = calculator2.get_charge_resolution(
        )
        ax.errorbar(x,
                    res,
                    yerr=res_error,
                    marker='x',
                    linestyle="None",
                    label='MC Charge Res')
        ax.errorbar(x2,
                    res2,
                    yerr=res_error2,
                    marker='x',
                    color='C1',
                    linestyle="None",
                    label='\'Lab\' Charge Res')
        x = np.logspace(np.log10(0.9), np.log10(1000 * 1.1), 100)
        requirement = ChargeResolutionCalculator.requirement(x)
        goal = ChargeResolutionCalculator.goal(x)
        poisson = ChargeResolutionCalculator.poisson(x)
        r_p = ax.plot(x, requirement, 'r', ls='--', label='Requirement')
        g_p = ax.plot(x, goal, 'g', ls='--', label='Goal')
        p_p = ax.plot(x, poisson, c='0.75', ls='--', label='Poisson')
        plt.legend()
        plt.yscale('log')
        plt.xscale('log')
        plt.xlabel('true charge')
        plt.ylabel('charge resolution')
        plt.show()
Пример #20
0
def test_requirement():
    value = ChargeResolutionCalculator.requirement(1)
    value2 = ChargeResolutionCalculator.requirement(np.arange(1, 10))[-1]
    assert (round(value, 7) == 2.0237963)
    assert (round(value2, 7) == 0.4501817)
Пример #21
0
class ChargeResolutionGenerator(Tool):
    name = "ChargeResolutionGenerator"
    description = "Generate the a pickle file of ChargeResolutionFile for " \
                  "a MC file."

    telescopes = List(Int,
                      None,
                      allow_none=True,
                      help='Telescopes to include from the event file. '
                      'Default = All telescopes').tag(config=True)
    output_name = Unicode('charge_resolution',
                          help='Name of the output charge resolution pickle '
                          'file').tag(config=True)

    aliases = Dict(
        dict(
            f='HessioFileReader.input_path',
            max_events='HessioFileReader.max_events',
            extractor='ChargeExtractorFactory.extractor',
            window_width='ChargeExtractorFactory.window_width',
            window_start='ChargeExtractorFactory.window_start',
            window_shift='ChargeExtractorFactory.window_shift',
            sig_amp_cut_HG='ChargeExtractorFactory.sig_amp_cut_HG',
            sig_amp_cut_LG='ChargeExtractorFactory.sig_amp_cut_LG',
            lwt='ChargeExtractorFactory.lwt',
            clip_amplitude='CameraDL1Calibrator.clip_amplitude',
            radius='CameraDL1Calibrator.radius',
            max_pe='ChargeResolutionCalculator.max_pe',
            T='ChargeResolutionGenerator.telescopes',
            O='ChargeResolutionGenerator.output_name',
        ))
    classes = List([
        HessioFileReader, ChargeExtractorFactory, CameraDL1Calibrator,
        ChargeResolutionCalculator
    ])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.file_reader = None
        self.r1 = None
        self.dl0 = None
        self.dl1 = None
        self.calculator = None

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

        self.file_reader = HessioFileReader(**kwargs)

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

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

        self.dl0 = CameraDL0Reducer(**kwargs)

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

        self.calculator = ChargeResolutionCalculator(**kwargs)

    def start(self):
        desc = "Filling Charge Resolution"
        with tqdm(desc=desc) as pbar:
            source = self.file_reader.read()
            for event in source:
                pbar.update(1)
                tels = list(event.dl0.tels_with_data)

                # Check events have true charge included
                if event.count == 0:
                    try:
                        if np.all(event.mc.tel[tels[0]].photo_electron_image ==
                                  0):
                            raise KeyError
                    except KeyError:
                        self.log.exception('Source does not contain '
                                           'true charge!')
                        raise

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

                if self.telescopes:
                    tels = []
                    for tel in self.telescopes:
                        if tel in event.dl0.tels_with_data:
                            tels.append(tel)

                for telid in tels:
                    true_charge = event.mc.tel[telid].photo_electron_image
                    measured_charge = event.dl1.tel[telid].image[0]
                    self.calculator.add_charges(true_charge, measured_charge)

    def finish(self):
        directory = self.file_reader.output_directory
        name = "{}.pickle".format(self.output_name)
        ouput_path = os.path.join(directory, name)
        self.calculator.save(ouput_path)
Пример #22
0
def test_requirement():
    value = ChargeResolutionCalculator.requirement(1)
    value2 = ChargeResolutionCalculator.requirement(np.arange(1, 10))[-1]
    assert(round(value, 7) == 2.0237963)
    assert(round(value2, 7) == 0.4501817)
Пример #23
0
class ChargeResolutionGenerator(Tool):
    name = "ChargeResolutionGenerator"
    description = "Generate the a pickle file of ChargeResolutionFile for " \
                  "a MC file."

    telescopes = List(Int, None, allow_none=True,
                      help='Telescopes to include from the event file. '
                           'Default = All telescopes').tag(config=True)
    output_name = Unicode('charge_resolution',
                          help='Name of the output charge resolution pickle '
                               'file').tag(config=True)

    aliases = Dict(dict(f='HessioFileReader.input_path',
                        max_events='HessioFileReader.max_events',
                        extractor='ChargeExtractorFactory.extractor',
                        window_width='ChargeExtractorFactory.window_width',
                        t0='ChargeExtractorFactory.t0',
                        window_shift='ChargeExtractorFactory.window_shift',
                        sig_amp_cut_HG='ChargeExtractorFactory.sig_amp_cut_HG',
                        sig_amp_cut_LG='ChargeExtractorFactory.sig_amp_cut_LG',
                        lwt='ChargeExtractorFactory.lwt',
                        clip_amplitude='CameraDL1Calibrator.clip_amplitude',
                        radius='CameraDL1Calibrator.radius',
                        max_pe='ChargeResolutionCalculator.max_pe',
                        T='ChargeResolutionGenerator.telescopes',
                        O='ChargeResolutionGenerator.output_name',
                        ))
    classes = List([HessioFileReader,
                    ChargeExtractorFactory,
                    CameraDL1Calibrator,
                    ChargeResolutionCalculator
                    ])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.file_reader = None
        self.r1 = None
        self.dl0 = None
        self.dl1 = None
        self.calculator = None

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

        self.file_reader = HessioFileReader(**kwargs)

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

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

        self.dl0 = CameraDL0Reducer(**kwargs)

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

        self.calculator = ChargeResolutionCalculator(**kwargs)

    def start(self):
        desc = "Filling Charge Resolution"
        source = self.file_reader.read()
        for event in tqdm(source, desc=desc):
            tels = list(event.dl0.tels_with_data)

            # Check events have true charge included
            if event.count == 0:
                try:
                    if np.all(event.mc.tel[
                                  tels[0]].photo_electron_image == 0):
                        raise KeyError
                except KeyError:
                    self.log.exception('Source does not contain '
                                       'true charge!')
                    raise

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

            if self.telescopes:
                tels = []
                for tel in self.telescopes:
                    if tel in event.dl0.tels_with_data:
                        tels.append(tel)

            for telid in tels:
                true_charge = event.mc.tel[telid].photo_electron_image
                measured_charge = event.dl1.tel[telid].image[0]
                self.calculator.add_charges(true_charge, measured_charge)

    def finish(self):
        directory = self.file_reader.output_directory
        name = "{}.h5".format(self.output_name)
        ouput_path = os.path.join(directory, name)
        self.calculator.save(ouput_path)
Пример #24
0
def test_goal():
    value = ChargeResolutionCalculator.goal(1)
    value2 = ChargeResolutionCalculator.goal(np.arange(1, 10))[-1]
    assert (round(value, 7) == 1.8017134)
    assert (round(value2, 7) == 0.4066657)
Пример #25
0
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        self.calculator = ChargeResolutionCalculator(**kwargs)
        self.plotter = ChargeResolutionPlotter(**kwargs)
Пример #26
0
def test_limit_curves():
    value = ChargeResolutionCalculator.limit_curves(1, 2, 3, 4, 5)
    assert (round(value, 7) == 6.78233)