def plot_charge_resolutions(chargeres_dict, parser, plot_cmdlines):
    run = 0
    for name, cmdline in plot_cmdlines.items():

        args, excess_args = parser.parse_known_args(cmdline)

        run += 1
        log.info('[run] Plotting block: {}/{}'.format(run, len(plot_cmdlines)))

        handles = []
        labels = []

        fig = plt.figure(figsize=(20, 8))
        ax_l = fig.add_subplot(121)
        ax_r = fig.add_subplot(122)

        fig.subplots_adjust(left=0.05, right=0.95, wspace=0.6)
        fig.suptitle(name)

        max_x_charge = 0
        for chargeres_name in args.chargeres_names:
            chargeres = chargeres_dict[chargeres_name]
            if args.binning == 'none':
                x_charge, res, error, scaled_res, scaled_error = \
                    chargeres.get_charge_resolution()
            elif args.binning == 'normal':
                x_charge, res, error, scaled_res, scaled_error = \
                    chargeres.get_binned_charge_resolution(False)
            else:
                x_charge, res, error, scaled_res, scaled_error = \
                    chargeres.get_binned_charge_resolution(True)

            eb_l = ax_l.errorbar(x_charge, res, yerr=error, marker='x',
                                 linestyle="None")
            ax_r.errorbar(x_charge, scaled_res, yerr=scaled_error,
                          marker='x', linestyle="None")

            handles.append(eb_l)
            labels.append(chargeres_name)

            current_max = x_charge[np.invert(np.isnan(x_charge))].max()
            if max_x_charge < current_max:
                max_x_charge = current_max

        # Get requirement and goal curves
        x = np.logspace(log10(0.9), log10(max_x_charge*1.1), 100)
        requirement = ChargeResolution.requirement(x)
        goal = ChargeResolution.goal(x)
        poisson = ChargeResolution.poisson(x)

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

        handles.append(r_p)
        labels.append("Requirement")
        handles.append(g_p)
        labels.append("Goal")
        handles.append(p_p)
        labels.append("Poisson")

        if not args.normalx:
            ax_l.set_xscale('log')
            ax_r.set_xscale('log')
        if not args.normaly:
            ax_l.set_yscale('log')

        ax_l.set_xlabel(r'True Charge $Q_T$ (p.e.)')
        ax_l.set_ylabel('Charge Resolution')
        ax_r.set_xlabel(r'True Charge $Q_T$ (p.e.)')
        ax_r.set_ylabel('Charge Resolution/Goal')

        ax_l.legend(handles, labels, bbox_to_anchor=(1.02, 1.), loc=2,
                    borderaxespad=0., fontsize=10)

        ax_l.set_xlim(0.9, max_x_charge*1.1)
        ax_r.set_xlim(0.9, max_x_charge*1.1)
        if max_x_charge > 2000:
            ax_r.set_xlim(0.9, 2000*1.1)
        if args.maxpeplot is not None:
            ax_l.set_xlim(0.9, args.maxpeplot)
            ax_r.set_xlim(0.9, args.maxpeplot)

        warnings.filterwarnings("ignore", module="matplotlib")
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            if args.output_path is None:
                plt.show()
            else:
                args.output_path = os.path.expanduser(args.output_path)
                output_dir = dirname(args.output_path)
                if not os.path.exists(output_dir):
                    log.info(
                        "[output] Creating directory: {}".format(output_dir))
                    os.makedirs(output_dir)
                log.info("[output] {}".format(args.output_path))
                fig.savefig(args.output_path, bbox_inches='tight')
Exemple #2
0
def plot_charge_resolutions(chargeres_dict, parser, plot_cmdlines):
    run = 0
    for name, cmdline in plot_cmdlines.items():

        args, excess_args = parser.parse_known_args(cmdline)

        run += 1
        log.info('[run] Plotting block: {}/{}'.format(run, len(plot_cmdlines)))

        handles = []
        labels = []

        fig = plt.figure(figsize=(20, 8))
        ax_l = fig.add_subplot(121)
        ax_r = fig.add_subplot(122)

        fig.subplots_adjust(left=0.05, right=0.95, wspace=0.6)
        fig.suptitle(name)

        max_x_charge = 0
        for chargeres_name in args.chargeres_names:
            chargeres = chargeres_dict[chargeres_name]
            if args.binning == 'none':
                x_charge, res, error, scaled_res, scaled_error = \
                    chargeres.get_charge_resolution()
            elif args.binning == 'normal':
                x_charge, res, error, scaled_res, scaled_error = \
                    chargeres.get_binned_charge_resolution(False)
            else:
                x_charge, res, error, scaled_res, scaled_error = \
                    chargeres.get_binned_charge_resolution(True)

            eb_l = ax_l.errorbar(x_charge,
                                 res,
                                 yerr=error,
                                 marker='x',
                                 linestyle="None")
            ax_r.errorbar(x_charge,
                          scaled_res,
                          yerr=scaled_error,
                          marker='x',
                          linestyle="None")

            handles.append(eb_l)
            labels.append(chargeres_name)

            current_max = x_charge[np.invert(np.isnan(x_charge))].max()
            if max_x_charge < current_max:
                max_x_charge = current_max

        # Get requirement and goal curves
        x = np.logspace(log10(0.9), log10(max_x_charge * 1.1), 100)
        requirement = ChargeResolution.requirement(x)
        goal = ChargeResolution.goal(x)
        poisson = ChargeResolution.poisson(x)

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

        handles.append(r_p)
        labels.append("Requirement")
        handles.append(g_p)
        labels.append("Goal")
        handles.append(p_p)
        labels.append("Poisson")

        if not args.normalx:
            ax_l.set_xscale('log')
            ax_r.set_xscale('log')
        if not args.normaly:
            ax_l.set_yscale('log')

        ax_l.set_xlabel(r'True Charge $Q_T$ (p.e.)')
        ax_l.set_ylabel('Charge Resolution')
        ax_r.set_xlabel(r'True Charge $Q_T$ (p.e.)')
        ax_r.set_ylabel('Charge Resolution/Goal')

        ax_l.legend(handles,
                    labels,
                    bbox_to_anchor=(1.02, 1.),
                    loc=2,
                    borderaxespad=0.,
                    fontsize=10)

        ax_l.set_xlim(0.9, max_x_charge * 1.1)
        ax_r.set_xlim(0.9, max_x_charge * 1.1)
        if max_x_charge > 2000:
            ax_r.set_xlim(0.9, 2000 * 1.1)
        if args.maxpeplot is not None:
            ax_l.set_xlim(0.9, args.maxpeplot)
            ax_r.set_xlim(0.9, args.maxpeplot)

        warnings.filterwarnings("ignore", module="matplotlib")
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            if args.output_path is None:
                plt.show()
            else:
                args.output_path = os.path.expanduser(args.output_path)
                output_dir = dirname(args.output_path)
                if not os.path.exists(output_dir):
                    log.info(
                        "[output] Creating directory: {}".format(output_dir))
                    os.makedirs(output_dir)
                log.info("[output] {}".format(args.output_path))
                fig.savefig(args.output_path, bbox_inches='tight')