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)
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)
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)
示例#4
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)