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()
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 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 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()